pygame 中的相机跟踪问题

Issue with camera tracking in pygame

所以我正在关注一个使用 tile-map 的 youtube 教程。添加了一个跟踪玩家水平移动的摄像机,我也实现了一个垂直摄像机,这些都很好用。我遇到的问题是玩家在屏幕外地图的左下角生成,而相机视图从左上角开始。因此,在游戏开始时,我必须等待几秒钟,让摄像机慢慢向下移动屏幕,让玩家看到。我的问题是如何让相机从已经在视野中的玩家开始?提前致谢

平铺 Class:

class Tile(pygame.sprite.Sprite):
def __init__(self,image, pos,size):
    super().__init__()
    file = pygame.image.load(image)
    self.image = pygame.transform.scale(file, (96, 96))
    
    self.rect = self.image.get_rect(bottomleft = pos)
    

def update(self,x_shift, y_shift):
    self.rect.x += x_shift
    self.rect.y += y_shift

滚动功能:

def scroll(self):
    player = self.player.sprite
    player_x = player.rect.centerx
    player_y = player.rect.centery
    direction_x = player.direction.x

    if player_x < screen_width / 4 and direction_x < 0:
        self.world_shift_x = 8
        player.speed = 0
    elif player_x > screen_width - (screen_width / 4) and direction_x > 0:
        self.world_shift_x = -8
        player.speed = 0
    else:
        self.world_shift_x = 0
        player.speed = 8

    if player_y < screen_height / 4 :
        self.world_shift_y = 8
        
    elif player_y > screen_height - (screen_height / 4):
        self.world_shift_y = -8
        
    else:
        self.world_shift_y = 0

更新级别:

self.tiles.update(self.world_shift_x, self.world_shift_y)

您必须初始化 world_shift_xworld_shift_y 并在 运行 应用程序循环之前移动一次磁贴。 运行 应用程序循环之前的以下代码

self.world_shift_x = screen_width // 2 - player.rect.centerx
self.world_shift_y = screen_height // 2 - player.rect.centery
self.tiles.update(self.world_shift_x, self.world_shift_y)
player.rect.centerx = screen_width // 2
player.rect.centery = screen_height // 2