• 大小: 9KB
    文件类型: .c
    金币: 1
    下载: 0 次
    发布日期: 2021-06-07
  • 语言: C/C++
  • 标签: 贪吃蛇  C/C++  游戏  

资源简介

本源代码为基于C语言实现的贪吃蛇,通过链表实现,具有打分、存储分数等功能,可以供新学习的同学进行参考与借鉴

资源截图

代码片段和文件信息

#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(“撞墙游戏结束按任意键退出!“);
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(“撞到自身游戏结束按任意键退出!“);
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  

评论

共有 条评论