我是C#的新手,我试图用C#制作乒乓游戏,直到现在,我已经能够使用以下计时器函数在Windows窗体面板内实现球的角度运动:

    System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
    System.Windows.Forms.Timer timer2 = new System.Windows.Forms.Timer();

    public Form1()
    {
        InitializeComponent();
        radians = (Angle(_start, _end) - 180) * -1;
        isFirstTime = true;
        timer.Interval = 25;
        timer.Tick += Timer_Tick;
        timer.Start();
    }
    private void Timer_Tick(object sender, EventArgs e)
    {
        middle.X -= Convert.ToInt16(interval * Math.Cos(radians));
        middle.Y -= Convert.ToInt16(interval * Math.Sin(radians));
        pictureBox2.Location = middle;

        //pictureBox1 is the paddle and pictureBox2 is the ball....

        if (IsTouching(pictureBox2, pictureBox1))  //Custom method to check whether the two picture boxes touch each other.
        {
            double relativeLocation = Math.Abs(((double)panel1.Left - (double)pictureBox2.Left) / (double)pictureBox2.Width);
            timer.Stop();
            timer2.Interval = 25;
            timer2.Tick += Timer2_Tick;                }
            timer2.Start();
        }
    }
    private void Timer2_Tick(object sender, EventArgs e)
    {
        isFirstTime = false;
        middle.X += Convert.ToInt16(interval * Math.Cos(radians));
        middle.Y += Convert.ToInt16(interval * Math.Sin(radians));
        pictureBox2.Location = middle;
        if (pictureBox2.Left < panel1.Left)
        {
            timer2.Stop();
            timer.Start();
        }
    }


这仅仅是我的项目的开始,是的,这段代码中也会有很多问题,但是目前我面临的问题是,当球击中桨时,我需要确定球是否在桨的哪一侧击中,在右侧或左侧,或者在中心,并且根据此,我必须设置球的角度运动,如果球击中了桨的右侧,则为右侧运动,对于球体,则为左侧运动。拨片的左侧,中间部分水平向上。

我尝试使用double relativeLocation = Math.Abs(((double)panel1.Left - (double)pictureBox2.Left) / (double)pictureBox2.Width)方法确定拨片上的某些相对位置,但仍然无法实现我要尝试执行的操作。我对于现在如何进行感到非常困惑。

简而言之,我必须确定当球击中桨时桨的部分,以便给球提供所需的角运动。

任何帮助,将不胜感激。谢谢。

最佳答案

假设球是pictureBox2
左拨片是pictureBox1 ...

在这种情况下,您必须检查pictureBox1.Right
 和
pictureBox2.Left


代码示例:

if (pictureBox1.Right == pictureBox2.Left)
{
    //Collision
}

关于c# - 在图片框,Windows窗体上查找碰撞区域,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47573442/

10-13 01:37