代码抄完之后,排了下错,就成了下面这样了,但为什么我抄的代码不能玩呢?
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#define SNAKE 'O'
#define FOOD '*'
#define MAX 50
typedef struct Snake
{
int x; //蛇的x坐标
int y; //蛇的y坐标
struct Snake *per;
struct Snake *next;
}
Snake;
int COUNT = 1; //蛇的长度
Snake * HEAD = NULL, *TAIL = NULL; //蛇的头尾指针
char FOOD_MAP [MAX][MAX];
char MAP[MAX][MAX];
void prinmain(void); //打印主菜单
void MAP_set_up(void); //生成地图
void FOOD_MAP_set_up(void); //生成食物地图
void snake_set_up(void); //生成蛇
void put_snake(void); //将蛇放置在地图中
void reset_MAP(void); //刷新地图
void print_all(void); //打印所有物品
int do_move(char move); //判断蛇的移动
int check_cross(void); //检测蛇是否越界
int check_kill(void); //检测蛇是否碰到自己
int check_food(void); //检测蛇是否吃到食物
void snake_add(char move); //增加蛇的长度
void game_over(); //游戏结束,释放链表
void game_over() //游戏结束,释放链表
{
Snake *temp;
while(HEAD != NULL)
{
temp = HEAD->next;
free(HEAD);
HEAD = temp;
}
}
void snake_add(char move) //增加蛇的长度
{
Snake *new;
new = (Snake *) malloc(sizeof(Snake));
if (new == NULL)
{
printf ("生成蛇失败!\n");
exit(0);
}
switch (move)
{
case 'w':
new->x = TAIL->x +1;
new->y = TAIL->y ;
break;
case 'a':
new->y = TAIL->y +1;
new->x = TAIL->x;
break;
case 's':
new->x = TAIL->x -1;
new->y = TAIL->y;
break;
case 'd':
new->y = TAIL->y -1;
new->x = TAIL->x;
break;
}
TAIL->next = new;
new->next = new;
TAIL = new;
}
int check_food(void) //检测蛇是否吃到食物
{
if (FOOD_MAP[HEAD->x][HEAD->y] == FOOD)
{
FOOD_MAP[HEAD->x][HEAD->y] = ' ';
return 1;
}
return 0;
}
int check_kill(void) //检测蛇是否碰到自己
{
Snake *temp;
temp = HEAD->next;
while (temp != NULL)
{
if (HEAD->x == temp->x && HEAD->y == temp->y)
{
return 0;
}
temp = temp->next;
}
return 1;
}
int check_cross(void) //检测蛇是否越界
{
if (HEAD->x == 0 || HEAD->x == MAX -1 || HEAD->y == 0 || HEAD->y == MAX -1)
{
return 0;
}
return 1;
}
int do_move (char move) //判断蛇的移动
{
int tempxy[COUNT][2];
int i = 0;
Snake * temp;
memset (tempxy, '0' , COUNT * 2 );
if (move == 'w' || move == 'a' || move == 's' || move == 'd')
{
temp = HEAD;
while (temp != NULL)
{
tempxy[i][0] = temp->x;
tempxy[i][1] = temp->y;
i++;
temp = temp -> next ;
}
i = 0;
temp = HEAD -> next;
while (temp != NULL)
{
temp -> x = tempxy[i][0];
temp -> y = tempxy[i][1];
i++;
temp = temp -> next;
}
temp = HEAD;
switch(move)
{
case 'w':
HEAD->x = tempxy[0][0] - 1;
break;
case 'a':
HEAD->y = tempxy[0][1] - 1;
break;
case 's':
HEAD->x = tempxy[0][0] + 1;
break;
case 'd':
HEAD->y = tempxy[0][1] + 1;
break;
}
if (!check_cross())
{
return 0;
}
if (!check_kill())
{
return 0;
}
if (!check_food())
{
COUNT++;
snake_add(move);
}
}
else if (move == 'q')
{
return 0;
}
return 1;
}
void print_all(void) //打印所有物品
{
static int count = 0;
printf ("当前得分:%d\n", COUNT);
if (COUNT - count == 4)
{
FOOD_MAP_set_up();
count = COUNT;
}
reset_MAP();
put_snake();
int i;
for(i = 0; i < MAX; i++)
{
int j;
for (j = 0; j < MAX; j++)
{
if (FOOD_MAP[i][j] != ' ')
{
printf ("%c", FOOD_MAP[i][j]);
}
else printf ("%c", MAP[i][j]);
}
putchar('\n');
}
}
void reset_MAP(void) //刷新地图
{
int i;
for (i =1; i < MAX -1; i++)
{
int j;
for (j =1; j < MAX -1; j++)
{
MAP[i][j] = ' ';
}
}
}
void put_snake(void) //将蛇放置在地图中
{
Snake * temp;
temp = HEAD -> next;
MAP[HEAD->x][HEAD->y] = 'c';
while (temp != NULL)
{
MAP[temp->x][temp->y] = SNAKE;
temp = temp->next;
}
}
void snake_set_up(void) //生成蛇
{
HEAD = (Snake *) malloc (sizeof(Snake));
if (HEAD == NULL)
{
printf ("生成蛇失败!\n");
exit(0);
}
srand (time(NULL) +1);
do
{
HEAD->x = rand()%MAX;
HEAD->y = rand()%MAX;
}
while (HEAD->x == 0 || HEAD->x == MAX -1 || HEAD->y == 0 || HEAD->y == MAX -1);
HEAD-> next = NULL;
TAIL = HEAD;
}
void FOOD_MAP_set_up(void) //生成食物地图
{
int i = 0, x, y;
memset (FOOD_MAP, ' ', MAX * MAX);
srand (time (NULL));
do
{
i++;
do
{
x = rand()%MAX;
y = rand()%MAX;
}
while (x == 0 || x == MAX -1 || y == 0 || y == MAX -1);
FOOD_MAP[x][y] = FOOD;
}
while(i!=4);
}
void MAP_set_up(void) //生成地图
{
memset (MAP, ' ', MAX * MAX);
int i;
for (i = 0; i < MAX; i++)
{
int j;
for (j = 0; j < MAX; j++)
{
if (i == 0 || j == 0 || i == MAX -1 || j == MAX -1)
{
MAP[i][j] = '0';
}
else MAP[i][j] = ' ';
}
}
}
void prinmain (void) //打印主菜单
{
printf("1.开始游戏\n");
printf("2.结束游戏\n");
printf(":");
}
int main(void)
{
char move;
prinmain();
scanf("%c",&move);
getchar();
if(move == '1')
{
MAP_set_up();
FOOD_MAP_set_up();
snake_set_up();
do
{
system("clear");
print_all();
printf("请输入指令(w/a/s/d/q):");
system("stty raw -echo");
scanf("%c", &move);
system("stty cooked echo");
}
while(do_move(move));
}
printf("\n游戏结束\n");
system("stty cooked echo");
game_over();
return 0;
}
|