0% found this document useful (0 votes)
12 views

Python 6 - Creating the Player Subclass 2022

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python 6 - Creating the Player Subclass 2022

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Year 11 Applied Computing

Python 6: Creating the Player Subclass


AOS2 - Programming

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.

Activity 6.1 - Adding the Player Subclass


First, be sure to remove the code we used earlier to test our Sprite class. These are the lines highlighted in red
below which we marked with the comment “Test Code”.

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.

Activity 6.2 - Adding the Keyboard Bindings


In the last step we created some methods that will control how the player sprite moves around the screen. Before
these can be used we need to tell python to listen for keypresses. We did this in Exercise 3, the difference is that
this time we will tell python to use the methods in our class instead of functions.
Add the following code just before the game loop…
You can now test your program and see that no errors fire. Pressing a, d or w won’t do anything yet. At this stage
we have just recorded that the key presses have occurred by setting some properties in the Player class.

Activity 6.3 - Changing the Update Method to Move the Player


To make the player move we will add an update method to our Player class. Note that this will override (replace)
the update method that was inherited from the Sprite class. Add the following code inside the Player class...

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.

Activity 6.4 - Checking for Collisions


To check for collisions you can add the code highlighted in yellow below. This goes inside the update method of
the player sprite immediately after the code we just added. Make sure the code is indented correctly so that it is
part of the Player.update method. You may like to add each section of code one after the other and test them to
see what they do.

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).

You might also like