c# - Winform需要顶部和底部PictureBox的碰撞
问题描述
我有一个大的红色盒子和一个蓝色盒子需要相互碰撞,盒子的左右反弹就好了。但是对于顶部和底部的盒子,我想不出我需要编写哪些正确的代码才能让它发生碰撞。我可以使用另一个代码,但它会反弹到它的来源而不是备用方向。所以我解决了这个问题。我只需要知道它将如何工作。任何帮助将不胜感激。
编辑:事实证明,由于在 Y 轴上使用 player.Bounds.Intersectswith 在第一个计时器上发生冲突,我必须使用第二个计时器才能工作。我得到了我想要的输出。
int enemyL = -10;
int enemyT = 10;
int playerL = 30;
int playerT = -30;
private void mainTimer_Tick(object sender, EventArgs e)
{
//player.Left += playerL;
player.Top += playerT;
//enemy.Left += enemyL;
enemy.Top += enemyT;
if (player.Left < 1 || player.Left + player.Width > ClientSize.Width - 2 || (player.Bounds.IntersectsWith(enemy.Bounds)))
{
playerL = -playerL;
}
if (player.Top < 1 || player.Top + player.Height > ClientSize.Width - 2)
{
playerT = -playerT;
}
if (enemy.Left < 1 || enemy.Left + enemy.Width > ClientSize.Width - 2 || (enemy.Bounds.IntersectsWith(player.Bounds)))
{
enemyL = -enemyL;
}
if (enemy.Top < 1 || enemy.Top + enemy.Height > ClientSize.Width - 2)
{
enemyT = -enemyT;
}
解决方案
推荐阅读
- azure - 如何将数据从 Azure Blob 存储导入 Neo4j?
- java - 移动到 ubuntu 20 后,在 azure 构建管道中跳过了 Testcontainer junit 测试
- jqgrid - 网格不会填充在 free-jqgrid 中
- javascript - 如何在画布中旋转对象时使用键盘射击子弹 360*?
- r - 如何使用R在最接近的数值上合并两个长度不均匀的数据结构
- react-native - 反应原生 | 默认设置宽度样式
- ios - 将 iOS 小部件添加到 macOS 目标
- java - 有没有一种优雅的方法来检查许多条件并调用相应的函数?
- php - 使用myth-auth Codeigniter 4登录后重定向到上一页
- r - 在 R 中添加白天/黄昏/夜晚/黎明的信息以跟踪数据