标题:求解释C# 打字小游戏的代码!最好能够帮我弄上注释!
只看楼主
cwdhdy
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2012-6-12
结帖率:100%
 问题点数:0 回复次数:0 
求解释C# 打字小游戏的代码!最好能够帮我弄上注释!
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DaZi
{
    public partial class Form1 : Form
    {
        private int speed;
        private int level;
        private Random random;
        private int right;
        private DateTime startTime;

        public Form1()
        {
            InitializeComponent();

            level = 1;
            speed = 10 * level;
            random = new Random();//随机字母
            right = 0;
        }

        private void Initialize(Label label)
        {
            label.Top = 0;
            label.Text = GetRandomCharactor();

            int red = random.Next(255);
            int green = random.Next(255);
            int blue = random.Next(255);

            label.ForeColor = Color.FromArgb(red, green, blue);
        }

        private string GetRandomCharactor()
        {
            string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            return s.Substring(random.Next(s.Length-1), 1);

            //int ascii = random.Next(32, 64);
            //char c = (char)ascii;

            //return c.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;

            radioButton1.Enabled = false;
            radioButton2.Enabled = false;
            radioButton3.Enabled = false;
                    
            Initialize(label1);  
            Initialize(label2);
            Initialize(label3);
            Initialize(label4);
            Initialize(label5);

            right = 0;
            label6.Text = "总分:0";

            startTime = DateTime.Now;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            MoveLabel(label1);   
            MoveLabel(label2);
            MoveLabel(label3);
            MoveLabel(label4);
            MoveLabel(label5);

            TimeSpan timeSpan = DateTime.Now - startTime;  //计算移动位置的时间差  //TimeSpan 表示一个时间间隔
            if (timeSpan.Seconds >= 30)
            {
                button2_Click(null, null);
            }
        }

        private void MoveLabel(Label label)
        {
            label.Top += speed;
            if (label.Top >= this.Height)
            {
                Initialize(label);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            CompareLabel(label1, e.KeyValue);
            CompareLabel(label2, e.KeyValue);
            CompareLabel(label3, e.KeyValue);
            CompareLabel(label4, e.KeyValue);
            CompareLabel(label5, e.KeyValue);
        }

        private void CompareLabel(Label label, int KeyValue)
        {
            int ascii = (int)Char.Parse(label.Text.ToUpper());

            if (KeyValue == ascii)
            {
                right++;
                label6.Text = "总分:" + (right * 10).ToString();
                Initialize(label);
            }
        }

        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)                  
                 level = 1;
            else if (radioButton2.Checked)
                level = 2;
            else if (radioButton3.Checked)
                level = 3;

            speed = 10 * level;
        }

        private void button2_Click(object sender, EventArgs e)
        {                                                
            timer1.Enabled = false;

            radioButton1.Enabled = true;
            radioButton2.Enabled = true;
            radioButton3.Enabled = true;
        }
    }
}
搜索更多相关主题的帖子: 小游戏 打字 private public Random 
2012-06-19 19:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-371898-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.088398 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved