您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

简单的拖动物理,左右移动时的动作有所不同

简单的拖动物理,左右移动时的动作有所不同

造成此问题的原因是,pygame.Rect存储整数坐标:

Rect对象的坐标都是整数。[…]

的部分组件dx,并dy当你丢失:

self.Rect.x += dx



self.Rect.y += dy

您必须以浮点精度进行计算。在类中添加一个xandy属性。递增属性move并同步Rect属性

class Player:
    def __init__(self, color):
        self.Rect = pygame.Rect([50, 50], [30, 50])
        self.x = self.Rect.x
        self.y = slef.Rect.y
        # [...]

    def move(self, dx, dy, platforms):
        # Test for collisions with platforms

        # handle movement on the X axis
        self.x += dx
        self.Rect.x = round(self.x)
        for platform in platforms:
            if self.Rect.colliderect(platform.Rect):
                if dx > 0:
                    self.Rect.right = platform.Rect.left
                if dx < 0:
                    self.Rect.left = platform.Rect.right
                self.x = self.Rect.x
                # Reset veLocity when collision with wall
                self.vx = 0

        # handle movement on the Y axis
        self.Rect.y += dy
        self.Rect.y = round(self.y)
        for platform in platforms:
            if self.Rect.colliderect(platform.Rect):
                if dy > 0:
                    self.Rect.bottom = platform.Rect.top
                if dy < 0:
                    self.Rect.top = platform.Rect.bottom
                self.y = self.Rect.y
                # Reset veLocity when collision with floor or roof
                self.vy = 0

        # return correctly collided rect to draw()
        return self.Rect
其他 2022/1/1 18:46:27 有610人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶