在winforms中移动pictureBox

时间:2018-11-20 03:04:13

标签: c# winforms

我的代码目标是在winforms中创建一个简单的pong游戏(应该使用monogame),但是我的代码应该可以工作。

问题:更改图片位置后,图片框位置不会更新。

我怀疑可能是我的一系列KeyDown / KeyUp事件占用了UI线程吗?

编辑:根据我最近进行的一些测试,似乎计时器没有计时,但是我可能错了。

代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        Thread t = new Thread(ChangePaddleState);
        t.Start();
        BringToFront();
        Activate();
    }

    //change this later?
    static bool upArrow = false;
    static bool downArrow = false;
    static bool wKey = false;
    static bool sKey = false;

    //movement speed of paddle
    const byte movementspeed = 3;

    Paddle paddleL = new Paddle();
    Paddle paddleR = new Paddle();

    void ChangePaddleState()
    {
        while (true)
        {
            //left paddle
            if (upArrow)
            {
                paddleL.idle = false;
                paddleL.movingDown = false;
                paddleL.movingUp = true;
            }
            else if (downArrow)
            {
                paddleL.idle = false;
                paddleL.movingUp = false;
                paddleL.movingDown = true;
            }
            //left paddle
            //right paddle
            if (wKey)
            {
                paddleR.movingDown = false;
                paddleR.idle = false;
                paddleR.movingUp = true;
            }
            else if (sKey)
            {
                paddleR.idle = false;
                paddleR.movingUp = false;
                paddleR.movingDown = true;
            }
            //checking if idle
            if(!paddleR.movingDown && !paddleR.movingUp)
            {
                paddleR.idle = true;
            }
            if (!paddleL.movingDown && !paddleL.movingUp)
            {
                paddleL.idle = true;
            }
            //checking if idle
            Thread.Sleep(50);
        }
    }



    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        KeySwitch.setKeysTrue(e);
    }

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        KeySwitch.setKeysFalse(e);
    }

    //Key Swicth Region
    #region
    static class KeySwitch
    {
        public static void setKeysTrue(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    upArrow = true;
                    Debug.Write("Up Arrow Pressed");
                    break;
                case Keys.Down:
                    downArrow = true;
                    Debug.Write("Down Arrow Pressed");
                    break;
                case Keys.W:
                    wKey = true;
                    Debug.Write("W Key Arrow Pressed");
                    break;
                case Keys.S:
                    sKey = false;
                    Debug.Write("S Key Pressed");
                    break;
            }
        }
        public static void setKeysFalse(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    upArrow = false;
                    Debug.Write("Up Arrow Released");
                    break;
                case Keys.Down:
                    downArrow = false;
                    Debug.Write("Down Arrow Released");
                    break;
                case Keys.W:
                    wKey = false;
                    Debug.Write("W Key Released");
                    break;
                case Keys.S:
                    sKey = false;
                    Debug.Write("S Key Released");
                    break;
            }
        }
    }
    #endregion //KeySwitch Class KeySwitchClass

    private void button1_Click(object sender, EventArgs e)
    {
        DebugHelper.GetPaddleState(paddleL, paddleR);
        ActiveControl = null;
    }

    private void gameLoop_Tick(object sender, EventArgs e)
    {
        if (paddleL.movingUp)
        {
            aPaddleLeft.Location = new Point(aPaddleLeft.Location.X, aPaddleLeft.Location.Y + movementspeed);
        }
        else if (paddleR.movingDown)
        {
            aPaddleRight.Location = new Point(aPaddleRight.Location.X, aPaddleLeft.Location.Y - movementspeed);
        }
    }
}

}

0 个答案:

没有答案