第二种形式的计时器不起作用

时间:2018-05-25 07:12:22

标签: c# timer

我编写了一个使用计时器的简单突破游戏。在我的程序中,我有两个表单,一个带有开始屏幕和一个简单的菜单,另一个包含突破游戏.`

问题是当我按下表单1上的“播放”按钮时,另一个游戏表单应该弹出并启动,但它不会。游戏形式确实显示但游戏并没有开始。我相信timer_tick事件存在一些问题,但即使key_down事件也不起作用。当我创建一个只有突破游戏形式的新项目时,它的工作非常完美,所以我对此问题毫无头绪。

我的代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace Slutprojekt_2._0_Edward
    {
        public partial class Form1 : Form
        {

            public Form1()
            {

                InitializeComponent();
            }

            private void btnPlay_Click(object sender, EventArgs e)
            {
                this.Hide();
                new Form2().ShowDialog();
                //Form2 f2 = new Form2(timer1);
                //f2.Show();

            }
        }
    }

我的游戏形式:

using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.Windows.Forms;

    namespace Slutprojekt_2._0_Edward
    {
        public partial class Form2 : Form
        {

            bool goRight;
            bool goLeft;
            int speed = 10;

            int ballx = 5;
            int bally = 5;

            int score = 0;

            private Random rnd = new Random();


            public Form2()
            {

                InitializeComponent();

                foreach (Control x in this.Controls)
                {
                    if (x is PictureBox && x.Tag == "block")
                    {
                        Color randomColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
                        x.BackColor = randomColor;
                    }
                }
            }

            private void keyisdown(object sender, KeyEventArgs e)
            {
                timer1.Enabled = true;


                if (e.KeyCode == Keys.Left && Player1.Left > 0)
                {
                    goLeft = true;
                }


                if (e.KeyCode == Keys.Right && Player1.Left + Player1.Width < 920)
                {
                    goRight = true;
                }
            }

            private void keyisup(object sender, KeyEventArgs e)
            {


                if (e.KeyCode == Keys.Left)
                {
                    goLeft = false;
                }


                if (e.KeyCode == Keys.Right)
                {
                    goRight = false;
                }
            }

            private void timer1_Tick(object sender, EventArgs e)
            {         

                Ball.Left += ballx;
                Ball.Top += bally;

                label1.Text = "Score: " + score;

                if (goLeft) { Player1.Left -= speed; }

                if (goRight) { Player1.Left += speed; }

                if (Player1.Left < 1)
                {
                    goLeft = false;
                }
                else if (Player1.Left + Player1.Width > 920)
                {
                    goRight = false;
                }

                if (Ball.Left + Ball.Width > ClientSize.Width || Ball.Left < 0)
                {
                    ballx = -ballx;
                }

                if (Ball.Top < 0 || Ball.Bounds.IntersectsWith(Player1.Bounds))
                {
                    bally = -bally;
                }

                if (Ball.Top + Ball.Height > ClientSize.Height)
                {
                    gameOver();
                }

                foreach (Control x in this.Controls)
                {
                    if (x is PictureBox && x.Tag == "block")
                    {
                        if (Ball.Bounds.IntersectsWith(x.Bounds))
                        {
                            this.Controls.Remove(x);
                            bally = -bally;
                            score++;
                        }
                    }
                }

                if (score > 34)
                {
                    gameOver();
                    MessageBox.Show("You won!");
                }
            }

            private void gameOver()
            {
                timer1.Stop();
                MessageBox.Show("You lost...");
            }


        }
    }

这是program.cs代码

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Slutprojekt_2._0_Edward
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您需要在timer1.Enabled = true中设置Form2或致电timer1.Start()

默认情况下,计时器不会启动。

https://msdn.microsoft.com/en-us/library/system.windows.forms.timer.enabled(v=vs.110).aspx

答案 1 :(得分:0)

尝试打开您评论过的表单

Form2 f2 = new Form2();
f2.Show();