Level 5: Help - IncrediCoders/Python1 GitHub Wiki
Intelli-Scents added this page on March 14, 2023
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 that 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, in Visual Studio Code or in Replit. 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, 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 levels), and then how to fly in levels 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
Let's start with some definitions so that you understand all the words that I've been telling you during class. 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. -
for
loop: Afor
loop is something that runs the same block of code a specific number of times. We first use thefor
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. -
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 thefor
loop 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, thisif
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). -
stop()
: On Line 8, this function stops the game and exits the program entirely. It runs if the player exits the window (based on theif
statement on Line 7). -
elif
: We use this on Line 9. This keyword comes from the combination of two other keywords:else
andif
, 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 anif
statement (like we do on Line 7). Then, in the following blocks that are similar to anif
condition, you would instead writeelif
. At the end of these conditions, you would still use anelse
statement for the very last code block (in case none of yourif
andelif
statements 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 level 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 level 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.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. -
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.x
variable. -
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. Thiselse
block runs if the player isn't holding down the right or left key. -
MY.grounded
: On Line 24, we run thisif
block, if Paul is touching the floor. -
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. -
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 hints on how to write your own code!
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`
These lines of code track the speed of Paul moving, as he accelerates to his top speed. MY.paul_run_left
makes sure we're showing the correct sprite animation of Paul running (to the left, in this case).
The variable, 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.
First, you copied Line 18, to check if the player holds down the Right Arrow key on the keyboard. This is similar to Line 13. You'll want to use the K_RIGHT
parameter instead of K_LEFT
.
Like I mentioned in the book, make sure Line 18 is lined up with the line above it!
Then on Line 20, you copied more code. You set MY.player.velocity.x
to 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. The reason is because Paul is moving to the right instead of to the left.
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 the book, to review the texts I sent. The code's similar to Line 16. You just change the Paul running variable from paul_run_left
to paul_run_right
instead (because we're animating Paul running to our right).
Starting on Line 23 and going through Line 31, these are the deceleration statements to stop Paul after the player stops pressing the arrow keys. 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, and the book, I sent you 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 Paul is facing to the right, then Line 27 slows Paul down, and Line 28 plays Paul's standing/idling animation, where he's facing to the right.
Similarly, on Line 29, we check if Paul is facing to the left. If he is, then we slow him down (on Line 30). On Line 31, we play his standing/idling animation, where he's facing to the left.
Next, the code comment on Line 32 is #TODO: Write the code here to track and control velocity when flying
. This is the part of the game (levels 4-6), where Paul flies around with his jetpack!
On Line 33, type else:
Back on Line 24, we had the statement, if MY.grounded:
So this else statement runs if Paul isn't on the ground. Line 33 should be at the same indentation level as the comment on Line 32.
In the text messages, I had you copy Lines 34-37. This is similar to Lines 26-31. Instead of slowing Paul down on the ground, you're slowing down his forward 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.
Here is the code that you'll need to type in,on Lines 34-37:
`if MY.player.velocity.x > 0:`
`MY.player.velocity.x = max(0, MY.player.velocity.x - PLAYER_AIR_DECEL)`
`elif MY.player.velocity.x < 0:`
`MY.player.velocity.x = min(0, MY.player.velocity.x + PLAYER_AIR_DECEL)`
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. 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.
Then, you copy 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, 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 if Paul is in the air. Line 41 checks if Paul is 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. And 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).
Find the code comment, #TODO: Write the code here to check for hazard collisions
, on Line 49. You're going to write on the line below that...
On Line 50, create a for
loop to check if Paul is touching a hazard tile:
`for hazard in MY.hazards:`
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).
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, 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 one 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. In the regular game (that you're building now), Paul only has one health. So, this if
statement runs when Paul touches one hazard tile. (Challenge 2 shows you how to add batteries and 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 type in the ==
equals operator, followed by a space, the number 0
and a colon.
As mentioned, this line determines if Paul is out of health.
The final code on Line 53 will look like this: if MY.player_health == 0:
Line 54 runs if Paul is out of health. 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 level 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 level.
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, if the if
condition (from Line 53) isn't met. Your program will then run the code on Lines 56-58. This block of code resets Paul after he gets hurt.
On Line 56, 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.
The final line of code on Line 56 is MY.player.sprite = MY.paul_pain_right
(to set the animation of Paul in pain while facing to your 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.
The final line of code on Line 57 is MY.player.location = MY.player_start_position
(to move Paul back to the starting point of the level).
Finally, 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). This sets Paul back to a state where he's not moving.
The final line of code on Line 58 is MY.player.set_velocity(0, 0)
(to make sure Paul is no longer moving, after he is repositioned to the beginning of the level).
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 levels 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!
--