Why is screen not background surface does not blit onto screen?

Viewed 141

I am a beginner in pygame and I have been learning how to create a Flappy Bird game. I found a image shown below in the FlappyBird/assets folder.

Flappy bird background

After doing so, I added code to show the image onto a screen:

# Pygame flappy bird program
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((288, 512))
bg_surface = pygame.image.load('assets/background-day.png').convert()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    screen.blit(bg_surface, (0, 0))

But it ended up showing part of the screen, shown as follows:

Image

Why is that? Any help or comments on my code will be appreciated.

1 Answers

You missed to update the display by either pygame.display.update() or pygame.display.flip():

import pygame
import sys

pygame.init()
screen = pygame.display.set_mode((288, 512))
bg_surface = pygame.image.load('assets/background-day.png').convert()

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

    screen.blit(bg_surface, (0, 0))
    pygame.display.flip()

You are actually drawing on a Surface object. If you draw on the Surface associated to the PyGame display, this is not immediately visible in the display. The changes become visibel, when the display is updated with either pygame.display.update() or pygame.display.flip().

See pygame.display.flip():

This will update the contents of the entire display.

The typical PyGame application loop has to:

Related