贪吃蛇:为什么没吃到食物有时也增加一个星号
#include<stdio.h>#include<windows.h>
//#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
#define TURN_NUM 1000
#define INIT_LENGTH 8
#define UP_EDGE 0
#define DOWN_EDGE 24
#define LEFT_EDGE 0
#define RIGHT_EDGE 79
#define X 0
#define Y 0
char buf[1000]="@*******";
char *snake=buf;
char FOOD='$';
int food_num=0;
int score=0;
int snake_length=INIT_LENGTH;
int cursor[2]={LEFT_EDGE+20+INIT_LENGTH+1,UP_EDGE+10};
int head[2]={LEFT_EDGE+20+INIT_LENGTH,UP_EDGE+10};
int tail[2]={0,0};
int old_tail[2];
int food[2]={0,0};
int init_position[2]={LEFT_EDGE+X,UP_EDGE+Y};
int direction=RIGHT;
int turn_point[TURN_NUM][2];
int head_turn_num=0;
int tail_turn_num=0;
int game_over=0;
void main()
{
void gotoxy(int x,int y);
void generate_food();
void cur_state();
void print_introduction();
void init();
void control();
void move();
void move_up();
void move_down();
void move_left();
void move_right();
void update_tail_position();
void eat_food();
char ch;
while(!game_over)
{
print_introduction();
printf("\t\t按任意键开始游戏\n");
getch();
system("cls");
//cur_state();
init();
control();
system("cls");
gotoxy(0,10);
printf("\t\t当前得分是%d\n",score);
printf("\t\t这条蛇共转了%d次,他的深长%d\n",head_turn_num,strlen(snake));
printf("\t\t游戏结束还玩么?\n");
scanf("%c",&ch);
getchar();
if(ch=='y'||ch=='Y')
continue;
else
{
system("cls");
printf("\n\n\t\t\t谢谢,再见\n");
game_over=1;
}
}
}
void print_introduction()
{
int i=0;
int rule_num;
char *rule[4];
rule[i++]="\n\n\t\t\t欢迎来玩贪吃蛇游戏\n";
rule[i++]="\t\t游戏规则如下:\n";
rule[i++]="\t\t1.按上下左右键控制蛇的运动方向\n";
rule[i++]="\t\t2.按CTRL+C结束当前游戏\n";
rule_num=i;
system("cls");
for(i=0;i<rule_num;i++)
{
puts(rule[i]);
}
}
void cur_state()
{
printf("游戏当前状态的参数如下:\n");
printf("snake=%s\n",snake);
printf("snake_body_length=%d\n",strlen(snake));
printf("head=(%d,%d)\n",head[0],head[1]);
printf("tail=(%d,%d)\n",tail[0],tail[1]);
printf("direction=%d\n",direction);
printf("game_over=%d\n",game_over);
}
void gotoxy(int x,int y)
{
COORD coord={x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void generate_food()
{
int i=0,j=0;
do
{
i=rand()%DOWN_EDGE;
}while(i<UP_EDGE||i>DOWN_EDGE);
do
{
j=rand()%RIGHT_EDGE;
}while(j<LEFT_EDGE||j>RIGHT_EDGE);
food[0]=j;
food[1]=i;
gotoxy(food[0],food[1]);
putchar(FOOD);
gotoxy(cursor[0],cursor[1]);
}
void init()
{
int i;
food_num=0;
score=0;
snake_length=INIT_LENGTH;
cursor[0]=init_position[0]+INIT_LENGTH;
cursor[1]=init_position[1];
head[0]=init_position[0]+INIT_LENGTH-1;
head[1]=init_position[1];
tail[0]=init_position[0];
tail[1]=init_position[1];
direction=RIGHT;
head_turn_num=0;
tail_turn_num=0;
game_over=0;
turn_point[0][0]=RIGHT_EDGE;
turn_point[0][1]=0;
generate_food();
gotoxy(init_position[0],init_position[1]);
for(i=snake_length;i>0;i--)
putchar(snake[i-1]);
}
void update_tail_position()
{
old_tail[0]=tail[0];
old_tail[1]=tail[1];
if(tail_turn_num<head_turn_num)
{
if(tail[0]<turn_point[tail_turn_num%TURN_NUM][0])
tail[0]+=1;
else if(tail[0]>turn_point[tail_turn_num%TURN_NUM][0])
tail[0]-=1;
else if(tail[1]<turn_point[tail_turn_num%TURN_NUM][1])
tail[1]+=1;
else if(tail[1]>turn_point[tail_turn_num%TURN_NUM][1])
tail[1]-=1;
else if(tail[0]==turn_point[(tail_turn_num)%TURN_NUM][0]&&tail[1]==turn_point[(tail_turn_num)%TURN_NUM][1])
{tail_turn_num+=1;}
}
else if(tail_turn_num==head_turn_num)
{
if(tail[0]<head[0])
tail[0]+=1;
else if(tail[0]>head[0])
tail[0]-=1;
else if(tail[1]<head[1])
tail[1]+=1;
else if(tail[1]>head[1])
tail[1]-=1;
}
}
void move_up()
{
if(cursor[1]>=UP_EDGE)
{
gotoxy(head[0],head[1]);
putchar('*');
gotoxy(head[0],head[1]-1);
putchar(snake[0]);
gotoxy(tail[0],tail[1]);
putchar(0);
update_tail_position();
head[1]-=1;
cursor[0]=head[0];
cursor[1]=head[1]-1;
gotoxy(cursor[0],cursor[1]);
}
else
gotoxy(head[0],head[1]);
}
void move_down()
{
if(cursor[1]<=DOWN_EDGE)
{
gotoxy(head[0],head[1]);
putchar('*');
gotoxy(head[0],head[1]+1);
putchar(snake[0]);
gotoxy(tail[0],tail[1]);
putchar(0);
update_tail_position();
head[1]+=1;
cursor[0]=head[0];
cursor[1]=head[1]+1;
gotoxy(cursor[0],cursor[1]);
}
else
gotoxy(head[0],head[1]);
}
void move_left()
{
if(cursor[0]>=LEFT_EDGE)
{
gotoxy(head[0],head[1]);
putchar('*');
gotoxy(head[0]-1,head[1]);
putchar(snake[0]);
gotoxy(tail[0],tail[1]);
putchar(0);
update_tail_position();
head[0]-=1;
cursor[0]=head[0]-1;
cursor[1]=head[1];
gotoxy(cursor[0],cursor[1]);
}
else
gotoxy(head[0],head[1]);
}
void move_right()
{
if(cursor[0]<=RIGHT_EDGE)
{
gotoxy(head[0],head[1]);
putchar('*');
gotoxy(head[0]+1,head[1]);
putchar(snake[0]);
gotoxy(tail[0],tail[1]);
putchar(0);
update_tail_position();
head[0]+=1;
cursor[0]=head[0]+1;
cursor[1]=head[1];
gotoxy(cursor[0],cursor[1]);
}
else
gotoxy(head[0],head[1]);
}
void move()
{
if(direction==UP)
move_up();
else if(direction==DOWN)
move_down();
else if(direction==LEFT)
move_left();
else if(direction==RIGHT)
move_right();
}
void eat_food()
{
snake[snake_length++]=snake[1];
tail[0]=old_tail[0];
tail[1]=old_tail[1];
gotoxy(tail[0],tail[1]);
putchar(snake[1]);
food_num++;
score=food_num;
gotoxy(cursor[0],cursor[1]);
}
void control()
{
char command;
while(1)
{
command=getch();
if(command=='W'&&(direction==LEFT||direction==RIGHT))
{
direction=UP;
turn_point[head_turn_num%TURN_NUM][0]=head[0];
turn_point[head_turn_num%TURN_NUM][1]=head[1];
head_turn_num++;
}
else if(command=='A'&&(direction==UP||direction==DOWN))
{
direction=LEFT;
turn_point[head_turn_num%TURN_NUM][0]=head[0];
turn_point[head_turn_num%TURN_NUM][1]=head[1];
head_turn_num++;
}
else if(command=='S'&&(direction==LEFT||direction==RIGHT))
{
direction=DOWN;
turn_point[head_turn_num%TURN_NUM][0]=head[0];
turn_point[head_turn_num%TURN_NUM][1]=head[1];
head_turn_num++;
}
else if(command=='D'&&(direction==UP||direction==DOWN))
{
direction=RIGHT;
turn_point[head_turn_num%TURN_NUM][0]=head[0];
turn_point[head_turn_num%TURN_NUM][1]=head[1];
head_turn_num++;
}
else
{
}
move();
}
}