Python 6 - Creating the Player Subclass 2022
Python 6 - Creating the Player Subclass 2022
In our game we are going to need two types of sprites. One will be used for the player sprite and the other will be
used for the missiles they will shoot at each other.
To make these we will create new classes based on our Sprite class. These are called subclasses because they will
be based on the Sprite class. This means they will inherit all of the code from our Sprite class but will also have
new code that is not in the Sprite class.
We are going to use some extra math functions to work out the way our ship moves. So add a line at the top of
your program to import the math module…
Now add the code for our Player class before the game loop…
Finally we will add some code to create and draw the player sprite (the highlighted code is new)...
Things to note…
● Line 93 - In the class definition Sprite is included in the brackets to tell python that our new class inherits
everything from the Sprite class.
● We’ve added an __init__ method to our Player class. This will replace the one in the Sprite class, so to
make sure we still run the code in the Sprite.__init__() method we have to call it (as on 95)
● In our __init__ method we’ve added some new properties that we will need and we’ve set the shape to
“ship”. If you gave your shape a different name you will need to change it (line 96)
● After we created the player_one object using our new Player class we changed it’s x property to move it to
the left of the screen where we want it to start.
● We have also created an array to hold all of our sprites (line 122), at the moment this only has the
player_one object in it. In the game loop we’ve added a loop that will render and update every sprite in
this array. This will make it easier to add more sprites later.
You should be able to test your program now and see that the sprite for player one appears on the left side of the
screen.
Test your code again. You should be able to use the a, d and w keys to move the player_one sprite around the
screen. It’s a bit tricky to control though. Especially since it can go outside of the window.
Note that you can adjust the speed of rotation by changing lines 131 and 133. You can adjust the speed of
acceleration by adjusting lines 139 and 140.
You might also notice that the sprite changes when you accelerate. If you don’t have a shape named “ship_accel”
you might get an error when you accelerate, fix this by changing line 135. In the next step we will get the sprite to
change back after a short delay.
Things to note…
● We’ve used a property named timer to set a delay before changing the sprite back after accelerating. Each
time the screen updates the timer value is reduced and when it gets to zero the sprite changes back.
● We used the WIDTH and HEIGHT constants we created earlier to test if the sprite leaves the screen. This is
handy because we can change the size of our screen without effecting this code.
● The code to check if we’ve moved past the inner walls is a bit tricky and we’ve used lots of logic operators
(and, or) to combine different conditions. There is one other logic operator we haven’t used (not).