|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#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[100]={{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-48 y 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[i].x,snake[i].y);
printf("\1");
}
else
{
gotoxy(snake[i].x,snake[i].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[1].x;
first.y=snake[1].y;
for(i=lenth-1;i>0;i--)
{
snake[i].x=snake[i-1].x;
snake[i].y=snake[i-1].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[0].x==first.x&&snake[0].y>first.y)
{
snake[0].y++;
}
if(snake[0].x==first.x&&snake[0].y<first.y)
{
snake[0].y--;
}
if(snake[0].y==first.y&&snake[0].x<first.x)
{
snake[0].x--;
}
if(snake[0].y==first.y&&snake[0].x>first.x)
{
snake[0].x++;
}
}
int turn(char dire)
{
switch (dire)
{
case 'e':snake[0].x==first.x?head():snake[0].y--;break;
case 'x':snake[0].x==first.x?head():snake[0].y++;break;
case 'a':snake[0].y==first.y?head():snake[0].x--;break;
case 'f':snake[0].y==first.y?head():snake[0].x++;break;
default:head();
}
return 0;
}
void eat(int *x1,int *y1)
{
gotoxy(*x1,*y1);
printf("\4");
if(*x1==snake[0].x&&*y1==snake[0].y)
{
lenth++;
srand((unsigned)time(NULL));
*x1=rand()%35+14;
*y1=rand()%19+4;
}
}
int die()
{
/* if(snake[0].y==3)
{
snake[0].y==23;
return 1;
}
if(snake[0].y==23)
{
snake[0].y==3;
return 1;
}
if(snake[0].x==49)
{
snake[0].x==13;
return 1;
}
if(snake[0].x==13)
{
snake[0].x==49;
return 1;
}*/
if(snake[0].y==3||snake[0].y==23||snake[0].x==13||snake[0].x==49)
{
return 0;
}
else
{
return 1;
}
}
int main()
{
start();
run();
getch();
getch();
return 0;
}
|
|