关于用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; }我这段代码主要是实现蛇的移动的、但是蛇的长度增加到一定程度以后就出问题了(会斜着走)这要怎么解决啊、求大神帮忙改改