65.9K
CodeProject 正在变化。 阅读更多。
Home

一个很酷的游戏,使用鼠标

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.24/5 (10投票s)

2004年5月19日

1分钟阅读

viewsIcon

31182

downloadIcon

956

一个很酷的游戏,使用鼠标

Sample Image - Codecs_smileys.jpg

引言

大家好。这里有一个有趣的小游戏,它始终在后台运行四个计时器。所以我们可以说这个应用程序完全由计时器驱动。我不会忘记我的朋友 Flesh_Eater,他帮了我很多,我仍然是个新手,正在努力学习做得更好。希望大家喜欢它。

代码示例 1

以下代码片段在按下“开始”按钮时激活。四个计时器被赋予间隔值,标签被赋予它们的位置。

 int A = 0;
        private void Beginbutton_Click(object sender, System.EventArgs e)
        {
            //This sets the score counter to 100
            (this.label5.Text = "100";
            (if(A == 0)
            {
                //Here the four timers are started that control this app
                (this.timer1.Interval = 1;
                (this.timer1.Start();
                (this.timer2.Interval = 1;
                (this.timer2.Start();
                (this.timer3.Interval = 1;
                (this.timer3.Start();
                (this.timer4.Interval = 1000;
                (this.timer4.Start();
                (this.label3.Text = "0";
                //Here the two smiley face labels positions are set
                this.label1.Left = 19;
                this.label1.Top = 96;
                this.label2.Left = 264;
                this.label2.Top = 216;
                //Setting the begin button to Restart
                this.Beginbutton.Text = "Restart";
                A = 1;
            }
            else
            {
                //This is to prevent any errors if A = 1 then all
                //the timers are stopped.Kind of like a reboot.
                this.timer1.Stop();
                this.timer2.Stop();
                this.timer3.Stop();
                this.timer4.Stop();
                this.Beginbutton.Text = "Begin";
                A = 0;
            }
        }

代码示例 2

在以下代码片段中,我解释了如何控制图像的两个计时器,以防止它们超出边界。

         //Declaring four new variables
        static int X = 0;
        static int Y = 0;
        static int X1 = 0;
        static int Y1 = 0;
        private void timer1_Tick(object sender, System.EventArgs e)
        {
            //This part is to make sure that the two images dont go past
            //The border of the game.
            //This program has two timers one controls the .left positions
            //And timer 2 controls the .top positions.
            switch(X)
            {
                case 0:
                    //If th location of X is under a certain amount
                    //then we give X a value.
                    if(this.label1.Left >= 280)
                    {
                        X = 1;
                    }
                    //And give the label a new position
                    this.label1.Left += 3;
                    break;
                case 1:
                    if(this.label1.Left <= 0)
                    {
                        X = 0;
                    }
                    this.label1.Left -= 2;
                    break;
                default:
                    break;
            }

代码示例 3

这是第三个计时器的描述。

         //This timer is important because it controls the score keeping
        //and if the images colide they have to split up again.
        private void timer3_Tick(object sender, System.EventArgs e)
        {
            //Four new variables with location values of the first label
            int S1 = (this.label1.Location.X +17);
            int S2 = (this.label1.Location.X -17);
            int S3 = (this.label1.Location.Y +17);
            int S4 = (this.label1.Location.Y -17);

            //Here we are saying that if Label 1 is "LIKE" Label 2
            //in positions then bounse them of again.
            if(this.label2.Location.X <= S1)
            {
                if(this.label2.Location.X >= S2)
                {
                    if(this.label2.Location.Y <= S3)
                    {
                        if(this.label2.Location.Y >= S4)
                        {
                            //The score is counted on by one if the
                            //images colide.
                            int Score = int.Parse(this.label3.Text);
                            Score++;
                            this.label3.Text = Score.ToString();
                            this.label1.ImageIndex = 1;
                            this.label2.ImageIndex = 1;

                            //Here the variables receive new values and timer 1
                            //and timer 2 take care of the rest.
                            if (Y == 0)
                            {
                                Y = 1;
                            }
                            else
                            {
                                Y = 0;
                            }
                            if (Y1 == 0)
                            {
                                Y1 = 1;
                            }
                            else
                            {
                                Y1 = 0;
                            }
                            if (X == 0)
                            {
                                X = 1;
                            }
                            else
                            {
                                X = 0;
                            }
                            if (X1 == 0)
                            {
                                X1 = 1;
                            }
                            else
                            {
                                X1 = 0;
                            }
                        }
                    }
                }

最终代码示例

这是应用程序中的最终计时器,它控制诸如计算时间以及停止其余计时器之类的功能。

         //This is the last timer that keeps track of the time.
        private void timer4_Tick(object sender, System.EventArgs e)
        {
            int Time = int.Parse(this.label5.Text);
            Time--;
            this.label5.Text = Time.ToString();
            this.label1.ImageIndex = 0;
            this.label2.ImageIndex = 0;
            //When the time = 0 then all four timers stop and
            //Form2 "Scoreform" is displayed.
            if (Time == 0)
            {
                this.timer4.Stop();
                this.timer3.Stop();
                this.timer2.Stop();
                this.timer1.Stop();
                EndScore = int.Parse(this.label3.Text);
                Form2 F2 = new Form2();
                F2.Show();
            }
        }

问题

这个应用程序的主要问题是我两个图像不断超出边界,结果它们没有反弹。这在我的朋友(Fles_Eater)的帮助下在代码示例 1 中得到了解决。这个程序是在学校无聊的课余时间开发的,我知道它并不完美。希望大家喜欢这个应用程序,并希望将来能看到更多。

© . All rights reserved.