Why does my display draw behind my screen in pygame

Viewed 23

So I'm learning pygame and I want to use pixel art for the sprites and I'm trying to scale up my display but it seems it's drawing behind the main display instead of Infront of it

import sys, math
import pygame
from pygame.locals import *

pygame.init()
        
fps = 60
is_running = True
clock = pygame.time.Clock()
window_size = [600, 400]
scale_size = [300, 200]
        
screen = pygame.display.set_mode(window_size)
display = pygame.Surface(scale_size)
        
player_img = pygame.image.load("images/player.png")
player_pos = [150, 100]
player_move_speed = 3
player_y_momentum = 1
        
while is_running:
    for event in pygame.event.get():        
        if event.type == QUIT:
            is_running = False
            pygame.quit()
            sys.exit()
            
    surf = pygame.transform.scale(display, window_size)
    screen.blit(surf, (0, 0))
        
    surf.blit(player_img, player_pos)
    surf.fill((255, 255, 255))
    
    pygame.display.update()
    clock.tick(fps)

I've tried to troubleshoot the problem but I can't seem to find the solution

1 Answers

The order of drawing/filling is important!


Change the order to the following:

while is_running:
    for event in pygame.event.get():
        if event.type == QUIT:
            is_running = False
            pygame.quit()
            sys.exit()

    surf = pygame.transform.scale(display, window_size)
    surf.fill((255, 255, 255))

    surf.blit(player_img, player_pos)
    screen.blit(surf, (0, 0))

    pygame.display.update()
    clock.tick(fps)
Related