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

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

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

C#实现打字小游戏

2022-09-07 14:37Cocksuck C#

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

本文实例为大家分享了C#实现打字小游戏的具体代码,供大家参考,具体内容如下

C#实现打字小游戏

?
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
using System;
using System.Drawing;
using System.Windows.Forms;
 
namespace 打字游戏
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }
  //创建飞机
  PictureBox plan = new PictureBox();
  Button kaiguan = new Button();
  Timer flytime = new Timer();//动画事件
  //创建的得分的label
  Label df = new Label();
  int x = 0;
  //实例化血条
  Label xt = new Label();
  Label xt1 = new Label();
  int xuetiao = 150;
  int dl = 0;
 
  //产生字母
  private void Form1_Load(object sender, EventArgs e)
  {
   this.KeyPreview = true;
   //游戏区域
   this.WindowState = FormWindowState.Maximized;
   this.Text = "打字游戏";
   panel1.Size = new Size(1000, 760);
   panel1.BackColor = Color.White;
 
   timer1.Start();
   bird.Tag = "b";
   bird.Width = 229;
 
 
   bird.Height = 111;
   bird.SizeMode = PictureBoxSizeMode.StretchImage;//
   bird.Location = new Point(0, 0);
   panel1.Controls.Add(bird);
   bird.Image = imageList1.Images[0];
 
   
   flytime.Interval = 80;
   //事件的添加:+、=、tab、tab;
   flytime.Tick += Flytime_Tick;
 
   plan.Tag = "plan";
   plan.Size = new Size(100, 100);
   //
   //plan.BackColor = Color.Red;
   plan.Location = new Point(panel1.Width / 2 - plan.Width / 2, panel1.Height - plan.Height);
   plan.Image = Image.FromFile(@"../../img/BP03.png");
   panel1.Controls.Add(plan);
 
   
   kaiguan.Text = "开始游戏";
   kaiguan.Location = new Point(1200,300);
   this.Controls.Add(kaiguan);
   kaiguan.Click += Kaiguan_Click;
 
   
  }
 
  private void Kaiguan_Click(object sender, EventArgs e)
  {
   if (kaiguan.Text=="开始游戏")
   {
    flytime.Start();
    timer2.Start();
    timer3.Start();
    kaiguan.Text = "暂停游戏";
   }
   else if (kaiguan.Text=="暂停游戏")
   {
    flytime.Stop();
    timer2.Stop();
    timer3.Stop();
    kaiguan.Text = "开始游戏";
   }
   //得分栏
   df.Size = new Size(130, 20);
   df.Location = new Point(1138, 210);
   df.Tag = "df";
   df.Text = "得分:0分";
   df.AutoSize = true;
   this.Controls.Add(df);
   //血条栏
   xt.Size = new Size(130,20);
   xt.Location = new Point(1200,500);
   xt.BackColor = Color.White;
   xt1.Size = new Size(130,20);
   xt1.Location=new Point(1200,500);
   xt1.BackColor = Color.Red;
   xt.Tag = "xt";
   xt1.Tag = "xt1";
   this.Controls.Add(xt);
   this.Controls.Add(xt1);
 
 
  }
 
  PictureBox bird = new PictureBox();//显示动画的容器;
  //动画事件
  int index = 0;
  private void Flytime_Tick(object sender, EventArgs e)
  {
   index++;
   bird.Image = imageList1.Images[index];
   if (index >= 10)
   {
    index = -1;
   }
 
  }
 
  Random r = new Random();
  //鸟的移动
  private void timer2_Tick(object sender, EventArgs e)
  {
   bird.Left += 2;
   if (bird.Left >= panel1.Width)
   {
    bird.Left = -bird.Width;
   }
  }
  //字母生成
  private void timer1_Tick_1(object sender, EventArgs e)
  {
   if (bird.Left >= 0 && bird.Left <= panel1.Width - bird.Width)
   {
    //每一个lebel是一个字母;
    Label lb = new Label();
    lb.Text = ((char)r.Next(97, 123)).ToString();
    lb.Font = new Font("", r.Next(20, 40));
    lb.Tag = "lb";
    lb.AutoSize = true;
    lb.ForeColor = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
    lb.Top = bird.Height;
    lb.Left = bird.Left + bird.Width / 2 - lb.Width / 2;
 
    panel1.Controls.Add(lb);
   }
  }
  //字母下落
  private void timer3_Tick(object sender, EventArgs e)
  {
   //遍历循环语法
   foreach (Control item in panel1.Controls)
   {
    //每一个对象都有一个方法GetType
    if (item.GetType().Name == "Label")
    {
     item.Top += 3;
     if (item.Top >= panel1.Height)
     {
      item.Dispose();
      xuetiao -= 10;
      xt.Width = xuetiao;
      dl++;
      if (xt.Width==0)
      {
       flytime.Stop();
       timer2.Stop();
       timer3.Stop();
       MessageBox.Show("Game over");
      }
      
     }
 
    }
    if(item.GetType().Name== "PictureBox")
    {
     if (item.Tag.ToString()=="zd")
     {
      item.Top -= 7;
      if (item.Top+item.Height<=-item.Height)
      {
       item.Dispose();
      }
      foreach (Control ad in panel1.Controls)
      {
       if (ad.Tag.ToString()=="lba")
       {
        if (ad.Top+ad.Height>=item.Top&&item.Left==ad.Left+ad.Width/2-item.Width/2)
        {
         item.Dispose();
         ad.Dispose();
         x += 10;
         df.Text = x.ToString()+"分";
 
         PictureBox baz = new PictureBox();//装播放爆炸图片的盒子;
         baz.Tag = 0;
         baz.Image = imageList2.Images[0];
         baz.Size = new Size(90,90);
         baz.SizeMode = PictureBoxSizeMode.StretchImage;
         baz.Location = new Point(ad.Left + ad.Width / 2 - baz.Width/2,ad.Top+ad.Height/2-baz.Height/2) ;
         panel1.Controls.Add(baz);
 
         Timer bofang = new Timer();
         bofang.Start();
         bofang.Interval = 60;
         bofang.Tag = baz;
         bofang.Tick += Bofang_Tick;
;        
        }
       }
      }
      
     }
    }
 
   }
 
  }
 
 
  private void Bofang_Tick(object sender, EventArgs e)
  {
   Timer mnb = (Timer)sender;
   PictureBox picture = (PictureBox)mnb.Tag;
   picture.Image = imageList2.Images[(int)picture.Tag];
   picture.Tag = (int)picture.Tag + 1;
   if ((int)picture.Tag>=31)
   {
    picture.Dispose();
 
    mnb.Dispose();
   }
 
  }
 
  //按键消除
  private void Form1_KeyPress(object sender, KeyPressEventArgs e)
  {
   //在按下任意按键的时候,判断一下本次按键值是否有对应的字母;
   //按键键值
   //被动行为
   foreach (Control item in panel1.Controls)
   {
    if (item.GetType().Name == "Label")
    {
     if (item.Text == e.KeyChar.ToString()&&item.Tag.ToString()=="lb")
     {
      item.Tag = "lba";
      plan.Left = item.Left + item.Width / 2 - plan.Width / 2;
      //创建子弹
 
      PictureBox bullet = new PictureBox();
      bullet.Tag = "zd";
      bullet.Size = new Size(6,20);
      bullet.Image = Image.FromFile(@"../../img/Ammo4.png");
      bullet.Location = new Point(plan.Left + plan.Width / 2 - bullet.Width / 2, plan.Top - bullet.Height);
      bullet.SizeMode = PictureBoxSizeMode.StretchImage;
      panel1.Controls.Add(bullet);
      return;
     }
    }
   }
  }
 }
 
}

C#实现打字小游戏

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

原文链接:https://blog.csdn.net/Cocksuck/article/details/103308263

延伸 · 阅读

精彩推荐
  • C#C#中for循环、while循环循环执行的方法

    C#中for循环、while循环循环执行的方法

    这篇文章主要介绍了C#中for循环、while循环循环执行的方法的相关资料,非常不错,具有参考借鉴价值,感兴趣的朋友一起学习吧...

    C#教程网6132021-11-29
  • C#C# ODP.NET 调用Oracle函数返回值时报错的一个解决方案

    C# ODP.NET 调用Oracle函数返回值时报错的一个解决方案

    这篇文章主要介绍了C# ODP.NET 调用Oracle函数返回值时报错的一个解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习...

    邀月6022022-08-10
  • C#C# 可空类型的具体使用

    C# 可空类型的具体使用

    这篇文章主要介绍了C# 可空类型的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着...

    爱晓俊4402022-08-29
  • C#C# 实现对PPT文档加密、解密及重置密码的操作方法

    C# 实现对PPT文档加密、解密及重置密码的操作方法

    这篇文章主要介绍了C# 实现对PPT文档加密、解密及重置密码的操作方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下...

    E-iceblue3962022-02-12
  • C#C#使用第三方组件生成二维码汇总

    C#使用第三方组件生成二维码汇总

    本文给大家汇总了几种C#使用第三方组件生成二维码的方法以及示例,非常的简单实用,都是项目中经常需要用到的,希望大家能够喜欢...

    C#教程网5472021-12-13
  • C#Unity3D使用Shader实现腐蚀消失

    Unity3D使用Shader实现腐蚀消失

    这篇文章主要为大家详细介绍了Unity3D使用Shader实现腐蚀消失,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    星空不语5062022-07-08
  • C#C#身份证识别相关技术功能详解

    C#身份证识别相关技术功能详解

    这篇文章主要介绍了C#身份证识别相关技术详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Alex_zha9612022-01-17
  • C#基于C#实现网页爬虫

    基于C#实现网页爬虫

    这篇文章主要为大家详细介绍了基于C#实现网页爬虫的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    秋荷雨翔11012021-11-16