在此基础上添加一个游戏开始界面,按空格进行游戏
时间: 2024-02-27 15:55:40 浏览: 17
好的,我可以为您添加游戏开始界面的代码,以下是修改后的代码:
```c
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define ROW 20
#define COL 30
#define SPEED 100
int map[ROW][COL] = {0}; // 0 表示空白位置,1 表示蛇身,2 表示食物
int len = 0; // 蛇身长度
int x = 0, y = 0; // 蛇头坐标
int fx = 0, fy = 0; // 食物坐标
int dir = 1; // 蛇头方向,1 上,2 下,3 左,4 右
void initMap(); // 初始化地图
void drawMap(); // 绘制地图
void createFood(); // 产生食物
int move(); // 移动蛇
int check(); // 检查游戏是否结束
void startGame(); // 游戏开始界面
int main()
{
startGame(); // 游戏开始界面
initMap();
createFood();
while(1)
{
system("cls"); // 清屏
if(drawMap()) // 绘制地图,如果游戏结束返回 1
break;
if(_kbhit()) // 检测键盘输入
{
int c = _getch();
if(c == 72 && dir != 2) // 上
dir = 1;
else if(c == 80 && dir != 1) // 下
dir = 2;
else if(c == 75 && dir != 4) // 左
dir = 3;
else if(c == 77 && dir != 3) // 右
dir = 4;
}
if(!move()) // 移动蛇,如果游戏结束返回 0
break;
Sleep(SPEED); // 延时
}
printf("Game Over!\n");
return 0;
}
void initMap()
{
int i, j;
for(i = 0; i < ROW; i++)
for(j = 0; j < COL; j++)
if(i == 0 || i == ROW - 1 || j == 0 || j == COL - 1)
map[i][j] = -1; // 边界标记为 -1
}
void drawMap()
{
int i, j;
for(i = 0; i < ROW; i++)
{
for(j = 0; j < COL; j++)
{
if(map[i][j] == 0)
printf(" "); // 空白位置
else if(map[i][j] == -1)
printf("#"); // 边界
else if(map[i][j] == 1)
printf("*"); // 蛇身
else if(map[i][j] == 2)
printf("$"); // 食物
}
printf("\n");
}
return check(); // 检查游戏是否结束
}
void createFood()
{
srand(time(NULL));
do{
fx = rand() % (COL - 2) + 1;
fy = rand() % (ROW - 2) + 1;
}while(map[fy][fx] != 0);
map[fy][fx] = 2;
}
int move()
{
int nx = x, ny = y; // 新的蛇头坐标
switch(dir)
{
case 1: ny--; break; // 上
case 2: ny++; break; // 下
case 3: nx--; break; // 左
case 4: nx++; break; // 右
}
if(map[ny][nx] == -1 || map[ny][nx] == 1) // 撞到边界或自己
return 0;
if(map[ny][nx] == 2) // 吃到食物
{
len++; // 长度加 1
createFood(); // 产生新的食物
}
map[y][x] = 1; // 旧蛇头变为蛇身
x = nx; y = ny; // 更新蛇头坐标
map[y][x] = -1; // 新蛇头坐标标记为 -1
int i, j, k = 0;
for(i = 0; i < ROW; i++)
for(j = 0; j < COL; j++)
if(map[i][j] == 1)
{
if(k >= len) // 蛇尾
map[i][j] = 0; // 变为空白位置
else
k++; // 计算蛇身长度
}
return 1;
}
int check()
{
if(len >= ROW * COL - 2) // 蛇身长度达到最大值
return 1;
return 0;
}
void startGame()
{
printf("Welcome to Snake Game!\n");
printf("Press Space to Start!\n");
while(1)
{
if(_kbhit()) // 检测键盘输入
{
int c = _getch();
if(c == 32) // 按下空格键
break;
}
Sleep(50); // 延时
}
}
```
说明:
游戏开始界面使用 `startGame()` 函数实现,输出欢迎语句和提示语句,等待玩家按下空格键开始游戏。
在游戏主循环中,通过 `_kbhit()` 函数检测键盘输入,如果玩家按下空格键则退出游戏开始界面,开始游戏。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)