Level 6: Help - IncrediCoders/Python1 GitHub Wiki
Queen Cobra added this page on June 6, 2023
Hello, class! This page is to help you, in case you get stuck with the instructions that I gave you during class. My class notes are in the book, so please refer to that first.
You'll want to make sure that your Level 6 files are loaded in Visual Studio Code. If you have any problems, please read the instructions at Load the IncrediCoders Files.
Here are the different sections on this page to help you out with your Boss Battle:
-
Play the Game - To see how the final version of the Boss Battle program looks (after you go through the instructions), run the Boss Battle program.
-
Glossary of Terms - This list goes through the 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 below). Check out my code to see what you missed! IMPORTANT: Please don't cheat yourself! Finish the game first!
In this Boss Battle 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 it's up to you two to help build a Boss Battle simulator. Paul is off fighting some Virobots, and he's going to need the simulator done to learn how he can defeat the Cryptic Creeper. And I'm not sure where SideWinder has slithered off to...
You can run the program to see what the finished simulator 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 6 program - Boss Battle:
Here are the instructions on how to play the Level 6 game: Level 6 Instructions
Let's start with some definitions so that you understand all the coding terms that I've been telling you during class:
-
from init import *
: Line 2. This line imports 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)
: Line 4. This function contains the code we want to run every time the simulator needs to be updated. It's where all the actual battle 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. -
Collision: This is when an object touches another object. Collision detection (checking if collisions happened) is very important in building any simulator or game. Starting on Line 6, we use several
if
blocks to check if Paul collided with (ran into) each of the walls. -
MY.player.location
: Line 6. This is the variable that holds the current location of Paul Python. -
MY.wall_height
: Line 6. This is the variable that holds the current height and width of the wall. We use these as boundaries so that Paul does not walk through walls (he's not a ghost!). -
handle_pillar_collision()
: Line 18. This function handles player collision detection with all the pillars in the simulator, so it checks if Paul is running into a pillar and stops him from walking through it. -
My.player.collides_with_boss()
: Line 21. This function checks if Paul runs into the boss (the evil Cryptic Creeper). As part of the book instructions, you're going to copy in Lines 21-24. This code block makes Paul lose health if he collides with the Creeper. -
player_pain_anim()
: Line 22. This function changes the current animation of Paul to show him in pain. We use this when he collides with the boss. -
MY.player_health -= 1
: Line 22. This line decrements the variable for Paul's health by one. -
Hitbox: The invisible box in front of a character that shows the range they can attack. In our case, this is the range in front of Paul Python, where he can hit the Creeper with a melee attack.
-
MY.player_hitbox.active = False
: Line 22. This line makes Paul's hitbox inactive by setting the variable to *False
. We do this when Paul collides with the boss so he is not able to attack while taking damage. -
MY.player_hitbox.location
: Line 28. This variable identifies the space in front of Paul, when he attacks. Attacks only succeed if the target is in his hitbox. -
pygame.math.Vector2()
: Line 28. This function lets us declare where a box begins and ends. We use it to create the location for Paul's hitbox.Vector2
is a method that comes from Pygame'smath
library. -
MY.player.location.x - 20
: Line 37. This is the first argument in the parentheses. It tells us that we're looking to the left of Paul (because - 20 is negative on the X axis). As part of the book instructions, you're going to copy in Lines 35-37. This code block adds the hit box to the left of Paul. -
MY.player.location.y
: Line 37. This variable is also passed to theVector2()
function to make sure the hitbox starts on the right spot on the y axis. -
MY.player_dir == RIGHT
: Line 39. This condition checks if Paul is facing to the right. If he is, then the rest of theelif
block (Lines 40-41) places the hitbox in front of Paul. As part of the book instructions, you're going to copy in Lines 39-41. This code block adds the hit box to the right of Paul. -
MY.boss_health
: Line 45. This variable keeps track of how much health the Creeper has left. As part of the book instructions, you're going to copy in Lines 44-46. This code block reduces Creeper's health when he gets attacked.
My instructions get a little trickier as we go through Level 6 in the book. Eventually, I ask you to write your own code. This section gives you some hints on how to write your code!
-
On Lines 6-7, we make sure that Paul doesn't collide with the wall on the left side of the screen.
MY.wall_height
represents the size of all the walls surrounding the game, so we use it to prevent Paul from going through the wall itself. -
On Lines 9-10, you will need to remove the hashtags. This code makes sure that Paul doesn't walk through the wall on the right side of the screen.
-
On Line 11, you'll find the comment:
#TODO: Uncomment Lines 12-13 to make sure that Paul doesn't walk through the wall on the top of the screen
. Make sure you remove the hash tags on Lines 12-13. -
On Line 14, you'll find another TODO comment:
#TODO: Copy the code here to make sure that Paul doesn't walk through the wall on the bottom of the screen
. You're going to write in the missing code on Lines 15-16. -
Lines 15-16 are a little similar to Lines 9-10. You use the y axis (like what you uncommented on Lines 12-13) instead of the x axis (which was used on Lines 9-10). Instead of
WINDOW_WIDTH - My.wall_height
, set the variable toWINDOW_LENGTH - (MY.wall_height + 15)
because we're looking at the location on the y axis. -
Here's the code that you need to type in, on Line 15 (make sure you include the colon at the end of the line):
if MY.player.location.y > WINDOW_LENGTH - (MY.wall_height + 15):
-
Next, type in Line 16. Make sure you indent to the right, at the beginning of the line:
MY.player.location.y = WINDOW_LENGTH - (MY.wall_height + 15)
-
Line 20 has the comment:
#TODO: Copy the code here for Paul to lose health if he collides with the Creeper
. You're going to write anif
block under that line. -
Starting on Line 21, an
if
statement will check if Paul collides with the boss. You'll start with theif
keyword and then call the functionMY.player.collides_with_boss()
. Make sure you end the line with a colon. -
On Line 22, make sure the code is indented to the right of the earlier
if
statement. Then type inplayer_pain_anim()
on that line. You are calling the function that switches Paul's art to show his pain animation, when he collides with the Creeper (ouch). -
On Line 23 (with the same indentation), type
MY.player_health -= 1
to subtract one of Paul's health points when the two collide. -
On Line 24, type
MY.player_hitbox.active = False
to give Paul some time before being able to attack again, since he is still in pain from just being hit.
-
On Lines 27-29, you uncommented more of the code. You're going to write similar code on Lines 31-33.
-
Line 34 contains the comment:
#TODO: Copy the code here, to add the hit box to the left of Paul
. -
On Line 35, type out
elif MY.player_dir == LEFT:
This checks if Paul is facing to the left. -
On Lines 36-37, you'll place the hitbox in front of Paul, when he is facing to the left.
-
On Line 36, make sure you're indented to the right, and then type
MY.player_hitbox.location = pygame.math.Vector2(
.- Just like on Line 32, you're breaking up one code statement over two lines. This way the lines of code aren't too long, and you don't have to scroll to the right to see all the code. It's easier for other developers (just like you) to read.
-
On Line 37, make sure you're indented to the right of Line 36, and then finish the rest of the code statement
MY.player.location.x - 20, MY.player.location.y)
on the new line. -
Line 38 contains the comment:
#TODO: Write the code here to add the hit box to the right of Paul
. -
On Lines 39-41, you're going to write the code that sets the hitbox to the right of Paul, when he is facing right.
-
On Line 39, start your
elif
block out with this statement:elif MY.player_dir == RIGHT:
- The
elif
statement checks for an additional condition in yourif
/elif
ladder. We call it a ladder because it kind of looks like a ladder of code, from the repeating pattern of how the code is indented and formatted. In this case, on Line 39, it's checking if Paul is facing to the right.
- The
-
On Line 40, indent to the right, and then type in this code:
MY.player_hitbox.location = pygame.math.Vector2(
- Just like on Line 36, you're only including part of the full line of code. The rest of the statement will be on the line below it.
-
Go ahead and type in the rest of the code on Line 41:
MY.player.location.x + 20, MY.player.location.y)
-
On Line 43, you'll find the comment:
#TODO: Copy the code here, to reduce Creeper's health when he gets attacked
. Under that, you're going to write anif
block of code. -
On Line 44, write the condition that checks if Paul's hitbox is active and if the Creeper is in the hitbox. In other words, you're checking whether Paul's attack has hit the Creeper! Type in this code:
-
if MY.player_hitbox.active and MY.boss.collides_with_hitbox():
-
The
and
operator adds a second condition so that both conditions have to be true. In other words, Paul's hitbox has to be both active and colliding with the Creeper, to meet this requirement.
-
-
On Line 45, you will reduce the Creeper's health by one point when Paul hits him. In the book (or the text messages that are recorded in the book), I mentioned how this line of code is very similar to Line 23. This time, you're using the variable
MY.boss_health
, so type:MY.boss_health -= 1
- As you might expect, this code removes
1
from theMY.boss_health
variable and then saves the new total back into the same variable.
- As you might expect, this code removes
-
Line 46 is the last line of this
if
block. Type the code that makes Paul's hitbox inactive. In the book, we broke down each part of this statement. Putting it together, you start the line of code with the full variable that represents Paul's hitbox:MY.player_hitbox.active
-
The attribute
.active
shows you that the hitbox is turned on. -
After the variable, add a space, the
=
equals sign, and another space, then type inFalse
to end the line. Now the player will have to wait before being able to hit the Creeper again.
-
-
On Line 48, calling the
player_attack_update()
function makes the player hitbox active and starts Paul's attack animation if the player is attacking. -
On Line 50, calling the
update_assets(delta_time)
function updates the assets to make sure all the animations are running. -
On Line 52, calling the
check_win()
function checks if you've defeated the Creeper or lost all your health, and updates the game state accordingly if the game is over. -
On Line 54, the
check_events()
function checks for different pygame events, including whether the Creeper is attacking Paul and if the player closed the game window.
-
On Lines 57-59, the three
Manager.register()
lines introduce code to manage the game states (the different parts of the game): playing the game (Line 57), animating the "Game Over"/"You Win" overlay (Line 58), and the Play Again screen (Line 59).-
Manager.register(sys.modules[__name__])
- On Line 57,sys.modules
is the current file. It registers all the code that we've written. -
Manager.register(GameOver)
- On Line 58, this opens the Game Over screen, where it says that you lost (Game Over) or that you won (You Win). You can choose to play the game again or quit. -
Manager.register(PlayAgain)
- On Line 59, this adds the Play Again state, where you can click the screen to restart the game.
-
-
On Line 62, the code runs the game:
Manager.run(SCREEN, WINDOW, BLACK, "MAIN")
. TheSCREEN
argument designates that all the images and text go into the screen. TheWINDOW
argument determines the window size.BLACK
sets the background color. And then"MAIN"
starts the game at the beginning.- Later, when you tackle the Challenges, you'll change the last argument (
"MAIN"
), when you add in the new features, like new abilities and sounds.
- Later, when you tackle the Challenges, you'll change the last argument (
I included a solution file for Level 6: Boss Battle. This file has all the code filled in, so if you run into any issues with your code (for example, if it doesn't compile/run, or if 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 7, IncrediCards!
-
Challenge 1: In this challenge, you are going to...
-
Challenge 2: In this second challenge, you are going to...
In addition to this Online Articles page and the instructions for our Level 6 challenges, we also have a Help Page, a Learning Quiz, an Unplugged Activity, and a Rewards article:
-
Level 6: Help - This page helps you complete the instructions in the book, in case you get stuck.
-
Level 6: Learning Quiz - I wrote some questions in case you want to quiz yourself about what you learned. Or you can teach others and quiz them!
-
Level 6: Unplugged Activity - I wrote this page with more details than what you saw in the book. In this game,
-
Level 6: Rewards - If you completed the Boss Battle 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 Mech Award digital download, to show off your accomplishment!
After you're completely done with Level 6 (did you do the challenges?), then it's time to move on to Level 7! While you read through Level 7 in your book, you can check out the resources from Grafika & Syntax, as they teach you how to build the IncrediCards program:
I hope you had fun learning about the Boss Battle!
--Queen Cobra