赞
踩
首先附上源代码,这个是大一C语言程序设计基础的大作业,所有代码为手打,部分链表内容参考了网上的程序
#include<stdio.h>
#include<process.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#define WIDTH 60
#define HEIGHT 20
enum direction
{
LEFT,RIGHT,UP,DOWN
};
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 start,end;
void hideCursor(void);
void gotoXY(int x,int y);
void initSnake(void);
void addNode(int x,int 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={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursorInfo);
}
void gotoXY(int x,int y)
{
COORD pos;
pos.X=x-1;
pos.Y=y-1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void addNode(int x,int 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(21,21);
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(21,21);
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(i,2);
}
void homePage(void)
{
hide
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。