Level 4: Challenge 1 - IncrediCoders/Python1 GitHub Wiki
SideWinder added this page on June 6, 2025
Let's begin your first challenge for Level 4!
You are going to add the sounds of the bullet for both Player 1 and Player 2 in the Space Wars game!
To find the Level 4 Challenge 1 code template, open the Level 4 folder, the Challenges folder, and then the Challenge 1 folder. You should already have the files downloaded onto your computer (see Load the IncrediCoders Files). Open the SpaceWarsChallenge1.py file in Visual Studio Code to build the program by following along with my instructions below!
I broke these instructions down into a few sections:
Explaining the Code
On Line 1, you will see a comment, # Runs the Init.py file and imports the libraries
. The next line of code imports the libraries and runs the init.py file.
On Line 2, from init import *
, initializes the init.py file. This file includes code that sets up the file path for the images, gets the fonts ready for this game, sets the window size, and loads the images. It also sets the global colors and directions, it includes some helper logic for the game, and it defines the classes that we use in this game. This statement on Line 2 allows you to use all the information in that file, for the rest of your program.
On Line 4, you will see the comment, # The Update method checks for all the key presses and button clicks
.
On Line 5, the statement def update(delta_time):
updates each time that a key is pressed (to control the ships) or a button is clicked (to restart the game or close the window). It will continue to check for these events throughout the program.
On Line 6, for event in pygame.event.get():
starts a for
loop that checks what the event was for each time that an event occurs (including button presses and mouse clicks).
On Line 7, you will see the comment, # Checks if you click the Replay button to play again
.
On Line 8, check_replay_click(event)
checks to see if the user clicked the replay button at the end of the game.
On Line 9, you will see the comment, # Checks if you closed the window
. The next line of code checks to see if the user closed the window to end the game. (On a PC, this is done by clicking the X in the upper right. On a Mac, the user clicks the red button on the upper left.)
On Line 10, if event.type == pygame.QUIT:
checks to see if the user closed the window. If the user does click the exit button on the window, then Line 11 runs...
On Line 11, stop()
stops the program from running and closes the window.
On Line 12, you'll see the comment, # Fires the two ships' weapons
. This happens on Lines 13-17, in the elif
block.
On Line 13, elif key_down(event, pygame.K_SPACE):
checks to see if the space key has been pressed. If it has, then the program runs the indented code below this line, on Lines 14-17.
Copy the Code
On Line 14, you should see the comment, #TODO: Write code here to add sound when firing the Player 1 weapon
. On Lines 15-16, you'll write the code to add the laser sound for Player 1.
On Line 15, write the following code on the same indentation level as Line 14:
pygame.mixer.music.load(get_file("Assets\LaserShoot1.wav"))
You're using the mixer
object in the pygame
module to load in your sound file. We provide the file in the Assets folder.
NOTE: Try changing the sound file! Change or create your own sound file (such as a WAV file) and then add it to the Assets folder. Change Line 15 to use the file name of your sound file. After you finish building this game, try it out and see what your ship's weapon sounds like!
On Line 16, write the code pygame.mixer.music.play()
at the same indentation level as Lines 14-15. This line plays the laser sound that you just loaded!
On Line 17, you'll see the fire_bullet(1)
function. This line fires the projectile for Player 1.
On Line 18, elif key_down(event, pygame.K_RETURN):
checks to see if the Enter/Return key has been pressed. (It's an Enter key on a PC and a Return key on a Mac.) If the key has been pressed, then the program runs the indented code below this line (Lines 19-22).
On Line 19, you'll see the code comment, #TODO: Write code here to add sound when firing the Player 1 weapon
.
On Line 20, you'll write the code that fires the bullet for Player 2's ship. What from Line 15 needs to be changed to complete this? Give up? It's the file name! Write out the code from Line 15 onto this line.
- Make sure this line is indented at the same level as Lines 14-17 and Line 19.
- Make sure you change the name of the sound file to
LaserShoot2.wav
instead. (Change the1
to a2
instead.)
On Line 21, type in the code from Line 16. It's the same line of code! Make sure you write this code at the same indentation level as Lines 14-17 and 19-20. This line of code plays the laser sound that you just loaded on Line 20.
Line 22 fires the bullet for Player 2. The only difference from Line 17 is that the argument passed in the function is 2
instead of 1
, which represents Player 2 instead of Player 1.
That's it for the new code! You have successfully added the sounds of the projectiles for Player 1 and Player 2 in the Space Wars game!
This challenge is simple but important, as you get used to adding sound effects to your games!
Explaining the Code
On Line 24, the code comment, # Rotates the Player 1 ship
, describes what Lines 25-28 do.
On Line 25, if key_held_down(pygame.K_a):
checks to see if the "a" button has been pressed, and if it has, then it will run the following indented code (which is just Line 26).
On Line 26, the code MY.player1.add_rotation(ship_rotate * delta_time)
rotates Player 1's ship counterclockwise, when the "a" key was pressed (from Line 25).
On Line 27, elif key_held_down(pygame.K_d):
checks to see if the "d" key has been pressed. If it has, then the program runs the following indented code (Line 28).
On Line 28, MY.player1.add_rotation(-ship_rotate * delta_time)
rotates Player 1's ship clockwise. It rotates the ship clockwise because there is a -
symbol next to the ship_rotate
variable. This code runs because the "d" key was pressed on the keyboard (from Line 27).
On Line 30, the comment, # Moves the Player 1 ship forward and backward
, describes that Lines 31-34 will make it so that Player 1 can move their ship both forward and backward.
On Line 31, if key_held_down(pygame.K_w):
checks to see if the "w" key has been pressed. If so, the program runs the following indented line of code, Line 32.
On Line 32, the code MY.player1.add_velocity(MY.player1.rotation, ship_accel, ship_max_speed)
moves Player 1's ship forward, since the "w" key was pressed. It uses the acceleration and maximum speed variables to determine its speed of movement.
On Line 33, elif key_held_down(pygame.K_s):
checks to see if the "s" key has been pressed. Similar to Line 31, if it has been pressed, then the program runs Line 34.
On Line 34, MY.player1.add_velocity(MY.player1.rotation, -ship_accel, ship_max_speed)
moves Player 1's ship backward. The -
symbol in front of the ship_accel
variable moves the ship's direction into the negative (backwards).
On Line 36, you'll see the code comment, # Rotates the Player 2 ship
. Lines 37-40 rotate the ship both clockwise (Lines 37-38) and counterclockwise (Lines 39-40).
On Line 37, if key_held_down(pygame.K_LEFT):
checks if Player 2 presses and holds the left arrow on the keyboard.
On Line 38, MY.player2.add_rotation(ship_rotate * delta_time)
rotates Player 2's ship counterclockwise. Notice that this line is indented to the right of Line 37 (so that the line is seen by Python as part of the 'if' block).
On Line 39, elif key_held_down(pygame.K_RIGHT):
, checks if Player 2 presses and holds the right arrow on the keyboard. If so, the program runs the following indented code (Line 40).
On Line 40, MY.player2.add_rotation(-ship_rotate * delta_time)
rotates Player 2's ship clockwise. The -
symbol to the left of the ship_rotate
variable sets the rotation direction as clockwise.
On Line 42, you'll see the comment, # Moves the Player 2 ship forward and backward
. This describes Lines 43-46.
On Line 43, if key_held_down(pygame.K_UP):
checks to see if the up arrow on the keyboard is being pressed by Player 2. If so, the program runs the following indented code on Line 44.
On Line 44, MY.player2.add_velocity(MY.player2.rotation, ship_accel, ship_max_speed)
moves the Player 2 ship forward. This runs when the player presses and holds the up arrow.
On Line 45, elif key_held_down(pygame.K_DOWN):
checks to see if Player 2 is pressing the down arrow on the keyboard. If so, the following indented code (Line 46) will run.
On Line 46, the code MY.player2.add_velocity(MY.player2.rotation, -ship_accel, ship_max_speed)
is the exact same as the code on Line 44. Since the down arrow is being pressed by Player 2, this line moves Player 2's ship backwards. The rotation direction is set by the -
symbol before the ship_accel
variable.
On Line 48, the comment, # Updates player objects on screen
, describes what Line 49 does.
On Line 49, the function update_players(delta_time)
updates the location of the players on the screen.
On Line 51, you'll see the comment, # Checks if bullets have been fired and updates their behavior on screen
. This describes Line 52.
On Line 52, update_bullets(delta_time)
updates the location of the projectiles on the screen.
On Line 54, # Check win condition
describes the next line of code (Line 55). Use comments like this to provide some context for what your lines of code do.
On Line 55, check_win()
checks the win condition, to see if anyone won the game.
On Line 58, # Registering the states
describes what Lines 59-60 do, which create the states needed to end the game.
On Line 59, you'll see the code, Manager.register(sys.modules[__name__]) #The current file
. This code registers the current state of the game.
On Line 60, Manager.register(GameOver)
loads the various modules for the end of the game, such as a background cleanup.
On Line 62, you'll see the comment # Run the game!
This describes what Line 63 does.
On Line 63, Manager.run(SCREEN, WINDOW, BLACK, "CHALLENGE1")
runs the game.
That's it! The last part of the code (in this "Explaining the Code" section) just explains what the code does. Hopefully, that was a helpful review for you, since you already created this game in Level 4 in the book!
The Final Code
I included a solution file for Level 4, Challenge 1. 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 Steps
Take the Next Challenge!
Next, you can take on Level 4, Challenge 2! In this challenge, you are going to write code that will allow two players to play the Space Wars game together! (Why not play this fun game with another person?)
More Level 4 Resources
In addition to this page, we also have Level 1 Help (for the main game, in the book), Online Articles, a Learning Quiz, an Unplugged Activity, and a Rewards article:
-
Level 4: Help - This page helps you complete the instructions in the book, in case you get stuck.
-
Level 4: Online Articles - I made you a list of different web pages I found, which will help you learn more about creating Turtle Graphics.
-
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. In this game!
-
Level 4: Rewards - If you completed the Space Wars project that we talked about in the car, 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 Space Wars Award digital download, to show off your accomplishment!
Level 5
After you're completely done with Level 5 (did you do Challenge 2?), then it's time to move on to Level 2! While you read through Level 2 in your book, you can check out the resources from WHO?, as he teaches you how to build the Creeper Chase program:
I'm sure you're an expert now at building a Space Wars game. After all, I taught you!
-- SideWinder