Level 5: Help - IncrediCoders/Python1 GitHub Wiki

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:
- 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!
- 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!
- 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!
- 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!
In this Creeper Chase 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 game 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 game's Nerdy Notes (factoids) and the Known Issue about how moving the window makes Paul drop down.
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):
-
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.
-
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. -
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. -
forloop: Aforloop is something that runs the same block of code a specific number of times. We first use theforloop 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. -
for event in pygame.event.get():: In this program, on Line 6, you will seefor 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 theforloop block. -
Function: A group of code that you can call from anywhere in your code. You can pass parameters, which are variables, into your function.
-
pygame.event.get(): This function returns a list of all the events that have happened. This is still on Line 6. -
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.
-
if event.type == pygame.QUIT: On Line 7, thisifexpression 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). -
stop(): On Line 8, this function stops the game and exits the program entirely. It runs if the player exits the window (based on theifstatement on Line 7). -
elif: We use this on Line 9. This keyword comes from the combination of two other keywords:elseandif, which you have seen before in previous levels.elifis used when you have more than one condition to check at the same time. You would start with anifstatement (like we do on Line 7). Then, in the following blocks that are similar to anifcondition, you would instead writeelif. At the end of these conditions, you would still use anelsestatement for the very last code block (in case none of yourifandelifstatements are true). -
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. -
(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. -
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! -
MY.grounded = FALSE: On Line 11, this sets theMY.groundedvariable 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. -
jetpack_up_animation(): On Line 12, this function displays the jetpack animation. -
key_held_down(): This function tells us whether a key is being held down. Just likekey_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. -
key_held_down(pygame.K_LEFT): On Line 13, this function checks if the left arrow key is held down. -
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. -
max(): Also on Line 15, themax()function calculates whether the speed you're moving or your top speed is higher. It then sets the higher number to theMY.player.velocity.xvariable. -
PLAYER_ACCEL: This is the player acceleration argument. It tracks how fast your character moves to get to the max speed. -
else: We're jumping down to Line 23. Thiselseblock runs if the player isn't holding down the right or left key. -
MY.grounded: On Line 24, we run thisifblock, if Paul is touching the floor. -
velocity: This is the speed that something is moving. We use the term as a part of theMY.player.velocity.xvariable that you'll find throughout this program. In this level, it's the speed that Paul is moving. When the velocity gets lower, Paul slows down. -
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. -
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.)
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_leftTo 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_leftmakes 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
ifstatement, 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
ifblock). You're setting the velocity from themax()function (to the right of the=operator) to theMy.player.velocity.xvariable 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.spritevariable.
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.spritevariable 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.groundedvariable is true. If not, that means that Paul is in the air. The wordnotis 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 theifblock 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_leftThen, Line 47 sets the gravity to determine how fast Paul moves. This line is already written in your CreeperChase.py template file:
- Here's the line of code:
MY.player.velocity.y = min(MY.player.velocity.y + GRAVITY_ACCEL, PLAYER_TERMINAL_VEL) - This runs the
min()function:- It adds the player's vertical (up and down) velocity (speed of movement) and the acceleration together. That's the first argument that's passed to the
min()function. - It then passes the vertical terminal velocity (the limit) to the function as well.
- The function then returns the lower number (the minimum number).
- It adds the player's vertical (up and down) velocity (speed of movement) and the acceleration together. That's the first argument that's passed to the
- The program sets the results to the
MY.player.velocity.yvariable, which is the speed that Paul is moving vertically. - This is one way to make sure that Paul's vertical movement or momentum has a max speed.
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:Make sure that Line 50 is indented at the same level as the code comment on Line 49.
This statement checks every hazard tile to see if Paul touches (or collides with) the tile. For every hazard tile, the program runs Lines 51-58.
On Line 51, you'll see the code comment, # Check if the player has touched a hazard.
On Line 52, you nest an if statement that runs if Paul collides with a hazard tile. The if block is nested inside the for block. (The hazard tiles are the lava tile on planet Lav-ash and the hot pepper tile on planet Foodera).
Make sure that Line 52 is lined up with Line 51 (both are indented to the right of the for statement on Line 50).
On Line 52:
- Type in the
ifkeyword and add a space. - Then type
MY.player.collides_with(hazard):This function detects that the player collides with an object. In this case, we put thehazardvariable in the parentheses as our argument. It's detecting whether Paul collided with a hazard tile. - We end the line with a colon (
:) as part of theifstatement. If the statement is true (if Paul collided with a hazard tile), then the rest of theifblock runs (Lines 53-58).
On Line 53, you'll see the code comment, # Show the animation for Paul getting injured.
Line 54 sets the pain animation to your My.player.sprite variable to run the animation:
- Make sure you're indented to the right of Line 52. (This is the same indentation level as Line 53.)
- Type the variable
MY.player.spritefirst. - Then add a space, an equal sign (
=), and another space. - Next, type in the
MY.paul_pain_rightvariable. Paul faces to the right when he restarts the level, when the pain animation plays. He drops down from the starting position.
The final line of code on Line 54 looks like this: MY.player.sprite = MY.paul_pain_right
On Line 55, you'll see the code comment, # Reset the location to the starting point.
On Line 56, you're going to reset Paul to the player's starting position on the same game stage:
- At the same indentation level as Line 55, start by typing in the
MY.player.locationvariable. - Just like on Line 54, you'll add a space, an equal sign (
=), and another space. - Then type in the
MY.player_start_positionvariable.
Line 56 moves Paul back to the starting point of the current stage. The final line of code on Line 56 looks like this: MY.player.location = MY.player_start_position
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!
On Line 57, you'll find the code comment, # Set the speed to 0 in both directions.
Line 58 sets Paul back to a state where he's not moving:
- On Line 58, stay at the same indentation level as Lines 53-57.
- Type in the
MY.player.set_velocity()function. - Next, type the
0, 0parameters into the function parentheses. Make sure you use a comma between the two numbers. The first0sets the horizontal velocity (sideways movement), and the second0sets 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)
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, 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!
-
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.
-
Challenge 2: In this challenge, you are going to add batteries to the game. Paul collects the batteries to regain his health.
-
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!
-
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!
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!
--