Skip to main content
 首页 » 编程设计

Python Pygame 相机运动

2024年11月01日12zhengyun_ustc

<分区>

因此,我正在使用 Pygame 在 Python 中制作一个 2d topview 游戏。我一直在尝试创建一个相机运动,让玩家保持在屏幕中央。我该怎么做?我想将“ map ”放在一个表面上,它将被 blit 到屏幕表面。如果这样做,我可以只构建一次 map ,然后以某种方式调整它的位置,以便玩家始终位于屏幕中央。我的播放器像这样更新它的位置:

   def update(self, dx=0, dy=0): 
        newpos = (self.pos[0] + dx, self.pos[1] + dy)  # Calculates a new position 
        entityrect = pygame.Rect(newpos, self.surface.get_size())  # Creates a rect for the player 
        collided = False 
        for o in self.objects:  # Loops for solid objects in the map 
            if o.colliderect(entityrect): 
                collided = True 
                break 
 
        if not collided: 
            # If the player didn't collide, update the position 
            self.pos = newpos 
 
        return collided 

我找到了 this ,但那是针对侧视平台游戏的。所以我的 map 看起来像这样:

map1 = pygame.Surface((3000, 3000)) 
img = pygame.image.load("floor.png") 
for x in range(0, 3000, img.get_width()): 
    for y in range(0, 3000, img.get_height()): 
        map1.blit(img, (x, y)) 

那么我将如何进行相机移动?任何帮助将不胜感激。

附言。我希望你能明白我在这里问什么,英语不是我的母语。 =)