三年前的代码,自己又没写注释忘得差不多了😅,复制代码可以直接运行!
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<time.h>
#include<bios.h>
#define size 10
#define w 18432
#define s 20480
#define a 19200
#define d 19712
int i,j,v=0;
int direct = d;
char str[10];
struct coor
{
int x;
int y;
}food;
struct snake
{
int n;
struct coor scr[1000];
}snake,snake1;
void paintsnake()
{
setcolor(0);
rectangle(snake.scr[snake.n].x, snake.scr[snake.n].y, snake.scr[snake.n].x + size, snake.scr[snake.n].y + size);
setcolor(15);
sprintf(str,"%d ",snake.n);
outtextxy(20,5,"snake.n:");
outtextxy(70+snake.n*15,5,str);
for (i = snake.n - 1; i > 0; i--)
{
rectangle(snake.scr[i].x, snake.scr[i].y, snake.scr[i].x + size, snake.scr[i].y + size);
}
setcolor(2);
rectangle(snake.scr[0].x, snake.scr[0].y, snake.scr[0].x + size, snake.scr[0].y + size);
}
void movesnake()
{
for (i = snake.n; i > 0; i--)
{
snake.scr[i].x = snake.scr[i - 1].x;
snake.scr[i].y = snake.scr[i - 1].y;
}
switch (direct)
{
case w:
snake.scr[0].y -= size; break;
case s:
snake.scr[0].y += size; break;
case a:
snake.scr[0].x -= size; break;
case d:
snake.scr[0].x += size; break;
}
}
void overturn()
{
for(i=snake.n;i>=0;i--)
{
snake1.scr[i].x=snake.scr[i].x;
snake1.scr[i].y=snake.scr[i].y;
}
for(i=snake.n-1;i>=0;i--)
{
snake.scr[snake.n-1-i].x=snake1.scr[i].x;
snake.scr[snake.n-1-i].y=snake1.scr[i].y;
}
}
void reverse()
{
switch(snake.scr[snake.n].x-snake.scr[snake.n-1].x)
{
case size:
overturn();
direct=d;break;
case -size:
overturn();
direct=a;break;
}
switch(snake.scr[snake.n].y-snake.scr[snake.n-1].y)
{
case size:
overturn();
direct=s;break;
case -size:
overturn();
direct=w;break;
}
}
void changedirect()
{
switch (bioskey(0))
{
case w:
if (direct == s)
{
reverse();
break;
}
direct = w;
break;
case s:
if (direct == w)
{
reverse();
break;
}
direct = s;
break;
case a:
if (direct == d)
{
reverse();
break;
}
direct = a;
break;
case d:
if (direct == a)
{
reverse();
break;
}
direct = d;
break;
}
}
void eatfood()
{ for(i=snake.n-1;i>=0;i--)
{
if (snake.scr[i].x == food.x&&snake.scr[i].y == food.y)
{
i=snake.n-1;
while(i>=0)
{
srand((unsigned)time(NULL));
food.x = rand() % 61 * size+2*size;
food.y = rand() % 45 * size+2*size;
for(i=snake.n-1;i>=0;i--)
{
if(snake.scr[i].x==food.x&&snake.scr[i].y==food.y)
break;
}
}
snake.n++;
v++;
break;
}
}
setcolor(4);
rectangle(food.x, food.y, food.x + size, food.y + size);
}
void gameover()
{
if(snake.scr[0].x<20||snake.scr[0].x>620||snake.scr[0].y<20||snake.scr[0].y>460)
{
cleardevice();
moveto(200,240);
outtext("gameover");
sleep(5);
}
for (i = snake.n - 1; i > 3; i--)
{
if (snake.scr[0].x == snake.scr[i].x&&snake.scr[0].y == snake.scr[i].y)
{
cleardevice();
outtextxy(220, 240,"gameover");
sleep(5);
}
}
}
void speed()
{
delay(100000);
delay(100000-snake.n*50);
}
int main()
{
int driver,mode;
driver=DETECT;
initgraph(&driver,&mode,"c:\\tc");
snake.n = 1;
snake.scr[0].x = 320;
snake.scr[0].y = 220;
food.x = 320;
food.y = 320;
setcolor(4);
rectangle(15,15,634,472);
while (1)
{
while (bioskey(1))
{
changedirect();
}
movesnake();
paintsnake();
gameover();
eatfood();
speed();
}
return 0;
}