Level 2: Help - IncrediCoders/Python1 GitHub Wiki
Mrs. Scratcher added this page on March 14, 2023
Hello, class! This page is to help you, in case you get stuck with the instructions that I gave you during class. My class notes are in the book, so please refer to that first.
You'll want to make sure that your Level 2 files are loaded in Visual Studio Code. If you have any problems, please read the instructions at Load the IncrediCoders Files.
Here are the different sections on this page to help you out with your Class Introductions:
- Play the Game - To see how the final version of class introductions program looks like (after you go through the instructions), run the Class Introduction program.
- Glossary of Terms - This list goes through the my instructions I sent you, and it explains every term and idea in depth, one topic at a time, as I bring it up in the book!
- Advice on Writing the Code - Eventually, in the book, I ask you to write your own code. Still stuck? Well, here are some hints on how to write the code!
- The Final Code - After you're done, it might not work the way you expected, or it might not work the same as our version (see Play the Game). Check out my code to see what you missed! IMPORTANT: Please don't cheat yourself! Finish the game first!
In this Class Introductions game, you can run the game to see what the finished game looks like before you build it (or while you're building it). You can see the full instructions on how to download and run the game at Try the IncrediCoders Games.
In this game we will create an introduction program for our class (called class introductions). The class introductions include an image of our characters and a piece of text next to it to describe the character. Characters are your classmates and we introduce each character in one page. Our class introductions will have 12 pages, one for each student. In Challenge 1, you'll add two more students (total 14 introduction pages).
You can run the program to see what the finished program looks like, before you build it (or while you're building it). You can see the full instructions on how to download and run the program at Try the IncrediCoders Games.
Here are the downloads to run the Level 2 program - Class Introductions:
Here are the instructions on how to use the Level 2 program: Level 2 Instructions
Let's start with some definitions so that you know what all the words mean, that I've been telling you during class:
-
ClassIntroductions.py
: This is the file that you'll use as your template. You should have downloaded the file and loaded it into Visual Studio Code. Here is the file on GitHub: ClassIntroductions.py -
Variable: A variable in Python is like a drawer (a location where you can store values). You can put stuff into it, take something out of it, or change the value later. For example, the variable
X
can have the value 10, but then you can change the value later to 15. In my messages to the class, I mentioned that you could have a variable namedfavorite_musician
. You can assign a string to that variable, and set it to"Nine-Inch Snails"
. Later, you can change the value of that variable to a different musician, like"Lady Baa-Baa"
instead. We refer to this as assigning a new value. For example, we put Annie's class introduction speech into thetext_annie_conda
variable. -
current_character
: The classmate who is currently giving an introduction (where we display her intro text on the screen). We set Annie Conda's image in this variable first. On Line 33, you'll find the code that displays her image,current_character = annie_conda
. This way, when you open the program, Annie's image is up, before you press any keys. Whenever you press a key to switch the classmate, you're changing the value of this variable (you're swapping in a different classmate's image). -
current_text
: The introduction text. We set Annie's introduction into this variable on Line 34. This way, Annie's intro speech appears on the screen, before you press any keys. Similarly, when you press a key to switch classmates, you are swapping in a different classmate's introduction text. -
Import: In our
ClassIntroductions.py
file, we import some code from theinit.py
file on Line 1. We put extra code in this file that you don't need to write yourself. For Level 2, we import Pygame to give us some gaming functions. It also includes functions that load our image files, display text to the screen, set up the font that we use, and set up the size and placement of the images. -
Comments: In software programming, a comment is a part of code that is added in order to explain what the code does. This way, the person reading your code can know what changes to make and can learn from what you're doing. Comments don't run and don't have any impact on the program. Putting a hashtag (# symbol) at the beginning of a line of code will turn that line into a comment. You can comment out some lines of code to test individual pieces of the program to see if they work, and find where your problems are. For example, on Line 3, you can see
#Loads the background and images
. This is a comment to tell you what the following lines are doing (Lines 4-16). - Image: This is a representation of a certain thing, like how the background for the challenges is a representation of the school map.
- .PNG: This is a type of image that stands for portable network graphics. It is often a low file size, so it is used in games like this.
-
background
: We use this variable to store the background image. This way, you can change the image easily, in one place. -
annie_conda
: This is our first variable for a character's image (where we store the picture of Annie). The image of Annie is shown on top of the background image. -
load_file()
: If you look at theinit.py
file, in Line 10 you can see the code,def load_file(fileName):
This is a definition of a function calledload_file()
. In the template file, you are calling this function on Lines 4 to 16. For example, on Line 4, in ourClassIntroductions.py
file, it saysbackground = load_file("Assets/Background.png")
. You call theload_file()
function and pass the string"Assets/Background.png"
to this function. This stores the function's output in thebackground
variable. -
while
Loop: You can use awhile
statement to create a loop. Thewhile
loop statement has two parts. The first part is the wordwhile
, and the second part is a condition. The section of code that is inside thewhile
loop runs repeatedly, as long as this condition is true. In order to exit the loop, you need to make this condition false. For example, in the game, thewhile running:
statement repeats the code inside of it, as long asrunning
is true. When you setrunning = false
(like if you close the program window), then you make the condition false, and you break the loop. (Python leaves the loop and goes on to execute any other lines in the code file.) -
Functions: This is a block of code that does something. A function usually performs one or more specific action(s), for example if a variable's value is equal to 1, then a function for displaying its value would display the number 1. One example is the
display()
function. - Event: In programming, when writing code, an event has a specific meaning. An event is an action that your code should react to. In our game, any action done by the player is an event, such as clicking on an image, or pressing a key on the keyboard.
-
for event in EVENTS:
: On Line 39, thisfor
loop iterates over theEVENTS
list, and it assigns each event element in theEVENTS
list to the variableevent
. A list is a group of items. In this case, it's the events that the user performs, like pressing a key on the keyboard. In thefor
loop, we go through each item in this list, and we assign that event item to theevent
variable, in order to check what event has happened. -
event.type
: The keyboard event type, likepygame.QUIT
(Line 40) andpygame.KEYDOWN
(Line 42). This tracks whether you're quitting the game (by pressing the ESC key or by clicking the Close button in the window) or whether you're pressing a key on the keyboard, to select a classmate. -
event.key
: You use the keyboard event to select the classmate you want to talk. We first use this variable on Line 43. For example, if you press 1 on the keyboard, you select Annie Conda's introduction, and the 1 key gets captured into this variable. -
if
Ladder: This is a sequence ofif
statements. Each statement has a condition, and if the condition evaluates to true, the part of the code that is indented under that if condition runs. For example,if event.key == pygame.K_3:
is an if condition or if statement. When you write some of these if statements sequentially in your code, we call it anif
Ladder. - TODO Comment: Line 46 is our first TODO comment, for this file. A TODO comment shows you where to write the code. In this case, the instructions are to remove the hashtag at the front of the next three lines of code (Lines 47-49). You will uncomment these lines so that they run.
-
event.key==pygame.K_1
: Choose Annie Conda by using the #1 key. -
event.key==pygame.K_2
: Choose Bayo Wolf by using the #2 key. -
event.key==pygame.K_3
: Choose Grafika Turtle by using the #3 key. -
current_character
: This is a variable name, and it will return whatever character it has stored in its first position or value. -
current_text
: This is a variable name, and it will return whatever text it has stored in its first position or value. -
pygame.quit()
:quit()
is a pre defined function in the pygame package. When you sayquit()
it means that you are callingquit
function. This piece of code,pygame.quit()
, closes the program and you will quit the game by running this line of code.
My instructions get a little trickier as we go through Level 2 in the book. Eventually, I ask you to write your own code. This section gives you some hints on how to write your code!
After Line 51, in your template file, I have you copy three lines of code. I included them in my message. Here they are again. Type these lines of code on Lines 52-54 in your file:
if event.key == pygame.K_3:
current_character = grafika_turtle
current_text = text_grafika_turtle
This section checks to see if the player presses the 3 key. It means if the player presses the 3 key, then your code sets the current_character
variable to grafika_turtle
and sets the current_text
variable to text_grafika_turtle
. The result is that your program displays Grafika.
Make sure Lines 53 and 54 are indented under the if statement on Line 52. Note that Line 52 needs to have a colon at the end of it, to run the contents of the if statement.
After Line 56, your goal is to write code to add instructions for other students in the class. You should look at how I assigned key 1 for annie_conda
and key 2 for bayo_wolf
, then follow the same pattern by writing the keys for the following students:
- Intelli Scents - assign key 4
- Java Lynn - assign key 5
- Captain Javo - assign key 6
- Jitter Bug - assign key 7
- Paul Python - assign key 8
- Quackintosh - assign key 9
- SB Turtle - assign key 0
- Side Winder - assign key q
- Syntax Turtle - assign key w
For each student you have to write a piece of code similar to the code you already pasted in for Grafika Turtle. Let's call this piece of code the if
block.
Each if
block has three lines:
- An
if
condition to check if the value of the key that's pressed down is a specific key. - The first line in the
if
block assigns the character's image variable to thecurrent_character
variable. - The second line in the
if
block assigns the character's text variable to thecurrent_text
variable.
For example, Lines 57-59 look like this:
if event.key == pygame.K_4:
current_character = intelli_scents
current_text = text_intelli_scents
Make sure you indent the lines. It's very important to indent all lines under the if condition, inside an if block.
You'll find the character variables back up in your file, on Lines 5-16.
The keys use the same syntax for the Pygame key variable, so the 9 key is represented by the pygame.K_9 object, and the w key is represented by the pygame.K_w object.
I included a solution file for Level 2: Class Introductions. This file has all the code filled in. Make sure you try everything on your own first! Then, if you're stuck, you can take a look at the final code file to see what you did differently:
IMPORTANT: Please don't cheat yourself! Finish making the game first!
Next, you can take on the two extra challenges to add to your Class Introductions program and learn more! When you're done, you can move on to Level 3, the Classroom Quiz!
-
Challenge 1: Add the introduction for RAM and ROM and one for Amphib Ian, totaling 14 introductions!
-
Challenge 2: Instead of using an if/else ladder, you will put the text and images into a python list (it is a list of variables).
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: Online Articles - I made you a list of different web pages I found, which will help you learn more about variables, events, and if statements, in addition to what you're learning in our Class Introductions project.
-
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 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!
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