Level 3: Challenge 1 - IncrediCoders/Python1 GitHub Wiki
Annie Conda added this page on March 15, 2023
Let's begin your first challenge for Level 3!
You're going to add a score to the game, so you can try to get all the questions right. When the game ends, you'll display the score!
To find the Level 3 Challenge 1 code template, open the Level 3 folder, the Challenges folder, and then the Challenge 1 folder. If you're using VS Code, you should already have the files downloaded onto your computer (see Load the IncrediCoders Files). Open the ClassroomQuizChallenge1.py file in Visual Studio Code to build the program by following along with my instructions below!
On Line 1, from init import *
initializes the program by running the init.py file. This file 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 3, you'll see the comment, #We pull each line of text from the file into a list
. As usual, comments don't do anything when you run the code (they just tell you information when you're looking at your files, so that you can remember what code does or explain what it does to someone else who is looking at the file). This comment tells you what you will be doing for the next line (for Line 4). In this case, you'll pull the text out of a file and put it into a list.
On Line 4, the code TRIVIA = read_file('Assets/Trivia.txt')
takes the trivia text from the file and puts it in the TRIVIA
list.
On Line 6, you'll see the comment, #We now have a list that holds our current question, correct answer, and the two wrong choices, in that order
. This comment is explaining what Line 4 just did.
On Line 7, we also have another comment, #We use these variables to display text on the screen
.
On Line 8, question = TRIVIA[0]
creates the question
variable for the first quiz question. This game updates the question
variable each time that it asks a different trivia question. The code on Line 8 assigns the question text in the 0
index position of the TRIVIA
list, to the question
variable.
Remember, back on Line 4, you assigned every line from the Trivia.txt file into the TRIVIA
list, where each line from the TXT file is assigned to a different index number (such as TRIVIA[0]
and TRIVIA[1]
). This means that the first line in the text file gets placed into the TRIVIA[0]
index position.
On Line 9, answer = TRIVIA[1]
creates the answer
variable and assigns text from the TRIVIA[1]
index position into the answer
variable.
Note that the second line of text from the Trivia.txt file was assigned to the number 1
index position in the TRIVIA
list. It's the correct answer to the first question.
On Line 10, wrong_choice_1 = TRIVIA[2]
creates the wrong_choice_1
variable. The text from the TRIVIA[2]
index position is assigned to the wrong_choice_1
variable. That's the third line of text from the Trivia.txt file and the first incorrect answer.
On Line 11, the code wrong_choice_2 = TRIVIA[3]
creates a variable for the second wrong choice (wrong_choice_2
). The text from the TRIVIA[3]
index position is assigned to this wrong_choice_2
variable. That's the fourth line of text from the Trivia.txt file and the second incorrect answer.
There is no code on Line 12 (just a space to divide the sections).
On Line 13, ANSWER_CHOICES = [answer, wrong_choice_1, wrong_choice_2]
creates a new list called ANSWER_CHOICES
, using the variables that we just made on Lines 9-11. The first place in this ANSWER_CHOICES
list is the correct answer, the second place is the first wrong choice, and the third place is the second wrong choice.
On Line 14, you'll see the code, randomize_answers(ANSWER_CHOICES) #Shuffles the first set of answer choices
. This randomizes where the answer and the wrong choices are when displaying the three choices to the user. This way, the user doesn't know where the correct answer is, and each time you play the game, the answers might be in a different order, so you can't memorize which position the correct answer was in, and also this way, the correct answer isn't always in the same position (such as at the top).
On Line 16, line_number = 0
creates the new variable line_number
and sets it to 0
. This variable is used to count the line number that the code is currently processing in the Trivia.txt file. The variable is used to know when you complete the trivia questions and then ends the game.
On Line 17, number_of_questions = 12
creates a new variable number_of_questions
, which is the amount of questions that there are going to be in the game. In this case, the number of questions is set to 12.
On Line 18, you'll see the code comment, #TODO: Uncomment this line to add the variable of score
. You'll uncomment the next line, Line 19, to add the "score" variable.
On Line 19, you'll see the code, #score = 0 #Records the correct answering
. As the comment on Line 18 mentioned, make sure you remove the # hashtag at the front/left of the line. This allows the code to run. This line creates the score
variable and sets it to 0
.
On Line 21, you'll see display_intro_screen() #Displays the intro screen
. This is a function that opens the introduction screen at the beginning of the game. This screen just says, "Welcome to the Trivia Game!" This is to let the player know a little bit of context about the game. Under that, it says, "Click here to start." It's often valuable to have this kind of introduction screen so that the player can prepare themselves before starting the game. They know that the game starts as soon they click the text to start the game. (It would suprise the player if the game started right away.) The code comment (#Displays the intro screen
) describes what the code does. Comments like these are helpful for when someone else sees your code, and it might not be as obvious to them what your code does.
On Line 23, running = True
creates the new running
variable and sets the variable to the True
value. This sets up the running
variable for the while
loop on Line 26, so that the game will run as long as the variable running
is set to True
. When it's time for the game to stop running, you will just set the variable to False
instead, which we do on Line 31, and later you'll also do this on Line 73.
On Line 25, you'll see the comment, #This displays the question screen until the last question or until the player closes the window
. This is a commented line that will not run in the code, but it tells you that the next lines of code (in the while
statement that starts on Line 26) will show you the question screen. This is the screen that displays each question and the possible answers. After the player gets the question right or wrong, then an image of Mrs. Codala appears over the screen. So the player stays on this screen througout the entire game. The player stays on the question screen until the last question or until the player closes the window.
On Line 26, while running:
starts the while
loop. The code in this while
loop (Lines 27-78) will keep running as long as the running
variable is still set to the True
value, which the code sets up back on Line 23. As long as the value is True
, once Line 78 runs, then the code starts again on Line 27 and runs again.
On Line 27, you'll find the code display_question(question, ANSWER_CHOICES) #Displays the new question and the three answer choices
. As the comment describes, this function displays the new question and the three answer choices, which it pulls from the question
variable and the ANSWER_CHOICES[]
list.
On Line 28, EVENTS = pygame.event.get()
creates the EVENTS
list and sets the list to the values that come from the pygame.event.get()
function. These events are mouse clicks that come from the player, either on the window's close button or on one of the answers.
On Line 29, for event in EVENTS:
is a nested loop, which means it is a loop within another loop. In this case, it's nested within the while
loop that starts on Line 26. The following code (Lines 30-78) will run until there are no more events in the EVENTS
list.
On Line 30, you'll see if event.type == pygame.QUIT: #If the player clicks the Close button, it exits the game
. This is an if
statement that checks to see if the player clicked the close button or not. If the player clicks the close button on the window, then it is recorded as a pygame.QUIT
event. The program will then run the next line.
On Line 31, running = False
sets the variable running
to False
, which exits the while
loop and closes the program.
On Line 32, mouse_position = pygame.mouse.get_pos()
sets the variable mouse_position
to the coordinates of where your mouse is on the screen. This value is set by the pygame.mouse.get_pos()
function.
On Line 33, you'll see the code and comment, if event.type == pygame.MOUSEBUTTONDOWN: #If the player clicks the mouse
. This is another nested if
loop like Line 30, and this line will run whenever the player clicks the mouse button.
On Line 34, you'll see the comment, # Check to see if player has clicked on one of the possible answers
. This comment describes what the next line, Line 35, is about to do.
On Line 35, we've got a longer if
statement: if answer_1_rect.collidepoint(mouse_position) or answer_2_rect.collidepoint(mouse_position) or answer_3_rect.collidepoint(mouse_position):
This statement is checking whether the player clicks any of the three answers. If so, then the rest of the code runs, moving on to Line 36, to check if the player clicked the first answer. If the player didn't click any of the three possible answers, then the game is essentially waiting for the player to click the mouse again (see Line 33), to progress the game forward.
On Line 36, you'll see the code, if answer_1_rect.collidepoint(mouse_position): #If the player clicks the 1st answer on the top
. This is another nested if
loop that checks to see if the answer selected was the first answer on the top. If so, the program then runs the indented code below Line 36, which is Lines 37-42. It's going to check whether the answer is correct or not.
On Line 37, you'll see the code if ANSWER_CHOICES[0] == answer: #If it's the correct answer
. This is checking if the answer variable (with the correct answer in it) is equal to the 0
placement in the ANSWER_CHOICES[]
list. If so, the answer is correct! The program then runs the indented code below it, which is Lines 38-40.
On Line 38, you'll find the comment, #TODO: Uncomment this line to add 1 score
. This is a comment that doesn't run in the program. It is helping show that you'll need to uncomment (remove the hashtag) of the next line of code, in order to run it.
On Line 39, uncomment #score += 1 #The player gets one score
, by deleting the first hashtag, at the beginning of the line of code. The program will then run the code (score += 1
), which will add one point to the player's score.
On Line 40, you'll find the code, display_codala(correct_a, "correct_text") #Displays codala and text for correct answer
. This line displays the image of Mrs. Codala with her text bubble saying that the player got the answer right.
On Line 41, you'll see else: #If it's an incorrect answer
. The only part of this line that is code is "else:". The code indented under this line (Line 42) runs only if Line 37 isn't true. That means that Line 42 is going to run if the first answer is not correct. (Line 37 checks to see if the first answer is correct, after the player clicks on the first answer, which is what Line 36 checked for.)
On Line 42, you'll see the code, display_codala(incorrect_a, "incorrect_text") #Display codala and text for incorrect ans,wer
. If the answer is incorrect, the image of Mrs. Codala will be displayed with a text bubble saying that the player got the answer wrong.
On Line 43, you'll find the code, if answer_2_rect.collidepoint(mouse_position): #If the player clicks the 2nd answer
. Similar to Line 36, the program will run the next line if the player selects the second answer from the top (the middle answer).
On Line 44, see the code, if ANSWER_CHOICES[1] == answer:
. If the second answer is correct, then the indented code below this line will run (Lines 45-47).
On Line 45, you'll find the comment, #TODO: Add 1 score
. This is something for you to do! You'll add the code on Line 46, which we'll cover in the next section, "Write Your Own Code." Here's an early hint for Line 46: Look back to Line 39 for an example of how to add one point to the score.
On Line 47, display_codala(correct_b, "correct_text")
, since this was the correct answer, Codala will be displayed with her text bubble saying that the player got the correct answer.
On Line 48, else:
, implies that you got the wrong answer, and the following indented text will be run.
On Line 49, display_codala(incorrect_b, "incorrect_text")
, since this was the incorrect answer, Codala will be displayed with her text bubble saying that the player got the incorrect answer.
On Line 50, if answer_3_rect.collidepoint(mouse_position): #If the player clicks the 3rd answer
, checks to see if the player selected the third option from the top, and if they did, the program will run the following indented code.
On Line 51, if ANSWER_CHOICES[2] == answer:
, if the answer that the player selected was correct, the following indented code will be run.
On Line 52, #TODO: Add 1 score
, like in Line 34, what code should you write to add one score?
On Line 54, display_codala(correct_a, "correct_text")
, since this was the correct answer choice, Codala will be displayed with her text bubble saying that the player got the correct answer.
On Line 55, else:
, implies that the answer choice selected was incorrect, and will run the following indented code.
On Line 56, display_codala(incorrect_a, "incorrect_text")
, since this was the incorrect answer choice, Codala will be displayed with her text bubble saying that the player got the incorrect answer.
Now we're going to tackle the parts where you wrote your own code!
First, let's go all the way back to Line 19. If you haven't yet, remove the #
hashtag so that the line runs. Without the #
symbol at the front of the line, the line should now look like this:
score = 0 #Records the correct answering
That line creates the variable score
and assigns 0
to it. It will later increment by one (the number one is added to it) each time the player gets an answer right!
On Line 56, #TODO: Check if it's the last question and display end screen with scores
, instructs you to write the code for the case that the last answer from the top is the correct answer.
On Line 57, #Modified from the check_if_last_question function in init.py
, tells you that you can use the "check_if_last_question" function from init.py.
On Line 58, you first want to check to see if the number of questions is greater than or equal to that number times four (the number of questions) and the minus the number of questions (4). What functions from the past lines of code have you seen that can help you achieve this goal?
On Line 59, you want to display the end screen, what functions have you seen that will help you do that?
On Line 60, write #Display the final score on the end screen
to organize what you are doing and help you to know what the next lines of code will be.
On Line 61, what code should you write if your goal is to check if the score is equal to the number of questions? Think about the variables that have been used previously.
On Line 62, from Line 61 it is implied that the player got all the questions right! What variable should you set equal to the text that displays that the player got all of their answers right.
On Line 63, what function combines else and if? You want to start this line with this function and do the same thing as Line 61, except if the player got all of the answers wrong.
On Line 64, from Line 63 it is implied that the player got all the questions wrong. Looking back at Line 62, what can you change to make it reflect getting all the answers wrong as opposed to getting them all right?
On Line 65, since this is last statement, you can just write else:
which will be for any other case where the player got more than 0 and less than 12 answers correct.
On Line 66, you want to write out the code that will display the text that they got a certain amount of questions correct, and think about what can you change from Line 64 so it achieves this goal?
On Line 67, what code can you right to set the score text size to (200,90)?
On Line 68, using the pygame function, how do you update the score of the player?
On Line 69, write the code that uses the pygame function that gets the event.
On Line 70, use the "time.sleep" function to set the screen to show up for just five seconds before it turns off after the game has been played.
On Line 71, set the "running" variable to "False" so that the program does not run again.
On Line 73, #If it's the not the last question, we display the next question
, is a commented line that describes if there are more than twelve questions, then line 74 will be displayed.
On Line 74, line_number, question, answer, ANSWER_CHOICES = move_to_next_question(TRIVIA, question, line_number, ANSWER_CHOICES)
, displays the text for if there is more than twelve lines of code.
On Line 75, pygame.quit()
, is the pygame function that quits out of the program.
I included a solution file for Level 3, 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, you can take on Level 3, Challenge 2! This is a challenge where you add in an incorrect answer that will create a total of four choices!
In addition to this Help page and the instructions for our Level 3 challenges, we also have Online Articles, a Learning Quiz, an Unplugged Activity, and a Rewards article:
-
Level 3: Help - This page helps you complete the instructions in the book, in case you get stuck.
-
Level 3: Challenge 1 - On this page, I show you how to add a score to the game, so you can get all the questions right, then when the game ends, displaying the score.
-
Level 3: Online Articles - I made you a list of different web pages I found, which will help you learn more about the Classroom Quiz.
-
Level 3: 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 3: Unplugged Activity - I wrote this page with more details than what you saw in the book. In this game you pick a character and an animal with your friend, then you flip a coin and if it lands on heads, you will act like your character and if it lands on tails, you will act like your animal!
-
Level 3: Rewards - If you completed the Classroom Quiz 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 Bow 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 4! While you read through Level 4 in your book, you can check out the resources from Sidewinder, as she teaches you how to build the Space Wars program: