繁体   English   中英

WinFrame App C#游戏设计:左右移动图片框

[英]WinFrame App C# game design: Moving pictureboxes left and right

我希望我的图片框向左和向右移动,到目前为止,它们仅向右移动并在到达边缘时消失。 在代码中,有3个敌人,它们都可以向右移动,当他们碰到“墙”时,如何使它们向左移动? 播放器可以双向移动。

{
Random _random;


public MainWindow()
{
    InitializeComponent();
    _random = new Random();
}

private void MainWindow_Load(object sender, EventArgs e)
{

    Size s = new System.Drawing.Size(800, 600);
    this.ClientSize = s; 
    this.FormBorderStyle = FormBorderStyle.FixedSingle;
}

private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
    {
        if (e.KeyCode == Keys.Left)
        {
            Player.Left -= 20;
        }

        if (e.KeyCode == Keys.Right)
        {
            Player.Left += 20;
        }

    }
}

private void timer1_Tick(object sender, EventArgs e)
{
    int z = _random.Next(0, 10);
    int x = _random.Next(0, 20);
    int y = _random.Next(0, 30);
    LargeEnemy.Left += z;
    MediumEnemy.Left += x;
    SmallEnemy.Left += y;

}

使用您的代码,首先创建3个全局bool LargeGoLeft = true, MediumGoLeft = true, SmallGoLeft = true;bool LargeGoLeft = true, MediumGoLeft = true, SmallGoLeft = true;
然后将代码放在您的计时器中,如下所示:

    bool moveLeft = false;
    public Form1()
    {
        InitializeComponent();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if((pictureBox1.Left + pictureBox1.Width) >= this.Width)
        {
            moveLeft = true;
        }
        if(pictureBox1.Left < 0)
        {
            moveLeft = false;
        }
        if(moveLeft)
        {
            pictureBox1.Left -= 15;
        }
        if (!moveLeft)
        {
            pictureBox1.Left += 15;
        }
    }

这是我能够测试的版本,与Picturebox完美搭配

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM