打字母游戏

很简单的一个小程序:

用一个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);

                }
            }
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值