|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
:cry 蛇长在3个以内的时候,完全没有问题啊,可是蛇长大于三有时候就会出错。求大神帮我,真心喜欢编程啊。
/* 贪吃蛇程序 */
#include <stdio.h>
#include <windows.h>
#include <malloc.h>
#include <conio.h>
#include<stdlib.h>
typedef struct snack
{
int x,y;
struct snack *next;
enum {down=0,left=1,up=2 ,right=3}turning;
} Snack;
//全局变量;
Snack *head=0,*p=0,*temp=0;
//申明部分;
void buildBox();
void functionAlter();
void beginGame();
void createSnack();
void move();
void control();
int ifeat(int a,int b) ;
void toLong(int a);
//光标定位函数;
void gotoxy(int x,int y)
{ COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
}
// 功能选择函数;
void functionAlter()
{
char ch=0;
printf("欢迎来到贪吃蛇小游戏程序,本游戏由Thomas编写\n\n请选择操作:\n1.开始游戏\n2.退出游戏\n\n");
ch= getchar();
switch(ch)
{
case '1': beginGame();break;
case '2': exit(0);
default: printf("输入有误!\n");
}
}
// switch 1,实现屏幕清空;
void beginGame()
{
system("cls");
}
//建立方框;
void buildBox()
{
int i=21;
while(--i)
printf("%41s\n","*");
i=41;
while(i--)
printf("*");
//此方框长度为40,宽度为20;
}
//创建一条蛇;静态链表
void createSnack()
{
p=head=(Snack*)malloc(sizeof(Snack));
p->x=20;
p->y=10;
p->turning=down; //up =0;down=1;left=3;right=2;
p->next=(Snack*)malloc(sizeof(Snack));
p=p->next ;
p->y=9;
p->x=20;
p->turning =down;
p->next=(Snack*)malloc(sizeof(Snack));
p=p->next ;
p->y=8;
p->x=20;
p->turning = down;
p->next=0;
//画图形;
p=head;
gotoxy(p->x,p->y);
printf("@");
p=p->next ;
gotoxy(p->x,p->y);
printf("*");
p=p->next ;
gotoxy(p->x,p->y);
printf("*");
}
// 蛇的移动实现;只移动一次;
void move()
{
p=head;
//清空蛇;
while(p!=0)
{
gotoxy(p->x,p->y);
printf(" ");
p=p->next;
}
p=head;
while(p!=0)
{
if(p->turning==up)
p->y-=1;
if(p->turning==down)
p->y+=1;
if(p->turning==right)
p->x+=1;
if(p->turning==left)
p->x-=1;
p=p->next;
}
// 动态输出蛇;
p=head;
gotoxy(p->x,p->y);
printf("@");
p=p->next ;
while(p!=0)
{
gotoxy(p->x,p->y);
printf("*");
p=p->next ;
}
Sleep(500);
//擦除尾巴;
}
//倒叙遍历链表;
void readChain(Snack* p1)//x是xia一个;
{
if(p1)
readChain(p1->next);
if(temp)
temp->turning =p1->turning;
temp=p1;
}
//控制蛇;
void control()
{
char ch=0;
//循环执行拐弯;
/* while(p&&p->next)
{
if(p->turning!=p->next->turning)//必须要倒叙;
{
// p->next->turning=p->turning;
// p=p->next ;
readChain(head);
*/
readChain(head);
temp=0;
p=head;
if(kbhit())
ch=getch();
switch(ch)
{
case'w':
if(head->turning!=down)
head->turning=up;
break;
case's':
if(head->turning!=up)
head->turning=down;
break;
case'a':
if(head->turning!=right)
head->turning=left;
break;
case'd':
if(head->turning!=left)
head->turning=right;
break;
}
}
//食物问题
int ifeat(int a,int b) //a,b为蛇头坐标; 返回0表示没吃上,返回1表示吃上了;
{
static int x=2,y=2;
if(x==2&&y==2)
{
gotoxy(x,y);
printf("#");
}
if(a==x&&b==y)
{
x+=4; y+=4;
gotoxy(x,y);
printf("#");
return 1;
}
else return 0;
}
//蛇变长!
void toLong(int a)
{
if(a==1)
{
p=head;
while(p->next )
{
p=p->next ;
}
p->next =(Snack*)malloc(sizeof(Snack));
p->next ->next =0;
switch(p->turning )
{
case right:
p->next ->x=p->x -1;
p->next ->y=p->y;
p->next ->turning=p->turning ;
break;
case left:
p->next ->x=p->x +1;
p->next ->y=p->y;
p->next ->turning=p->turning ;
break;
case up:
p->next ->x=p->x ;
p->next ->y=p->y-1;
p->next ->turning=p->turning ;
break;
case down:
p->next ->x=p->x;
p->next ->y=p->y+1;
p->next ->turning=p->turning ;
break;
}
}
}
int main()
{
int i=10;
functionAlter();
buildBox();
createSnack();
while(i)
{
move();
control();
toLong(ifeat(head->x ,head->y ) );
if(head->x >=40||head->y >=20||head->x<0||head->y<0 )
break;
}
printf("\n\n\n 游戏结束");
getchar();
}
|
|