Warning - This site is moving to https://getthecodingbug.anvil.app
Topics covered
Sprites
Sprite movement
What is a sprite?
A sprite is a computer graphics term for any object on the screen that can move around. When you play any 2D game, all the objects you see on the screen are sprites. Sprites can be animated, they can be controlled by the player, and they can even interact with each other.
We will take care of updating and drawing our sprites in the UPDATE and DRAW sections of our game loop. But you can probably imagine, if your game has a large number of sprites then these sections of your game loop could get very long and complicated.
Fortunately, Pygame has a good solution for this: the sprite group.
A sprite group is just a collection of sprites that you can act on all at the same time.
Let’s make a sprite group to hold all the sprites in our game, add the "all_sprites = " line after "clock = ":
clock = pygame.time.Clock() all_sprites = pygame.sprite.Group()
# Update all_sprites.update() # Draw / render screen.fill(BLACK) all_sprites.draw(screen)
We can take advantage of the new group by adding the following two "all_sprites" lines in our loop :
Now for every sprite that we create we just make sure we add it to the "all_sprites" group, and it will automatically be drawn on the screen and updated each time through the loop.
Creating a sprite
Now we’re ready to make our first sprite. In Pygame, sprites are objects.
You have worked with classes and objects in the previous lesson 8, they are a convenient way of grouping data and code into a single entity.
We start by defining our new sprite:
With the Python IDLE editor, create a new file and save it as "player.py" in your "Invaders" folder.
Your new class, "Player", will "inherit" some of the attributes and methods of the pygame's "Sprite" class.
We are going to need some constants from our "common.py" file, so add this at the top.
from common import *
Now add the class definition below. As usual, we have defined an "__init__()" function, which defines what code will run whenever a new object of this type is created. There are also two properties that every Pygame sprite must have: an image and a rect:
class Player(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((50, 50)) self.image.fill(GREEN) self.rect = self.image.get_rect()
The first line, "pygame.sprite.Sprite.__init__(self)" is required by Pygame - it runs the built-in Sprite classes initialiser.
Next, we define the image property - in this case, we’re just creating a simple 50 x 50 square and filling it with the colour GREEN. Later we’ll learn how to make the sprite’s image be something fancier, like a character or spaceship, but a solid square is good enough for now.
We have defined the sprite’s rect, which is short for “rectangle”. Rectangles are used all over the place in Pygame to keep track of an object’s coordinates. the get_rect() command just looks at the image and calculates the rectangle that will enclose it. We can use the rect to put the sprite wherever we want it on the screen.
Let’s start with the sprite in the bottom, centre, by adding this line after "self.rect = self.image.get_rect()":
self.rect.center = (WIDTH / 2, HEIGHT - 40)
from player import Player
Now that we’ve defined our Player sprite we need to import it into our "main.py" program, at the top after "import random".
We also need to “spawn” (meaning create) it by making an instance of the Player class and make sure we add the sprite to the all_sprites group. Add these lines after "all_sprites = pygame.sprite.Group()".
player = Player() all_sprites.add(player)
Now, if you run your program, you’ll see a green square at the bottom, centre of the screen.
Sprite movement
Remember, in the game loop, we have the "all_sprites.update()". This means that for every sprite in the group,
Pygame will look for an "update()" function and run it. So to get our sprite to move, we just need to define its update rules.
In the file "player.py", add this function to the "Player" class, lined-up, below the "def __init__()" function:
def update(self): self.rect.x += 5
Now, if you run your program, you’ll see the green square move to the right of the screen.
Go ahead and experiment - notice that anything you put in the update() method of the sprite will happen every frame. Try making the sprite move up and down (change the y coordinate) or making it bounce off the wall (reverse the direction when the rect reaches the edge).
In the next section we will use some art for your sprite - changing it from a plain square into an animated character.
Click this link to move on to the next section: Lesson 9 b