Level 2: Challenge 1 - IncrediCoders/Python1 GitHub Wiki

Scratcher_PointSide

Mrs. Scratcher added this page on March 14, 2023


Let's begin your first challenge for Level 2!

You're going to add two more classmate introductions to your app. One is for the twins, RAM & ROM, and one is for Amphib Ian. That will be a total of 14 introductions (you previously had 12).

We're going to add the keyboard keys "E" and "R."

To find the Level 2 Challenge 1 code template, open the Level 2 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 ClassIntroductionsChallenge1.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: Load the Images

On Line 1, you'll find the code from init import * #Gives us many helpful functions. Like in Level 1, it initializes the functions that will be used throughout this project. (That's why we call it the init file.) It imports pygame and a lot of other functions, including bringing in our image files, displaying text, setting up our font, and setting the size and placement of our images.

On Line 3, #Loads the background and images, this is a comment, as it has a "#" before the text, which means it is describing what will happen in the next lines: Lines 4-16 load the background and the classmates' images.

On Line 4, background = load_file("Assets/Background.png"), loads the background picture into the project. This way you can see my classroom in your window.

On Line 5, annie_conda = load_file("Assets/AnnieConda.png"), loads the picture of your classmate Annie Conda into the project.

On Line 6, bayo_wolf = load_file("Assets/BayoWolf.png"), loads the picture of your classmate Bayo Wolf into the project.

On Line 7, grafika_turtle = load_file("Assets/GrafikaTurtle.png"), loads the picture of your classmate Grafika Turtle into the project.

On Line 8, intelli_scents = load_file("Assets/IntelliScents.png"), loads the picture of your classmate Intelli Scents into the project.

On Line 9, java_lynn = load_file("Assets/JavaLynn.png"), loads the picture of your classmate Java Lynn into the project.

On Line 10, captain_javo = load_file("Assets/CaptainJavo.png"), loads the picture of your classmate Captain Javo into the project.

On Line 11, jitter_bug = load_file("Assets/JitterBug.png"), loads the picture of your classmate Jitter Bug into the project.

On Line 12, paul_python = load_file("Assets/PaulPython.png"), loads the picture of your classmate Paul Python into the project.

On Line 13, quackintosh = load_file("Assets/Quackintosh.png"), loads the picture of your classmate Quackintosh into the project.

On Line 14, sb_turtle = load_file("Assets/SBTurtle.png"), loads the picture of your classmate SB Turtle into the project.

On Line 15, sidewinder = load_file("Assets/SideWinder.png"), loads the picture of your classmate SideWinder into the project.

On Line 16, syntax_turtle = load_file("Assets/SyntaxTurtle.png"), loads the picture of your classmate Syntax Turtle into the project.

Write Your Own Code: Add New Images

The first code that you're going to write is after Line 17.

The code comment on Line 17, #TODO: Add the images for RAM & ROM and Amphib Ian here, directs you to add the images (like the previous lines) for RAM and ROM and Amphib Ian.

On Line 18, copy the following code:

ram_rom = load_file("Assets/RAMROM.png")

That loaded in the RAM & ROM image. (They presented to our class together.)

On Line 19, type in amphib_ian on the line. That's the variable that you'll use to load in Amphib Ian's image. Press the Spacebar, and then type the equals symbol (=). Add another space, and then type in the load_file() function. Next, type in the image path, "Assets/AmphibIan.png" inside the parentheses. Make sure the path is in quotes and there's a closing parenthesis after the end quote!

Explaining the Code: Set the Introduction Text

On Line 21, you'll see the comment, #Stores the character text into the variables. The "#" symbol, before the text, means that the line of code won't run (it's a comment). This comment explains that the next section of code (Lines 22-36) will store the character text into the variables. We call this setting the variables.

On Line 22, you'll find the code, text_annie_conda = "Hello! I'm Annie Conda. \nI come from Sanfran-Hissco, Cowlifornia. I've done a little coding. My favorite musician is Justin Timbersnake. I'm also partial to Hissy Elliott. My favorite Pigxar movie is Rattle-toulle. I love to make trivia games and word games.". This code stores the character text of Annie Conda into the variable for Annie Conda's introduction (text_annie_conda).

On Line 23, you'll find the code, text_bayo_wolf = "I'm Bayo Wolf, from Little Squawk, Barkansas. I'm the best at SpaceWars and great at Mega Mechs in my Grendel mech. My favorite movies are The Dogfather, Jurassic Bark, Citizen Canine, and Stall Wars: The Empire Strikes Cats. My top actors are Brad Pitbull, Howly Berry, and Sandra Bulldog. ". It stores the character text of Bayo Wolf into his text variable.

On Line 24, you'll find the code, text_grafika_turtle = "My name is Grafika Turtle. I live here, in Red-mutt, Washeepton. Now I get to go to school with my best friend. Hi, Paul! I love the movie Wizard of Paws, and my favorite artist is Pablo Pigcasso. I like coding in Turtle Graphics, and my brother Syntax and I are pretty good at coding card games. ". It stores the character text of Grafika Turtle into her text variable.

On Line 25, you'll find the code, text_intelli_scents = "Hi. I'm Intelli-Scents from Minnea-pawlis, Minnow-soda. My top movies are Hack to the Future, Mission Impawsible with Eat'n Hunt, and Hair-Spay. My top artist is Vincent van Gopher, top book is The Time Machine by H.G. Gills, and my favorite neurologist is Digmund Freud.". It stores the character text of Intelli-Scents into her text variable.

On Line 26, you'll find the code, text_java_lynn = "I'm Java Lynn, also from Minnea-pawlis. I shop at Blooming Tails and read Vanity Fur. Top movies are Hairy Otter 8, Catsaway with Tomcat Hanks, and the Sound of Mew-sic. My actors are Bill Furry and Scarlett Johamster. I love the art \"Squirrel with the Acorn Earrings.\" ". It stores the character text of Java Lynn into her text variable.

On Line 27, you'll find the code, text_captain_javo = "I'm Captain Javo, from Indiana-pawlis, Fin-diana. The movies I like are The Fast and Furry-us, Paws, and Clawshank Redemption. My actors are Woodchuck Norris, Billy Grrr-ystal, and Will Ferret. My musicians are Kitty Purry and Britney Ears. My favorite programming language is Java. ". It stores the character text of Captain Javo into his text variable.

On Line 28, you'll find the code, text_jitter_bug = "I am Jitter Bug, from Ant-aheim, Cowlifornia. My favorite movies are Mrs. Doubtspider and Twi-mite. I love the Stall Wars character Luke Flywalker. My top actors are Kristin Ear-Wiig and Molly Ringworm. My favorite musicians are Beeyonce, Flyley Flyrus, and Nine-Inch Snails. ", stores the character text of Jitter Bug into the variable for Jitter Bug's character image.

On Line 29, you'll find the code, text_paul_python = "Hi. I'm Paul Python. My home is just over the bridge in Sea-cattle. Mega Mechs is my favorite game, and the reason why I'm here, by winning the tournament! I love the actor David Hisselhoff, and I agree with Annie that Justin Timbersnake makes awesome music! But I also like White Snake. ", stores the character text of Paul Python into the variable for Paul Python's character image.

On Line 30, you'll find the code, text_quackintosh = "Hello. I'm Quackintosh, from nearby Bill-view, Washeepton. My top actors are Audrey Honkburn, Goose Willis, Squawkin Phoenix, Robird De Niro, Hennifer Lawrence, and Woody Owlen. My musicians are Swan Bon Jovi, Michael Quackson, and Ozzy Ostrich. My top art is \"Son of Duck.\" ", stores the character text of Quackintosh into the variable for Quackintosh's character image.

On Line 31, you'll find the code, text_sb_turtle = "Hey. I'm SB Turtle, from New-ark, Moo Jersey. My top actresses are Shelly Long and Zooey Deshell. I'm a founding member of the Shell Scouts, and my favorite programming language is Microsoft Small Basic. Oh, and I grow into a giant monster, but that's for a different book. "

On Line 32, you'll find the code, text_sidewinder = "I'm SideWinder, from Salt-Snake City, Mewtah. I'm the best at SpaceWars, despite what Bayo thinks. My favorite movie is Snakes on a Glider, my favorite actor is Sylvester Stallion, and my favorite musician is Nine-Inch Snails. Jitter Bug has good taste. ", stores the character text of Side Winder into the variable for Side Winder's character image.

On Line 33, you'll find the code, text_syntax_turtle = "What's up? Syntax Turtle in the house. I'm from here, Red-Mutt, with my twin sis Grafika. My favorite actress is Natalie Porkman, and my favorite artist is Lizardnardo Da Vinci. My top games are \"Skate and Fly\" and Porkymon, and I'm looking forward to making a Codu Kids card game! ", stores the character text of Syntax Turtle into the variable for Syntax Turtle's character image.

Uncomment the Code: Add More Introduction Text

On Line 34, you'll find the comment, #TODO: Uncomment the introduction for RAM & ROM and Amphib Ian below. After you uncomment Lines 35 and 36, they will run as code just like the text in Line 33.

On Line 35, you'll find the code, #text_ram_rom = "Heya! We're RAM and ROM. We came from the other side of the country, Woolshington DC. We like Meryl Sheep, Dustin Hoofman, and Eva Longhornia. Our favorite musician is Lady Baa-Baa, and our sensei is the Dali Llama. We're working on a top-secret project! " Delete the hash tag at the beginning of this line, to uncomment this line of code. This line stores the text into the text_ram-rom variable. You'll use this variable later, to show the text on the screen.

On Line 36, you'll find the code, #text_amphib_ian = "Yo, yo. What's hopping, peeps? Name's Amphib Ian. My launchpad is Croaklahoma City. My choice singer is Demi Lovatoad. And my fave artwork is \"Mourning Son\" by Edwart Hopper. My go-to game to code is Froggy Road. That's it. I'll catch you on the flip flop! ". Remove the hash tag at the beginning of this line of code as well. This is the second line you need to uncomment. This is the text that is said by Amphib Ian.

Explaining the Code: Set the Key Events

On Line 38, you see a comment #Assigns our character and text variables, that tells you what the next lines will do which is set variable names to each of the characters.

On Line 39, current_character = annie_conda sets the variable current_character to the character Annie Conda.

On Line 40, current_text = text_annie_conda sets the variable current_character to the text of the character Annie Conda.

On Line 42, while running:, starts the while loop that will repeat the text a total of five times.

On Line 43, you can see display(background, current_text, current_character), that is a function call. If you look at the inti.py file, you will see this function on line 45. This function displays the current text and the current character on the background image that was loaded in earlier.

On Line 44, EVENTS = pygame.event.get() sets the variable "EVENTS" to the pygame.event that was downloaded earlier.

On Line 45, for event in EVENTS: is a nested loop inside of the while loop that will run when there is an event in the "EVENTS" variable.

A nested loop is when you have a loop (such as a for loop) inside of another loop. Each time the outer loop runs, then the inner loop starts over and runs however many times it is set for.

On Line 46, the if statement if event.type == pygame.QUIT:, will evaluate to true or false. When the condition is false, then the code will skip on Line 47 and move to Line 48. Otherwise Line 47 will run.

On Line 47, as mentioned running = False runs when the event.type == pygame.QUIT condition is true. The running = False will store false value in the "running" variable.

On Line 48, if event.type == pygame.KEYDOWN:, this is another evaluation of true or false whether the variable event.type is equal to a certain command from the pygame file which in this case is KEYDOWN.

On Line 49, if event.key == pygame.K_1:, this evaluation of true or false (and all following code on Lines 49 to 84) assuming that Line 48 was true, and then after this line, there will be more commands of what each of these loops create or assign. It is said that all code on Line 49 to 84 are inside the if event.type == pygame.KEYDOWN: block. The if condition event.key == pygame.K_1 is true when you pressed key 1 on the keyboard.

On Line 50, current_character = annie_conda, if Line 49 is true, then the variable current_character will be set to be the character of Annie Conda, and if Line 49 is false, it will move on to the next if statement on Line 52.

On Line 51, current_text = text_annie_conda, if Line 49 is true, after running Line 50 and set the current_character, then the variable "current_text" will be set to the text from Annie Conda.

On Line 52, if event.key == pygame.K_2:, this is another if statement that checks whether key 2 is pressed or not. If this condition is true, then code on Line 53 and 54 will run, otherwise will move on to Line 55.

On Line 53, current_character = bayo_wolf, if Line 52 is true, then the variable current_character will be set to be the character of Bayo Wolf, and if Line 49 is false, it will move on to the next if statement on Line 55.

On Line 54, current_text = text_bayo_wolf, if Line 52 is true, then the variable current_text will be set to the text from Annie Conda.

On Line 55, if event.key == pygame.K_3:, this is another if statement that checks whether key 3 is pressed or not. If this condition is true, then code on Line 56 and 57 will run, otherwise will move on to Line 58.

On Line 56, current_character = grafika_turtle, if Line 55 is true, then the variable current_character will be set to be the character of Grafika Turtle, and if Line 55 is false, it will move on to the next if statement on Line 58.

On Line 57, current_text = text_grafika_turtle, if Line 55 is true, then the variable current_text will be set to the text from Grafika Turtle.

On Line 58, if event.key == pygame.K_4:, this is another if statement that checks whether key 4 is pressed or not. If this condition is true, then code on Line 59 and 60 will run, otherwise the program will move on to Line 61.

On Line 59, current_character = intelli_scents, if Line 58 is true, then the variable current_character will be set to be the character of Intelli Scents, and if Line 58 is false, it will move on to the next if statement on Line 61.

On Line 60, current_text = text_intelli_scents, if Line 58 is true, then the variable current_text will be set to the text from Intelli Scents.

On Line 61, if event.key == pygame.K_5:, this is another if statement that checks whether key 5 is pressed or not. If this condition is true, then code on Line 62 and 63 will run, otherwise the program will move on to Line 64.

On Line 62, current_character = java_lynn, if Line 61 is true, then the variable current_character will be set to be the character of Java Lynn, and if Line 61 is false, it will move on to the next if statement on Line 64.

On Line 63, current_text = text_java_lynn , if Line 61 is true, then the variable _current_text_ will be set to the text from Java Lynn.

On Line 64, if event.key == pygame.K_6:, this is another if statement that checks whether key 6 is pressed or not. If this condition is true, then code on Line 65 and 66 will run, otherwise the program will move on to Line 67.

On Line 65, current_character = captain_javo, if Line 64 is true, then the variable current_character will be set to be the character of Captain Javo, and if Line 64 is false, it will move on to the next if statement on Line 67.

On Line 66, current_text = text_captain_javo, if Line 64 is true, then the variable current_text will be set to the text from Captain Javo.

On Line 67, if event.key == pygame.K_7:, this is another if statement that checks whether key 7 is pressed or not. If this condition is true, then code on Line 68 and 69 will run, otherwise the program will move on to Line 70.

On Line 68, current_character = jitter_bug, if Line 67 is true, then the variable current_character will be set to be the character of Jitter Bug, and if Line 67 is false, it will move on to the next if statement on Line 70.

On Line 69, current_text = text_jitter_bug, if Line 67 is true, then the variable current_text will be set to the text from Jitter Bug.

On Line 70, if event.key == pygame.K_8:, this is another if statement that checks whether key 8 is pressed or not. If this condition is true, then code on Line 62 and 63 will run, otherwise the program will move on to Line 64.

On Line 71, current_character = paul_python, if Line 70 is true, then the variable current_character will be set to be the character of Paul Python, and if Line 70 is false, it will move on to the next if statement on Line 73.

On Line 72, current_text = text_paul_python, if Line 70 is true, then the variable current_text will be set to the text from Paul Python.

On Line 73, if event.key == pygame.K_9:, this is another if statement that checks whether key 9 is pressed or not. If this condition is true, then code on Line 74 and 75 will run, otherwise the program will move on to Line 76.

On Line 74, current_character = quackintosh, if Line 73 is true, then the variable current_character will be set to be the character of Quackintosh, and if Line 73 is false, it will move on to the next if statement on Line 76.

On Line 75, current_text = text_quackintosh, if Line 73 is true, then the variable current_text will be set to the text from Quackintosh.

On Line 76, if event.key == pygame.K_0:, this is another if statement that checks whether key 0 is pressed or not. If this condition is true, then code on Line 77 and 78 will run, otherwise the program will move on to Line 79.

On Line 77, current_character = sb_turtle, if Line 76 is true, then the variable "current_character" will be set to be the character of SB Turtle, and if Line 76 is false, it will move on to the next loop statement on Line 79.

On Line 78, current_text = text_sb_turtle, if Line 76 is true, then the variable "current_text" will be set to the text from SB Turtle.

On Line 79, if event.key == pygame.K_q:, this is another if statement that checks whether key q is pressed or not. If this condition is true, then code on Line 80 and 81 will run, otherwise the program will move on to Line 82.

On Line 80, current_character = sidewinder, if Line 79 is true, then the variable current_character will be set to be the character of Side Winder, and if Line 79 is false, it will move on to the next if statement on Line 82.

On Line 81, current_text = text_sidewinder, if Line 79 is true, then the variable current_text will be set to the text from Side Winder.

On Line 82, if event.key == pygame.K_w:, this is another if statement that checks whether key w is pressed or not. If this condition is true, then code on Line 83 and 84 will run, otherwise the program will move on to Line 85.

On Line 83, current_character = syntax_turtle, if Line 82 is true, program will move to this line and set the variable current_character to be the character of Syntax Turtle. If Line 82 is false, it will move on to the next statement after the if block, on Line 85, which is a comment that tells what you will make yourself.

On Line 84, current_text = text_syntax_turtle, if Line 82 is true, then the variable current_text will be set to the text from Syntax Turtle.

Write Your Own Code: Add RAM & ROM

On Line 85, you'll find the code comment, #TODO: Add the if statements for RAM & ROM and Amphib Ian here. As the comment says, on the next three lines (Lines 86-88), you'll need to assign RAM & ROM as the current character (they're together on the same screen), and to set the current_text variable to hold their class introduction text.

On Line 86, write if first. The if statement checks whether or not the user presses the E key. Pygame uses K_e to represent the E key. These key names are Pygame constants. Look back to see how we wrote the code for the other characters. For example, take a look at Line 82, for Syntax Turtle's if statement. Your code should look the same as Line 82, except the key is written as K_e instead, for the E key.

On Line 87, you're going to set the current character's current_character variable to the new character that you're adding in, which in this case is RAM and ROM (they are doing their presentation together). This line of code sets the image of RAM and ROM, to display on the background. You'll use the variable, ram_rom, which is where we stored their image. For an example of how to write this line of code, see Line 83, where you had set Syntax Turtle as the current character.

Here are the instructions for writing Line 87:

  1. Make sure your cursor is indented. You should be at the same indentation level as Line 83. So, you currently should be indented to the right of the if statement on Line 86.
  2. Type in the current_character variable.
  3. Add a space and an equal sign (=).
  4. Add another space, and then type in the ram_rom variable.

On Line 88, type in the current_text variable, which contains the text for the featured character. You're going to change this variable to change the text for the character that you are adding in, which is RAM and ROM. This displays the text from RAM and ROM's class introduction, on the background.

Add a space, an equal sign (=), and another space. Then write in the text_ram_rom variable. This variable is where we stored the string of text for RAM and ROM. For an example of how to write this line of code, see Line 84, where you had set Syntax Turtle's class intro text as the current text.

Write Your Own Code: Add Amphib Ian

On Line 89, write an if statement to add Amphib Ian to your program. Look back to see how we wrote it out for the other characters (Lines 49-84) and how you wrote it for RAM and ROM (Lines 86-88). Decide what you need to change. For example, you just wrote the if statement (for RAM and ROM) on Line 86. Pygame uses the keyword K_r to represent the R key. In other words, copy Line 86 here as well. Change the key name to K_r instead of K_e.

On Line 90, set the variable of the character to the character that you are adding, which is Amphib Ian, in this case. This is the same process that you did for adding in RAM and ROM on Line 87. You can copy Line 87 and use Amphib Ian's amphib_ian variable instead. Line 90 should be indented at the same level as Lines 83-84 and 87-88. The final Line of code is: current_character = amphib_ian

On Line 91, set the variable of the text to the character that you are adding, which is Amphib Ian still. You can copy Line 88 and replace text_ram_rom with amphib_ian instead. Make sure this line is indented at the same level as Line 90.

And then Line 92 is already written: pygame.quit() It ends the game.

You did it! You created the 14 class introductions!

The Final Code

I included a solution file for Level 2, 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 2, Challenge 2! This is a challenge where you do not use an if/else ladder, and instead put text and images into a Python list.

More Level 2 Resources

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

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

  • Level 2: Online Articles - I made you a list of different web pages I found, which will help you learn more about creating Class Introudctions.

  • Level 2: 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 2: Unplugged Activity - I wrote this page with more details than what you saw in the book. In this game, you'll have one person act as the developer, and one person act as the variable where the developer choose a noun for the variable to say in the story that they tell!

  • Level 2: Rewards - If you have completed the Class Introductions project that we talked about, 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 Apple Award digital download, to show off your accomplishment!

Level 3

After you're completely done with Level 2 (did you do the challenges?), then it's time to move on to Level 3! While you read through Level 3 in your book, you can check out the resources from Mrs. Codala, as she teaches you how to build the Classroom Quiz program:

I hope you had fun learning about class introductions! This is something that is very important for all of the future levels! Enjoy!

-- Mrs. Scratcher

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