当前位置:   article > 正文

通过C语言编程一个小游戏(乐趣无边)_编程一个最简单游戏代码

编程一个最简单游戏代码

首先附上源代码,这个是大一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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/84710
推荐阅读
相关标签
  

闽ICP备14008679号