//大一时的一个程序,用的是turboC2.0,很原始的一个游戏程序,因为graphics.h被淘汰了,但是自己还是对自己研究graphics.h的那段时光很是怀念,使她引领我进入彩色的
//编程世界。
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<graphics.h>
int snake_len=1;
int snake_loc[50][2]={30,12};
int snake_head[2]={31,12};
int food[2];
char snake_direction='s';
int delay=200;
int eat_flag=0;
int liv_stat=0;
void init()
{
int i;
snake_len=1;
snake_loc[0][0]=31;
snake_loc[0][1]=12;
snake_head[0]=31;
snake_head[1]=12;
snake_direction='s';
delay=200;
eat_flag=0;
liv_stat=0;
for(i=1;i<50;i++)
{
snake_loc[i][0]=0;
snake_loc[i][1]=0;
}
}
void creat_window()
{
gotoxy(0,0);
printf("***************************************************************************************");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("* * *");
printf("************************************************************************************");
}
void update_score()
{
gotoxy(64,12);
printf("%2d",snake_len);
}
void creat_food()
{
time_t t;
srand(time(&t));
while(1)
{
food[0]=rand()%62+1;
food[1]=rand()%22+1;
if(food[0]!=snake_head[0]&&food[1]!=snake_head[1])break;
}
gotoxy(food[0],food[1]);
printf("*");
}
void direction()
{
char keyhit=0,i;
while(kbhit()!=0)
keyhit=getch();
if(((keyhit=='a')||(keyhit=='d')||(keyhit=='w')||(keyhit=='s'))&&(abs(snake_direction/16-keyhit/16)==1))snake_direction=keyhit;
else if(keyhit==' ')
{
gotoxy(30,24);
for(i=0;i<19;i++)
printf(" ");
}
else if(keyhit==27)
exit(0);
}
void state()
{
if(snake_head[0]<=0||snake_head[0]>63||snake_head[1]<1||snake_head[1]>=22)
liv_stat=1;
}
void eat()
{
switch(snake_direction)
{
case 'w':snake_head[1]--;break;
case 's':snake_head[1]++;break;
case 'a':snake_head[0]--;break;
case 'd':snake_head[0]++;break;
}
if((food[0]==snake_head[0])&&(food[1]==snake_head[1]))
{
eat_flag=1;
switch(snake_direction)
{
case 'w':snake_head[1]--;break;
case 's':snake_head[1]++;break;
case 'a':snake_head[0]--;break;
case 'd':snake_head[0]++;break;
}
}
}
void show_snake()
{
gotoxy(snake_head[0],snake_head[1]);
printf("*");
gotoxy(snake_loc[snake_len-1][0],snake_loc[snake_len-1][1]);
printf(" ");
}
void update_maxtrix()
{
int i;
if(eat_flag!=1)
{
for(i=snake_len-1;i>0;i--)
{
snake_loc[i][0]=snake_loc[i-1][0];
snake_loc[i][1]=snake_loc[i-1][0];
}
}
else
{
snake_len++;
if(snake_len>3&&delay>100)delay-=30;
for(i=snake_len-1;i>1;i--)
{
snake_loc[i][0]=snake_loc[i-2][0];
snake_loc[i][1]=snake_loc[i-2][1];
}
snake_loc[1][0]=food[0];
snake_loc[1][1]=food[1];
eat_flag=0;
creat_food();
update_score();
}
snake_loc[0][0]=snake_head[0];
snake_loc[0][1]=snake_head[1];
}
main()
{
int gd=DETECT,gm;
registerbgidriver(EGAVGA_driver);
initgraph(&gd,&gm,"c:\program files\winyes\tcpp30h");
LOOP:
cleardevice();
init();
creat_window();
creat_food();
while(1)
{
int i;
sleep(delay);
direction();
eat();
show_snake();
update_maxtrix();
state();
if(liv_stat==1)
{
for(i=1;i<snake_len;i++)
{
gotoxy(snake_loc[i][0],snake_loc[i][1]);
printf(" ");
}
gotoxy(food[0],food[1]);
printf(" ");
gotoxy(30,12);
printf("Game over!\n");
gotoxy(25,13);
printf("continue...press y,exit...press n");
while(1)
{
i=getch();
if(i='y')
goto LOOP;
else if(i=='n')
break;
}
break;
}
}
}