You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pygame玩家绕中心旋转正常,修复碰撞后相机抖动问题求助

玩家旋转时相机抖动问题(靠近墙体场景)

我实现了玩家绕自身中心平滑旋转的功能,但之前遇到一个问题:当玩家靠近墙体并斜向旋转时,会被传送到地图的其他位置,推测是旋转后玩家的rect尺寸变大导致的。

编辑:我通过创建一个包围原始玩家图像的固定尺寸矩形self.hitbox_rect解决了墙体碰撞问题——旋转时这个矩形尺寸保持不变,碰撞检测仅基于这个更小的矩形。但现在出现了新问题:当玩家靠近墙体并旋转时,相机持续抖动。我附上了更新后的Player类代码以及Camera类的相关代码,问题可能和相机逻辑有关?

Player类代码

class Player(pygame.sprite.Sprite):
    def __init__(self, pos):
        super().__init__()
        self.player_img = pygame.image.load("handgun/move/survivor-move_handgun_0.png").convert_alpha()
        self.image = self.player_img
        self.image = pygame.transform.rotozoom(self.image, 0, 0.35)
        self.original_image = self.image  
        self.rect = self.image.get_rect(center = pos)

        self.player_speed = 10 # was 4
        self.shoot = False
        self.shoot_cooldown = 0
        self.bullet = Bullet(self.rect.centerx, self.rect.centery)
        self.pos = pos

        self.hitbox_rect = self.original_image.get_rect(center = pos)

 
    def player_turning(self): 
        self.mouse_coords = pygame.mouse.get_pos() 

        self.x_change_mouse_player = (self.mouse_coords[0] - (WIDTH // 2))
        self.y_change_mouse_player = (self.mouse_coords[1] - (HEIGHT // 2))
        self.angle = int(math.degrees(math.atan2(self.y_change_mouse_player, self.x_change_mouse_player)))
        self.angle = (self.angle + 360) % 360

        self.image = pygame.transform.rotate(self.original_image, -self.angle)
        self.rect = self.image.get_rect(center=self.rect.center)


    def check_collision(self, direction): 
        for sprite in obstacles_group:
            if sprite.rect.colliderect(self.hitbox_rect):
                if direction == "horizontal":
                    if self.velocity_x > 0:
                        self.rect.right = sprite.rect.left
                        self.hitbox_rect.right = sprite.rect.left 
                    if self.velocity_x < 0:
                        self.rect.left = sprite.rect.right
                        self.hitbox_rect.left = sprite.rect.right
                
                if direction == "vertical":
                    if self.velocity_y < 0:
                        self.rect.top = sprite.rect.bottom
                        self.hitbox_rect.top = sprite.rect.bottom
                    if self.velocity_y > 0:
                        self.rect.bottom = sprite.rect.top
                        self.hitbox_rect.bottom = sprite.rect.top

    def move(self):
        self.rect.centerx += self.velocity_x
        self.hitbox_rect.x += self.velocity_x
        self.check_collision("horizontal")
        self.rect.centery += self.velocity_y
        self.hitbox_rect.y += self.velocity_y
        self.check_collision("vertical")

Camera类代码

class Camera(pygame.sprite.Group):
    def __init__(self):
        super().__init__()
        self.offset = pygame.math.Vector2()
        self.floor_rect = background.get_rect(topleft = (0,0))

        self.create_map()

    def custom_draw(self):
        self.offset.x = player.rect.centerx - (WIDTH // 2)
        self.offset.y = player.rect.centery - (HEIGHT // 2)

        #draw the floor
        floor_offset_pos = self.floor_rect.topleft - self.offset
        screen.blit(background, floor_offset_pos)

        for sprite in all_sprites_group:
            offset_pos = sprite.rect.topleft - self.offset
            screen.blit(sprite.image, offset_pos)

内容的提问来源于stack exchange,提问作者juba1029

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 07:01:01