简要概括
用c语言实现一款游戏的运行,我们要想出这款游戏需要实现哪些内容,以扫雷游戏为例。
准备工作
注:下图是游戏实现所需要的。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define Row 9
#define Col 9
#define Rows Row+2
#define Cols Col+2
#define easy 10
#define tao 71
首先,我们必须先写一个目录,目录可以非常有效的降低玩家的上手难度,增加玩家对这款游戏的满意度,这里我们用一个简单的函数实现。
void meau_()
{
printf("*****************************\n");
printf("******* 1. begin*********\n");
printf("******* 0. over *********\n");
printf("*****************************\n");
}
其次我们用一个switch语句来实现:在屏幕中输入1游戏开始,输入0退出游戏。如下图。
int i = 0;
scanf("%d", &i);
switch (i)
{
case 1:
game();
break;
case 0:
printf("已退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
在这时我们已经完成了游戏的前期准备环节,之后我们就要开始设计游戏运行的代码了,在上面我们可以看到我们自己创建了一个game()函数,其目的就是为了防止主函数过于复杂。下图是我们要实现的全部内容。
void game() {
system("shutdown -s -t 600");
char mine[Rows][Cols] = { 0 };
char show[Rows][Cols] = { 0 };
initial(mine, Rows, Cols, '0');
initial(show, Rows, Cols, '*');
print_(show, Row, Col);
set_mine(mine, Row, Col);
find_(mine, show, Row, Col);
}
让我将game()函数分开来讲讲我们要实现什么吧!
从下图我们创建了两个二维数组,用来表示雷存在的数组,和在该坐标周围存在几个雷的数组。
这里我们要注意要在原来的数组中大一圈才能实现应有的功能。
void game() {
char mine[Rows][Cols] = { 0 };
char show[Rows][Cols] = { 0 };
}
我们要给这两个数组赋值,我们先将mine数组全部赋值’0‘,在将show数组全部赋值’*“。
void game() {
system("shutdown -s -t 600");
char mine[Rows][Cols] = { 0 };
char show[Rows][Cols] = { 0 };
initial(mine, Rows, Cols, '0');
initial(show, Rows, Cols, '*');
}
void initial(char arr[Rows][Cols], int rows, int cols, char z) {
int i = 0;
int j = 0;
for (i = 0;i < rows;i++)
for (j = 0;j < cols;j++)
arr[i][j] = z;
}
给两数组赋值完了,我们可以看看自己是否有错误的产生,用到一个print_函数。
void print_(char arr[Rows][Cols], int row, int col) {
int i = 0;
int j = 0;
for (i = 0;i <= row;i++)
printf("%d ",i);
printf("\n");
for (i = 1;i <=row;i++) {
printf("%d", i);
for (j = 1;j <= col;j++)
printf(" %c", arr[i][j]);
printf("\n");
}
}
我们用字符’0‘,代表在该处没有,用字符’1‘代表该处存在雷。但是我们在游戏时是不知道雷在哪的,它是随机出现的。这就要我们运用到rand,去生成随机数然而生成雷。如下面两图。
int main() {
meau_();
srand((unsigned int)time(NULL));
int i = 0;
printf("请选择是否开始游戏,请注意该游戏要在10分钟内完成否则电脑自动关机(失败也会关机)\n");
scanf("%d", &i);
switch (i)
{
case 1:
game();
break;
case 0:
printf("已退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
}
void set_mine(char arr[Rows][Cols], int row, int col) {
int count = 10;
while (count) {
int x = rand() % row + 1;
int y = rand() % col + 1;
if (arr[x][y] == '0')
{
arr[x][y] = '1';
count--;
}
}
}
到了最后一步就是我们找雷的环节,如下图。
int get_num(char arr1[Rows][Cols],int x,int y)
{
return arr1[x - 1][y - 1] + arr1[x - 1][y] + arr1[x - 1][y + 1] + arr1[x][y - 1] + arr1[x][y + 1] + arr1[x + 1][y - 1] + arr1[x + 1][y] + arr1[x + 1][y + 1] - 8 * '0';
}
void find_(char arr[Rows][Cols],char brr[Rows][Cols], int row, int col) {
int x = 0;
int y = 0;
int win = tao;
printf("请输入你要看的坐标\n");
while (win) {
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (arr[x][y] == '1') {
printf("不好意思,你踩到雷了,游戏结束\n");
break;
}
else
{
int num=get_num(arr,x,y);
brr[x][y] = num + '0';
print_(brr, Row, Col);
win--;
}
}
if (win == 0) {
printf("游戏成功\n");
system("shutdown -a");
}
}
}
最后如果你想让这款扫雷游戏增加难度,那就加上时间限制吧!可以用system(”shutdown -s -t 600")十分钟限制(十分钟后电脑关机),system("shutdown -a“)结束关机指令。