7.2.4 - Displaying sprites

Now we want to load an image and move it around the screen. We will start off with a simple space ship. 

Most of the time we do not create objects using the draw command of pygame but we use sprites. One of this sprites that we will use is a spaceshipspaceship

Sprites are usually in png or gif format. To use this sprite it must be in the same folder as our program. 

Right click on the spaceship and save it as spaceship.png inside the folder Module7.2

This is another sprite of a meteor meteor

Right click on the meteor and save it as meteor.png inside the folder Module7.2

To draw the sprites we have to load them first and the blit them.

Save the previous program that you already created to display the background as avoid_v0.1

Inside the program add these lines after the command that loads the background image:

# Load the sprites spaceshipImg = pygame.image.load("spaceship.png") meteorImg = pygame.image.load("meteor.png")



and the following two lines inside the main loop in the place where the drawing code should go:

screen.blit(spaceshipImg, (480, 360)) screen.blit(meteorImg, (700, 400))


taking care of indentation.


The final code can be downloaded from here.