Eylun
Eylun

Reputation: 1

Background image not appearing in pygame

My code currently looks like this import pygame from pygame.locals import *

pygame.init()
width,height = 1080,810
keys = [False,False,False,False]
screen = pygame.display.set_mode((width,height))
game_running = True
background = pygame.image.load("resources/images/background.png")
while game_running:
    screen.fill((0,0,0))
    screen.blit(background,(0,0))

When the code is run, a black window pops up, but the background isn't there.

I checked the directories to make sure the loading of the image has no issue too.

Upvotes: 0

Views: 2801

Answers (1)

furas
furas

Reputation: 142631

pygame draws in buffer in RAM memory (to make animation less flicking and tearing).

You have to use pygame.display.update() or pygame.display.update() to send from buffer to video card which will display it on monitor.

import pygame

width = 1080
height = 810

keys = [False, False, False, False]

pygame.init()
screen = pygame.display.set_mode((width,height))

background = pygame.image.load("resources/images/background.png").convert()

game_running = True
while game_running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                game_running = False

    screen.fill((0,0,0))
    screen.blit(background, (0,0))
    pygame.display.flip()

pygame.quit()    

Wikipedia: Double buffering in computer graphics

enter image description here

Upvotes: 2

Related Questions