1

My code runs with no errors, but the background image does not display and neither does the caption at the top of the window. I have checked that my script is importing the correct pygame module, and tried different images. I have Python 3.6.2 on a Mac.

import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((400, 433))
pygame.display.set_caption('Pac Man')
clock = pygame.time.Clock()
background = pygame.image.load('/Users/MyMacbook/Desktop/pac-man/background.png')
gameDisplay.blit(background, (100, 0))


run = True

while run:
    gameDisplay.blit(background, (0, 0))
    pygame.display.update()

pygame.quit()
quit()
  • Try to add an event loop (see [section 5.5 of Program Arcade Games](http://programarcadegames.com/index.php?chapter=introduction_to_graphics)). Events should be handled every frame, so that the event queue gets emptied. Actually, you should be able to see the image even without an event loop, but maybe it has to do with the problem. – skrx May 16 '18 at 18:47
  • @skrx, thanks a lot! – OnEdge Gaming May 16 '18 at 19:48

1 Answers1

1

Yeah it appears that @skrx is correct, there's something blocking by not clearing the event buffer. This code works for me:

import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((400, 433))
pygame.display.set_caption('Pac Man')
clock = pygame.time.Clock()
background = pygame.image.load('/Path/To/Background.png')
gameDisplay.blit(background, (100, 0))


run = True

while run:
   for e in pygame.event.get():
       if e.type == pygame.QUIT:
            run = False
    gameDisplay.blit(background, (0, 0))
    pygame.display.update()

pygame.quit()
quit()
Will
  • 4,299
  • 5
  • 32
  • 50
  • Sure thing man, not sure why it does this but still good to know – Will May 16 '18 at 21:27
  • Nice, there's an answer as to why here: https://stackoverflow.com/questions/48450396/pygame-does-not-blit-unless-i-call-pygame-event-get – Will May 16 '18 at 21:34