A3 - Switching Between Views
Open level_1.py, and paste the following code into the Level_1 class below the on_create method:
def on_step(self):
# This next line runs the on_step function in level_base.py
super().on_step()
if self.player.x > 1200:
from level_2 import Level_2
oa.show_view(Level_2)
The line super().on_step() is a concept that is covered in grade 12 - but in short, it makes sure that the on_step method in both level_1.py and level_base.py get run.
The last three lines are what you should focus on - they cause the view to switch to Level_2 when the player's x position is greater than 1200.
Your Task
Part 1
Change it so that instead of advancing to Level 2 at a certain x location, it advances after the player collects a certain number of coins.
Part 2
Add a Game Over screen to your game. You can model it off of the title screen.
When the player's health reaches zero, switch to the Game Over screen.
I would recommmend adding this logic to the on_step insdie level_base.py,
so that it will work on every level.
Extension
Add a second condition for advancing from level 1 to level 2: When all enemies have been killed.
To make this work, you will need to add an attribute to your level 1 class that keeps track of how many enemies there currently are. You would initialize it to the number of enemies you create in the level. Then, any time an enemy is killed, you'd need to add logic to decrease that counter.