本文实例为大家分享了C#实现简单飞行棋小游戏的具体代码,供大家参考,具体内容如下
1.玩家姓名的输入
2.对屏幕进行清屏
3.初始化地图
4.玩家A和玩家B玩游戏
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
using System; namespace homework { class data { //静态字段来模拟全局变量 static int [] Maps = new int [100]; //声明一个静态数组用来存储玩家A跟玩家B的坐标 static int [] PlayerPos = new int [2]; static string [] PlayerNames = new string [2]; //两个玩家的标记 static bool [] Flags = new bool [2]; //flags[0]默认是false public static void Main(String[] args) { GameShow(); //玩家姓名的输入 #region Console.WriteLine( "请输入玩家A的姓名:" ); PlayerNames[0] = Console.ReadLine(); while (PlayerNames[0] == "" ) { Console.WriteLine( "玩家A的姓名不能为空,请重新输入" ); PlayerNames[0] = Console.ReadLine(); } Console.WriteLine( "请输入玩家B的姓名:" ); PlayerNames[1] = Console.ReadLine(); while (PlayerNames[1] == "" || PlayerNames[1] == PlayerNames[0]) { if (PlayerNames[0] == "" ) { Console.WriteLine( "玩家A的姓名不能为空,请重新输入" ); PlayerNames[1] = Console.ReadLine(); } else { Console.WriteLine( "玩家B的姓名不能与玩家A相同" ); PlayerNames[1] = Console.ReadLine(); } } #endregion //对屏幕进行清屏 #region Console.Clear(); //清屏 GameShow(); Console.WriteLine( "{0}的士兵用A表示" , PlayerNames[0]); Console.WriteLine( "{0}的士兵用B表示" , PlayerNames[1]); #endregion //画地图之前 初始化地图 #region InitailMap(); DrawMap(); #endregion //当玩家A跟玩家B没有一个人在终点时候 两个玩家不停地去玩游戏 #region while (PlayerPos[0] < 99 && PlayerPos[0] < 99) { if (Flags[0] == false ) { PlayGame(0); } else { Flags[0] = false ; } if (PlayerPos[0] > 99) { Console.WriteLine( "玩家{0}赢了玩家{1}" , PlayerNames[0], PlayerNames[1]); break ; } if (Flags[1] == false ) { PlayGame(1); } else { Flags[1] = false ; } if (PlayerPos[0] > 99) { Console.WriteLine( "玩家{0}赢了玩家{1}" , PlayerNames[0], PlayerNames[1]); break ; } } //while #endregion Console.WriteLine( "游戏结束!!!" ); Console.ReadLine(); } //画地图头 #region public static void GameShow() { Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine( "*************************************" ); Console.WriteLine( "*************************************" ); Console.WriteLine( "****************飞行棋***************" ); Console.WriteLine( "*************************************" ); Console.WriteLine( "*************************************" ); } #endregion //初始化地图 #region public static void InitailMap() { int [] luckyturn = { 6, 23, 40, 55, 69, 83 }; //幸运轮盘 for ( int i = 0; i < luckyturn.Length; i++) { Maps[luckyturn[i]] = 1; } int [] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 }; //地雷 for ( int i = 0; i < landMine.Length; i++) { Maps[landMine[i]] = 2; } int [] pause = { 2,3,4,9, 27, 60, 93 }; //暂停 for ( int i = 0; i < pause.Length; i++) { Maps[pause[i]] = 3; } int [] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 }; //时空隧道 for ( int i = 0; i < timeTunnel.Length; i++) { Maps[timeTunnel[i]] = 4; } } #endregion //画地图 #region public static void DrawMap() { Console.WriteLine( "图例:幸运圆圈:○ 地雷:☆ 暂停:▽ 时空隧道:◎" ); //第一横行 for ( int i = 0; i < 30; i++) { //如果玩家A跟玩家B的坐标相同,画一个尖括号 Console.Write(DrawStringMap(i)); } //for Console.WriteLine(); // 第一竖行 for ( int i = 30; i < 35; i++) { for ( int j = 0; j <= 28; j++) { Console.Write( " " ); } Console.WriteLine(DrawStringMap(i)); } //第二横行 for ( int i = 64; i >= 35; i--) { Console.Write(DrawStringMap(i)); } //画第二行 需要进行换行 Console.WriteLine(); //第二竖行 for ( int i = 65; i <= 69; i++) { Console.WriteLine(DrawStringMap(i)); } //第三横行 for ( int i = 70; i <= 99; i++) { Console.Write(DrawStringMap(i)); } Console.WriteLine(); } #endregion //画地图中抽象出一个方法 #region public static string DrawStringMap( int i) { string str = "" ; if (PlayerPos[0] == PlayerPos[1] && PlayerPos[1] == i) { str = "<>" ; } else if (PlayerPos[0] == i) { str = "A" ; } else if (PlayerPos[1] == i) { str = "B" ; } else { switch (Maps[i]) { case 0: str = "□" ; break ; case 1: str = "○" ; break ; case 2: str = "☆" ; break ; case 3: str = "▽" ; break ; case 4: str = "◎" ; break ; } //switch } //else return str; } #endregion //双方掷骰子过程 #region public static void PlayGame( int playerNumber) { Random r = new Random(); int rNumber = r.Next(1, 7); Console.WriteLine( "{0}按人任意键开始掷骰子" , PlayerNames[playerNumber]); Console.ReadKey( true ); Console.WriteLine( "{0}掷骰子掷出了{1}" , PlayerNames[playerNumber], rNumber); PlayerPos[playerNumber] += rNumber; Console.ReadKey( true ); Console.WriteLine( "{0}按任意键开始行动" , PlayerNames[playerNumber]); Console.ReadKey( true ); Console.WriteLine( "{0}行动完了" , PlayerNames[playerNumber]); Console.ReadKey( true ); //玩家A有可能踩到了玩家B 方块 幸运玩家 地雷 暂停 时空隧道 if (PlayerPos[playerNumber] == PlayerPos[1 - playerNumber]) { Console.WriteLine( "玩家{0}踩到了玩家{1},玩家{1}退6格" , PlayerNames[playerNumber], PlayerNames[1 - playerNumber]); PlayerPos[1 - playerNumber] -= 6; Console.ReadKey( true ); } else //踩到关卡 { //玩家坐标 switch (Maps[PlayerPos[playerNumber]]) { case 0: Console.WriteLine( "玩家{0}踩到了方块,安全" , PlayerNames[playerNumber]); Console.ReadKey( true ); break ; case 1: Console.WriteLine( "玩家{0}踩到幸运圆圈,请选择1--交换位置 2--轰炸对方" , PlayerNames[playerNumber]); string input = Console.ReadLine(); while ( true ) { if (input == "1" ) { Console.WriteLine( "玩家{0}选择跟玩家{1}交换位置" , PlayerNames[playerNumber], PlayerNames[1 - playerNumber]); Console.ReadKey( true ); int temp = PlayerPos[playerNumber]; PlayerPos[playerNumber] = PlayerPos[1 - playerNumber]; PlayerPos[1 - playerNumber] = temp; Console.WriteLine( "交换完成!!!按任意键继续游戏!!!" ); Console.ReadKey( true ); break ; } else if (input == "2" ) { Console.WriteLine( "玩家{0}选择轰炸玩家{1},玩家{1}退6格" , PlayerNames[playerNumber], PlayerNames[1 - playerNumber]); Console.ReadKey( true ); PlayerPos[1] -= 6; Console.WriteLine( "玩家{0}退了6格" , PlayerNames[1 - playerNumber]); Console.ReadKey( true ); break ; } else { Console.WriteLine( "只能输入1或者2 1--交换位置 2--轰炸对方" ); input = Console.ReadLine(); } } Console.ReadKey( true ); break ; case 2: Console.WriteLine( "玩家{0}踩到了地雷,退6格" , PlayerNames[playerNumber]); Console.ReadKey( true ); PlayerPos[1 - playerNumber] -= 6; Console.WriteLine( "玩家{0}退了6格" , PlayerNames[playerNumber]); Console.ReadKey( true ); break ; case 3: Console.WriteLine( "玩家{0}踩到了暂停,暂停一回合" , PlayerNames[playerNumber]); Flags[playerNumber] = true ; Console.ReadKey( true ); break ; case 4: Console.WriteLine( "玩家{0}踩到了时空隧道,前进10格" , PlayerNames[playerNumber]); Console.ReadKey( true ); PlayerPos[playerNumber] += 10; break ; } //switch } //else ChangePos(); Console.Clear(); DrawMap(); } #endregion //当玩家坐标发生改变需要判断两者是否超出范围 #region public static void ChangePos() { if (PlayerPos[0] < 0) { PlayerPos[0] = 0; } if (PlayerPos[0] >= 99) { PlayerPos[0] = 99; } if (PlayerPos[1] < 0) { PlayerPos[1] = 0; } if (PlayerPos[1] > 99) { PlayerPos[1] = 99; } } #endregion } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/m0_54015126/article/details/121431221