服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#纯代码实现打字游戏

C#纯代码实现打字游戏

2022-09-07 14:43不染-何程龙 C#

这篇文章主要介绍了这篇文章主要为大家详细介绍了C#纯代码实现打字游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

 本文实例为大家分享了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;
  }
  }
 }
 }
 }
 }
}

三、运行结果

C#纯代码实现打字游戏

C#纯代码实现打字游戏

C#纯代码实现打字游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_44034384/article/details/105892023

延伸 · 阅读

精彩推荐
  • C#Unity3D Ui利用shader添加效果

    Unity3D Ui利用shader添加效果

    这篇文章主要为大家详细介绍了Unity3D Ui利用shader添加效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    htwzl10602022-03-11
  • C#C# FileStream实现多线程断点续传

    C# FileStream实现多线程断点续传

    这篇文章主要为大家详细介绍了C# FileStream实现多线程断点续传,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    airforce0948052022-02-21
  • C#Unity实现简单虚拟摇杆(附代码)

    Unity实现简单虚拟摇杆(附代码)

    这篇文章主要为大家详细介绍了Unity实现简单虚拟摇杆,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    臣定保幼主周全ぃ3382022-09-01
  • C#C#面向对象设计的七大原则

    C#面向对象设计的七大原则

    这篇文章主要为大家详细介绍了C#面向对象设计的七大原则,未读文章之前大家说一说都有哪七大原则,是不是七大原则都可以全部列出,想要了解的朋友...

    天尽头的那片海7232021-11-21
  • C#C#组件FormDragger窗体拖拽器详解

    C#组件FormDragger窗体拖拽器详解

    这篇文章主要为大家详细介绍了C#组件FormDragger窗体拖拽器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    AhDung11842021-12-30
  • C#轻松学习C#的装箱与拆箱

    轻松学习C#的装箱与拆箱

    轻松学习C#的装箱与拆箱,在之前的文章简单的提到了轻松学习C#的装箱与拆箱,本文带着大家更加详细的介绍轻松学习C#的装箱与拆箱,感兴趣的小伙伴们...

    丿木呈广予口贝5772021-11-03
  • C#C#实现XML与实体类之间相互转换的方法(序列化与反序列化)

    C#实现XML与实体类之间相互转换的方法(序列化与反序列化)

    这篇文章主要介绍了C#实现XML与实体类之间相互转换的方法,涉及C#序列化与反序列化操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    smartsmile20125952021-11-25
  • C#C#实现的xml操作类完整实例

    C#实现的xml操作类完整实例

    这篇文章主要介绍了C#实现的xml操作类,包含C#针对xml的创建、删除、遍历、插入等常见操作,需要的朋友可以参考下...

    smartsmile201211762021-11-26