游戏界面
程序代码
复制代码
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339using 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 飞机大战 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Panel BG = new Panel();// Panel 容器 游戏区对象 Panel cebainqu = new Panel(); // 按钮区对象 PictureBox player = new PictureBox(); // 实例化飞机对象 Random rand = new Random();// 实例化随机对象 Timer CreateTime = new Timer();// 字母生成定时器 Timer Flytime = new Timer();// 字母下落定时器 Label btn = new Label(); // 创建按钮对象 Label defeng = new Label();// 得分卡对象 Label xuetiao = new Label();// 血条对象 int count = 0; // 存储器 记录得分 SoundPlayer baozhasound = new SoundPlayer(@"../../music/game_over.wav"); // 爆炸音乐对象 PictureBox feijwei1 = new PictureBox();// 创建飞机尾气对象 PictureBox feijwei2 = new PictureBox();// 创建飞机尾气对象 List<Label> labels = new List<Label>(); // 实例化泛型和对象labels用来存储字母 List<PictureBox> picts = new List<PictureBox>(); // 实例化泛型对象picts用来存储子弹 private void Form1_Load(object sender, EventArgs e) { this.Size = new Size(1000, 700); //this.FormBorderStyle= FormBorderStyle.FixedToolWindow; // 去掉窗体图标 this.Text = "字母大战"; this.BackColor = Color.FromArgb(80, 00, 80); // this.BackgroundImage = Image.FromStream(@""); this.Left = Screen.PrimaryScreen.WorkingArea.Width / 2 - this.Width / 2; this.Top = Screen.PrimaryScreen.WorkingArea.Height / 2 - this.Height / 2; // 创建游戏区 BG.Width = 800; BG.Height = 600; BG.BackColor = Color.White; this.Controls.Add(BG); BG.Location = new Point(20, 20); // 字母生成 CreateTime.Interval = 1000; // 设置生成毫秒数 CreateTime.Tick += CreateTime_Tick; // 控制下落 Flytime Flytime.Interval = 40; Flytime.Tick += Flytime_Tick; //创建的飞机 player.Size=new Size(80, 80); player.Top = BG.Height - player.Height-50; player.Left = BG.Width / 2 - player.Width / 2; player.Image = Image.FromFile(@"../../img/YP03.png"); player.SizeMode = PictureBoxSizeMode.StretchImage; // 自适应大小 player.Tag = "player"; BG.Controls.Add(player); // 创建尾气 两个对象 feijwei1.Size = new Size(15, 30); feijwei1.Tag = 0; feijwei1.Top = player.Top + player.Height+feijwei1.Height/2-15; feijwei1.Left = player.Left + player.Width / 2 -feijwei1.Width-5; feijwei1.Image = imageList2.Images[0]; BG.Controls.Add(feijwei1); feijwei2.Size = new Size(15, 30); feijwei2.Tag = 0; feijwei2.Top= player.Top + player.Height + feijwei1.Height / 2 -15; feijwei2.Left = player.Left + player.Width / 2 + feijwei1.Width-5; feijwei2.Image = imageList3.Images[0]; BG.Controls.Add(feijwei2); // 尾气1定时器 Timer weiqitimer1 = new Timer(); weiqitimer1.Tag = feijwei1; weiqitimer1.Tick += Weiqitimer1_Tick; weiqitimer1.Start(); //尾气2定时器 Timer weiqitimer2 = new Timer(); weiqitimer2.Tag = feijwei2; weiqitimer2.Tick += Weiqitimer2_Tick; weiqitimer2.Start(); //添加键盘事件 this.KeyPress += Form1_KeyPress; // 创建按钮区 cebainqu.Width = 160; cebainqu.Height = 600; cebainqu.Location = new Point(820,20); cebainqu.BackColor = Color.FromArgb(180, 15, 123); this.Controls.Add(cebainqu); // 创建按钮 btn.Location = new Point(20, 20); btn.BorderStyle = BorderStyle.FixedSingle; btn.AutoSize = true; btn.Text = "游戏开始"; btn.Cursor = Cursors.Hand; // 鼠标移入到变为手型 btn.Font = new Font("", 15); btn.ForeColor = Color.FromArgb(97,177,48); btn.BackColor = Color.FromArgb(191,143,243); cebainqu.Controls.Add(btn); btn.Click += Btn_Click; // 得分卡 defeng.Font = new Font("", 15); defeng.Location = new Point(20, 50); defeng.AutoSize = true; defeng.Text = "得分: "+count+"分"; cebainqu.Controls.Add(defeng); //血条字体 Label xueTiao = new Label(); xueTiao.Text = " 能量"; xueTiao.Size = new Size(100, 30); xueTiao.Font = new Font("楷体",20); xueTiao.ForeColor = Color.Yellow; xueTiao.Location = new Point(20, 70); cebainqu.Controls.Add(xueTiao); // 血条 xuetiao.Size = new Size(100, 20); xuetiao.BackColor = Color.Red; xuetiao.Location = new Point(20, 100); cebainqu.Controls.Add(xuetiao); // 血条底部 Label xuetdi = new Label(); xuetdi.Size = new Size(100, 20); xuetdi.BackColor = Color.White; xuetdi.Location = new Point(20, 100); cebainqu.Controls.Add(xuetdi); } //飞机尾气定时器1 private void Weiqitimer1_Tick(object sender, EventArgs e) { Timer weiqi1 = sender as Timer; PictureBox weiQi = weiqi1.Tag as PictureBox; weiQi.Image = imageList2.Images[(int)weiQi.Tag]; weiQi.Tag = (int)weiQi.Tag + 1; if ((int)weiQi.Tag > 1) { weiQi.Tag = 0; } } //飞机尾气定时器2 private void Weiqitimer2_Tick(object sender, EventArgs e) { Timer weiqi2 = sender as Timer; PictureBox weiQi = weiqi2.Tag as PictureBox; weiQi.Image = imageList3.Images[(int)weiQi.Tag]; weiQi.Tag = (int)weiQi.Tag + 1; if ((int)weiQi.Tag>1) { weiQi.Tag = 0; } } // 游戏开始/暂停 private void Btn_Click(object sender, EventArgs e) { //Label btn = (Label)sender; // 获取始发者 if (btn.Text=="游戏开始") { CreateTime.Start(); // 字母生成定时器启动 Flytime.Start(); // 字母下落定时器启动 btn.BackColor = Color.Red; btn.ForeColor = Color.White; btn.Text = "游戏暂停"; } else if(btn.Text=="游戏暂停") { CreateTime.Stop(); // 字母生成定时器关闭 Flytime.Stop(); // 字母下落定时器关闭 btn.BackColor = Color.FromArgb(191, 143, 243); btn.ForeColor = Color.FromArgb(97, 177, 48); btn.Text = "游戏开始"; } } private void CreateTime_Tick(object sender, EventArgs e) { // 生成字母在label中 Label lb = new Label(); lb.Text = ((char)rand.Next(97, 123)).ToString(); // 97-123 随机ASCLL字母 lb.Font = new Font("", rand.Next(20, 30)); // 字体随机15-20 lb.ForeColor =Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256)); lb.Top = 0; lb.Left = rand.Next(0, BG.Width - lb.Width); // 随机到游戏区的宽度 lb.AutoSize = true; // 自适应大小 lb.BackColor = Color.Transparent; // 透明 lb.Tag = "zimu"; BG.Controls.Add(lb); // 字母添加到游戏区 labels.Add(lb); // 字母添加到labels中 } // 控件字母下落,子弹上升 private void Flytime_Tick(object sender, EventArgs e) { foreach (Control item in BG.Controls) { if (item.Tag.ToString()=="zimu" || item.Tag.ToString() == "biaoji") { item.Top += 5; if (item.Top > BG.Height) // 字母超过bg高度 字母删除 { item.Dispose(); xuetiao.Width -= 10; if (xuetiao.Width==0) { CreateTime.Stop(); // 字母生成定时器关闭 Flytime.Stop(); // 字母下落定时器关闭 qing();// 调用清除字母方法 zdqing(); // 调用清除子弹方法 xuetiao.Size = new Size(100, 20);// 显示血条 defeng.Text = "得分: " + count + "分"; btn.Text = "游戏开始"; // MessageBox.Show弹框第一个参数为弹框内容,第二参数为标题,第三个参数为按钮,第四个参数为图标 MessageBox.Show("游戏结束"+"得分为:"+count+"分","游戏结束",MessageBoxButtons.YesNo,MessageBoxIcon.Information); count = 0; defeng.Text = "得分: " + count + "分"; } } } //判断子弹 if (item.Tag.ToString()=="zidan") { item.Top -= 5; if (item.Top<0) // 当子弹超出bg高度 子弹删除 { item.Dispose(); } foreach (Control zm in BG.Controls) { // 子弹碰到字母 if (zm.Tag.ToString()=="biaoji") { if (item.Top<=zm.Top+zm.Height && item.Left+item.Width/2==zm.Left+zm.Width/2) // 子弹的位置小于字母的位置 子弹穿过字母 { // 子弹,字母消失 item.Dispose(); zm.Dispose(); // 播放动画 PictureBox bombBox = new PictureBox(); // 动画对象 bombBox.Tag = 0; // 给bombBox的属性Tag 设置为0 是为遍历动画图片 bombBox.Size = new Size(50, 50); // 设置爆炸图片在字母位置 bombBox.Location = new Point(zm.Left + zm.Width / 2 - bombBox.Width / 2, zm.Top - zm.Height / 2 + bombBox.Height / 2); BG.Controls.Add(bombBox); // 添加到游戏区 // 动画添加定时器 Timer bombTimer = new Timer(); bombTimer.Tag = bombBox; // 将bombBox存储到bombTimer的Tag属性中 bombTimer.Interval = 10; bombTimer.Tick += BombTimer_Tick; bombTimer.Start();// 开启定时器 // 记录得分 count++; defeng.Text = "得分: " + count.ToString() + "分"; // 开启爆炸声音 baozhasound.Play(); } } } } } } // 爆炸定时器 private void BombTimer_Tick(object sender, EventArgs e) { Timer bombtimer = (Timer)sender; // BombTimer的发起者 即bombTimer,重新bombtimer名 PictureBox picture = (PictureBox)bombtimer.Tag;// 获取bombTimer的Tag属性,即bombBox picture.Image = imageList1.Images[(int)picture.Tag];// 添加图片 picture.Tag = (int)picture.Tag + 1; // 索引加1 if ((int)picture.Tag>31) // 超过索引为31时,定时器清除,图片清除 { bombtimer.Dispose(); picture.Dispose(); } } // 键盘事件 private void Form1_KeyPress(object sender, KeyPressEventArgs e) { // 事件 e参数 foreach (Control item in BG.Controls) { // 判断按键值和字母对应 if (item.Text==e.KeyChar.ToString()&& item.Tag.ToString()=="zimu") { item.Tag = "biaoji"; //飞机的移动 飞机的left=字母的left+字母的width-飞机的width/2; player.Left = item.Left + item.Width / 2 - player.Width / 2; // 尾气移动 feijwei1.Left = player.Left + player.Width / 2 - feijwei1.Width - 5; feijwei2.Left = player.Left + player.Width / 2 + feijwei1.Width - 5; // 创建子弹 PictureBox bullet = new PictureBox(); bullet.Size = new Size(8,28); bullet.Tag = "zidan"; bullet.Image = Image.FromFile(@"../../img/Ammo1.png"); bullet.SizeMode = PictureBoxSizeMode.StretchImage; // 自适应大小 bullet.Left = player.Left + player.Width / 2 - bullet.Width / 2; // 在飞机上面 bullet.Top = player.Top - bullet.Height; BG.Controls.Add(bullet); picts.Add(bullet); // 子弹添加到picts中 return; // 跳出创建 只创建一颗子弹 } } } // 字母清除方法 private void qing() { foreach (Label item in labels) { item.Dispose(); } } // 子弹清除方法 private void zdqing() { foreach (PictureBox item in picts) { item.Dispose(); } } // 任务栏中右键点击关闭事件 private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e) { this.Close();// 关闭窗体 } } }
到此这篇关于C#飞机打字游戏的代码示例(winform版)的文章就介绍到这了,更多相关C#飞机打字游戏内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!
最后
以上就是干净大雁最近收集整理的关于C#飞机打字游戏的代码示例(winform版)的全部内容,更多相关C#飞机打字游戏内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复