Game Development with PyGame

Images

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This video segment demonstrates how to load and display images in pygame.

Keywords

  • pygame
  • images
  • loading images
  • showing images
  • png
  • jpg

About this video

Author(s)
Coen de Groot
First online
21 December 2019
DOI
https://doi.org/10.1007/978-1-4842-5661-9_9
Online ISBN
978-1-4842-5661-9
Publisher
Apress
Copyright information
© Coen de Groot 2019

Video Transcript

Pygame lets you easily load and display images. To load an image, call Pygame.image.load and give it the path and image name so Pygame can find the image. This will return a Pygame surface. To show the image, blit its surface onto the canvas using canvas.blit. As we’ve seen before, you also need to say where to place the image. For instance, 0, 0 for the top left-hand corner.

We first load the image using Pygame.image.load, and we give it the image name. This is a picture I took on holiday. Then we take the surface that was returned and we blit it onto the canvas like this. We could also take one image and blit it multiple times to our canvas so we get multiple copies. Here’s another image, a lantern. We go through a grid of x and y-coordinates, and at each point, we put the lantern image.

Images can have an alpha layer. This is a special layer which shows which parts of the image should be transparent. We start by creating a background image, which we just put on 0, 0. We load the image again, but this time it has an alpha layer which isolates the lantern and removes the sky behind it. And again, we show this in a grid. And here is a grid of images where you can clearly see the background coming through behind it. Now that we can load and show images, let’s see how to rotate, scale, and flip them.