自己写的贪吃蛇,新手
#include<stdio.h>#include<dos.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<windows.h>
int x,y;//坐标
int i;///次数控制
int lenth=4;
void gotoxy(int x,int y)
{
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
struct
{
int x;
int y;
}snake={{20,9},{19,9},{18,9},{17,9}},first;
int start()
{
printf("\t\t");
printf("***********贪食蛇**********\n");
printf("\t\t");
printf(" ********包鹏举制作*******\n");
gotoxy(1,2);
printf("e上\nx下\na左\nf右");
//x 14-48y 4-22
for(y=3;y<=23;y++)
{
x=13;
gotoxy(x,y);
printf("\3");
x=49;
gotoxy(x,y);
printf("\3");
}
for(x=13;x<=49;x++)
{
y=3;
gotoxy(x,y);
printf("\3");
y=23;
gotoxy(x,y);
printf("\3");
}
gotoxy(13,3);
x=14;y=4;
gotoxy(x,y);
}
int shape()
{
for(i=0;i<lenth;i++)
{
if(i==0)
{
gotoxy(snake.x,snake.y);
printf("\1");
}
else
{
gotoxy(snake.x,snake.y);
printf("\2");
}
}
}
int run()
{
int die();
void eat(int *x1,int *y1);
int turn(char);
int x1,y1;
void head();
srand((unsigned)time(0));
x1=rand()%38+14;
y1=rand()%19+4;
while(1)
{
eat(&x1,&y1);
shape();
first.x=snake.x;
first.y=snake.y;
for(i=lenth-1;i>0;i--)
{
snake.x=snake.x;
snake.y=snake.y;
}
if(die()==0)
{
gotoxy(27,10);
printf("game over\n");
break;
return 1;
}
if(kbhit())
{
turn(getch());
}
else
{
head();
}
Sleep(100);
system("cls");
start();
shape();
}
}
void head()
{
if(snake.x==first.x&&snake.y>first.y)
{
snake.y++;
}
if(snake.x==first.x&&snake.y<first.y)
{
snake.y--;
}
if(snake.y==first.y&&snake.x<first.x)
{
snake.x--;
}
if(snake.y==first.y&&snake.x>first.x)
{
snake.x++;
}
}
int turn(char dire)
{
switch (dire)
{
case 'e':snake.x==first.x?head():snake.y--;break;
case 'x':snake.x==first.x?head():snake.y++;break;
case 'a':snake.y==first.y?head():snake.x--;break;
case 'f':snake.y==first.y?head():snake.x++;break;
default:head();
}
return 0;
}
void eat(int *x1,int *y1)
{
gotoxy(*x1,*y1);
printf("\4");
if(*x1==snake.x&&*y1==snake.y)
{
lenth++;
srand((unsigned)time(NULL));
*x1=rand()%35+14;
*y1=rand()%19+4;
}
}
int die()
{
/* if(snake.y==3)
{
snake.y==23;
return 1;
}
if(snake.y==23)
{
snake.y==3;
return 1;
}
if(snake.x==49)
{
snake.x==13;
return 1;
}
if(snake.x==13)
{
snake.x==49;
return 1;
}*/
if(snake.y==3||snake.y==23||snake.x==13||snake.x==49)
{
return 0;
}
else
{
return 1;
}
}
int main()
{
start();
run();
getch();
getch();
return 0;
}
真是难得给力的帖子啊。
页:
[1]