I'm following this guide to try and display a basic PNG image inside a Pygame window. My image is a simple 150x150 green ball with no transparency called ball.png, located in the same directory as my Python script:
However, when I start my program, all that appears is a glitchy image of the correct size and location:
I am using this code to display the image (identical to the code given in the tutorial linked above):
import pygame
import os
_image_library = {}
def get_image(path):
global _image_library
image = _image_library.get(path)
if image == None:
canonicalized_path = path.replace('/', os.sep).replace('\\', os.sep)
image = pygame.image.load(canonicalized_path)
_image_library[path] = image
return image
pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill((255, 255, 255))
screen.blit(get_image('ball.png'), (20, 20))
pygame.display.flip()
clock.tick(60)
Why would this happen? Is there a flaw in the code? I have tried replacing the PNG with different ones, but that just changes how the glitchy image looks - the image is still not displayed correctly. I'm using OS X 10.11 El Capitan, Python 3.5.0 and Pygame 1.9.2.


get_imagefunction is supposed to store images that have already been loaded according to the tutorial. It doesn't appear to be the problem though, because if I change the function to justreturn pygame.image.load(path).convert()the issue still occurs.