zoukankan      html  css  js  c++  java
  • C#编写一个大字母游戏,详细代码,不懂问博主。。。。

      1 using System;
      2 using System.Drawing;
      3 using System.Windows.Forms;
      4 using System.Media;
      5 
      6 namespace dazimuyouxi_2
      7 {
      8     public partial class Form1 : Form
      9     {
     10         public Form1()
     11         {
     12             InitializeComponent();
     13         }
     14         int i = 0, j = 0;
     15 
     16         private void Form1_Load(object sender, EventArgs e)
     17         {
     18             label1.BackColor = Color.Green;
     19             label2.BackColor = Color.Red;
     20             label1.Text = "分数:" + i;
     21             label2.Text = "错误:" + j;
     22             this.KeyPress += Form1_KeyPress;
     23             this.comboBox1.SelectedIndex = 0;
     24         }
     25         bool istrue = true;
     26         SoundPlayer s = new SoundPlayer("GRAY2WEA.WAV");
     27         private void Form1_KeyPress(object sender, KeyPressEventArgs e)
     28         {
     29 
     30             foreach (Control item in Panel1.Controls)
     31             {
     32                 if (item.Text == e.KeyChar.ToString().ToUpper())
     33                 {
     34                     if (istrue == true)
     35                     {
     36                         item.Tag = "bj";
     37                         feiji.Location = new Point(new Size(item.Left + item.Width / 2 - feiji.Width / 2, feiji.Top));
     38                         Label zd = new Label();
     39                         zd.Size = new Size(40, 40);
     40                         zd.Location = new Point(new Size(feiji.Left + feiji.Width / 2 - zd.Width / 2 + 20, feiji.Top));
     41                         zd.Text = item.Text;
     42                         zd.Tag = "zidan";
     43                         zd.Image = imageList2.Images[0];
     44                         zd.AutoSize = true;
     45                         Panel1.Controls.Add(zd);
     46                         istrue = false;
     47                     }
     48                 }
     49             }
     50         }
     51         Random a = new Random();
     52         private void timer1_Tick(object sender, EventArgs e)
     53         {
     54             Label lab = new Label();
     55             lab.BackColor = Color.Transparent;
     56             lab.Size = new Size(20, 20);
     57             lab.Text = Convert.ToChar(a.Next(65, 91)).ToString();
     58             lab.ForeColor = Color.FromArgb(a.Next(256), a.Next(256), a.Next(256));
     59             lab.Font = new Font("微软雅黑", 20);
     60             lab.Location = new Point(new Size(a.Next(0, Panel1.Width - lab.Width - 10), 0));
     61             lab.AutoSize = true;
     62             lab.Tag = "zimu";
     63             this.Panel1.Controls.Add(lab);
     64         }
     65         private void timer2_Tick(object sender, EventArgs e)
     66         {
     67             foreach (Control item in Panel1.Controls)
     68             {
     69                 if (item.GetType().Name == "Label")
     70                 {
     71                     if (item.Tag.ToString() == "zimu" || item.Tag.ToString() == "bj")
     72                     {
     73                         item.Top += 5;
     74                         if (item.Bottom >= feiji.Top)
     75                         {
     76                             item.Dispose();
     77                             j += 1;
     78                             label2.Text = "错误:" + j;
     79                         }
     80                     }
     81                     if (item.Tag.ToString() == "zidan")
     82                     {
     83                         item.Top -= 20;
     84                         foreach (Control xz in Panel1.Controls)
     85                         {
     86                             if (xz.GetType().Name == "Label")
     87                             {
     88                                 if (xz.Tag.ToString() == "bj")
     89                                 {
     90                                     if (xz.Bottom >= item.Top)
     91                                     {
     92                                         PictureBox pb = new PictureBox();
     93                                         pb.Image = imageList1.Images[0];
     94                                         pb.BackColor = Color.Transparent;
     95                                         pb.Location = item.Location;
     96                                         pb.Tag = 0;
     97                                         pb.Size = new Size(30, 30);
     98                                         pb.SizeMode = PictureBoxSizeMode.StretchImage;
     99                                         //pb.BackColor = Color.Red;
    100                                         Panel1.Controls.Add(pb);
    101                                         timer3.Start();
    102                                         s.Play();
    103                                         item.Tag = 0;
    104                                         xz.Dispose();
    105                                         item.Dispose();
    106                                         i += 1;
    107                                         label1.Text = "分数:" + i;
    108                                         istrue = true;
    109                                     }
    110                                 }
    111                             }
    112                         }
    113                     }
    114                 }
    115             }
    116         }
    117 
    118         private void timer3_Tick(object sender, EventArgs e)
    119         {
    120             foreach (Control item in Panel1.Controls)
    121             {
    122                 if (item.GetType().Name == "PictureBox" && item.Tag != null)
    123                 {
    124                     int n = int.Parse(item.Tag.ToString());
    125                     n++;
    126                     if (n > 26)
    127                     {
    128                         item.Dispose();
    129                         return;
    130                     }
    131                     ((PictureBox)item).Image = imageList1.Images[n];
    132                     ((PictureBox)item).Tag = n;
    133                 }
    134             }
    135         }
    136 
    137         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    138         {
    139             comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
    140             if (comboBox1.Text == "长平之战")
    141             {
    142                 this.BackgroundImage = new Bitmap("2.jpg");
    143             }
    144             if (comboBox1.Text == "赤壁之战")
    145             {
    146                 this.BackgroundImage = new Bitmap("3.jpg");
    147             }
    148             if (comboBox1.Text == "蓝色星球")
    149             {
    150                 this.BackgroundImage = Image.FromFile("1.jpg");
    151             }
    152         }
    153 
    154         private void button1_Click(object sender, EventArgs e)
    155         {
    156             if (button1.Text == "开始")
    157             {
    158                 timer1.Start();
    159                 timer2.Start();
    160                 button1.Text = "暂停";
    161             }
    162             else
    163             {
    164                 button1.Text = "开始";
    165                 timer1.Stop();
    166                 timer2.Stop();
    167             }
    168         }
    169 
    170         private void timer4_Tick(object sender, EventArgs e)
    171         {
    172 
    173             foreach (Control item in Panel1.Controls)
    174             {
    175                 if (item.GetType().Name == "Label" && item.Tag.ToString() == "zidan")
    176                 {
    177                     item.BackColor = Color.Transparent;
    178                     int m = int.Parse(((Label)item).Image.Tag.ToString());
    179                     m++;
    180                     if (m > 3)
    181                     {
    182                         item.Dispose();
    183                         return;
    184                     }
    185                     ((Label)item).Image = imageList2.Images[m];
    186                     ((Label)item).Image.Tag = m;
    187                 }
    188             }
    189         }
    190     }
    191 }
  • 相关阅读:
    Linux
    SpringData JPA复合主键
    SpringData JPA实现CRUD,分页与多参数排序
    springboot自定义配置文件
    drools入门示例
    Java 自定义注解与注解解析实例
    GitHub创建项目,保存代码。
    SpringAOP的应用实例与总结
    springmvc与fastjson的整合,注解@RequestBody的使用
    graphviz画图与中文乱码等问题总结
  • 原文地址:https://www.cnblogs.com/wjrelax/p/8068110.html
Copyright © 2011-2022 走看看