大神,求问为什么我的贪吃蛇动不了,怎么控制啊
#include<stdio.h>#include<stdlib.h>
#include<windows.h>
//
#include<time.h>
//
#include<unistd.h>
#include<conio.h>
//预定义
#define U 1
#define D 2
#define L 3
#define R 4
//
//定义全局变量区
typedef struct snake
{
int x;
int y;
struct snake* next;
} snake;
snake *head;//
snake *food;//
snake *q;//
int status=R;
//函数声明
void createMap();
//坐标转换
void gotoXY(int a,int b);
//颜色转换
int color(int x);
//创建蛇
void initSnake();
//
void creatFood();
//
void keyboradControl();
//
int snakeMove();
//
void screenTips();
void screenTips()
{
color(11);
gotoXY(64,4);
printf("最高记录");
color(11);
gotoXY(64,6);
printf("当前记录");
gotoXY(73,11);
printf("提示");
}
int cantCrossWall();
int cantCrossWall()
{
if(head->x==0||head->x==56||head->y==0||head->y==26)
{
system("cls");
gotoXY(30,6);
printf("撞墙");
return 1;
}
return 0;
}
int snakeMove()
{
snake *newNode;
newNode = (snake*)malloc(sizeof(snake));
if(status==U)
{
newNode->x=head->x;
newNode->y=head->y-1;
/*newNode->next=head;
//
head=newNode;
//
q=head;
if(newNode->x==food->x&&newNode->y==food->y)
{
while(q!=NULL)
{
//TODO
gotoXY(q->x,q->y);
color(14);
printf("◆");
q=q->next;
}
//
creatFood();
}
else
{
while(q->next->next!=NULL)
{
//TODO
gotoXY(q->x,q->y);
color(14);
printf("◆");
q=q->next;
}
//
gotoXY(q->next->x,q->next->y);
color(3);
printf("■");
//
free(q->next);
q->next=NULL;
}
}*/
if(status ==D)
{
newNode->x=head->x;
newNode->y=head->y+1;
}
if(status ==L)
{
newNode->x=head->x-2;
newNode->y=head->y;
}
if(status ==R)
{
newNode->x=head->x+2;
newNode->y=head->y;
}
newNode->next=head;
//
head=newNode;
//
if(cantCrossWall())
{
status=0;
return 3;
}
q=head;
if(newNode->x==food->x && newNode->y==food->y)
{
while(q!=NULL)
{
//TODO
gotoXY(q->x,q->y);
color(14);
printf("◆");
q=q->next;
}
//
creatFood();
}
else
{
while(q->next->next!=NULL)
{
//TODO
gotoXY(q->x,q->y);
color(14);
printf("◆");
q=q->next;
}
//
gotoXY(q->next->x,q->next->y);
color(3);
printf("■");
//
free(q->next);
q->next=NULL;
}
}}
void keyboradControl()
{ status = R;//
while(1)
{
//TODO
//
if(GetAsyncKeyState(VK_UP) && status !=D)
{
//
status = U;
}
else if(GetAsyncKeyState(VK_DOWN)&&status !=U)//暂停
{status = D;}
else if(GetAsyncKeyState(VK_LEFT)&&status !=R)
{ status = L;}
else if(GetAsyncKeyState(VK_RIGHT)&&status !=L)
{status = R;}
//
Sleep (500);
snakeMove();
}
}
void creatFood()
{
snake *food1;
food1 = (snake*)malloc(sizeof(snake));
//
srand((unsigned)time(NULL));
//
food1->x=rand()%53+2;
while((food1->x%2)!=0)
{
//
food1->x = rand()%53+2;
}
//
food1->y = rand()%23+1;
//
q = head;
while(q->next!=NULL)
{
//
if(q->x==food1->x&&q->y==food1->y)
{
free(food1);
creatFood();//
}
q=q->next;
}
//
gotoXY(food1->x,food1->y);
color(10);
printf("★");
food = food1;
}
void initSnake()
{ int i=1;
//
snake *ss;
//分配内存空间,使用头插法(单链表),以设定的x,y位置开始插入
ss= (snake*)malloc(sizeof(snake));
ss->x = 24;
ss->y = 5;
ss->next = NULL;
//
for(i=1;i<=4;i++)
{ //
head=(snake*)malloc(sizeof(snake));
head -> next = ss;
head -> x = 24 + i*2;
head -> y = 5;
ss = head;//
}
//
color(14);
while(ss !=NULL){
//TODO
gotoXY(ss->x,ss->y);
printf("◆");
ss = ss->next;
}
}
int color(int x){
//更加待打印内容的颜色属性
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),x);
return 0;
}
void gotoXY(int a,int b)
{
COORD c;
c.X =a;
c.Y =b;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
}
//函数定义、函数实现
void createMap(){
int i,j;
//因为是中文字符,所有横向占位2个坐位空间
color(5);
for(i=0;i<58;i+=2)
{ gotoXY(i,0);
printf("□");
gotoXY(i,26);
printf("□");
}
for(j=1;j<26;j++)
{
gotoXY(0,j);
printf("□");
gotoXY(56,j);
printf("□");
}
//打印中间堂哥
color(3);
for(i=2;i<56;i+=2)
{
for(j=1;j<26;j++)//j的增量为一
{
gotoXY(i,j);
printf("■");
}
}
}
int main()
{ //设置控制台的宽高
system("mode con cole=110 lines=30");
//调用创建
createMap();
screenTips();
initSnake();
creatFood();
keyboradControl();
snakeMove();
while(1)
{
//TODO
}
return 0;
}