Level 4: Challenge 2 - IncrediCoders/Python1 GitHub Wiki

SW_avatar2

SideWinder added this page on June 6, 2023

Let's begin your second challenge for Level 4!

You are going to write code that will add asteroids to the game. You can even hide behind the asteroids and use them as a shield!!

To find the Level 4 Challenge 2 code template, open the Level 4 folder, the Challenges folder, and then the Challenge 2 folder. You should already have the files downloaded onto your computer (see Load the IncrediCoders Files). Open the SpaceWarsChallenge2.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 22, #Rotates the Player 1 ship, describes what the next lines of code will do which is rotate Player 1's ship.

On Line 23, 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 24, 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 25, 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 26, 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 28, #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 29, 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 30, 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 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, if key_held_down(pygame.K_LEFT):, checks if Player 2 presses and holds the left arrow on the keyboard.

On Line 36, 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 37, 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 38, 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 40, #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 41, 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 42, 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 43, 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 44, 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 45, #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 46, 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 50, # Updates player objects on screen, describes what the next line of code will do which is update the players that are needed on the screen.

On Line 51, update_players(delta_time), updates the players on the screen, keep track of the different steps so that when you write this later, you can apply the steps to that update.

On Line 53, #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 54, 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 56, # Updates asteroid objects on screen, gives you the instructions for what you will do for your challenge on the next line of code.

On Line 57, this is where you write the code for the challenge given on Line 56. Look back at Lines 51 and 54, and think about what you need to change in order to update the asteroid objects on the screen.

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

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

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

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

On Line 65, Manager.register(SpaceWars), registers the result of the specific space wars game that was just played.

On Line 66, 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 68 and 69 will do.

On Line 68, #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 69, 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 made the Space Wars game for two players!"

The Final Code

I included a solution file for Level 4, Challenge 2. 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

More Level 4 Resources

In addition to this page, we also have Level 4 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 4 (did you do Challenge 1?), 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 WHO?, as he teaches you how to build the Creeper Chase program:

You better be amazing by now at making a Space Wars game! I do have a reputation to uphold.

-- SideWinder