#include
#include
#include
#include
#include
#define N 100
int i, s, m;
char key;
//
struct Food
{
int x;
int y;
int flag;
}
food;
//
struct Snake
{
int x[N];
int y[N];
int node;
int dir;
int life;
}
snake;
//
void welcome()
{
initgraph(640, 480);
IMAGE img;
loadimage(img, _T("123.jpg"));
putimage(0, 0, img);
setlinecolor(YELLOW);
setlinestyle(PS_SOLID, 7);
circle(320, 210, 100);//
setcolor(YELLOW);
setbkmode(TRANSPARENT);
settextstyle(30, 0, _T(""));
outtextxy(277, 183, _T(""));//
int i = 255;
int c = 255;
while (!_kbhit())
{
setcolor(RGB(c, 0, 0));
setbkmode(TRANSPARENT);
settextstyle(15, 0, _T(""));
outtextxy(260, 400, _T(""));
c -= 8;
if (c 0; i--)
{
snake.x[i] = snake.x[i - 1];
snake.y[i] = snake.y[i - 1];
}
switch (snake.dir)
{
case 1:
snake.x[0] = snake.x[0] + 10; //
break;
case 2: snake.x[0] = snake.x[0] - 10; //
break;
case 3:
snake.y[0] = snake.y[0] - 10; //
break;
case 4: snake.y[0] = snake.y[0] + 10; //
break;
}
//
for (i = 3; i 470 || snake.y[0]430)
{
gameover();
snake.life = 1;
break;
}
//
setfillcolor(GREEN);
solidrectangle(snake.x[0], snake.y[0], snake.x[0] + 10, snake.y[0] + 10);
for (i = 1; i {
setfillcolor(BLUE);
solidrectangle(snake.x[i], snake.y[i], snake.x[i] + 10, snake.y[i] + 10);
}
//
if (snake.x[0] == food.xsnake.y[0] == food.y)
{
setfillcolor(BLACK);
solidrectangle(food.x, food.y, food.x + 10, food.y + 10);
snake.node++;
food.flag = 1;
}
setfillcolor(RED);
solidrectangle(snake.x[snake.node - 1], snake.y[snake.node - 1], snake.x[snake.node - 1] + 10, snake.y[snake.node - 1] + 10);
Sleep(s);
setfillcolor(BLACK);
solidrectangle(snake.x[snake.node - 1], snake.y[snake.node - 1], snake.x[snake.node - 1] + 10, snake.y[snake.node - 1] + 10);
}
if (snake.life == 1)
{
break;
}
key = _getch();
if (key == 'w' snake.dir != 4) //
{
snake.dir = 3;
}
else if (key == 's' snake.dir != 3) //
{
snake.dir = 4;
}
else if (key == 'd' snake.dir != 2) //
{
snake.dir = 1;
}
else if (key == 'a' snake.dir != 1) //
{
snake.dir = 2;
}
else if (key == 32) {
_getch();
}
} while (s != 0 key != 27);
}
void main() {
initgraph(640, 480);
welcome();
s = speed();
Dwall();
play();
_getch();
closegraph();
}