恋轩念伊人 发表于 2008-3-2 20:45

做完没有多久的打字游戏

using System;
using System.Collections.Generic;
using System.ComponentModel;
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度向下移动,相当不够水准。
汗颜啊!!!

恋轩念伊人 发表于 2008-3-7 13:04

哎,这个打字游戏太逊啦,都没有人来光顾[bc02]

梦心 发表于 2008-3-7 14:26

我来看看~

nodeadbird520 发表于 2008-3-8 07:35

怎么我运行你的程序,总提示有错误,public partial class Form1 : Form这行,说partial有错,
不知道怎么改,所以想请教下.

恋轩念伊人 发表于 2008-3-9 04:36

我给你只是主代码,并没有全部复制到这里来。
你可以删除哪个看可以不咯
我是用vs05写的

cjlgood 发表于 2008-3-16 11:13

初来乍到

学习新知识啊

hoodlum1980 发表于 2008-3-16 22:19

[quote][bo]以下是引用 [un]nodeadbird520[/un] 在 2008-3-8 07:35 的发言:[/bo]

怎么我运行你的程序,总提示有错误,public partial class Form1 : Form这行,说partial有错,
不知道怎么改,所以想请教下. [/quote]
在vs2005开始把用户逻辑和窗体设计器代码分离到不同文件之中了,所以多了个关键字partial,你把这个关键字去掉即可。

hoodlum1980 发表于 2008-3-16 22:23

总体来说数据都放到控件属性里了因此效率较低。真正实际应用时不可能这样做。

恋轩念伊人 发表于 2008-3-27 14:10

回复 8# 的帖子

以前没有感觉,那是初学的时候,现在明白这其中的道理

andey 发表于 2008-3-27 15:34

建议搞成附件...
代码看着眼晕

恋轩念伊人 发表于 2008-3-27 16:12

回复 10# 的帖子

以后会这样的!!

cjy1314 发表于 2008-6-4 12:02

错误 帮忙

错误        1        “Typer.MainForm.Dispose(bool)”: 没有找到适合的方法来重写        H:\姓名\Typer\MainForm.Designer.cs        22        27        Typer

cjy1314 发表于 2008-6-4 12:06

错误        1        命名空间“System”中不存在类型或命名空间名称“Drawing”(是缺少程序集引用吗?)        E:\ConsoleApplication4\ConsoleApplication4\Program.cs        5        14        ConsoleApplication4
错误        2        命名空间“System”中不存在类型或命名空间名称“Windows”(是缺少程序集引用吗?)        E:\ConsoleApplication4\ConsoleApplication4\Program.cs        7        14        ConsoleApplication4
错误        3        找不到类型或命名空间名称“Form”(是否缺少 using 指令或程序集引用?)        E:\ConsoleApplication4\ConsoleApplication4\Program.cs        12        34        ConsoleApplication4
错误        4        找不到类型或命名空间名称“Label”(是否缺少 using 指令或程序集引用?)        E:\ConsoleApplication4\ConsoleApplication4\Program.cs        102        13        ConsoleApplication4
错误        5        找不到类型或命名空间名称“Label”(是否缺少 using 指令或程序集引用?)        E:\ConsoleApplication4\ConsoleApplication4\Program.cs        103        25        ConsoleApplication4
错误        6        找不到类型或命名空间名称“KeyPressEventArgs”(是否缺少 using 指令或程序集引用?)        E:\ConsoleApplication4\ConsoleApplication4\Program.cs        20        52        ConsoleApplication4
错误        7        找不到类型或命名空间名称“Label”(是否缺少 using 指令或程序集引用?)        E:\ConsoleApplication4\ConsoleApplication4\Program.cs        46        9        ConsoleApplication4
怎么  越来越多的错误啊 ??

duanzelong 发表于 2008-6-5 14:45

确实看着有点发晕

页: [1]

编程论坛