C# 中图片框之间的冲突检测
本文关键字:之间 冲突检测 | 更新日期: 2023-09-27 17:57:02
我的表单上有 2 个图片框,一个是移动的,其方向由按键控制,另一个是稳定的,怎么可能检测到它们之间的碰撞.....这是我尝试过的;但失败了.....
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private enum disha { up, down, left, right };
private disha ekdisha = disha.down;
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
ekdisha = disha.up;
}
else if (e.KeyCode == Keys.Down)
{
ekdisha = disha.down;
}
else if (e.KeyCode == Keys.Left)
{
ekdisha = disha.left;
}
else if (e.KeyCode == Keys.Right)
{
ekdisha = disha.right;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (pictureBox1.Top <= 0)
{
pictureBox1.Top = 0;
}
if (ekdisha == disha.up)
{
pictureBox1.Top -= 3;
}
else if (ekdisha == disha.down)
{
pictureBox1.Top += 3;
}
else if (ekdisha == disha.left)
{
pictureBox1.Left -= 3;
}
else if (ekdisha == disha.right)
{
pictureBox1.Left += 3;
}
}
private void Form1_Load(object sender, EventArgs e)
{
if (pictureBox2.Bounds.IntersectsWith(pictureBox1.Bounds))
{
MessageBox.Show("Something is collided");
}
}
}
}
您需要
在单独的函数中编写用于检查边界的代码(您现在已经用Form1_Load编写了代码),并从timer1_Tick调用它