How to draw images and sprites in pygame?

Question:

I would like to know how to draw images using pygame. I know how to load them.
I have made a blank window.
When I use screen.blit(blank, (10,10)), it does not draw the image and instead leaves the screen blank.

Asked By: jtl999

||

Answers:

After using blit or any other update on your drawing surface, you have to call pygame.display.flip() to actually update what is displayed.

Answered By: jsbueno
import pygame, sys, os
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((100, 100))

player = pygame.image.load(os.path.join("player.png"))
player.convert()

while True:
    screen.blit(player, (10, 10))
    pygame.display.flip()

pygame.quit()

Loads the file player.png.
Run this and it works perfectly. So hopefully you learn something.

Answered By: John Riselvato

This is a typical layout:

myimage = pygame.image.load("myimage.bmp")
imagerect = myimage.get_rect()

while 1:
    your_code_here

    screen.fill(black)
    screen.blit(myimage, imagerect)
    pygame.display.flip()
Answered By: joaquin

Images are represented by "pygame.Surface" objects. A Surface can be created from an image with pygame.image.load:

my_image_surface = pygame.load.image('my_image.jpg')

However, the pygame documentation notes that:

The returned Surface will contain the same color format, colorkey and alpha transparency as the file it came from. You will often want to call convert() with no arguments, to create a copy that will draw more quickly on the screen.
For alpha transparency, like in .png images, use the convert_alpha() method after loading so that the image has per pixel transparency.

Use the appropriate conversion method for best performance:

image_surface = pygame.load.image('my_image.jpg').convert()
alpha_image_surface = pygame.load.image('my_icon.png').convert_alpha()

A Surface can be drawn on or blended with another Surface using the blit method. The first argument to blit is the Surface that should be drawn. The second argument is either a tuple (x, y) representing the upper left corner or a rectangle. With a rectangle, only the upper left corner of the rectangle is taken into account. It should be mentioned that the window respectively display is also represented by a Surface. Therefore, drawing a Surface in the window is the same as drawing a Surface on a Surface:

window_surface.blit(image_surface, (x, y))
window_surface.blit(image_surface,
    image_surface.get_rect(center = window_surface.get_rect().center))

Minimal example:

import pygame

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

pygameSurface = pygame.image.load('apple.png').convert_alpha()

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    window.fill((127, 127, 127))
    window.blit(pygameSurface, pygameSurface.get_rect(center = window.get_rect().center))
    pygame.display.flip()

pygame.quit()
exit()

pygame.image.load is bale to load most images. According to the documentation the following formats are supported: JPG, PNG, GIF (non-animated), BMP, PCX, TGA (uncompressed), TIF, LBM (and PBM), PBM (and PGM, PPM), XPM.

If you want to use images in PyGame that are loaded with other libraries, see:

For information on loading Scalable Vector Graphics (SVG) files, see:

Loading animated GIF files is presented at:

Or see how to load NumPy frames:

Answered By: Rabbid76
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.