Max Armstrong
Max Armstrong

Reputation: 21

How to 'blit' the image to the screen in Pygame

I'm making a game analyser, and I thought it would be nice if I had a user iterface instead of just using text and raw input to communicate. I am having problems with 'blitting' an image to my screen.

My image is inside the pycharm file called 'CATANYLISER' as is my code.

import pygame
pygame.init()


# py-game variables
(width, height) = (1000, 600)
window = pygame.display.set_mode((width, height))
window_title = pygame.display.set_caption('the CATANALYSER')
does_quit = False

# py-game images
empty_board = pygame.image.load('empty_board.png')


# py-game window loop
while not does_quit:

    # receives input from window
    for event in pygame.event.get():

        # stops program when red x clicked
        if event.type == pygame.QUIT:
            does_quit = True

    window.blit(empty_board, (0, 0))

    pygame.display.update()

# activates when the loop finishes, just makes sure everything shuts down properly
pygame.quit()

The expected result is the image on the screen in the top left corner. However when I run the program, I have an empty screen (pygame.QUIT still works).

When I run this code there is no error message, and I am completely lost about how to fix this.

Upvotes: 1

Views: 1594

Answers (1)

Rotartsi
Rotartsi

Reputation: 567

first, make sure that the empty_board.png is in your working directory.

Second, you have to clear the screen before each frame using window.fill([255, 255, 255])

Finally, you could try using pygame.display.flip() instead of update()

My code would look like:

import pygame
pygame.init()

window = pygame.display.set_mode([640, 480])
doQuit = False

board = pygame.image.load("empty_board.png")

while not doQuit:
   window.fill([255, 255, 255])
   window.blit(board, (0, 0))
   pygame.display.flip()
   for event in pygame.event.get():
       if event.type == pygame.QUIT:
           doQuit = True

pygame.quit()


Upvotes: 2

Related Questions