[Python] How to prevent image blurring/flashing while moving?

Discussão em 'Python' iniciado por Stack, Novembro 28, 2024.

  1. Stack

    Stack Membro Participativo

    I've recently created a camera which follows the player around the map. The camera centers the player in the middle and can zoom in and out with CTRL+Scroll wheel.

    camera_handler.py:

    import pygame

    class CameraHandler:
    camera_x, camera_y = 0, 0
    zoom_scale = 1.0

    @staticmethod
    def update_camera_coords(player_rect):
    width, height = pygame.display.get_surface().get_size()
    CameraHandler.camera_x = min(player_rect.centerx - width // 2, width * 2 - width)
    CameraHandler.camera_y = min(player_rect.centery - height // 2, height * 2 - height)

    def handle_camera_event(event):
    if event.type == pygame.MOUSEWHEEL and pygame.key.get_mods() & pygame.KMOD_CTRL:
    if event.y == 1:
    CameraHandler.zoom_scale = max(CameraHandler.zoom_scale - 0.2, 0.5)
    elif event.y == -1:
    CameraHandler.zoom_scale = min(CameraHandler.zoom_scale + 0.2, 2.5)

    @staticmethod
    def get_camera_surface(sprite_group):
    camera_surface_size = pygame.display.get_surface().get_size()
    camera_surface = pygame.Surface(camera_surface_size, pygame.SRCALPHA)
    camera_surface_size_vector = pygame.math.Vector2(camera_surface_size)
    camera_surface.fill((129, 167, 69))

    offset_x = -CameraHandler.camera_x
    offset_y = -CameraHandler.camera_y

    for sprite in sprite_group:
    camera_surface.blit(sprite.image, (sprite.rect.x + offset_x, sprite.rect.y + offset_y))

    scaled_surface = pygame.transform.scale(camera_surface, camera_surface_size_vector * CameraHandler.zoom_scale)
    centered_rect = scaled_surface.get_rect(center=(camera_surface_size[0] // 2, camera_surface_size[1] // 2))
    return scaled_surface, centered_rect


    I blit the result of the get_camera_surface() onto the main screen in my game loop:

    def main(self):
    while self.playing:
    self.events()
    self.update()
    self.draw()
    self.dt = self.clock.tick(self.framerate) / 1000
    self.running = False

    def draw(self):
    camera_surface, rect = CameraHandler.get_camera_surface(self.all_sprites)
    self.screen.blit(camera_surface, rect)
    pygame.display.update(rect)


    When I zoom out, and move around the map in the x-axis direction, all sprites have this weird flickering effect, when you look closely it looks like a white wave. How do I fix this?

    [​IMG]

    I've tried to set the screen flag to pygame.DOUBLEBUF but I haven't noticed any changes. I tried using .flip() instead of update() but there is also no change.

    Continue reading...

Compartilhe esta Página