Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使图像每5秒从一个位置移动到另一个随机位置_C#_Winforms - Fatal编程技术网

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

C# 使图像每5秒从一个位置移动到另一个随机位置,c#,winforms,C#,Winforms,所以我尝试制作一个迷你游戏,当一张图片与另一张图片相交时,他们会得到一分。这是我所有的。我试图使一个图像(又名飞贼)移动到一个随机位置,每次picplayer拦截飞贼。有没有人能帮我修复我的代码,因为我的玩家出于某种原因随机传送。多谢各位 public partial class Form1 : Form { int x, y; public Form1() { InitializeComponent();

所以我尝试制作一个迷你游戏,当一张图片与另一张图片相交时,他们会得到一分。这是我所有的。我试图使一个图像(又名飞贼)移动到一个随机位置,每次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);

正如其他人所说,没有必要每次都为你的恶棍创建新的PictureBox。下面,我将您的随机实例和反派图片框移出到表单级别(与您现有的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();
    }
}

你对你的villian和你的播放器使用了相同的x和y变量。我尝试了这个picPlayer。位置=新点(10,10);但是它一次又一次地把我带到那个坐标系。当然,这会让玩家一直移动到10,10。你需要的是一个player_x和player_y,以及一个villian_x和villian_y。除了对x和y用法的评论外,为什么每次都要创建一个新的反派形象?另外:
villain.Height=-->2150@Loocid你所说的player_x和player_y,以及villian_x和villian_y是什么意思。我该怎么做?