python - 如何在pygame中创建与对角线运动的完美碰撞
问题描述
我有碰撞。我的问题是对墙的对角线运动不起作用。我想要发生的是,您可以通过按住向左和向上键向左移动靠墙的底部。但是,当我这样做时,它会将我传送到墙的右侧(从块内向左移动会将角色的左侧放在墙的右侧)。我知道为什么它会传送我,我只是不知道如何使它完美地适应对角线方向的运动。我不知道如何做到这一点,所以在对角线移动时我不会被放置在墙壁的两侧。
我已经尝试了许多墙壁碰撞的变体,到目前为止,这是我通过实施所有 8 个方向运动获得的最接近完美碰撞的结果。(这 4 个基本方向运动非常完美,但在尝试斜靠墙时都出错了。)
def collision_check(self):
# Move the rect
self.rect.x += self.vel.x
self.rect.y += self.vel.y
self.pos = vec(self.rect.x,self.rect.y)
# If you collide with a wall, move out based on velocity
for wall in s.OBSTACLES:
if self.rect.colliderect(wall.rect):
if self.vel.y > 0: # Moving down; Hit the top side of the wall
self.rect.bottom = wall.rect.top
self.pos = vec(self.rect.x,self.rect.y)
self.vel.y = 0
if self.vel.y < 0: # Moving up; Hit the bottom side of the wall
self.rect.top = wall.rect.bottom
self.pos = vec(self.rect.x,self.rect.y)
self.vel.y = 0
if self.vel.x > 0: # Moving right; Hit the left side of the wall
self.rect.right = wall.rect.left
self.pos = vec(self.rect.x,self.rect.y)
self.vel.x = 0
if self.vel.x < 0: # Moving left; Hit the right side of the wall
self.rect.left = wall.rect.right
self.pos = vec(self.rect.x,self.rect.y)
self.vel.x = 0
我需要你拿着 4 个移动键中的两个靠墙和另一个方向移动,而不是被传送到你拿着的一边。
解决方案
推荐阅读
- javascript - Android WebView.evaluateJavascript() 和未捕获的参考错误
- database - 导出 IBM DB2 对象的 sql 脚本
- eclipse - Eclipse 中的计算启动按钮工具提示错误
- java - JAVA REGEX:匹配直到特定字符
- pentaho-cde - 如何使用 Pentaho CDE 表盘组件进行动态范围
- swift - UICollection 视图部分间隙,在 iphoneX 上添加了子视图
- elasticsearch - C# NEST 的序列化 FieldValue & AggegateDictionary 全部为空
- azure - 无需打开浏览器即可实现 Azure MFA
- angular - 创建一项通用服务是一种好习惯吗?
- sh - 为什么参数扩展不起作用?