编程

来源:互联网 发布:ce6.0软件 编辑:程序博客网 时间:2024/05/17 06:56

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace TypeWriteGame {
 public partial class Form1 : Form {
  public Form1() {
   InitializeComponent();
   Form.CheckForIllegalCrossThreadCalls=false;
  }

  private void Form1_Load(object sender, EventArgs e) {
   this.time.Start();
  }
  int i=0;
  private void time_Tick(object sender, EventArgs e) {
   try{
    Random rPic=new Random();
    Label lable=new Label();
    lable.Size=new Size(51,51);
    lable.Left=rPic.Next(this.Width-50);
    lable.Image=Image.FromFile(rPic.Next(10)+".jpg");
    lable.Text=Convert.ToChar(65+rPic.Next(57)).ToString();
    lable.TextAlign=ContentAlignment.MiddleCenter;
    lable.Refresh();
    lable.Font=new Font("宋体",15,FontStyle.Bold);
    
    this.Controls.Add(lable);

    Thread thread = new Thread(Mover);
    thread.IsBackground = true;
    thread.Start(lable);
    i++;
    if (i == 10) {
     this.time.Interval -= 10;
     i = 0;
    }
   }
   catch(System.Exception E)
   {
     MessageBox.Show(E.Message);
   }
  }

  Random random = new Random();

  public void Mover(Object o) {
   Label lbl = o as Label;
   int speed = random.Next(3) + 2;

   while (lbl.Top < this.Height) {
    Thread.Sleep(speed * 5);
    this.Invoke(new ParameterizedThreadStart(ChangeLocation), new Object[] { lbl });
   }
  }

  public void ChangeLocation(Object o) {
   Label lbl = o as Label;
   lbl.Top += 1;
  }

  private void Form1_KeyPress(object sender, KeyPressEventArgs e) {
    foreach (Label lbl in this.Controls)
    {
      if (lbl == null)
        break;
      if (lbl.Text[0] == e.KeyChar)
      {
        lbl.Dispose();
        break;
      }
    }
  }
 }
}

 

 

刚做的打字游戏,希望大家看看,帮我评价一下。

原创粉丝点击