Level 7: Challenge 1 - IncrediCoders/Python1 GitHub Wiki

Grafika Turtle added this page on June 6, 2023


Let's begin your first challenge for Level 7!

You're going to add two more cards to each player's deck! This way, each player can switch between 5 cards instead of 3!

To find the Level 7 Challenge 1 code template, open the Level 7 folder, the Challenges folder, and then the Challenge1 folder. (In our GitHub repo, you can find the code template in the Challenge1 folder.) You should already have the files ready to go (see Set up the files). Open the IncrediCards_Challenge1.py file to build the program by following along with my instructions below!

Explaining the Code

You should have your IncrediCards_Challenge1.py file loaded into your code editor.

On Line 1, from init import * imports code from the init.py file, which sets up the game and includes the features and functions that you'll use throughout this game. The asterisk specifies that we're importing everything from the file.

Lines 4-23 create all the cards, including the primary variables for the cards. Then, we also create all the attributes for the cards... the names on the cards, the primary programming languages they use (we call these their "TechTypes"), the TechTypes that they are weak to, the TechTypes that they are strong/resistant to, their PNG file variables (such as annie_conda_img), and their TechType icon variables (such as icon_python). For example, on Line 4, we have the code, annie_conda = Card('Annie Conda', 'python', 'java', 'bash', annie_conda_img, icon_python). Can you describe what content is in each variable?

On Line 25, the code DECK = [] creates the DECK list. A list is a variable that's an array. You can put a lot of information into the one list, like other variables, strings, numbers, etc. It's just like how you can use multiple drawers in one filing cabinet. Each drawer is like a spot in that list. Annie mentioned this to Paul back in Level 3.

And that's what we do in our code! Lines 28-47 append our variables into different spots in our DECK list. For example, on Line 44, DECK.append(viralmuto) appends all the attributes (back from Line 20) into the one spot in the DECK list.

On Line 49, random.shuffle(DECK) shuffles the cards that are in the deck. This means that all the variables (the cards) that are in the DECK list are arranged in a random order.

On Line 51, you'll see the code, class PlayScreen(GameState): The game's play screen is one of a few different game states, which are like modes or screens, that the player can move between. Other game states are the welcome screen and the game over screen.

In Lines 53-64, we pass the self variable into our _init_() function, to set up (or initialize) the play screen, with all the graphics on it.

On Line 66, we define the start() function, with the code, def start(self, players): Lines 67-69 set up the self.players, self.player1, and self.player2 variables, so that we can use them later in the game.

On Line 71, we set the active player to be the attacker. This is based on the coin toss that the players see, before they pick their cards.

On Line 72, we set the defender, which is the player who is going to be attacked for that turn.

NEW CODE: Next, you're going to update Lines 75-76. See the next section, "Write Your Own Code." Let's keep explaining the code for now.

On Lines 78-79, we display the players' healthbars. This is what visually shows how much life the players have left.

NEW CODE: Next, you're going to update some more code, on Lines 82-83. See the next section, "Write Your Own Code," for some guidance.

On Line 85, self.dialog_box = DialogBox((200,300), (X_CENTER-100, 50)) displays the dialog box in the middle of the screen, between the two players' cards. This is the box where the players will see the instructions and information about the game they're playing.

On Lines 87-92, you define the get_event() function. It checks if the currently active player clicks the TechType Attack button or any of the On Deck cards.

On Lines 94-97, you define the button_action() function. This function flips the coin when the active player clicks the TechType Attack button.

On Lines 99-152, you define the update() function. This is the main function of the game. It updates the graphics on the screen, transitions between the players' turns, and it displays the messages that describe the results of each turn.

Lines 150 check if there's a winner yet (if one of the players ran out of cards). Line 151 then assigns the winner, using the code winner.set_as_winner(). Line 152 (self.done = True) tells the play screen that there's a winner, and it needs to go to the victory screen.

Lines 154-156 define the flip_coin() function. It randomly determines which side is up.

Lines 169-177 define the check_game_end() function. It checks the status of both players. If one player survives, then we end the game (see Line 177).

Lines 179-196 define the draw() function. It updates all of the graphics on the screen, for each new round.

On Lines 198-205, states = {} declares a collection of states, or different screens that are needed for the game. This is also called a state machine, which provides the overall structure for the game logic. That includes any different modes or screens. It's a logical structure for your game code, where you don't need to put all your code into a loop to run the game.

Line 199 enters the TitleScreen() into the state machine, which is the screen you see when you start the game. Line 200 adds in GetNameScreen(), which is the screen where you enter in the names of the two players. Line 201 adds in the CoinFlipScreen(), which is where you flip a coin to see who goes first.

NEW CODE: On Line 202, the code comment says, "#TODO: Add a second argument (boolean True) to the ChooseHandScreen parameters". You're going to update the code on Line 203. See the next section, "Write Your Own Code," for some guidance.

Currently, Line 203 adds in ChooseHandScreen(DECK), which is where you pick the card that you want to start with. Line 204 adds in PlayScreen(), which is the main game screen. And, finally, Line 205 adds in VictoryScreen(), which is the screen you go to when the game is over.

Line 208 is game = GameRunner(SCREEN, states, "Title"). It's the line that actually starts the game runner. Everything up to this line was all the setup, but the code starts running with this line.

Write Your Own Code

On Line 74, in your IncrediCards_Challenge1.py file, you should see the code comment, "#TODO: Update the card display data for both players (Hint: look at lines 585-624 in the init.py file)".

You're going to update Lines 75-76, to redraw the play screen. This will change the size and placement of the active cards (one for Player 1 and one for Player 2). This way, we can show the larger On Deck box, which will now start with four cards in it, instead of two.

As the comment's hint says, take a look at Lines 585-624 in the init.py file. You can click this link to go directly to that file, in our GitHub repo: Level 7, Challenge 1 init.py file

Specifically, look on Line 606 (of the init.py file), to see what we renamed the Player 1 card data variable.

Similarly, on Line 76 in the IncrediCards_Challenge1.py file, you need to update the Player 2 card display data variable. The variable name is what you'd expect, based on your change to Line 75, but you can find the full new variable name on Line 616 of the init.py file.

Next, you're going to update Lines 82-83 (back in our IncrediCards_Challenge1.py file). See the code comment on Line 81, "#TODO: Update the on deck data for both players (Hint: look at lines 626-677 in the init.py file)".

On Line 82, you're going to update the Player 1 On Deck data variable. The new variable name is what you'd expect, based on your changes to Lines 75-76. You can find the new variable name on Line 653 of the init.py file.

Similarly, on Line 83, you're going to update the Player 2 On Deck data variable. The new variable name is what you'd expect, based on your changes to Lines 75-76 and Line 82. The new variable name is on Line 666 of the init.py file.

For your final change you'll make, to get the extra two On Deck cards (per character), take a look at the code comment way down on Line 202, of your IncrediCards_Challenge1.py file. The comment says, "#TODO: Add a second argument (boolean True) to the ChooseHandScreen parameters".

Inside the ChooseHandScreen() initializer, you're going to add a second parameter (in addition to the DECK parameter). Inside the parentheses, add a comma after DECK, and then add a space. Type in the argument that activates the challenge form (that shows 5 cards instead of 3, on the ChooseHand screen). A hint is that the name of the argument is one word, and it's in the code comment.

Okay, fine. You can have another hint. It's the word after "boolean" in the code comment. Remember, you can check the final code file in the repo, if you really get stuck. See the next section, below.

By the way, an initializer is responsible for creating the ChooseHand screen (that's the screen at the beginning of the game, where you pick which card you want to start with). It's part of the state machine that we have (on Lines 198-206).

The Final Code

I included a solution file for Level 7, 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 7, Challenge 2! This time you'll add a second attack option, the Coded Attack, which gives your cards special abilities!

More Level 7 Resources

In addition to this Help page and the instructions for our Level 7 challenges, we also have Online Articles, a Learning Quiz, an Unplugged Activity, and a Rewards article:

  • Level 7: Help - This page helps you complete the instructions in the book, in case you get stuck.

  • Level 7: Online Articles - I made you a list of different web pages I found, which will help you learn more about the lessons we covered in Level 7.

  • Level 7: 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 7: Unplugged Activity - In this game, we recreate the basic aspects of the IncrediCards game, but in real life!

  • Level 7: Rewards - If you completed the IncrediCards project that we talked about in our text messages, then I set up this page to act as a reward. You can see some illustrations of how the art of the card game was made!

⚠️ **GitHub.com Fallback** ⚠️