private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox1.Top = pictureBox1.Top - 2;
            pictureBox1.Left = pictureBox1.Left +2;
            if (pictureBox1.Location.X == 0 || pictureBox1.Location.Y == 0)
            {
                //pictureBox1.Bottom = pictureBox1.Bottom + 2;
                pictureBox1.Top = pictureBox1.Top + 2;
                pictureBox1.Left = pictureBox1.Left - 2;
            }加个时间timer然后写代码,我知道思路就是判断边缘,但是不知道如何应用!请大侠帮忙!

解决方案 »

  1.   

    假定小球是在一个panel里运动.
     int xoffset = 5;
            int yoffset = -5;        private void timer1_Tick(object sender, EventArgs e)
            {
                this.pictureBox1.Top += yoffset;
                this.pictureBox1.Left += xoffset;
                if (this.pictureBox1.Top < 0)
                {
                    this.pictureBox1.Top = -this.pictureBox1.Top;
                    yoffset = -yoffset;
                }
                if (this.pictureBox1.Top > (this.panel1.Height - this.pictureBox1.Height))
                {
                    this.pictureBox1.Top = this.panel1.Height - (this.pictureBox1.Top + this.pictureBox1.Height - this.panel1.Height) - this.pictureBox1.Height;
                    yoffset = -yoffset;
                }
                if (this.pictureBox1.Left < 0)
                {
                    this.pictureBox1.Left = -this.pictureBox1.Left;
                    xoffset = -xoffset;
                }
                if (this.pictureBox1.Left > (this.panel1.Width - this.pictureBox1.Width))
                {
                    this.pictureBox1.Left = this.panel1.Width - (this.pictureBox1.Left + this.pictureBox1.Width - this.panel1.Width) - this.pictureBox1.Width;
                    xoffset = -xoffset;
                }
            }