4-10 More Sprites

In this lesson, we’ll create a few interesting and useful sprite variations: (1) a Brick sprite that positions itself at a random location in the game window, (2) a mouse following sprite, and (3) a label sprite that lets you display text at a given position.   We’ll add these Sprite subclasses to the mySprites.py module we used for our Box sprite last time, and show how to use each one.

A Brick Sprite

In our first example, we’re going to create a Brick sprite to represent a brick wall image.   The initializer method will position the sprite at some random location in the game window. This sprite will be static (i.e., not move) once instantiated so you will notice that it does not have an update() method defined.

As we’ve done before, we receive a parameter referring to the screen in the __init__() method (game window).   This is necessary because in the last two lines of the method need to call get_width() and get_height() for the game window so that the brick wall image can be randomly positioned within it.

The mainline code is pretty standard, the only really new code is in the Entities (IDEA) section:

On lines 18 to 24 we create an empty bricks list, then use a basic for loop to instantiate 10 Brick sprites and append() them to the list.   Now you can appreciate why lists (see U3-6) are useful data structures for organizing data/objects in a game!   On line 24 we add the entire list of Brick sprites to the allSprites group.   This way, in the main game loop (ALTER), all 10 Brickobjects can be drawn with only a few lines of code.   Using this technique I can handle any number of bricks without adding any new lines of code.

The rest of the main code is the same as we used for our Box sprite last time.   Because the Brick class is not overriding the update() method inherited from the Sprite class, the call to allSprites.update() on line 46 will not do anything.

A Mouse-Following Sprite

Next, let’s create a Sprite subclass called Circle that will update the position of a blue circle based on the current position of the mouse pointer.   Add this class to the mySprites.py module.

By now you should be familiar with most of this code.  On line 10, I set the image background to a colour different than the colour I will choose for my circle, in this case, black.   On line 11, the set_colorkey() method indicates that I would like any black pixels on the image to be transparent, so that the screen background shows through.   To understand this better, try commenting out line 11 and see what happens.   Line 12 draws a blue circle onto the image of our sprite.

The easiest way to make the sprite follow the mouse pointer is to set the (x, y) coordinate for the centre of our rect to the mouse’s current (x, y) position.   This is exactly what line 17 does every time update() is called.

Here’s the IDEA sections for the main code, only lines 19 and 20 of the Entities (IDEA) section is different from the last example:

A Text Label Sprite

In U4-6 we learned how to instantiate Font and SysFont objects and use their render() methods to draw text on the screen.   Putting text into a Sprite object makes it easy to place text in the game window and update it as needed, for example, to keep track of a user’s score.   Add the following code to our mySprites.py module:

The Label class does the same job as any other sprite, except the image of this class will actually be rendered text.   The font, text, and center instance variables are custom for this Label class.   To make it possible to change the text for a Labelafter it is instantiated, I provide a setText() mutator method.   Since the text could change between any frames, the image and rect attributes are (re)initialized each time the update() method is called rather than only once in the initializer.

Sprites become really handy when you use more than one in your program: You think once about how to make a general case work (as you define the class), and then you can make as many instances as you want – and they’ll all work independently, the same way, without any additional code.

In the demo code below we’ll create 3 instances of our Label class.  The first two labels (label1 and label2) will be used for static (unchanging) text.   The labelEvent sprite will be used to dynamically report on different types of events as they occur.

As you can see in the Event Handling (ALTER) section, whenever a mouse or keyboard event occurs, the text of the labelEventsprite is updated using the setText() mutator method.

You Try!

  1. Start a new page in your Learning Journal titled “4-10 More Sprites”.  Carefully read the notes above and in your own words summarize the key ideas from each section.