C# 在picturebox、windows窗体上查找冲突区域
我是C#的新手,我正在尝试用C#构建一个乒乓球游戏,到目前为止,我已经能够使用以下计时器功能在我的windows窗体面板内实现球的角度运动:C# 在picturebox、windows窗体上查找冲突区域,c#,winforms,c#-4.0,C#,Winforms,C# 4.0,我是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() {
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();
}
}
System.Windows.Forms.Timer Timer=new System.Windows.Forms.Timer();
System.Windows.Forms.Timer timer2=新的System.Windows.Forms.Timer();
公共表格1()
{
初始化组件();
弧度=(角度(_起点,_终点)-180)*-1;
isFirstTime=true;
时间间隔=25;
timer.Tick+=定时器_Tick;
timer.Start();
}
私有无效计时器(对象发送方、事件参数)
{
middle.X-=转换为16(间隔*数学Cos(弧度));
middle.Y-=转换为16(间隔*数学Sin(弧度));
图2.位置=中间位置;
//pictureBox1是桨,pictureBox2是球。。。。
if(IsTouching(pictureBox2,pictureBox1))//检查两个图片框是否相互接触的自定义方法。
{
双相对位置=数学Abs(((双)面板1.Left-(双)pictureBox2.Left)/(双)pictureBox2.Width);
timer.Stop();
时间间隔=25;
timer2.Tick+=timer2_Tick;}
timer2.Start();
}
}
私有无效计时器2_刻度(对象发送方,事件参数e)
{
isFirstTime=false;
middle.X+=转换为16(间隔*数学Cos(弧度));
middle.Y+=转换为16(间隔*数学Sin(弧度));
图2.位置=中间位置;
如果(图B左<面板1左)
{
timer2.Stop();
timer.Start();
}
}
这仅仅是我项目的开始,是的,在这段代码中也会有很多问题,但目前我面临的问题是,当球击中球拍时,我需要确定球是击中球拍的哪一侧,在右侧还是左侧,或者可能在中间,根据这一点,我必须设置球的角度运动,如果球碰到球拍的右侧,则设置球的右侧运动,球拍左侧的左侧运动,中间部分的水平向上运动
我尝试使用double relativeLocation=Math.Abs(((双)面板1.Left-(双)pictureBox2.Left)/(双)pictureBox2.Width)
方法来确定桨上的一些相对位置,但我仍然无法实现我正在尝试的操作。我现在对如何进行感到非常困惑
简言之,我必须在球撞击桨叶时确定桨叶的部分,以便给球提供所需的角运动
任何帮助都将不胜感激。谢谢。假设球是
pictureBox2
左边的拨片是pictureBox1
在这种情况下,您必须选中pictureBox1.Right
及
pictureBox2.左
代码示例:
if (pictureBox1.Right == pictureBox2.Left)
{
//Collision
}
可能的重复项仅添加一次事件处理程序。您将在第一个计时器的每个刻度上添加第二个刻度处理程序,因此它将在每个刻度上多次运行该代码。