Python – How to have an image appear in python/pygame

pygamepython

I am trying to learn to make a basic game using pygame. I want to import and display an image in .png format. so far my attempt has been:

import pygame
from pygame.locals import*
pygame.image.load('clouds.png')

white = (255, 64, 64)
w = 640
h = 480
screen = pygame.display.set_mode((w, h))
screen.fill((white))
running = 1

while running:
    screen.fill((white))

    pygame.display.flip()

The Image (clouds.png) is in the same folder as the file. when i try to run this i get an error:

Traceback (most recent call last):
  File "C:\Users\Enrique\Dropbox\gamez.py", line 3, in <module>
    pygame.image.load('clouds.png')
error: Couldn't open clouds.png

Best Solution

Here you go. It blits the image to 0,0. Your other problem is that your pyimage doesn't seem to be built with png support

import pygame
from pygame.locals import*
img = pygame.image.load('clouds.bmp')

white = (255, 64, 64)
w = 640
h = 480
screen = pygame.display.set_mode((w, h))
screen.fill((white))
running = 1

while running:
    screen.fill((white))
    screen.blit(img,(0,0))
    pygame.display.flip()