Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

c# - Make a an image move from one location to another random location every 5 seconds

So I am trying to make a mini-game where when one image intersects with another image they would score a point. This is everything I have. I am trying to make an image (aka snitch) move to a random location every time picplayer intercepts with snitch. Can someone also help me fix my code as my player teleports randomly for some reason. Thank you

    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);
question from:https://stackoverflow.com/questions/65948819/make-a-an-image-move-from-one-location-to-another-random-location-every-5-second

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As others have stated, there's no need to create new PictureBox for your villain each time. Below I've moved your Random instance and the villain PictureBox out to Form level (the same place as you existing x and y variables). Next we setup the villain in the Load() event as these properties don't change. Finally, we use different variables for the random location of the villain so that the player doesn't get changed:

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();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...