A5 - Text Objects
Run update.bat to retrieve the template for this assignment.
Then, from Visual Studio Code, run a5.py.
This is similar to the previous assignment, with the addition of text at the top displaying the player's health. The code that was added to create the text display was:
-
Line 10: Create a Text Object - the three required arguments are the actual text (here we're initializing it with an empty string), the x position, and the y position. We also use here the keyword argument
font_size. There are other optional keyword arguments offering further customization, documented here. -
Lines 29-30: We update the text within the text object on every frame refresh with a string containing the player's current health, then draw the text object to the screen.
The other new thing you'll see in this assignment is code to keep track of the player's health, and to update it when colliding with an enemy. The relevant parts are:
-
In the player's
on_create, we add an attribute for its health and initialize it to 30: -
In the bee's
on_create, we add an attribute for the amount of damage it gives to the player upon a collision: -
In the player's
on_step, we check for a collision with an enemy. If there is one, we subtract from our health the value of the enemy'sdamage_givenattribute, then move our player back to the start:
Your Task
Part 1
Add another enemy with a different damage level. You can model it off of the bee. Then, create one or two instances of your new enemy.
Part 2
Keep track of, and display, the number of items that have been collected. For this, you'll need to:
-
Give the player an attribute to store this it its
on_create, such as -
Create a text object to constantly display this value, modelled off of the health text object.