本文实例为大家分享了c#实现打字游戏的具体代码,供大家参考,具体内容如下
一、需求分析
1、界面设计 布局
- 需要哪些内容控件
- 容器的概念
- 集合的概念
2、开始游戏
- 字母的生成 26个字母 ascii码值
- 字母的载体 控件
- 字母、位置、大小、颜色
- 要求随机的
- 计时器
3、字母从上往下运动
- top变化
- 注意:垃圾回收问题,未消除的字母进行销毁,释放资源
- 计时器
4、需要产生对应的从下往上生成字母子弹,打掉字母
- 处理与键盘之间的交互,键盘相关事件
- 字母需要转换,ascii码值
- 知识点:事件参数:eventargs e
- 子弹从下往上运动
- 子弹与字母相遇,两者消失
5、添加动画效果、音效等
二、代码实现
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
|
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; //导入媒体命名空间 using system.media; namespace planetest_02 { public partial class form1 : form { public form1() { initializecomponent(); } //初始化panel panel game = new panel(); //初始化picturebox picturebox plane = new picturebox(); //初始化一个播放器 soundplayer sound = new soundplayer(); //创建爆炸效果图 imagelist imagelist = new imagelist(); //初始化随机数对象 random ra = new random(); //添加timer1,控制字母生成 timer timer1 = new timer(); //添加timer2,控制字母、子弹运动 timer timer2 = new timer(); //添加timer3,控制爆炸效果图的播放 timer timer3 = new timer(); //设置游戏开始按钮 button beginbtn = new button(); //实现记录分数 label result = new label(); //记录分数 int count = 0; //实现记录失误 label list = new label(); //记录失误 int rex = 0; private void form1_load( object sender, eventargs e) { //设置窗体属性 this .text = "天天打字母" ; this .size = new size(1000,700); this .centertoscreen(); //设置game属性 game.size = new size(800,650); game.backcolor = color.skyblue; game.borderstyle = borderstyle.fixed3d; game.location = new point(5,5); this .controls.add(game); //设置plane属性 plane.image = image.fromfile( "../../images/plane/plane4.png" ); plane.sizemode = pictureboxsizemode.autosize; plane.location = new point(game.width/2-plane.width/2,game.height-plane.height); plane.tag = "plane" ; game.controls.add(plane); //设置timer1属性,此计时器控制字母生成 timer1.tick += timer1_tick; timer1.interval = 1000; //设置timer2属性,此计时器控制字母运动 timer2.tick += timer2_tick; timer2.interval = 200; //添加键盘按下事件 this .keypress += form1_keypress; //设置按钮属性 beginbtn.text = "开始游戏" ; beginbtn.size = new size(100,30); beginbtn.location = new point(game.width+50, 20); this .controls.add(beginbtn); //生成按钮点击事件 beginbtn.click += beginbtn_click; //设置记录分数label的属性 result.size = new size(50,50); result.backcolor = color.white; result.location = new point(beginbtn.left+ beginbtn.width/2-result.width/2, beginbtn.top+ beginbtn.height+10); result.textalign = contentalignment.middlecenter; result.font = new font( "楷体" , 22); result.forecolor = color.green; this .controls.add(result); //设置记录失误label的属性 list.size = new size(50, 50); list.backcolor = color.white; list.location = new point(beginbtn.left + beginbtn.width / 2 - list.width / 2, result.top + result.height + 10); list.textalign = contentalignment.middlecenter; list.font = new font( "楷体" , 22); list.forecolor = color.red; this .controls.add(list); } /// <summary> /// 按钮点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void beginbtn_click( object sender, eventargs e) { if (beginbtn.text == "开始游戏" ) { //开启计时器 timer1.start(); timer2.start(); timer3.start(); beginbtn.text = "暂停游戏" ; //开启窗体获取键盘事件的焦点 this .keypreview = true ; } else { //关闭计时器 timer1.stop(); timer2.stop(); timer3.stop(); beginbtn.text = "开始游戏" ; //关闭窗体获取键盘事件的焦点 this .keypreview = false ; } } /// <summary> /// 字母生成 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer1_tick( object sender, eventargs e) { //初始化label label zimu = new label(); zimu.text = (( char )ra.next(97, 123)).tostring(); zimu.forecolor = color.fromargb(ra.next(255), ra.next(255), ra.next(255)); zimu.font = new font( "楷体" ,ra.next(30, 41)); zimu.location = new point(ra.next(game.width - zimu.width),0); zimu.autosize = true ; zimu.textalign = contentalignment.middlecenter; zimu.tag = "zimu" ; game.controls.add(zimu); } /// <summary> /// 字母运动 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer2_tick( object sender, eventargs e) { result.text = count.tostring(); list.text = rex.tostring(); //遍历panel中的控件 foreach (control item in game.controls) { //判断控制为label if (item.gettype().name == "label" ) { //判断其tag为字母的tag if (item.tag.tostring() == "zimu" || item.tag.tostring() == "change" ) { item.top += 10; //释放资源 if (item.top > plane.top - 40) { rex++; item.dispose(); } } } //判断为picturebox if (item.gettype().name == "picturebox" ) { //判断其tag为子弹的tag if (item.tag.tostring() == "zidan" ) { item.top -= 40; //判断字母与子弹相遇 //遍历panel中的所有控件 foreach (control it in game.controls) { //判断控件类型 if (it.gettype().name == "label" ) { //判断tag if (it.tag.tostring() == "change" ) { //判断相遇 if (it.top + it.height >= item.top) { count++; //销毁 item.dispose(); it.dispose(); //添加爆炸音效 //设置路径 sound.soundlocation = "../../sound/bomb.wav" ; //播放 sound.play(); //设置爆炸效果图大小 imagelist.imagesize = new size(50, 50); //创建爆炸图片 picturebox bomb = new picturebox(); bomb.size = new size(50, 50); bomb.tag = 0; bomb.location = new point(it.left+it.width/2-bomb.width/2,it.top+it.height/2-bomb.height/2); game.controls.add(bomb); //创建字符串表示路径 string path; for ( int i = 0; i < 32; i++) { if (i<10) { path = string .concat( "../../images/bomb/1000" , i.tostring(), ".png" ); } else { path = string .concat( "../../images/bomb/100" , i.tostring(), ".png" ); } imagelist.images.add(image.fromfile(path)); bomb.image = imagelist.images[convert.toint32(bomb.tag)]; } //设置控制爆炸图播放效果计时器(timer3)的属性 timer3.tick += timer3_tick; timer3.tag = bomb; timer3.interval = 30; timer3.start(); } } } } } } } } /// <summary> /// 控制爆炸图片 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer3_tick( object sender, eventargs e) { //通过事件发起者(sender),获取计时器的tag timer timerbox = sender as timer; //通过计时器的tag,找到bomb的tag picturebox picturebox = timerbox.tag as picturebox; //picturebox的image与imagelist的images相关联 picturebox.image = imagelist.images[convert.toint32(picturebox.tag)]; //给picturebox的tag+1 picturebox.tag = convert.toint32(picturebox.tag) + 1; if (convert.toint32(picturebox.tag) >= 31) { timerbox.dispose(); picturebox.dispose(); } } /// <summary> /// 键盘事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void form1_keypress( object sender, keypresseventargs e) { //遍历panel中的控件 foreach (control item in game.controls) { //判断是控件 if (item.gettype().name == "label" ) { //判断其tag是字母 if (item.tag.tostring() == "zimu" ) { if (item.text == e.keychar.tostring()) { //改变字母的tag item.tag = "change" ; //改变plane坐标 plane.location = new point(item.left + item.width / 2 - plane.width / 2, game.height - plane.height); //生成子弹 picturebox zidan = new picturebox(); zidan.image = image.fromfile( "../../images/bullet/ammo_enemy3.png" ); zidan.sizemode = pictureboxsizemode.autosize; zidan.tag = "zidan" ; zidan.location = new point(item.left + item.width / 2 - zidan.width / 2, game.height - plane.height); game.controls.add(zidan); //字母与子弹对应,则跳出当前循环 return ; } } } } } } } |
三、运行结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_44034384/article/details/105892023