//??????
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
//??????
#define MAX 32
//??????
char a[20][51] = {"################################################",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"################################################"};
int game_s_long = 4;
//??
struct Body
{
int x;
int y;
} body[999];
struct Head
{
int x;
int y;
char turn;
} head;
struct Food
{
int x;
int y;
} food;
//??????
void game_s_put(void)
{
system("cls");
for (int i = 0; i < 20; i++)
{
puts(a[i]);
}
printf("score:%d\n", game_s_long);
}
void game_s_start(void)
{
a[10][24] = '@';
for (int i = 1; i < 5; i++)
{
a[10 + i][24] = 'O';
}
head.x = 10, head.y = 24;
body[0].x = 11, body[0].y = 24;
body[1].x = 12, body[1].y = 24;
body[2].x = 13, body[2].y = 24;
body[3].x = 14, body[3].y = 24;
head.turn = 'w';
}
int game_s_move(void)
{
int x1 = 0, x2 = 0, y1 = 0, y2 = 0, i = 0;
if (head.turn == 'w')
{
if (a[head.x - 1][head.y] == 'O' || a[head.x - 1][head.y] == '#')
{
return 0;
}
}
if (head.turn == 's')
{
if (a[head.x + 1][head.y] == 'O' || a[head.x + 1][head.y] == '#')
{
return 0;
}
}
if (head.turn == 'a')
{
if (a[head.x][head.y - 1] == 'O' || a[head.x][head.y - 1] == '#')
{
return 0;
}
}
if (head.turn == 'd')
{
if (a[head.x][head.y + 1] == 'O' || a[head.x][head.y + 1] == '#')
{
return 0;
}
}
switch (head.turn)
{
case 'w':
if (head.turn != 's')
{
x1 = head.x;
y1 = head.y;
head.x--;
for (i = 0; i < game_s_long; i += 2)
{
x2 = body[i].x;
y2 = body[i].y;
body[i].x = x1;
body[i].y = y1;
if (game_s_long - i == 1)
{
break;
}
x1 = body[i + 1].x;
y1 = body[i + 1].y;
body[i + 1].x = x2;
body[i + 1].y = y2;
}
body[i + 1].x = x2;
body[i + 1].y = y2;
}
break;
case 's':
if (head.turn != 'w')
{
x1 = head.x;
y1 = head.y;
head.x++;
for (i = 0; i < game_s_long; i += 2)
{
x2 = body[i].x;
y2 = body[i].y;
body[i].x = x1;
body[i].y = y1;
if (game_s_long - i == 1)
{
break;
}
x1 = body[i + 1].x;
y1 = body[i + 1].y;
body[i + 1].x = x2;
body[i + 1].y = y2;
}
body[i + 1].x = x2;
body[i + 1].y = y2;
}
break;
case 'a':
if (head.turn != 'd')
{
x1 = head.x;
y1 = head.y;
head.y--;
for (i = 0; i < game_s_long; i += 2)
{
x2 = body[i].x;
y2 = body[i].y;
body[i].x = x1;
body[i].y = y1;
if (game_s_long - i == 1)
{
break;
}
x1 = body[i + 1].x;
y1 = body[i + 1].y;
body[i + 1].x = x2;
body[i + 1].y = y2;
}
}
body[i + 1].x = x2;
body[i + 1].y = y2;
break;
case 'd':
if (head.turn != 'a')
{
x1 = head.x;
y1 = head.y;
head.y++;
for (i = 0; i < game_s_long; i += 2)
{
x2 = body[i].x;
y2 = body[i].y;
body[i].x = x1;
body[i].y = y1;
if (game_s_long - i == 1)
{
break;
}
x1 = body[i + 1].x;
y1 = body[i + 1].y;
body[i + 1].x = x2;
body[i + 1].y = y2;
}
}
body[i + 1].x = x2;
body[i + 1].y = y2;
break;
}
}
void game_s_get(void)
{
if (kbhit())
{
head.turn = getch();
}
}
void game_s_release(void)
{
strcpy(a[0], "##################################################");
strcpy(a[1], "# #");
strcpy(a[2], "# #");
strcpy(a[3], "# #");
strcpy(a[4], "# #");
strcpy(a[5], "# #");
strcpy(a[6], "# #");
strcpy(a[7], "# #");
strcpy(a[8], "# #");
strcpy(a[9], "# #");
strcpy(a[10], "# #");
strcpy(a[11], "# #");
strcpy(a[12], "# #");
strcpy(a[13], "# #");
strcpy(a[14], "# #");
strcpy(a[15], "# #");
strcpy(a[16], "# #");
strcpy(a[17], "# #");
strcpy(a[18], "# #");
strcpy(a[19], "##################################################");
a[head.x][head.y] = '@';
if (food.x != '\0' && food.y != '\0')
{
a[food.x][food.y] = '*';
}
for (int i = 0; i < game_s_long; i++)
{
a[body[i].x][body[i].y] = 'O';
}
}
void game_s_food(void)
{
int x, y;
time_t t;
while(1)
{
srand((unsigned)time(&t));
x = rand() % 20;
srand((unsigned)time(&t));
y = rand() % 50;
if (a[x][y] != '#' && a[x][y] != 'O' && a[x][y] != '@')
{
food.x = x;
food.y = y;
break;
}
}
}
int game_s_eat(void)
{
if (head.turn == 'w')
{
if (a[head.x - 1][head.y] == '*')
{
food.x = '\0';
food.y = '\0';
game_s_long++;
return 1;
}
}
if (head.turn == 's')
{
if (a[head.x + 1][head.y] == '*')
{
food.x = '\0';
food.y = '\0';
game_s_long++;
return 1;
}
}
if (head.turn == 'a')
{
if (a[head.x][head.y - 1] == '*')
{
food.x = '\0';
food.y = '\0';
game_s_long++;
return 1;
}
}
if (head.turn == 'd')
{
if (a[head.x][head.y + 1] == '*')
{
food.x = '\0';
food.y = '\0';
game_s_long++;
return 1;
}
}
return 0;
}
void game_s(void)
{
char ch;
int q,p;
game_s_food();
game_s_start();
while (1)
{
game_s_get();
q = game_s_move();
if (!q)
{
exit(0);
}
p=game_s_eat();
if(p)
{
game_s_food();
}
game_s_release();
game_s_put();
Sleep(250);
}
}
//??????
int main(void)
{
game_s();
return 0;
}
不喜勿喷