Level 5: Help - IncrediCoders/Python1 GitHub Wiki

Intelli_Avatar_1

Intelli-Scents added this page on March 14, 2025


Hello, Paul Python's posse! I'm your friendly teacher's aide, Intelli-Scents. This page is to help you, in case you get stuck with the instructions that I gave you, while you were hunting down Bayo Wolf and the Virobots! My text messages are all archived in the Python Adventures book, so please refer to the book first. For this level, you're going to build a simulator to help Paul Python chase down the Cryptic Creeper!

You'll want to make sure that your Level 5 files are loaded in your editor, Visual Studio Code. If you have any problems, please read the instructions at Set up the files and Load the IncrediCoders Files.

Here are the different sections on this page to help you out with your Class Introductions:

  1. Play the Game - Download the Creeper Chase game and play it, to see what it's like before you code the game. I mentioned in my text messages that this is a simulator to help Paul practice as he chases down the Cryptic Creeper!
  2. Glossary of Terms - This list goes through the my instructions I sent you, and it explains every term and idea in depth, one topic at a time, as I bring it up in the book!
  3. Advice on Writing the Code - Eventually, in the book, I ask you to write your own code. Still stuck? Well, here are some hints on how to write the code!
  4. The Final Code - After you're done, it might not work the way you expected, or it might not work the same as our version (see Play the Game). Check out my code to see what you missed! IMPORTANT: Please don't cheat yourself! Finish the game first!

1. Play the Game

In this Creeper Chase game, you can run the game to see what the finished game looks like before you build it (or while you're building it). You can see the full instructions on how to download and run the game at Try the IncrediCoders Games.

In this game we will create a simulator to help Paul track down the Cryptic Creeper. Paul will need to chase the Creeper through the portals and across the planets of Lavash and Foodera. This simulator will show Paul how to navigate the terrain and how to use his jetpack to jump and float down to the ground (for the first three stages of the game), and then how to fly in stages 4-6!

You can run the program to see what the finished program looks like, before you build it (or while you're building it). You can see the full instructions on how to download and run the program at Try the IncrediCoders Games.

Here is the download to run the Level 5 program - Creeper Chase:

Here are the instructions on how to play the Level 5 game: Level 5 Instructions. We also include the Known Issue about how moving the window makes Paul drop down.

2. Glossary of Terms

Let's start with some definitions so that you understand all the words that I mentioned to you while walking you through this project. These help define and explain the code and terms that I used in our text conversations (which can be found in the book):

  1. State: In this game, we're going to update the state of Paul's health, movement, and location. The state includes everything that the computer knows about the variables and the objects in your game, at a specific point in time.

  2. from init import *: On Line 2, this code imports everything in the init.py file, which sets up the game and includes the features and functions that you'll use throughout this game.

  3. def update(delta_time): On Line 5, this function contains the code that we want to run every time the game needs to be updated. It's where all the fun game logic happens! The function takes one parameter, delta_time, which represents the change in time since the last update. This helps us keep the animations in their proper places and helps keep the framerate running smoothly.

  4. for loop: A for loop is something that runs the same block of code a specific number of times. We first use the for loop on Line 6. It can use the variables from a list (also called an array), one at a time. Or it can also be used to do the same thing every time, without using a list/array.

  5. for event in pygame.event.get():: In this program, on Line 6, you will see for event in pygame.event.get():, which means the code block below it will run again each time an event happens. After each event is checked, the program finishes the loop and will run the next line of code that is outside of the for loop block.

  6. Function: A group of code that you can call from anywhere in your code. You can pass parameters, which are variables, into your function.

  7. pygame.event.get(): This function returns a list of all the events that have happened. This is still on Line 6.

  8. Event: An event is any kind of input from the user. In this case, it's button presses to make the Paul Python character move and jump.

  9. if event.type == pygame.QUIT: On Line 7, this if expression checks to see if the player has closed the window screen of the program (for example, the player clicks the X in the top-right corner of the window and quits the game).

  10. stop(): On Line 8, this function stops the game and exits the program entirely. It runs if the player exits the window (based on the if statement on Line 7).

  11. elif: We use this on Line 9. This keyword comes from the combination of two other keywords: else and if, which you have seen before in previous levels. elif is used when you have more than one condition to check at the same time. You would start with an if statement (like we do on Line 7). Then, in the following blocks that are similar to an if condition, you would instead write elif. At the end of these conditions, you would still use an else statement for the very last code block (in case none of your if and elif statements are true).

  12. elif key_down(event, " "): Also on Line 9, this statement checks if the player presses the spacebar. The quotes, with the blank space in it, tell the Python programming language that the spacebar was pressed.

  13. (My.grounded or MY.level_num > 3): At the end of Line 9, this statement checks if Paul is on the ground or if he's on stage 4 or higher. If he's on the ground, then he can jump, by using his jetpack. If he's in the air, but he's on stage 4 or higher, then he can use his jetpack to fly even higher.

  14. My.player.velocity.y: On Line 10, this variable controls the speed that Paul is going up. (The y axis is up and down.) The top of the window is 0 in the y-axis. Below the y-axis, the numbers are positive. So, we subtract from Paul's y-axis velocity to send him up. That's why the number is negative (-700)... it sends Paul up into the air!

  15. MY.grounded = FALSE: On Line 11, this sets the MY.grounded variable to show that Paul is in the air. Later on, you'll check this variable to bring Paul back down, if he's in the air.

  16. jetpack_up_animation(): On Line 12, this function displays the jetpack animation.

  17. key_held_down(): This function tells us whether a key is being held down. Just like key_down(), the key can be a letter key, number key, or special character key. The difference is we are checking if the key is being pressed down for a longer period of time.

  18. key_held_down(pygame.K_LEFT): On Line 13, this function checks if the left arrow key is held down.

  19. MY.player.velocity.x: On Line 15, this variable tracks the speed Paul is moving to the left. Make sure you remove the hashtag symbol (pound sign) from the beginning of the line, so that the line runs. We refer to this as uncommenting.

  20. max(): Also on Line 15, the max() function calculates whether the speed you're moving or your top speed is higher. It then sets the higher number to the MY.player.velocity.x variable.

  21. PLAYER_ACCEL: This is the player acceleration argument. It tracks how fast your character moves to get to the max speed.

  22. else: We're jumping down to Line 23. This else block runs if the player isn't holding down the right or left key.

  23. MY.grounded: On Line 24, we run this if block, if Paul is touching the floor.

  24. velocity: This is the speed that something is moving. In this level, it's the speed that Paul is moving. When the velocity gets lower, Paul slows down.

  25. register(): This is the act of informing the computer about a state. "Registering" stores collected data in a state for later use. For our game, this is done on Lines 102-104.

  26. Manager.run(): This is on our last line of the game, Line 107. This is the function that actually runs the game on your computer, after all the code logic has been completed. It initializes the screen, window, and the version of the game. ("MAIN" is the game you're making now. In Challenge 1 and Challenge 2, you'll make new versions of the game.)

3. Advice on Writing the Code

This section provides you more hints and details on how to write the code that I went over in the book!

First, you need to uncomment the code on Lines 15-16:

#MY.player.velocity.x = max(MY.player.velocity.x - PLAYER_ACCEL, -PLAYER_MAX_SPEED)
#MY.player.sprite = MY.paul_run_left

To uncomment the code, delete the # symbol at the front (left) of Lines 15-16.

These lines of code track the speed of Paul moving, as he accelerates to his top speed.

  • On Line 15, the x part of the MY.player.velocity.x just tracks that this is for the X axis (horizontal movement to the left and right). The Y axis movement (up and down) is managed for the jetpack stages on Lines 9-12.
  • On Line 16, MY.paul_run_left makes sure we're showing the correct sprite animation of Paul running (to the left, in this case).

The variable, My.player.sprite, saves the animation so that we can use it later. A sprite is a computer term to describe an image or animation in your game or program.

Next, we'll go over the code that you copied from the book.

You copied Line 18, to check if the player holds down the Right Arrow key on the keyboard. This is similar to Line 13, but it's an elif statement, and you'll need to use the K_RIGHT parameter instead of K_LEFT. Here's how the code should look:

elif key_held_down(pygame.K_RIGHT):

Like I mentioned in the book, make sure Line 18 is lined up with the code comment on Line 17!

Next, you'll see the code comment on Line 19: #TODO: Copy the code here to set Paul's velocity.

Then on Line 20, you copied another line of code. You set the MY.player.velocity.x variable to the lower velocity that you got from the min() function. As I mentioned, this line of code is similar to Line 15. This time, we add the acceleration argument (PLAYER_ACCEL) instead of subtracting it, like we did on Line 15. Similarly, this time the PLAYER_MAX_SPEED isn't negative. The reason is because Paul is moving to the right instead of to the left. Make sure your code is at the same indentation level as Lines 19 and 21.

Next, you'll see the code comment on Line 21, #TODO: Write the code here to set Paul's righthand movement animation.

Under that line, on Line 22, you copy the code I sent you. You can see this code on Page 53 in the book, to review the texts I sent you. The code's similar to Line 16. You just change the Paul running variable from My.paul_run_left to My.paul_run_right instead (because we're animating Paul running to our right).

Lines 24-31 are an if block, which includes deceleration statements that stop Paul after the player stops pressing the arrow keys. In other words, the code slows Paul down.

On Line 25, you'll find the comment, #TODO: Copy the code here to track and control velocity when grounded. By velocity, we mean the speed Paul is moving, where he slows down and speeds up a little, to make it feel more realistic.

In the text messages that I sent you in the book, I included the code for Lines 26-31:

  • Line 26 starts an if statement, to check if Paul's directional movement (MY.player.velocity.x) is greater than zero (whether he's facing to the right or to the left). If it's greater than zero, then that means Paul is facing to the right. Make sure Line 26 is indented at the same level as the code comment on Line 25.
  • If Paul is facing to the right, then Line 27 slows Paul down. Line 27 should be indented one level to the right of Line 26 (to be part of the if block). You're setting the velocity from the max() function (to the right of the = operator) to the My.player.velocity.x variable that's on the left side of the operator.
  • Then Line 28 plays Paul's standing/idling animation, where he's facing to the right. This line should be at the same indentation level as Line 27. You're assigning Paul's right idle animation to the MY.player.sprite variable.

Similarly, on Line 29, we check if Paul is facing to the left. This time you start with an elif statement. If so, then the program runs Lines 30-31:

  • If he is facing left, then we slow him down on Line 30, by using the min() function again.
  • On Line 31, we play his standing/idling animation, where he's facing to the left. We set the MY.player.sprite variable to his left idle animation.

Next, the code comment on Line 32 is #TODO: Write the code here to track and control velocity when falling. This is the part of the game (stages 1-3), where Paul uses the jetpack to jump high and then float back down safely when he's falling.

On Line 33, we had you type in else: Line 33 should be at the same indentation level as the comment on Line 32. Back on Line 24, we had the statement, if MY.grounded: This else statement is a new code block that runs if Paul isn't on the ground.

In the text messages, I had you copy Lines 34-37 on Page 54 in the book. This is similar to Lines 26-31, but this time we don't have the idle animations for when Paul stops, because that code runs when he's on the ground. Instead of slowing Paul down on the ground, you're ending his downward movement so that he stops and falls. Lines 34-35 stop Paul if he's facing to the right, and Lines 36-37 stop Paul if he's facing to the left.

You'll notice that we use the variable PLAYER_AIR_DECEL on Lines 35 and 37, instead of PLAYER_DECEL (like we do on Lines 27 and 30). This is because Paul is falling through the air and not on the ground. As I mentioned, you'll also notice that we have fewer lines of code for the air deceleration. This is because we aren't displaying Paul's idle animation, like we did on Lines 28 and 31.

Next, the code comment on Line 39 is #TODO: Write the code here to track and control velocity when flying. This is the part of the game (stages 4-6), where Paul flies around with his jetpack!

Then, you copied the code to start a new if statement that begins on Line 40:

if not MY.grounded:

Make sure you align the indentation of Line 40 with the comment on Line 39 above it. This if block goes through Line 44.

In the Level 5 text messages (which you can find in our book, on Page 55), I shared what to type on Lines 41-44. Make sure you indent to the right, under the if statement on Line 40.

  • Line 40 checks whether the MY.grounded variable is true. If not, that means that Paul is in the air. The word not is a logical operator in Python to check whether something isn't true. If so (Paul is in the air), then the program runs Line 41. If not (Paul isn't in the air), then we skip the if block of code (Lines 41-44), and the program continues running on Line 46.
  • Line 41 checks if Paul is moving to the right. This is similar to an X axis, where positive movement is on the right side of the grid.
    • If he's moving to the right, Line 42 then displays the animation of Paul flying to the right.
  • Line 43 checks if Paul is flying to the left. Similar to the X axis on a grid, a negative number means that Paul is moving left.
    • If he is moving left, then Line 44 displays the animation of Paul flying to the left.

Here is all of Line 44 (because I gave it to you in pieces in the text message):

MY.player.sprite = MY.paul_jetpack_left

Then, Line 47 sets the gravity to determine how fast Paul moves. This line is already written in your CreeperChase.py template file.

Next, Lines 50-58 will check if Paul collides with the hazard tiles. Basically, it's whether or not Paul touches the lava tiles (when on planet Lav-ash) or the pepper tiles (on planet Foodera).

On Line 49, you'll find the code comment, #TODO: Write the code here to check for hazard collisions. You're going to write on the line below that...

On Line 50, you create a for loop to check if Paul is touching a hazard tile:

for hazard in MY.hazards:

This statement checks every hazard tile to see if Paul collides with it.

On Line 51, we're going to embed an if statement, to run if Paul collides with a hazard tile (the lava tile on planet Lav-ash and the pepper tile on planet Foodera). Be sure to indent to the right of the for statement (on Line 50). Line 51 should be lined up with Line 43.

  • After the if keyword, add a space, and then type MY.player.collides_with(hazard): This function detects that the player collides with an object. In this case, we put the hazard variable in the parentheses (our argument), so it's detecting whether Paul collided with a hazard tile.

  • We end the line with a colon (:) as part of the if statement. If the statement is true (if Paul collided with a hazard tile), then the rest of the if block runs, starting with Line 52.

On Line 52, you're going to subtract one health point from Paul's total health.

  • Start by typing in the MY.player_health variable that represents Paul's health.
  • Add a space, and then type in -= 1 to subtract one health.

The -= operator symbol subtracts a number value (in this case, 1) and then sets the new value to the MY.player_health variable.

On Line 53, you check if Paul's health has been reduced down to zero. Paul only has one health in this game. So, this if statement runs when Paul touches one hazard tile. (Challenge 2 shows you how to add batteries, so Paul can gain more than just one health point.)

  • Since it's an if statement, you start Line 53 with the if keyword.
  • Next, add a space, and then type in the variable for Paul's health, MY.player_health (this is the same variable that you used on Line 52).
  • Then you type in the == equals operator, followed by a space, the number 0 and a colon.

If Paul's health is down to 0, then he's out of health and has to start the level over!

The final code on Line 53 will look like this: if MY.player_health == 0:

Line 54 runs if Paul is out of health. It's time to write Line 54:

  • Be sure to indent to the right of the if statement on Line 53.
  • Type in the function, restart_level(MY.level_num) (this code restarts the current stage over, and the player has to try again).

The good news is that if the player dies, they don't have to start the whole game over again... just the stage that they're on!

Next, you write Line 55:

  • On Line 55, make sure you're at the same indentation level as Line 53 (the if statement).
  • Type in else: to tell Python what to run next. Your program will then run the code on Lines 56-58. This block of code animates and resets Paul after he gets hurt.

Line 56 plays Paul's pain animation, facing to the right:

  • Make sure you're indented to the right of Line 55. (Line up the indentation level with Line 54.)
  • Type the variable MY.player.sprite first.
  • Then add a space, an equal sign (=), and another space.
  • Next, type in the MY.paul_pain_right variable.

Line 56 sets the pain animation to your My.player.sprite variable to run the animation. The final line of code on Line 56 looks like this: MY.player.sprite = MY.paul_pain_right

On Line 57, you're going to reset Paul to the player's starting position on that game stage.

  • At the same indentation level as Line 56, start by typing in the MY.player.location variable.
  • Just like on Line 56, you'll add a space, an equal sign (=), and another space. * Then type in the MY.player_start_position variable.

Line 57 moves Paul back to the starting point of the current stage. The final line of code on Line 57 looks like this: MY.player.location = MY.player_start_position

Line 58 sets Paul back to a state where he's not moving.

  • On Line 58, stay at the same indentation level as Lines 56-57.
  • Type in the MY.player.set_velocity() function.
  • Next, type in the 0, 0 parameters into the function parentheses. Make sure you use a comma between the two numbers. The first 0 sets the horizontal velocity (sideways movement), and the second 0 sets the vertical velocity (moving up and down).

Line 58 sets Paul so that he is not moving up or to the left/right, after he is repositioned to the beginning of the stage. The final line of code looks like this: MY.player.set_velocity(0, 0)

4. The Final Code

I included a solution file for Level 5: Creeper Chase. This file has all the code filled in, so if you run into any issues with your code (for example, it doesn't compile/run, or something isn't working correctly in your program), then you can take a look at the final code file to see what you did differently:

IMPORTANT: Please don't cheat yourself! Finish the game first!

Next Steps

Next, you can take on the two extra challenges to chase the Creeper and learn more! When you're done, you can move on to Level 6, the Boss Battle!

Take the Challenges!

  1. Challenge 1: In this challenge, you are going to write code that will add a time limit for 45 seconds for each stage of the game.

  2. Challenge 2: In this challenge, you are going to add batteries to the game. Paul collects the batteries to regain his health.

  3. Bonus Challenge: In this extra challenge, you'll see how to make your own stages for Paul to run and jump (and fly) through! Practice your game design skills!

More Level 5 Resources

  • Level 5: Unplugged Activity - I wrote this page with more details than what you saw in the book. In this game,

  • Level 5: Rewards - If you completed the Creeper Chase program that we talked about, then I set up this page to act as a reward. You can see some illustrations of me and learn more about who I am! You'll also find the **WHAT **Award digital download, to show off your accomplishment!

  • Level 5: Creeper Chase: All Online Articles - Return back to the main Level 5 page, with all our online resources!

Level 6

After you're completely done with Level 5 (did you do the challenges?), then it's time to move on to Level 6! While you read through Level 6 in your book, you can check out the resources from Paul Python, as he teaches you how to build the Boss Battle program:

I hope you had fun learning about the Creeper Chase game! We didn't catch the Creeper, of course, but he didn't lose us! Overall, I think this was a very helpful simulator for Paul to use on his mission! If you haven't noticed in the game, when each stage starts, you can see a quick animation of the Creeper jumping into the portal at the end of each stage!

--

⚠️ **GitHub.com Fallback** ⚠️