每 5 秒将图像从一个位置移动到另一个随机位置

时间:2021-01-29 04:32:19

标签: c# winforms

所以我正在尝试制作一个迷你游戏,当一个图像与另一个图像相交时,他们会得分。这就是我所拥有的一切。每次 picplayer 用告密者拦截时,我都试图让图像(又名告密者)移动到随机位置。当我的播放器出于某种原因随机传送时,有人也可以帮我修复我的代码。谢谢

    public partial class Form1 : Form
    {
        int x, y;

        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            x = 0;
            y = 0;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //enable the timer when the start button is clicked
            timer1.Enabled = true;
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            //create a random x and y coordinate
            Random r = new Random();
            x = r.Next(1, 500);
            y = r.Next(1, 500);
           
            //Creates the picturebox on your form
            PictureBox villain = new PictureBox();
            villain.Location = new Point(x, y);
            villain.Height = 150;
            villain.Width = 150;
            villain.SizeMode = PictureBoxSizeMode.Zoom;
            villain.Image = Properties.Resources.snitch;
            this.Controls.Add(villain);
          
            if (picPlayer.Bounds.IntersectsWith(villain.Bounds))
            {
                MessageBox.Show("You won");
                villain.Dispose();

            }

        }   

        //Moves harry potter according to the keys pressed
        private void textBox1_Keydown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                y -= 10;
            }
            if (e.KeyCode == Keys.Down)
            {
                y += 10;
            }
            if (e.KeyCode == Keys.Left)
            {
                x -= 10;
            }
            if (e.KeyCode == Keys.Right)
            {
                x += 10;
            }
            picPlayer.Location = new Point(x, y);

1 个答案:

答案 0 :(得分:0)

正如其他人所说,没有必要每次都为你的反派创建新的图片框。下面我将您的 Random 实例和恶棍 PictureBox 移到 Form 级别(与您现有的 x 和 y 变量相同的位置)。接下来我们在 Load() 事件中设置反派,因为这些属性不会改变。最后,我们对反派的随机位置使用不同的变量,这样玩家就不会被改变:

int x, y;
Random r = new Random();
PictureBox villain = new PictureBox();

private void Form1_Load(object sender, EventArgs e)
{
    x = 0;
    y = 0;
    villain.Height = 150;
    villain.Width = 150;
    villain.SizeMode = PictureBoxSizeMode.Zoom;
    villain.Image = Properties.Resources.snitch;
    this.Controls.Add(villain);
    villain.Hide();
}

private void button1_Click(object sender, EventArgs e)
{
    //enable the timer when the start button is clicked
    timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
    //create a random x and y coordinate
    int villX = r.Next(1, 500);
    int villY = r.Next(1, 500);         
    villain.Location = new Point(villX, villY);
    villain.Show();
    
    if (picPlayer.Bounds.IntersectsWith(villain.Bounds))
    {
        MessageBox.Show("You won");
        villain.Hide();
    }
}