做一个简单的贪吃蛇小游戏
Snake.c
#include "Snake.h"
void CreatWall()
{
int i = 0;
for (i = 0; i <= 58; i+=2)
{
SetPos(i, 0);
printf(WALL);
}
for (i = 0; i <= 58; i += 2)
{
SetPos(i, 28);
printf(WALL);
}
for (i = 1; i <= 28; i++)
{
SetPos(0, i);
printf(WALL);
}
for (i = 1; i <= 28; i++)
{
SetPos(58, i);
printf(WALL);
}
SetPos(80, 20);
}
void GameHelp()
{
SetPos(65, 12);
printf("用↑,↓,←,→来控制方向");
SetPos(65, 13);
printf("按空格暂停,按F1加速,按F2减速");
SetPos(65, 14);
printf("按ESC退出");
}
void GameStart(pSnake ps)
{
WelcomeToGame();
CreatWall();
InitSnake(ps);
CreateFood(ps);
GameHelp();
ps->_Addscore = 1;
ps->_Dir = RIGHT;
ps->_Sleeptime = 200;
ps->_Status = OK;
ps->_Totalscore = 0;
GameRun(ps);
GameEnd(ps);
}
void WelcomeToGame()
{
system("mode con cols=100 lies=30");
SetPos(45, 14);
printf("welcome to Snake\n");
SetPos(100, 30);
system("pause");
system("cls");
SetPos(45, 14);
printf("use up,down,left,right to control snake\n");
SetPos(45, 15);
printf("different speed diffferent score\n");
SetPos(100, 30);
system("pause");
system("cls");
}
void SetPos(int x, int y)
{
COORD pos = { 0 };
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);}
pSnakeNode BuyNode()
{
pSnakeNode pNewNode = (pSnakeNode)malloc(sizeof(Snake));
pNewNode->x = 0;
pNewNode->y = 0;
pNewNode->next = NULL;
return pNewNode;
}
void InitSnake(pSnake ps)
{
pSnakeNode frist = BuyNode();
pSnak