資源簡(jiǎn)介
本源代碼為基于C語(yǔ)言實(shí)現(xiàn)的貪吃蛇,通過(guò)鏈表實(shí)現(xiàn),具有打分、存儲(chǔ)分?jǐn)?shù)等功能,可以供新學(xué)習(xí)的同學(xué)進(jìn)行參考與借鑒
代碼片段和文件信息
#include
#include
#include
#include
#include
#include
#define?WIDTH?60
#define?HEIGHT?20
enum?direction
{
LEFTRIGHTUPDOWN
};
struct?structFood
{
int?x;
int?y;
};
struct?structNode
{
int?x;
int?y;
struct?structNode?*pNext;
};
struct?structSnake
{
int?length;
enum?direction?dir;
};
struct?structFood?*pFood;
struct?structSnake?*pSnake;
struct?structNode?*pNode*pTail;
int?speech=250;
double?score=0;
double?lastscore=0;
char?level=‘F‘;
double?maxeat=0;
int?smark=0;
int?stop=0;
int?counttime=0;
int?countfood=0;
time_t?startend;??
void?hideCursor(void);
void?gotoXY(int?xint?y);
void?initSnake(void);
void?addNode(int?xint?y);
void?initFood(void);
void?homePage(void);
void?keybordHit(void);
void?move(void);
void?draw(void);
void?eatFood(void);
void?addTail(void);
void?showresult(void);
void?record(void);
void?getlastscoreandlevel(void);
int?main(void)
{
start?=time(NULL);
homePage();
while(stop==0)
{
keybordHit();
move();
draw();
Sleep(speech);
}
return?0;
}
void?hideCursor(void)
{
CONSOLE_CURSOR_INFO?cursorInfo={10};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE)&cursorInfo);
}
void?gotoXY(int?xint?y)
{
COORD?pos;
pos.X=x-1;
pos.Y=y-1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE)pos);
}
void?addNode(int?xint?y)
{
struct?structNode?*newnode=(struct?structNode*)malloc(sizeof(struct?structNode));
struct?structNode?*p=pNode;
newnode->pNext=pNode;
newnode->x=x;
newnode->y=y;
pNode=newnode;
if(x<2||x>=WIDTH||y<2||y>=HEIGHT)
{
stop=1;
gotoXY(2121);
printf(“撞墻游戲結(jié)束按任意鍵退出!“);
getch();
free(pNode);
free(pSnake);
showresult();
exit(0);
}
while(p!=NULL)
{
if(p->pNext!=NULL)
{
if((p->x==x)&&(p->y==y))
{
stop=1;
gotoXY(2121);
printf(“撞到自身游戲結(jié)束按任意鍵退出!“);
getch();
free(pNode);
free(pSnake);
showresult();
exit(0);
}
}
p=p->pNext;
}
}
void?initSnake(void)
{
int?i;
pSnake=(struct?structSnake?*)malloc(sizeof(struct?structSnake));
pFood=(struct?structFood?*)malloc(sizeof(struct?structFood));
pSnake->length=5;
pSnake->dir=RIGHT;
for(i=2;i<=pSnake->length+2;i++)
addNode(i2);
}
void?homePage(void)
{
hideCursor();
printf(“xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n“);
printf(“x\t\t\t\t\t\t\t??x\n“);
printf(“x\t\t\t\t\t\t\t??x\n“);
printf(“x\t\t\t\t\t\t\t??x\n“);
printf(“x\t\t\t\t\t\t\t??x\n“);
printf(“x\t\t\t\t\t\t\t??x\n“);
printf(“x\t\t\t\t\t\t\t??x\n“);
printf(“x\t\t\t\t\t\t\t??x\n“);
printf(“x\t\t\t\t\t\t\t??x\n“);
printf(“x\t\t\t\t\t\t\t??x\n“);
printf(“x\t\t\t\t\t\t\t??x\n“);
printf(“x\t\t\t\t\t\t\t??x\n“);
printf(“x\t\t\t\t\t\t\t??x\n“);
printf(“x\t\t\t\t\t\t\t??x\n“);
printf(“x\t\t\t\t\t\t\t??x\n“);
printf(“x\t\t\t\t\t\t\t??x\n“);
printf(“x\t\t\t\t\t\t\t??x\n“);
printf(“x\t\t\t\t\t\t\t??
評(píng)論
共有 條評(píng)論