菜鸟上路,可能会存在一些问题,欢迎各位同志指出改正哦!!!
废话不多说,上正文
头文件内容:
#ifndef __SNAKE_H__
#define __SNAKE_H__
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
enum Direction
{
UP=1,
DOWN,
LEFT,
RIGHT
};
enum GameStatus
{
OK,
NORMAL_END,
KILL_BY_WALL,
KILL_BY_SELF
};
#define RED "/033[0;32;31m"
#define BLUE "/033[0;32;34m"
#define WALL "■"
#define FOOD "●"
#define FLOWER "*"
#define INIT_X 10
#define INIT_Y 10
typedef struct SnakeNode
{
int x;
int y;
struct SnakeNode* next;
}SnakeNode, *pSnakeNode;
typedef struct Snake
{
pSnakeNode _psnake;
pSnakeNode _pFood;
int _TotalScore;
int _AddScore;
int _SleepTime;
enum Direction _Dir;
enum GameStatus _Status;
}Snake, *pSnake;
void GameStart(pSnake ps);
void WelcomeToGame();
void CreateMap();
void InitSnake(pSnake ps);
void CreateFood(pSnake ps);
void Pause();
void SnakeMove(pSnake ps);
int NextHasFood(pSnakeNode next,pSnake ps);
void EatFood(pSnakeNode Next,pSnake ps);
void NoFood(pSnakeNode Next,pSnake ps);
void GameRun(pSnake ps);
void GameEnd(pSnake ps);
#endif //__SNAKE_H__
函数设计:
#include"snake.h"
#include<Windows.h>
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include <WinBase.h>
void SetPos(int x,int y)
{
COORD pos={0};
HANDLE hOutput=NULL;
CONSOLE_CURSOR_INFO CursorInfo;
pos.X=x;
pos.Y=y;
hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput,pos);
GetConsoleCursorInfo(hOutput, &CursorInfo);
CursorInfo.bVisible = 0;
SetConsoleCursorInfo(hOutput, &CursorInfo);
}
void Line()
{
int i = 0;
for(i=20;i<=80;i+=2)
{
SetPos(i, 8);
printf(FLOWER );
}
for(i=20;i<=80;i+=2)
{
SetPos(i,22);
printf(FLOWER );
}
for(i=9;i<=22;i++)
{
SetPos(20,i);
printf(FLOWER);
}
for(i=9;i<=22;i++)
{
SetPos(80,i);
printf(FLOWER);
}
}
void WelcomeToGame()
{
system("mode con cols=100 lines=30");
Line();
SetPos(40,14);
printf("欢迎来到贪吃蛇游戏");
SetPos(0,28);
system("pause");
system("cls");
SetPos(62,11);
printf("游戏规则如下:");
SetPos(62,12);
printf("请通过↑,↓,←,→控制贪吃蛇的移动");
SetPos(62,13);
printf("F1加速可获得高分,F2减速");
SetPos(0,28);
system("pause");
system("cls");
}
void CreatMap()
{
int i = 0;
for(i=0;i<=58;i+=2)
{
SetPos(i, 0);
printf(WALL);
}
for(i=0;i<=58;i+=2)
{
SetPos(i,27);
printf(WALL);
}
for(i=1;i<=27;i++)
{
SetPos(0,i);
printf(WALL);
}
for(i=1;i<=27;i++)
{
SetPos(58,i);
printf(WALL);
}
}
pSnakeNode BuyNode()
{
pSnakeNode pRet=(pSnakeNode)malloc(sizeof(SnakeNode));
if(pRet==NULL)
{
perror("BuyNode::malloc()");
exit(EXIT_FAILURE);
}
pRet->x=0;
pRet->y=0;
pRet->next=NULL;
return pRet;
}
void InitSnake(pSnake ps)
{
pSnakeNode First=(pSnakeNode)malloc(sizeof(SnakeNode));
int i=0;
pSnakeNode cur=NULL;
First->x=INIT_X;
First->y=INIT_Y;
First->next=NULL;
for(i=1;i<=4;i++)
{
cur=BuyNode();
cur->x=(First->x)+2;
cur->y=(First->y);
cur->next=First;
First=cur;
}
while(cur)
{
SetPos(cur->x,cur->y);
printf(FOOD);
cur=cur->next;
}
ps->_SleepTime=500;
ps->_TotalScore=0;
ps->_Status=OK;
ps->_Dir=RIGHT;
ps->_AddScore=10;
ps->_psnake=First;
}
void CreatFood(pSnake ps)
{
pSnakeNode pFood=BuyNode();
pSnakeNode cur=ps->_psnake;
pFood->y=rand()%26+1;
do
{
pFood->x=rand()%55+2;
}while(pFood->x%2!=0);
while(cur)
{
if( (cur->x==pFood->x) && (cur->y==pFood->y) )
{
CreatFood(ps);
return ;
}
cur=cur->next;
}
SetPos(pFood->x,pFood->y);
printf(FOOD);
ps->_pFood=pFood;
}
void GameStart(pSnake ps)
{
WelcomeToGame();
CreatMap();
InitSnake(ps);
CreatFood(ps);
SetPos(0,28);
ps->_TotalScore=0;
ps->_AddScore=10;
}
int NextHasFood(pSnakeNode next,pSnake ps)
{
return( (next->x == ps->_pFood->x) && (next->y== ps->_pFood->y) );
}
void EatFood(pSnakeNode Next,pSnake ps)
{
pSnakeNode cur=NULL;
Next->next=ps->_psnake;
ps->_psnake=Next;
cur=ps->_psnake;
while(cur != NULL)
{
SetPos(cur->x,cur->y);
printf(FOOD);
cur=cur->next;
}
ps->_TotalScore+=ps->_AddScore;
CreatFood(ps);
}
void NoFood(pSnakeNode Next,pSnake ps)
{
pSnakeNode cur=NULL;
Next->next=ps->_psnake;
ps->_psnake=Next;
cur=ps->_psnake;
while(cur->next->next != NULL)
{
SetPos(cur->x,cur->y);
printf(FOOD);
cur=cur->next;
}
SetPos(cur->next->x,cur->next->y);
printf(" ");
free(cur->next);
cur->next=NULL;
}
void SnakeMove(pSnake ps)
{
pSnakeNode nextNode=BuyNode();
nextNode->x=ps->_psnake->x;
nextNode->y=ps->_psnake->y;
SetPos(65,8);
printf("总分:%d \n",ps->_TotalScore);
SetPos(65,7);
printf("加分:%d \n",ps->_AddScore);
switch (ps->_Dir)
{
case UP:
{
(nextNode->y)-=1;
if(NextHasFood(nextNode,ps))
EatFood(nextNode,ps);
else
NoFood(nextNode,ps);
break;
}
case DOWN:
{
(nextNode->y)+=1;
if(NextHasFood(nextNode,ps))
EatFood(nextNode,ps);
else
NoFood(nextNode,ps);
break;
}
case LEFT:
{
(nextNode->x)-=2;
if(NextHasFood(nextNode,ps))
EatFood(nextNode,ps);
else
NoFood(nextNode,ps);
break;
}
case RIGHT:
{
(nextNode->x)+=2;
if(NextHasFood(nextNode,ps))
EatFood(nextNode,ps);
else
NoFood(nextNode,ps);
break;
}
}
}
void Pause()
{
while(1)
{
Sleep(100);
if(GetAsyncKeyState(VK_SPACE))
break;
}
}
int KillByWall(pSnake ps)
{
if( (ps->_psnake->x==0) || (ps->_psnake->x==58)
||(ps->_psnake->y==0) || (ps->_psnake->y==27) )
{
ps->_Status=KILL_BY_WALL;
return 1;
}
return 0;
}
int KillByItself(pSnake ps)
{
pSnakeNode pnext=ps->_psnake->next;
while(pnext)
{
if( (pnext->x == ps->_psnake->x)&&(pnext->y == ps->_psnake->y) )
{
ps->_Status=KILL_BY_SELF;
return 1;
}
pnext=pnext->next;
}
return 0;
}
void PrintfHelp()
{
SetPos(62,11);
printf("游戏规则如下:");
SetPos(62,12);
printf("请通过↑,↓,←,→控制贪吃蛇的移动");
SetPos(62,13);
printf("F1加速可获得高分,F2减速");
}
void GameRun(pSnake ps)
{
do{
PrintfHelp();
if( GetAsyncKeyState (VK_UP) && ps->_Dir!=DOWN )
{
ps->_Dir=UP;
}
else if( GetAsyncKeyState (VK_DOWN) && ps->_Dir!=UP )
{
ps->_Dir=DOWN;
}
else if( GetAsyncKeyState (VK_LEFT) && ps->_Dir!=RIGHT )
{
ps->_Dir=LEFT;
}
else if( GetAsyncKeyState (VK_RIGHT) && ps->_Dir!=LEFT )
{
ps->_Dir=RIGHT;
}
else if( GetAsyncKeyState (VK_SPACE) )
{
Pause();
}
else if( GetAsyncKeyState (VK_ESCAPE) )
{
ps->_Status=NORMAL_END;
}
else if( GetAsyncKeyState (VK_F2))
{
if(ps->_SleepTime <=560)
{
ps->_SleepTime +=20;
ps->_AddScore-=2;
}
}
else if( GetAsyncKeyState (VK_F1))
{
if(ps->_SleepTime >=200)
{
ps->_SleepTime -=20;
ps->_AddScore+=2;
}
}
Sleep(ps->_SleepTime);
SnakeMove(ps);
if(KillByWall(ps))
break;
if(KillByItself(ps))
break;
}while(ps->_Status == OK);
}
void GameEnd(pSnake ps)
{
pSnakeNode cul=ps->_psnake;
pSnakeNode del=NULL;
switch(ps->_Status )
{
case KILL_BY_WALL:
{
SetPos(25,14);
system("color 04");
printf("游戏结束,你撞墙了");
SetPos(0,28);
while(cul->next)
{
del=cul;
cul=cul->next;
free(del);
del=NULL;
}
free(ps->_pFood);
ps->_pFood=NULL;
break;
}
case KILL_BY_SELF:
{
SetPos(25,14);
system("color 04");
printf("游戏结束,你撞到自己了");
SetPos(0,28);
while(cul->next)
{
del=cul;
cul=cul->next;
free(del);
del=NULL;
}
free(ps->_pFood);
ps->_pFood=NULL;
break;
}
}
}
测试代码:
#include"snake.h"
#include<stdlib.h>
void test()
{
Snake snake;
srand((unsigned int) (time(NULL)));
GameStart(&snake);
GameRun(&snake);
GameEnd(&snake);
}
int main()
{
test();
system("pause");
return 0;
}