Level 4: Challenge 1 - IncrediCoders/Python1 GitHub Wiki

SW_avatar7

SideWinder added this page on June 6, 2023

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, that describes what the next line of code will do which is importing the libraries and running the init.py file.

On Line 2, from init import *, initializes the program (init). This includes code that sets up the file path for the images and text file, gets the fonts ready for this game, sets the window size, and loads the images. This statement 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, that describes what the next line of code will do which is check for all key presses and button clicks.

On Line 5, def update(delta_time):, updates each time that a key is pressed or that a button clicks and it will continue to check throughout the program.

On Line 6, for event in pygame.event.get():, starts a for loop that runs for every event in the pygame file.

On Line 7, you will see the comment, #Checks if you click the Replay button to play again, which describes what the next line of code will do which is checking to see if the user clicked the replay event.

On Line 8, check_replay_click(event), is the actual code that runs and it checks to see if the user clicked the replay button (replay_click).

On Line 9, you will see the comment, #Checks if you closed the window, which describes what the next line of code will do which is check to see if the user closed the window to the game.

On Line 10, if event.type == pygame.QUIT:, will check to see if the user closed the window, however, this line does not stop the program, it is just a check.

On Line 11, stop(), actually stops the program from running if the previous line's statement is true.

Write Your Own Code

On Line 12, #Fires the two ships' weapons, will not run in the code, but it describes what the next lines of code will do which is fire the two ships' weapons.

On Line 13, elif key_down(event, pygame.K_SPACE):, this is a check to see if the space key has been pressed, if it has, then the indented code will be run below this line.

On Line 14 and 15, you want write the code to add the laser sound for player one. In order to accomplish this, you want to look at the functions that you have and think about what objects are involved with this laser sound. For example, this laser sound is going to come from firing a bullet (sound_laser[random.randint(0, len(sound_laser) - 1)]). Then, you want to make sure that you are only writing the code for player one on this line and that this sound will play (.play()).

On Line 16, elif key_down(event, pygame.K_RETURN):, this is a check to see if the return key has been pressed, if it has, then the indented code will be run below this line.

On Line 17, you want to write out the code that fires the bullet for Player 2's ship. What from Line 14 needs to be changed to complete this?

On Line 18, you want to fire the bullet for player 2, look back at Line 15 and see what you need to change to make it fire the bullet for Player 2 instead of Player 1.

Copy the Code

On Line 20, #Rotates the Player 1 ship, describes what the next lines of code will do which is rotate Player 1's ship.

On Line 21, 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.

On Line 22, MY.player1.add_rotation(ship_rotate * delta_time), since the "a" key was pressed, this code will rotate Player 1's ship counter-clockwise.

On Line 23., #TODO: Copy the code here for the Player 1 ship to rotate clockwise, indicates where you will write the code for allowing Player 1's ship to rotate clockwise

On Line 24, elif key_held_down(pygame.K_d):, checks to see if the "d" key has been pressed, and if it has, then it will run the following indented code.

On Line 25, MY.player1.add_rotation(-ship_rotate * delta_time), rotates Player 1's ships clockwise since there is a "-" next to the variable "ship_rotate" and also this code runs because the "d" key was pressed on the keyboard.

On Line 27, #Moves the Player 1 ship forward and backward, this describes what will be done in the next lines of code which is to move Player 1's ship forward and backward.

On Line 28, if key_held_down(pygame.K_w):, checks to see if the "w" key has been pressed, and if it has, the following indented line of code will be run.

On Line 29, MY.player1.add_velocity(MY.player1.rotation, ship_accel, ship_max_speed), moves Player 1's ship forward, since the "w" key was pressed, using the acceleration and maximum speed variables.

On Line 30, #TODO: Copy the code here for the Player 1 ship to move backward, indicates where you will copy the code for move Player 1's ship backwards.

On Line 31, elif key_held_down(pygame.K_s):, checks to see if the "s" key has been pressed, and as in Line 29, if it has, then the following line of indented code will run.

On Line 32, MY.player1.add_velocity(MY.player1.rotation, -ship_accel, ship_max_speed), moves Player 1's ship backward, since there is a "-" next to the "ship_accel" variable.

On Line 34, #Rotates the Player 2 ship, 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:

On Line 35, #TODO: Write the code here to rotate the Player 2 ship, indicates where you will copy the code described on Line 34.

On Line 36, if key_held_down(pygame.K_LEFT):, checks if Player 2 presses and holds the left arrow on the keyboard.

On Line 37, MY.player2.add_rotation(ship_rotate * delta_time), rotates Player 2's ship counter-clockwise, make sure to indent to the right (so that the line is seen by Python as part of the 'if' statement).

On Line 38, elif key_held_down(pygame.K_RIGHT):, checks is Player 2 presses and holds the right arrow on the keyboard, and if so, runs the following indented code.

On Line 39, MY.player2.add_rotation(-ship_rotate * delta_time), rotates Player 2's ship clockwise, since there is a "-" next the the "ship_rotate" varaible.

On Line 41, #Moves the Player 2 ship forward and backward, describes what the next lines of code will do which is to move Player 2's ship forward and backward.

On Line 42, #TODO: Write the code here to move the Player 2 ship, indicates where you will write the code described on Line 41.

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, and if so, runs the following indented code.

On Line 44, MY.player2.add_velocity(MY.player2.rotation, ship_accel, ship_max_speed), since the up arrow is being pressed, the ship for Player 2 moves forward.

On Line 45, elif key_held_down(pygame.K_DOWN):, checks to see if Player 2 is pressing the down arrow on the keyboard, and if so, the following indented code will be run.

On Line 46, MY.player2.add_velocity(MY.player2.rotation, -ship_accel, ship_max_speed), since the down arrow is being pressed by Player 2, this line moves Player 2's ship backwards, since there is a "-" before the variable "ship_accel."

On Line 48, #Updates player objects on screen, describes what the next line of code does, which is to update the player objects on the screen.

On Line 49, update_players(delta_time), is the actual code for updating the players on the screen, so that the scene displays all the players that are actually playing in the game.

On Line 51, #Checks if bullets have been fired and updates their behavior on screen, describes what the next line of code does, which is checking and updating if the bullets have been fired.

On Line 52, update_bullets(delta_time), is the actually code for checking and updating if the bullets have been fired, and also the behavior of those bullets, once they have been confirmed to have been fired.

On Line 54, # Check win condition, describes the next line of code, which is seeing which of the players won the game.

On Line 55, check_win(), is the actual code that displays who won based on the winning conditions.

On Line 58, #Registering the states, describes what the next lines of code do which is creating states that will eventually end the game.

On Line 59, Manager.register(sys.modules[__name__]) #The current file, registers the name of the current file and stores that in the manager module.

On Line 60, Manager.register(GameOver), this is the code that tells the program that the game is over, and to restart the program which is what Lines 62 and 63 will do.

On Line 62, #run the game!, is a commented out line that describes that Line 61 will restart the game, allowing each player to play again.

On Line 63, Manager.run(SCREEN, WINDOW, BLACK), resets the game back to beginning of the program, allowing the player, or players to play again.

You did it! You have successfully added the sounds of the bullet for Player 1 and Player 2 in the Space Wars game!"

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