如何用C语言写贪吃蛇

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
#include <time.h>
#include <math.h>

#define H 25
#define W 40
void map();
int move(struct snake * p);
void key(char key, int * direction);

struct snake//蛇的结构体
{
int * x, * y;
int joint;//蛇的长度
int direction;//蛇的方向
int life;
}play;

void gotoxy(int x, int y)
{
COORD c;
c.X = 2 * x, c.Y = y; //本游戏只使用双字节符号
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

void main()
{

int i,j;
struct snake * p;
p=(struct snake *)malloc(sizeof(struct snake));
p->x=(int *)malloc(sizeof(int));
p->y=(int *)malloc(sizeof(int));

p->x[0] = 15;
p->y[0] = 12;

p->x[1] = 15;
p->y[1] = 13;

p->x[2] = 15;
p->y[2] = 14;

p->x[3] = 15;
p->y[3] = 15;
p->direction=1;
p->joint=4;
printf("%d %d %d %d ",p->x[0],p->y[0],p->direction);
char ch;
while(1)
{
if(kbhit())
{
ch = getch();
key(ch,&p->direction);
p->joint++;//在这里我想测试下蛇的长度是否会增加
}
move(p);
Sleep(200);

}
getch();
}

void key(char key, int * direction)
{
if(key == 'w'&& (*direction)!=2)
(*direction)=1;
else if(key == 's'&& (*direction)!=1)
(*direction)=2;
else if(key == 'a'&& (*direction)!=4)
(*direction)=3;
else if(key == 'd'&& (*direction)!=3)
(*direction)=4;
}

int move(struct snake * p)
{
int i;
int food = 0;
if(p->direction == 1)
p->y[0]=p->y[0]-1;
else if(p->direction == 2)
p->y[0]=p->y[0]+1;
else if(p->direction == 3)
p->x[0]=p->x[0]-1;
else if(p->direction == 4)
p->x[0]=p->x[0]+1;

p->x[p->joint] = p->x[p->joint-1];
p->y[p->joint] = p->y[p->joint-1];
gotoxy(p->x[p->joint],p->y[p->joint]);
printf(" ");
for(i = (p->joint-1);i > 0;--i)
{
p->x[i] = p->x[i-1];
p->y[i] = p->y[i-1];
}
gotoxy(p->x[i],p->y[i]);
printf("■");
return 0;
}

我定义了蛇的长度是4,但是显示出来的蛇长度只有3、这个问题要怎么解决、
对于迟到食物蛇的长度就增加、这个要怎么实现、我原本只是通过改变长度来解决,但是在运行的时候蛇刚开始的时候可以正常增加长度,但是到达一定长度就会出现问题
不要一来就复制一堆代码让我看、我要的是帮我找出这个程序的错误并修正

#include<conio.h> #include<graphics.h> #include<time.h> #include<string.h> #include<malloc.h> #include<stdio.h> int grade=5,point=0,life=3; void set(),menu(),move_head(),move_body(),move(),init_insect(),left(),upon(),right(),down(),init_graph(),food_f(),ahead(),crate(); struct bug { int x; int y; struct bug *last; struct bug *next; }; struct fd { int x; int y; int judge; }food={0,0,0}; struct bug *head_f=NULL,*head_l,*p1=NULL,*p2=NULL; void main() { char ch; initgraph(800,600); set(); init_insect(); while(1) { food_f(); Sleep(grade*10); setcolor(BLACK); circle(head_l->x,head_l->y,2); setcolor(WHITE); move_body(); if(kbhit()) { ch=getch(); if(ch==27) { ahead(); set(); } else if(ch==-32) { switch(getch()) { case 72:upon();break; case 80:down();break; case 75:left();break; case 77:right();break; } } else ahead(); } else { ahead(); } if(head_f->x==food.x&&head_f->y==food.y) { Sleep(100); crate(); food.judge=0; point=point+(6-grade)*10; if(food.x<30||food.y<30||food.x>570||food.y>570) life++; menu(); } if(head_f->x<5||head_f->x>595||head_f->y<5||head_f->y>595) { Sleep(1000); life--; food.judge=0; init_graph(); init_insect(); menu(); } for(p1=head_f->next;p1!=NULL;p1=p1->next) { if(head_f->x==p1->x&&head_f->y==p1->y) { Sleep(1000); life--; food.judge=0; init_graph(); init_insect(); menu(); break; } } if(life==0) { outtextxy(280,300,"游戏结束!"); getch(); return; } move(); }; } void init_graph() { clearviewport(); setlinestyle(PS_SOLID,1,5); rectangle(2,2,600,598); setlinestyle(PS_SOLID,1,1); } void set() { init_graph(); outtextxy(640,50,"1、开始 / 返回"); outtextxy(640,70,"2、退出"); outtextxy(640,90,"3、难度"); outtextxy(640,110,"4、重新开始"); switch(getch()) { case '1': menu();setcolor(GREEN);circle(food.x,food.y,2);setcolor(WHITE);return; case '2': exit(0);break; case '3': outtextxy(700,90,":1 2 3 4 5");grade=getch()-48;set();break; case '4': food.judge=0,grade=5;point=0;life=3;init_insect();menu();break; default: outtextxy(250,300,"输入错误!"); set();break; } } void menu() { char str[20],str1[]={"分数:"},str2[]={"难度:"},str3[]={"生命值:"}; init_graph(); sprintf(str,"%d",point); strcat(str1,str); outtextxy(640,50,str1); sprintf(str,"%d",grade); strcat(str2,str); outtextxy(640,70,str2); sprintf(str,"%d",life); strcat(str3,str); outtextxy(640,90,str3); outtextxy(640,110,"设置:ESC"); } void init_insect() { head_f=(struct bug *)malloc(sizeof(struct bug)); head_f->last=NULL; head_f->x=300; head_f->y=300; p2=head_f->next=p1=(struct bug *)malloc(sizeof(struct bug)); p1->last=head_f; p1->x=295; p1->y=300; p1=p1->next=(struct bug *)malloc(sizeof(struct bug)); p1->next=NULL; p1->x=290; p1->y=300; p1->last=p2; head_l=p1; } void move() { for(p1=head_f;p1!=NULL;p1=p1->next) { circle(p1->x,p1->y,2); } } void move_head() { } void move_body() { for(p1=head_l,p2=p1->last;p2!=NULL;p1=p2,p2=p2->last) { p1->x=p2->x; p1->y=p2->y; } } void ahead() { p1=head_f; p2=p1->next; p2=p2->next; if(p1->x==p2->x) { if(p1->y>p2->y) head_f->y+=5; else head_f->y-=5; } else { if(p1->x>p2->x) { head_f->x+=5; } else head_f->x-=5; } } void upon() { p1=head_f->next; p1=p1->next; head_f->y-=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->y+=5; ahead(); } } void down() { p1=head_f->next; p1=p1->next; head_f->y+=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->y-=5; ahead(); } } void left() { p1=head_f->next; p1=p1->next; head_f->x-=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->x+=5; ahead(); } } void right() { p1=head_f->next; p1=p1->next; head_f->x+=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->x-=5; ahead(); } } void food_f() { if(!food.judge) { food.x=(rand()%117+1)*5; food.y=(rand()%117+1)*5; food.judge=1; if(food.x<30||food.y<30||food.x>570||food.y>570) { setcolor(RED); circle(f
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-13
p->x和p->y都是指向int的指针,不是数组,所以x[1]、y[1]等数据,使用的全是未申请的内存。
你应该定将他们成一个整型数组,大小足够大,或者干脆使用链表。追问

我已经用malloc函数给他们分配内存了,而且就是不想用链表所以才用的数组

追答

那就改一下main里的这两句。
p->x=(int *)malloc(sizeof(int)*N);
p->y=(int *)malloc(sizeof(int)*N);
N是你想蛇最长的时候的节数,比如,N=100*70

追问

改了和没改没区别、都会出现我说的那种问题

第2个回答  推荐于2016-11-09
我调试了一下发现有三种情况:
(断点在main 中的move函数处)
1 每一次移动有输入,方向已改。
2 每一次无输入。
3 前几次有输入,到后几次无。
结果:第一种直接在第6次就出错,斜着移动。
第二种几下就错了。
第三种,调试 一次输入一次方向,直到7次后,单句执行发现上一次蛇头(28,11),下一次蛇头(29,12)。
看出问题了吧,就是在move的时候出错,第一次的值光标位置就成了(15,11),这个是不是就不符合原意呢?
等有时间帮你再看。追问

我也发现是move那里错了、所以就只贴出来这部分的代码、但就是想不出来怎么改啊

本回答被提问者和网友采纳
第3个回答  2013-09-16
没写过……用javap写过
第4个回答  2020-06-19
相似回答