这是我跟着学习视频完成的第一个小游戏,运用到的知识不多都是数组相关的知识,重要的是思路,在设计的时候要先绘制出大概的框图,要知道游戏的根本,这样会让你写程序的时候更加方便。
下面看代码:
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void test();
void menu();
void game();
int main()
{
test();
return 0;
}
void menu()
{
printf ( "*************************\n" );
printf ( "****** 1.play ******\n" );
printf ( "****** 0.exit ******\n" );
printf ( "*************************\n" );
}
void test()
{
int input = 0;
srand ((unsigned int ) time (NULL));
do
{
menu();
printf ( "请选择: " );
scanf ( "%d" , &input);
switch (input)
{
case 1:
printf ( "扫雷游戏!\n" );
game();
break ;
case 0:
printf ( "已退出游戏!\n" );
break ;
default :
printf ( "输入错误,请重新输入!\n" );
break ;
}
} while (input);
}
void game()
{
//雷的信息存储
//1.布置雷的信息
char mine[ROWS][COLS] = { 0 }; //11*11
//2.排查出的雷的信息
char show[ROWS][COLS] = { 0 };
//初始化
InitBoard(mine, ROWS, COLS, '0' );
InitBoard(show, ROWS, COLS, '*' );
//打印棋盘
DisplyBoard(mine, ROW, COL);
//DisplyBoard(show, ROW, COL);
//布置雷
SetMine(mine, ROW, COL);
DisplyBoard(mine, ROW, COL);
//扫雷
FindMine(mine, show, ROW, COL);
}
|
game.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
//'1'-'0'=1
//'3'-'0'=3
int get_mine_count( char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y - 1] - '0' +
mine[x][y - 1] - '0' +
mine[x + 1][y - 1] - '0' +
mine[x + 1][y] - '0' +
mine[x + 1][y + 1] - '0' +
mine[x][y + 1] - '0' +
mine[x - 1][y + 1] - '0' +
mine[x - 1][y] - '0' ;
}
void InitBoard( char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DisplyBoard( char board[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 " , board[i][j]);
}
printf ( "\n" );
}
}
void SetMine( char board[ROWS][COLS], int row, int col)
{
int count = COUNT;
while (count)
{
int x = rand () % row + 1;
int y = rand () % col + 1;
if (board[x][y] == '0' )
{
board[x][y] = '1' ;
count--;
}
}
}
void FindMine( char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win < ROW * COL - COUNT)
{
printf ( "请输入坐标: " );
scanf ( "%d%d" , &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
//坐标合法
//1.踩雷
if (mine[x][y] == '1' )
{
printf ( "你被炸死了!!!\n" );
DisplyBoard(mine, row, col);
break ;
}
//不是雷
else
{
//计算x,y坐标周围有几个雷
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0' ;
DisplyBoard(show, row, col);
win++;
}
}
else
{
printf ( "坐标非法,请重新输入!" );
}
}
if (win == ROW * COL - COUNT)
{
printf ( "恭喜你,排雷成功!!!\n" );
DisplyBoard(mine, row, col);
}
}
|
game.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define COUNT 80
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void InitBoard( char board[ROWS][COLS], int rows, int cols, char set);
void DisplyBoard( char board[ROWS][COLS], int row, int col);
void SetMine( char board[ROWS][COLS], int row, int col);
void FindMine( char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_48720506/article/details/121205363