很简单的一个小程序:
用一个label数组来表示下降的字母,当从键盘键入的字母与下降的字母有相同的就在最上面(X坐标随机)随机生成一个新的字母。
下面是主要的程序代码段:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace dazi
{
public partial class Form2 : Form
{
Random ran = new Random();
Label[] arry;
int num;
public Form2()
{
InitializeComponent();
}
//初始状态
void Form2_Load(object sender, System.EventArgs e)
{
if (arry != null)
{
for (int i = 0; i < arry.Length; i++)
{
arry[i].Dispose();
}
}
arry = new Label[num];
for (int i = 0; i < arry.Length; i++)
{
arry[i] = new Label();
arry[i].Visible = true;
arry[i].Top = ran.Next(0, pictureBox1.Height / 3 * 2);
arry[i].Text = Convert.ToChar(ran.Next(97, 97 + 26)).ToString();
arry[i].Left = ran.Next(0, pictureBox1.Width);
arry[i].Font = new Font("宋体", 16);
arry[i].Visible = true;
this.pictureBox1.Controls.Add(arry[i]);
}
}
//难度选择
void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (this.comboBox1.SelectedIndex == 0)
num = 4;
else if (this.comboBox1.SelectedIndex == 1)
num = 8;
else if (this.comboBox1.SelectedIndex == 2)
num = 16;
Form2_Load(sender,e);
}
void timer1_Tick(object sender, System.EventArgs e)
{
for (int i = 0; i < arry.Length; i++)
{
arry[i].Top += 20;
if (arry[i].Top > this.pictureBox1.Height)
{
arry[i].Top = -10;
arry[i].Text = Convert.ToChar(ran.Next(97, 97 + 26)).ToString();
arry[i].Left = ran.Next(0, pictureBox1.Width);
}
}
}
//开始与暂停
private void button1_Click(object sender, EventArgs e)
{
if (this.button1.Text == "开始")
{
this.timer1.Enabled = true;
this.button1.Text = "暂停";
}
else
{
this.timer1.Enabled = false;
this.button1.Text = "开始";
}
}
private void Form2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
for (int i = 0; i < arry.Length; i++)
{
if (e.KeyChar == Convert.ToChar(arry[i].Text))
{
arry[i].Top = -10;
arry[i].Text = Convert.ToChar(ran.Next(97, 97 + 26)).ToString();
arry[i].Left = ran.Next(0, pictureBox1.Width);
}
}
}
}
}