求助!我做了一个贪吃蛇但是围墙和蛇都在闪,求令围墙不闪的方法~
#include <stdio.h>#include <malloc.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <string.h>
#define N 20
int SPEED;
int game[N][N];
enum
{
UP,DOWN,LEFT,RIGHT
}point;
typedef struct snake
{
struct snake* perior;
struct snake* next;
int x;
int y;
}snake;
snake *head,*tail;
struct Foodinfo
{
int food_x;
int food_y;
}food;
void greate_food()
{
srand((unsigned)time(NULL));
do
{
food.food_x=rand()%18;
food.food_y=rand()%18;
}while(1==game[food.food_x][food.food_y]);
game[food.food_x][food.food_y]=1;
}
void add_head(int a,int b)
{
snake *temp=(struct snake*)malloc(sizeof(struct snake));
temp->x=a;
temp->y=b;
temp->perior =NULL;
if(head==NULL)
{
head=tail=temp;
temp->perior=NULL;
}
else
{
head->perior = temp;
temp->next = head;
head=head->perior;
}
game[a][b]=1;
}
void delete_tail()
{
snake *temp=tail;
game[tail->x][tail->y]=0;
tail=temp->perior;
tail->next=NULL;
free(temp);
}
void move()
{
void output();
void greate_food();
int a=head->x;
int b=head->y;
switch(point)
{
case UP:--a;break;
case DOWN:++a;break;
case LEFT:--b;break;
case RIGHT:++b;break;
}
if(1==game[a][b]&& a !=food.food_x && b!=food.food_y)
{
output();
printf("YOU ARE LOSE !\n");
exit(0);
}
if(a==food.food_x && b==food.food_y)
{
add_head(a,b);
greate_food();
return ;
}
add_head(a,b);
delete_tail();
}
void change_point(char keydown)
{
switch(keydown)
{
case 'w':
case 'W':point=UP;break;
case 's':
case 'S':point=DOWN;break;
case 'a':
case 'A':point=LEFT;break;
case 'd':
case 'D':point=RIGHT;break;
}
}
void init()
{
int i;
for(i=0;i<=N-1;i++)
{
game[i][0]=game[i][N-1]=1;
game[0][i]=game[N-1][i]=1;
}
head=tail;
point=RIGHT;
add_head(6,4);
add_head(6,5);
add_head(6,6);
}
void output()
{
int i,j;
for(i=0;i<= N-1;i++)
{
for(j=0;j<=N-1;j++)
{
if(1==game[i][j])
printf("* ");
else
printf(" ");
}
printf("\n");
}
}
int main()
{
char level;
c1: printf("请选择游戏难度:\n 简单: 1 / 普通: 2 / 困难: 3\n");
level=getch();
switch(level)
{
case '1':SPEED=500;break;
case '2':SPEED=200;break;
case '3':SPEED=50;break;
default :
{
system("cls");
printf("请输入正确的选项。\n");
goto c1;
}
}
init();
greate_food();
output();
printf("操作方法:W ↑S ↓A ←D →\n按任意键开始游戏");
while(1)
{
char keydown=getch();
change_point(keydown);
while(!kbhit())
{
system("cls");
move();
output();
_sleep(SPEED);
}
}
}