做完没有多久的打字游戏
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Threading;
namespace playee
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int number=0;
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
this.Focus();
foreach (Label lblzimu in this.Controls)
{
if (lblzimu.Text.Equals(e.KeyChar.ToString()))
{
this.Controls.Remove(lblzimu);
number+=10;
//MessageBox.Show(rod.ToString());
}
}
lblnumber.Text = number + "";
//textBox1.Text = number + "";
//最好用foreach,用for的时候循环过大,机器可能运行不过来
//for (int i = 0; i < arr.Count; i++)
//{
// if (((Label)arr[i]).Text == e.KeyChar.ToString())
// {
// this.Controls.Remove((Label)arr[i]);
// arr.Remove(arr[i]);
// break;
// }
//}
}
Label lb = null;
private void Form1_Load(object sender, EventArgs e)
{
label1.Top = -2;
label2.Top = -2;
label3.Top = -2;
}
char rod=(char) new Random().Next(97, 122);
private void timer1_Tick(object sender, EventArgs e)
{
label1.Top += 10;
label2.Top += 10;
label3.Top += 10;
if (label1.Top >= this.Height - 60)
{
timer1.Stop();
}
}
private void timer2_Tick(object sender, EventArgs e)
{
//this.Controls.Add(lb);
//lb.Text = rod.ToString();
//this.label1.ForeColor = System.Drawing.Color.Red;
//Color[] col = { Red,Yellow,Green,Olive,Orange};
//int shuzi = new Random().Next(0, 4);
Col();//改变字体颜色的随便用法
Chan();
}
//随机产生label控件
ArrayList arr=null;
public void Chan()
{
//this.label1.Font = new System.Drawing.Font("宋体", 20F);
//this.Controls.Add(lb);
lb = new Label();
arr = new ArrayList();
arr.Add(lb);
//lb.Top = new Random().Next(10);
//lb.Left = new Random().Next(455);
lb.Font = new Font("宋体", 20F);
lb.ForeColor = Color.Red;
lb.AutoSize = true;//这个最好要写
lb.Text = ""+(char)new Random().Next(97,122)+"";
lb.Location = new Point(new Random().Next(600),new Random().Next(20));
this.Controls.Add(lb);
Star st = new Star(lb);
Thread th = new Thread(new ThreadStart(st.retu));
th.IsBackground = true;
th.Start();
}
class Star
{
Label lbl;
public Star(Label lbl)
{
this.lbl = lbl;
}
public void retu()
{
while (true)
{
lbl.Top += 10;
if (lbl.Top >= 500)
{
Thread.CurrentThread.Abort();
break;
}
Thread.Sleep(50);
}
}
}
public void Col()
{
int i = new Random().Next(0, 10);
if (i == 0)
{
this.label1.ForeColor = System.Drawing.Color.Yellow;
this.label2.ForeColor = System.Drawing.Color.Yellow;
this.label3.ForeColor = System.Drawing.Color.Yellow;
//this.lb.ForeColor = System.Drawing.Color.Yellow;
}
else if (i == 1)
{
this.label1.ForeColor = System.Drawing.Color.Tomato;
this.label2.ForeColor = System.Drawing.Color.Tomato;
this.label3.ForeColor = System.Drawing.Color.Tomato;
//this.lb.ForeColor = System.Drawing.Color.Tomato;
}
else if (i == 2)
{
this.label1.ForeColor = System.Drawing.Color.Orange;
this.label3.ForeColor = System.Drawing.Color.Orange;
this.label2.ForeColor = System.Drawing.Color.Orange;
//this.lb.ForeColor = System.Drawing.Color.Orange;
}
else if (i == 3)
{
this.label1.ForeColor = System.Drawing.Color.Green;
this.label2.ForeColor = System.Drawing.Color.Green;
this.label3.ForeColor = System.Drawing.Color.Green;
//this.lb.ForeColor = System.Drawing.Color.Green;
}
else if (i == 4)
{
this.label1.ForeColor = System.Drawing.Color.Olive;
this.label2.ForeColor = System.Drawing.Color.Olive;
this.label3.ForeColor = System.Drawing.Color.Olive;
//this.lb.ForeColor = System.Drawing.Color.Olive;
}
else if (i == 5)
{
this.label1.ForeColor = System.Drawing.Color.Purple;
this.label2.ForeColor = System.Drawing.Color.Purple;
this.label3.ForeColor = System.Drawing.Color.Purple;
//this.lb.ForeColor = System.Drawing.Color.Purple;
}
else if (i == 6)
{
this.label1.ForeColor = System.Drawing.Color.Pink;
this.label3.ForeColor = System.Drawing.Color.Pink;
this.label2.ForeColor = System.Drawing.Color.Pink;
//this.lb.ForeColor = System.Drawing.Color.Pink;
}
else if (i == 7)
{
this.label1.ForeColor = System.Drawing.Color.PapayaWhip;
this.label2.ForeColor = System.Drawing.Color.PapayaWhip;
this.label3.ForeColor = System.Drawing.Color.PapayaWhip;
//this.lb.ForeColor = System.Drawing.Color.PapayaWhip;
}
else if (i == 8)
{
this.label1.ForeColor = System.Drawing.Color.SeaShell;
this.label2.ForeColor = System.Drawing.Color.SeaShell;
this.label3.ForeColor = System.Drawing.Color.SeaShell;
//this.lb.ForeColor = System.Drawing.Color.SeaShell;
}
else if (i ==9)
{
this.label1.ForeColor = System.Drawing.Color.RosyBrown;
this.label2.ForeColor = System.Drawing.Color.RosyBrown;
this.label3.ForeColor = System.Drawing.Color.RosyBrown;
//this.lb.ForeColor = System.Drawing.Color.RosyBrown;
}
else if (i == 10)
{
this.label1.ForeColor = System.Drawing.Color.MediumSpringGreen;
this.label2.ForeColor = System.Drawing.Color.MediumSpringGreen;
this.label3.ForeColor = System.Drawing.Color.MediumSpringGreen;
//this.lb.ForeColor = System.Drawing.Color.MediumSpringGreen;
}
}
private void label2_Click(object sender, EventArgs e)
{
}
}
}
以上的游戏界面没有美观,不是很好看,所以来这里只是各位看看,如果运行的话,大家可能会发现一个问题,那个标签掉的路线总是估算的到,根本不是我想的那样,所以我很想那位告诉我一个很好解决随机数的方法:我做的随机数所产生的路线总的与界面成45度向下移动,相当不够水准。
汗颜啊!!!