pygame :尝试在 Surface.blit() 中使用 "area" 标签

pygame : Trying to use the "area" tag in Surface.blit()

我正在尝试弄清楚如何使用 area 参数,当 blitting pygame 表面时可以传递。 想象一个等距 rpg 视图,其中相机跟随角色。

只要我的播放器停留在起始屏幕边界内,我的代码就可以正常工作,但是一旦我们走出去,播放器就会被剪掉。你能帮我弄清楚我错过了什么吗?


def run(fullmap):
    pygame.init()
    clock=pygame.time.Clock()
    screen = pygame.display.set_mode((400,400))

    # - background -
    fullmap_rect = fullmap.get_rect()

    # - player -
    player = pygame.Surface((100,100))
    player.fill((255,255,255))
    player_rect = player.get_rect()
    player_rect.center = (200, 200)

    # - camera -
    camera = screen.get_rect()
    camera.center = player_rect.center

    # - game loop -
    while 1:
        clock.tick(25)
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()

        # - input management -
        keys=pygame.key.get_pressed()
        if keys[pygame.K_DOWN]:
            player_rect.y += 10
            camera.y += 10
        if keys[pygame.K_LEFT]:
            player_rect.x -= 10
            camera.x -= 10
        if keys[pygame.K_UP]:
            player_rect.y -= 10
            camera.y -= 10
        if keys[pygame.K_RIGHT]:
            player_rect.x += 10
            camera.x += 10

        # - screen repaint -
        screen.fill((0,0,0))
        screen.blit(fullmap, (0,0), camera)
        screen.blit(player, player_rect, camera)
        pygame.display.flip()

if __name__ == '__main__':
    RED = (150, 50, 0)
    GREEN = (0, 150, 5)
    BLUE = (50, 0, 150)
    COLORS = (RED, GREEN, BLUE)

    TILE_SIZE = 10
    fullmap = pygame.Surface((1000, 1000))
    for y in range(0, 1000, TILE_SIZE):
        for x in range(0, 1000, TILE_SIZE):
            pygame.draw.rect(fullmap, random.choice(COLORS), (x, y, TILE_SIZE, TILE_SIZE))
    
    run(fullmap)

我期待在整个 fullmap 中跟随我的玩家。

谢谢你,祝你有愉快的一天!

可选的 area 参数表示源的较小部分。这对游戏地图很有用,但对玩家没有用。您想绘制完整的播放器图像,而不仅仅是其中的一部分。
您必须计算玩家相对于相机的位置:

screen.blit(fullmap, (0, 0), camera)
screen.blit(player, (player_rect.x - camera.x, player_rect.y - camera.y))

但是,由于您的播放器位于屏幕中央,只需将其绘制在屏幕中央即可:

screen.blit(fullmap, (0, 0), camera)
screen.blit(player, player.get_rect(center = screen.get_rect().center))