在图片框,窗体表格上找到碰撞区域

时间:2017-11-30 12:25:05

标签: c# winforms c#-4.0

我是C#的新手,我正在尝试用C#构建一个Ping Pong游戏,直到现在我已经能够使用计时器功能在我的窗体面板内实现球的角度运动,如下所示:

                 col
id                 
1   [[a, x], [b, y]]
2           [[c, z]]
3           [[d, m]]

这只是我项目的开始,是的,这个代码也会有很多问题,但目前我面临的问题是,当球击中球拍时我需要确定是否在球的哪一侧球击中,在右侧或左侧或可能在中心,根据我必须设置球的角度运动,右侧运动,如果球击中球的右侧,左侧桨叶左侧移动,中心部分水平向上移动。

我尝试使用 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(); } } 方法来确定桨上的一些相对位置,但我仍然无法实现我想要做的事情。我现在非常困惑如何进行。

总之,我必须在球击中球拍时确定球拍的一部分,以便为球提供所需的角度运动。

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

1 个答案:

答案 0 :(得分:0)

比方说,球是pictureBox2 左拨片为pictureBox1 ...

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

代码示例:

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