#include
#include
#include
#include
#include
#include "snake.h"
int Score = 0;
int snakespeed = 50; // 蛇的运行速度
// 角色行为
// 暂时不知道咋写 --!
// 游戏框架
static void Game_Menu(void); // 主菜单界面
static void Start_Game(void); //开始游戏
static void Game_Help(void); // 游戏帮助
static void Game_Setting(void); // 游戏设置
static void Game_Over(void); // 结束游戏
void gotoxy(int x, int y);
// 主函数
int main(void)
{
Game_Menu();
return 0;
}
void Game_Menu(void)
{
static char key;
while(1)
{
system("cls");
system ("title 贪吃蛇");
system("DATE /T");
system("TIME /T");
//system("color ");
//system(mode)// 写完再调整
printf("|_________________________________________________|\n");
printf("| * | 1.START |\n");
printf("| * * | 2.help |\n");
printf("| * | 3.game setting |\n");
printf("| * | 0.exit |\n");
printf("| * |____________________|\n");
printf("| * | |\n");
printf("| * | |\n");
printf("| * | |\n");
printf("| * | |\n");
printf("| * | |\n");
printf("| * * | |\n");
printf("| * * | |\n");
printf("|____________________________|____________________|\n");
//响应按键
key = getch();
switch(key){
case '1': Start_Game(); break;
case '2': Game_Help(); break;
case '3': Game_Setting(); break;
case '0': Game_Over(); break;
fflush(stdin);
}
}
}
void Start_Game(void)
{
static char ch;
static int i, j;
static int Game_Over_Flag = 0;
// 初始化食物
srand((unsigned int)time(NULL));
food.x = rand() % (MAP_WIDTH - 2) + 1; // 应该是多余的 不知道为啥食物老是出现在0, 0 处
food.y = rand() % (MAP_HEIGHT - 2) + 1;
// 初始化蛇
snake.x[0] = 2;
snake.y[0] = 2;
snake.x[1] = 3;
snake.y[1] = 2;
snake.node = 2;
snake.direction = 3;
snake.life = 3;
// 初始化地图
system("cls"); // 不加这一句打印的是乱码-- 不知道为啥
for (i = 0; i < MAP_HEIGHT; i++)
for (j = 0; j < MAP_WIDTH; j++)
{
if (i == 0 || i == MAP_HEIGHT-1 || j == 0 ||j == MAP_WIDTH-1)
Map[i][j] = '.';
else
Map[i][j] = ' ';
}
for (i = 0; i < MAP_HEIGHT; i++)
puts(Map[i]);
while(1)
{
while(!kbhit())
{ // 初始化食物
if (food.yes == 1)
{
food.x = rand() % (MAP_WIDTH - 1) + 1;
food.y = rand() % (MAP_HEIGHT - 1) + 1;
food.yes = 0; // 已有食物 ,重置为1
}
if (food.yes == 0)
{
gotoxy(food.x, food.y);
putchar('g');
}
for (i = snake.node-1; i >= 0; i--)// 从尾部开始比从头部开始具有更高的流畅性
{
gotoxy(snake.x[i], snake.y[i]);
putchar('*');
}
for (i = snake.node-1; i > 0; i--)
{
snake.x[i] = snake.x[i-1];
snake.y[i] = snake.y[i-1];
}
// 判断方向
if (snake.direction == 1)
snake.y[0]--;
else if(snake.direction == 2)
snake.x[0]--;
else if (snake.direction == 3)
snake.y[0]++;
else if (snake.direction = 4)
snake.x[0]++;
// 蛇是否撞到自己
for (i = 3; i < snake.node; i++)
{
if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0])
snake.life = 0;
}
// 蛇是否撞墙
if (snake.x[0] <= 0 || snake.x[0] >= MAP_WIDTH-1 || snake.y[0] <= 0 || snake.y[0] >= MAP_HEIGHT-1)
snake.life = 0;
// 生命值为0 退出游戏
if (snake.life == 0)
{
Game_Over();
Game_Over_Flag = 1;
break;
}
// 蛇是否吃到食物
if (snake.x[0] == food.x && snake.y[0] == food.y)
{
snake.node++; // 蛇身变长
food.yes = 1; // 重置为1 不在刷新食物
Score += 100;
}
// gotoxy(snake.x[snake.node-1], snake.y[snake.node-1]); // 暂且选用高频刷新的方案
// putchar(' ');
Sleep(snakespeed);
system("cls");
for (i = 0; i < MAP_HEIGHT; i++)
puts(Map[i]);
printf("*****************************\n");
printf("当前分数: %d\n", Score);
}
// 选择方向
ch = getch();
switch(ch){
case 'w': snake.direction = 1; break;
case 'a': snake.direction = 2; break;
case 's': snake.direction = 3; break;
case 'd': snake.direction = 4; break;
}
if (Game_Over_Flag == 1)
break;
}
}
// 游戏设置安排存在较大问题
void Game_Help(void)
{
char ch;
system("cls");
printf("游戏版本:1.0\n");
printf("w,a,s,d控制移动,死亡后返回主菜单\n");
while((ch = getch()) == 'q')
{
system("cls");
return;
}
}
void Game_Setting(void)
{
char ch;
system("cls");
printf ("默认速度设置: 50\n");
printf("_________________\n");
printf("当前速度设置: %d\n", snakespeed);
printf("请输入蛇的速度值大小\n");
scanf("%d", &snakespeed);
printf("请稍后, 系统正在处理中.........\n");
Sleep(1000);
printf("设置完毕!\n");
while((ch = getch()) == 'q')
{
system("cls");
return;
}
}
void Game_Over()
{
system("cls");
gotoxy(MAP_WIDTH/2, MAP_HEIGHT/2);
printf("游戏结束!!!\n");
Sleep(2000);
system("cls");
return;
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
写了一个贪吃蛇,能运行, 可以玩,但是死过一次后再在主菜单调用Start_Game开始游戏时会闪退, 怎么回事 数据定义在头文件里