PictureBox 的 C# 碰撞检查(相交)
C# Collision Check for PictureBox (Intersect)
我一直在挑战这个问题很长一段时间。简单地说,我想避免两个图片框之间的碰撞。 pictureBox1 在看到 pictureBox2 时必须停止。但是,我确实设法通过 Intersect 做到了这一点。喜欢下面的代码;
private void method(PictureBox b)
{
if (pictureBox1.Bounds.IntersectsWith(b.Bounds)) {
movement = false;}
}
问题出在按键上。我正在使用 KeyDown(W, A, S, D) 移动 pictureBox1,当我按住键时,相交代码工作得很好。但是当我一个一个地按下按键时,它只是滑入 pictureBox2。我怎样才能避免这种情况?我确实尝试用布尔值来阻止它,但它没有用。我想我需要 if 语句,但我就是无法表达逻辑。寻找您的解决方案。提前致谢。
KeyDown 事件;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (movement == true)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.W) y -= chrspeed;
else if (e.KeyCode == Keys.A) x -= chrspeed;
else if (e.KeyCode == Keys.D) x += chrspeed;
else if (e.KeyCode == Keys.S) y += chrspeed;
pictureBox1.Location = new Point(x, y);
method(pictureBox2);
}
}
chrspeed 为 20;
KeyUp 事件;
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
movement = true;
}
我会将其放入 KeyDown 事件处理程序并删除 KeyUp 事件处理程序。您的代码的问题是,每次您释放一个键时,movement
标志都会重置为 true。
这应该可以满足您的要求:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.W) y -= chrspeed;
else if (e.KeyCode == Keys.A) x -= chrspeed;
else if (e.KeyCode == Keys.D) x += chrspeed;
else if (e.KeyCode == Keys.S) y += chrspeed;
Rectangle newRect = new Rectangle(x, y, pictureBox1.Width, pictureBox1.Height);
if (!newRect.Bounds.IntersectsWith(pictureBox2.Bounds)) {
pictureBox1.Location = new Point(x, y);
}
}
}
不需要 movement
标志。它的作用是设置新位置,如果两个图片框不相交。
请注意,当图片框发生碰撞时,您仍然可以按另一个键再次移开。
Edit
:
您的评论完全正确,对此深感抱歉。
我现在修改了代码,所以它使用新的位置来计算它们是否相交。
你的代码中发生的事情 "worked"(没有,你很幸运)是,第一次两个 picboxes 相交,picbox1 在 picbox2 中移动,method
返回 false
而下一次它没有输入 if
语句。
您需要在 移动 picbox1 之前调用 method()
并 然后 检查 true or false
:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.W) y -= chrspeed;
else if (e.KeyCode == Keys.A) x -= chrspeed;
else if (e.KeyCode == Keys.D) x += chrspeed;
else if (e.KeyCode == Keys.S) y += chrspeed;
method(new Rectangle(x, y, pictureBox1.Width, pictureBox1.Height), pictureBox2 );
if (movement == true)
{
pictureBox1.Location = new Point(x, y);
}
}
和
private void method(Rectangle rect, PictureBox b)
{
if (rect.IntersectsWith(b.Bounds)) {
movement = false;}
}
我一直在挑战这个问题很长一段时间。简单地说,我想避免两个图片框之间的碰撞。 pictureBox1 在看到 pictureBox2 时必须停止。但是,我确实设法通过 Intersect 做到了这一点。喜欢下面的代码;
private void method(PictureBox b)
{
if (pictureBox1.Bounds.IntersectsWith(b.Bounds)) {
movement = false;}
}
问题出在按键上。我正在使用 KeyDown(W, A, S, D) 移动 pictureBox1,当我按住键时,相交代码工作得很好。但是当我一个一个地按下按键时,它只是滑入 pictureBox2。我怎样才能避免这种情况?我确实尝试用布尔值来阻止它,但它没有用。我想我需要 if 语句,但我就是无法表达逻辑。寻找您的解决方案。提前致谢。
KeyDown 事件;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (movement == true)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.W) y -= chrspeed;
else if (e.KeyCode == Keys.A) x -= chrspeed;
else if (e.KeyCode == Keys.D) x += chrspeed;
else if (e.KeyCode == Keys.S) y += chrspeed;
pictureBox1.Location = new Point(x, y);
method(pictureBox2);
}
}
chrspeed 为 20;
KeyUp 事件;
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
movement = true;
}
我会将其放入 KeyDown 事件处理程序并删除 KeyUp 事件处理程序。您的代码的问题是,每次您释放一个键时,movement
标志都会重置为 true。
这应该可以满足您的要求:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.W) y -= chrspeed;
else if (e.KeyCode == Keys.A) x -= chrspeed;
else if (e.KeyCode == Keys.D) x += chrspeed;
else if (e.KeyCode == Keys.S) y += chrspeed;
Rectangle newRect = new Rectangle(x, y, pictureBox1.Width, pictureBox1.Height);
if (!newRect.Bounds.IntersectsWith(pictureBox2.Bounds)) {
pictureBox1.Location = new Point(x, y);
}
}
}
不需要 movement
标志。它的作用是设置新位置,如果两个图片框不相交。
请注意,当图片框发生碰撞时,您仍然可以按另一个键再次移开。
Edit
:
您的评论完全正确,对此深感抱歉。 我现在修改了代码,所以它使用新的位置来计算它们是否相交。
你的代码中发生的事情 "worked"(没有,你很幸运)是,第一次两个 picboxes 相交,picbox1 在 picbox2 中移动,method
返回 false
而下一次它没有输入 if
语句。
您需要在 移动 picbox1 之前调用 method()
并 然后 检查 true or false
:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.W) y -= chrspeed;
else if (e.KeyCode == Keys.A) x -= chrspeed;
else if (e.KeyCode == Keys.D) x += chrspeed;
else if (e.KeyCode == Keys.S) y += chrspeed;
method(new Rectangle(x, y, pictureBox1.Width, pictureBox1.Height), pictureBox2 );
if (movement == true)
{
pictureBox1.Location = new Point(x, y);
}
}
和
private void method(Rectangle rect, PictureBox b)
{
if (rect.IntersectsWith(b.Bounds)) {
movement = false;}
}