Level 4: Help - IncrediCoders/Python1 GitHub Wiki
SideWinder added this page on June 6, 2023
Alright, Paul. You better be paying attention (and not wasting my time). I wrote this page to help you, in case you get stuck with the instructions that I gave you during Mr. Mutt's class. My text messages I sent you are in the book, so make sure you read that first.
You'll want to make sure that your Level 4 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 Space Wars game:
- Play the Game - To see what the final version of the Space Wars program looks like (after you go through the instructions), run the Space Wars 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). Check out my code to see what you missed! IMPORTANT: Please don't cheat yourself! Finish the game first!
If you have any questions about the information on this page, or if any of the information or instructions seems wrong, then please let us know by creating a GitHub Issue.
1. Play the Game
In this Space Wars 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...
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 are the downloads to run the Level 4 program - Space Wars:
Here are the instructions on how to play the Level 4 game: Level 4 Instructions
2. Glossary of Terms
Let's start with some definitions so that you understand all the words that I've been telling you during class:
-
from init import *
: This code imports everything in the init.py file, which sets up the game and includes game features and functions that you'll use throughout this game. -
def update(delta_time)
: This function contains the code 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, using the elements of a list in order. In this program, you will seefor event in pygame.event.get():
, which means the code block below it will run again each time for every element in the list frompygame.event.get()
. When the list is completed, the program will run the next line of code that is outside of thefor
loop block. -
Event: An event is a player interaction with the computer, like pressing a key on the keyboard or clicking a mouse. For example, pressing the down arrow on the keyboard would send an event to your game, where your code decides what happens now that a player has pressed that key. In this case, the ship moves down. Similarly, when the player clicks the mouse, a mouse click event is sent to the game to determine what to do.
-
pygame.event.get()
: This function returns a list of all the events that have been sent to the game. -
if event.type == pygame.QUIT
: Thisif
expression checks to see if the player has closed the window screen of the program (for example, the player quit the game). -
stop()
: This function stops the game and exits the program entirely. -
elif
: 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, and then in every subsequent block that is similar to anif
condition, you would instead writeelif
. At the end of these conditions, you would still put anelse
statement for the very last code block. -
key_down()
: This function tells us whether a key is pressed. The key can be a letter key, number key, or special character key. (Since we only care about when the keys are first pressed down, we don't need a "key up" function.) -
K_SPACE
: This is a variable that represents the space (SPACE) key (K) on the keyboard. -
fire_bullet(1)
: This function runs the code that fires a bullet for a ship. The number argument (in the parentheses) represents which player you want to fire a bullet, 1 for Player 1 or 2 for Player 2. -
K_RETURN
: This is a variable that represents the return (RETURN) key (K) on the keyboard. -
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, but the difference is we are checking if the key is being pressed down for a longer period of time. -
Rotation: This is the act of turning an object. In this level you are "rotating" a ship, which means that you are turning it on its axis. You're not moving it forward or backward, but you're just turning it toward one direction or the other (clockwise or counterclockwise).
-
ship_rotate
: This is a number variable that represents the rate at which the ship should rotate (how fast it turns). -
Velocity: This is the speed of something. In this level, it's the speed of the ship. The higher the velocity of something is, the faster it goes, no matter what direction it is traveling in.
-
State: Everything that the computer knows about the variables and objects in your game, at a specific point in time.
-
Register: This is the act of informing the computer about a state. "Registering" stores collected data in a state for later use.
-
Modules: This is a set of standardized parts or independent units that can be used to construct a more complex structure. Think of it as the collection of tools and equipment that you need to build a house. You just need to assemble it yourself using those tools.
-
Manager.run()
: This is the function that actually runs the game on your computer after all the code logic has been completed.
3. Advice on Writing the Code
This section provides you hints on how to write your own code!
First, we'll go back over the code that you copied from the book.
Important: When you finish adding in your line of code, do not press Enter in your IDE/editor (such as Visual Studio Code and Replit). If you press Enter in the editor, then you will add lines to the template, and the template numbering will be off from the Line number system used in the book and in these help pages!
Line 22 is an elif
statement that checks if Player 1 presses the D key:
- Make sure you're aligned at the same indentation level as Line 21.
- Type in
elif
first. (You use theelif
statement to skip the rest of theelif
statements, if there areelif
statements beneath it, in the same if block. In this case, there aren't anyelif
statement that it skips.) - Next, add a space, and then type in the function
key_held_down(pygame.K_d)
from the Pygame library. It checks if you're pressing the D key! - Finish the
elif
statement with a colon at the end of line. - Your full line of code should look like this:
elif key_held_down(pygame.K_d):
On Line 23, make sure your cursor is indented to the right of Line 22 (the elif
statement). While you're still on Line 23, type in the MY.player1.add_rotation()
function. In the parentheses, add -ship_rotate * delta_time
as the argument formula. This rotates the Player 1 ship clockwise. Back on Lines 19-20, you assigned the A key to rotate the Player 1 ship counterclockwise, so Lines 22-23 are similar.
Here's your final code on Line 23: MY.player1.add_rotation(-ship_rotate * delta_time)
The next line of code that you need to add to the SpaceWars.py file is on Line 29. Keep same indentation that Line 28 has (the code comment, #TODO: Copy the code here for the Player 1 ship to move backward
). Copy this code onto Line 29:
elif key_held_down(pygame.K_s):
The line starts of with an elif
statement, which runs if the if
statement on Line 26 isn't true (if the player doesn't press the W key). This time (on Line 29), the program checks if the player presses the S key on the keyboard. If that happens (the conditional statement is true), then Line 30 runs.
On Line 30, make sure you're indented to the right of Line 29, and then enter this code:
MY.player1.add_velocity(MY.player1.rotation, -ship_accel, ship_max_speed)
What you just entered is this function with arguments added into the parentheses: MY.player1.add_velocity()
That function moves the Player 1 ship (the Monarch ship). The first argument added into the parentheses is My.player1.rotation
, which tells the program what direction (or degree/angle) that the ship is facing. The second argument is -ship_accel
, which can either slow down the acceleration of the ship or make you move backwards. The third argument is ship_max_speed
, which is the limit of the speed you can reach when flying backwards.
The code snippet in the book doesn't show Line 30 as far indented as the code will. For Line 30, make sure you fully indent your code: MY.player1.add_velocity(MY.player1.rotation, -ship_accel, ship_max_speed)
On Line 34, you want to write the code for Player 2 to rotate the Nocturn ship counterclockwise (to the left). This line checks if Player 2 presses and holds the left arrow on the keyboard. Enter this code on Line 34:
if key_held_down(pygame.K_LEFT):
Be sure to include the colon at the end of the line! This if
statement uses the key_held_down()
function to check if the player presses the Left arrow on the keyboard.
On Line 35, indent to the right (so that the line is part of the if
block). Then, copy this line of code, which rotates the ship:
MY.player2.add_rotation(ship_rotate * delta_time)
The function uses the ship_rotate
variable to rotate the Player 2 Nocturn ship counterclockwise (to the left). The argument in this function is the product of the ship_rotate
variable multiplied by the delta_time
variable, which is what gives us the ship's rotation speed.
On Line 36, make sure you're indented at the same level as Line 34. If Line 34 isn't true (you didn't press the Left Arrow key), then the elif
statement on Line 36 runs to see if you pressed the Right Arrow key. This line is similar to Line 34, where the Player 2 Nocturn ship rotates, but this time the ship rotates clockwise (to the right).
Enter this code on Line 36, to check if Player 2 presses and holds the Right Arrow key on the keyboard:
elif key_held_down(pygame.K_RIGHT):
Once again, do not forget the colon, at the end of the line (since it's an elif
statement).
On Line 37, indent to the right so that this line lines up with Line 35. Then, the code for this line is almost the same as Line 35, except since the ship is being rotated the opposite way (clockwise), there needs to be a -
in front of the ship_rotate
variable (to make it negative and to rotate in the opposite direction). Enter this code:
MY.player2.add_rotation(-ship_rotate * delta_time)
Just like on Line 35, the argument is the product of multiplying these two variables together to give us the ship's rotation speed.
On Line 38, leave the line blank so it separates the code for rotating the ship, and moving the ship.
See Line 40, the code comment, #TODO: Write the code here to move the Player 2 ship
. Under that comment, on Lines 41-44, you're going to add the code that moves the Nocturn ship forward and backward.
On Line 41, you start writing the code to move the Nocturn ship forward. This line checks to see if Player 2 pressed the up arrow on the keyboard:
if key_held_down(pygame.K_UP):
Once again, do not forget the colon at the end of the line!
On Line 42, indent code to the right so that it lines up with Line 37. Type in this code:
MY.player2.add_velocity(MY.player2.rotation, ship_accel, ship_max_speed)
Here's what the code does:
MY.player2.add_velocity()
is the function that moves the Player 2 ship forward.MY.player2.rotation
is the direction that your ship is currently facing.ship_accel
determines the acceleration rate of the ship.ship_max_speed
determines the maximum speed that the ship can reach when accelerating.
Starting on Line 43, you write the code to move the Nocturn ship backward. This line checks to see if Player 2 pressed the down arrow on the keyboard:
elif key_held_down(pygame.K_DOWN):
Remember to include the colon! An elif
statement runs if the if
statement isn't true.
On Line 44, indent this code to the right, so that it lines up with Line 42. The difference between moving the ship forward (Line 42) and moving it backward is the ship's acceleration. To represent the opposite (negative acceleration), put a -
next to the ship_accel
variable. Using the ship's acceleration and maximum speed, this is how to write the code that moves the ship backward:
MY.player2.add_velocity(MY.player2.rotation, -ship_accel, ship_max_speed)
4. The Final Code
I included a solution file for Level 4: Space Wars. 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 add to your Space Wars program and learn more! When you're done, you can move on to Level 5, the Creeper Chase!
Take the Challenges!
-
Challenge 1: In this challenge, you are going to add sound to the game when projectiles are fired!
-
Challenge 2: In this challenge, you are going to write code that will add asteroids to the game!
More Level 4 Resources
In addition to this Help page and the instructions for our Level 4 challenges, we also have Online Articles, a Learning Quiz, an Unplugged Activity, and a Rewards article:
-
Level 4: Online Articles - I made you a list of different web pages I found, which will help you learn more about creating the Space Wars program.
-
Level 4: 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 4: Unplugged Activity - I wrote this page with more details than what you saw in the book. You're going to recreate the SpaceWars game in person! You'll get a chance to review and practice key events and how they work with spaceships.
-
Level 4: Rewards - If you completed the Space Wars project, then you're welcome to check out this page as a reward. You can see some illustrations of me and learn more about who I am! You'll also find the Goth Award digital download, to show off your accomplishment!
Level 5
After you're completely done with Level 4 (did you do the challenges?), then it's time to move on to Level 5! While you read through Level 5 in your book, you can check out the resources from Intelli-Scents, as she teaches you how to build the Creeper Chase program:
I hope you had fun learning about space wars!
-- SideWinder