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

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

服务器之家 - 编程语言 - C# - C#飞机打字游戏的代码示例(winform版)

C#飞机打字游戏的代码示例(winform版)

2022-11-08 12:21Dust_SongYunfei C#

这篇文章主要介绍了C#飞机打字游戏的代码示例(winform版),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

游戏界面

C#飞机打字游戏的代码示例(winform版)

C#飞机打字游戏的代码示例(winform版)

程序代码

?
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
339
340
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 飞机大战
{
  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#飞机打字游戏内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/dust__/article/details/103117508

延伸 · 阅读

精彩推荐
  • C#C# Xamarin利用ZXing.Net.Mobile进行扫码的方法

    C# Xamarin利用ZXing.Net.Mobile进行扫码的方法

    这篇文章主要介绍了C# Xamarin利用ZXing.Net.Mobile进行扫码的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    kiba51811312022-07-24
  • C#C# 字符串、数组和List的截取和转换实例

    C# 字符串、数组和List的截取和转换实例

    下面小编就为大家分享一篇C# 字符串、数组和List的截取和转换实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    杜子烟8982022-02-10
  • C#Unity3D实现摄像机镜头移动并限制角度

    Unity3D实现摄像机镜头移动并限制角度

    这篇文章主要为大家详细介绍了Unity3D实现摄像机镜头移动并限制角度,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一...

    Studious_S5162022-09-07
  • C#C#实现JSON解析器MojoUnityJson功能(简单且高效)

    C#实现JSON解析器MojoUnityJson功能(简单且高效)

    MojoUnityJson 是使用C#实现的JSON解析器 ,算法思路来自于游戏引擎Mojoc的C语言实现 Json.h。这篇文章主要介绍了C#实现JSON解析器MojoUnityJson的方法,需要的朋友...

    scottcgi11802022-02-19
  • C#C#实现无限级联下拉列表框

    C#实现无限级联下拉列表框

    这篇文章主要为大家详细介绍了C#实现无限级联下拉列表框的相关资料,感兴趣的小伙伴们可以参考一下...

    C#教程网9112021-11-15
  • C#区分c# 前台和后台线程

    区分c# 前台和后台线程

    这篇文章主要介绍了c# 前台线程和后台线程的区别与联系,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...

    团队buff工具人7432022-09-23
  • C#C#中字符串的一般性和特殊性

    C#中字符串的一般性和特殊性

    本篇文章主要介绍了C#中字符串的一般性和特殊性的相关知识,具有很好的参考价值,下面跟着小编一起来看下吧...

    luobao5872021-12-27
  • C#C#对Windows服务组的启动与停止操作

    C#对Windows服务组的启动与停止操作

    这篇文章主要为大家详细介绍了C#对Windows服务组的启动与停止操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    马洪彪10432022-02-21