Level 3: Challenge 2 - IncrediCoders/Python1 GitHub Wiki
Annie Conda added this page on March 15, 2025
Let's begin your second challenge for Level 3!
In this challenge, you'll make the text more interactive, so that it highlights and changes color when you click an answer, depending on whether you got the answer right. If the answer is wrong, the text you clicked changes to red. If the answer is right, the text changes color to green!
To find the Level 3 Challenge 2 code template, open the Level 3 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 ClassroomQuizChallenge2.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 (init
is short for initialization). 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 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). This comment tells you what you will be doing for the next line (for Line 4). In this case, you will put the text from the file into a list.
On Line 4, the code TRIVIA = read_file('Assets/Trivia.txt')
takes the trivia text from the Trivia.txt file and puts it in the TRIVIA
list variable.
On Line 6, you'll see the comment, #We now have a list of variables that holds our current question, correct answer, and the two wrong choices, in that order
. This comment continues onto another comment on Line 7, #We use these variables to display text on the screen
. You will create a Python list that holds the text from the file for the current question (on Line 8), the correct answer (Line 9), and the two wrong choices (Lines 10-11).
The comments on Lines 6-7 are an example of using multiple lines of code for a code comment. This helps you explain a section of code, so that the reader can fix the code (if there's a bug), add features or capabilities to the code, or to copy the code for a different application (and save time in the process).
On Line 8, the code question = TRIVIA[0]
assigns the question
variable a spot in the TRIVIA[]
list for the current question. This is the question that the player will be asked on the screen. Line 8 sets the string of text (the question) to the first place in the TRIVIA
list that we created on Line 4. The first place in a Python list is the 0
(zero) position.
On Line 9, the code answer = TRIVIA[1]
accesses the second place in the TRIVIA[] list, which is the correct answer to the question. (In the Trivia.txt file, the correct answer comes right after the question. We later will randomize the order of the three answers as they appear on the screen, so that the player won't know which one is the right answer, based on the order they're displayed.) Line 9 assigns the value to the answer
variable, so that it's clear what the answer is.
On Line 10, the code wrong_choice_1 = TRIVIA[2]
creates the wrong_choice_1
variable for the first of two wrong answers. (Every question has one correct answer and two wrong answers.) The code then assigns the #2 spot in the TRIVIA[]
list to the wrong_choice_1
variable. As I mentioned above, we do this so that we can randomize the order of the questions. (This way the questions will likely be in a different order almost every time that the player takes the quiz. So, the player can't memorize the order of the answers to know which answer is the correct answer.)
On Line 11, the code wrong_choice_2 = TRIVIA[3]
creates the wrong_choice_2
variable for the second wrong answer. The code assigns the #3 spot in the TRIVIA[]
list to the wrong_choice_2
variable. Because Python lists (and all arrays) start on spot #0, this is our fourth spot in the TRIVIA[]
list. (Back on Line 8, we assigned the #0 spot to the question
variable.)
On Line 13, the code ANSWER_CHOICES = [answer, wrong_choice_1, wrong_choice_2]
creates the new ANSWER_CHOICES
type list variable.
Type List Variable: A type list variable is a type of variable that lets you list a few different items in it. It's a little like a list (or array), but you usually create it just for a few items that you won't need to revisit. In our case, we're putting three different variables in the ANSWER_CHOICES
type list variable. This is going to allow us to randomize (or mix up) the order of these three variables, so that when they appear on the screen, the player isn't going to know which one is the correct answer (at least not by memorizing the order of the answers).
On Line 13, for the three variables that we add to the ANSWER_CHOICES
type list variable: The first place in this list is the correct answer, the second place is the first wrong answer, and the third place is the second wrong answer.
On Line 14, you'll see the code: randomize_answers(ANSWER_CHOICES) #Shuffles the first set of answer choices
. This line randomizes the order of the answer and the two wrong choices, so that when the three answer choices are displayed on the screen, the player won't know which is the correct answer, based on the order of the answers.
Also, at the end of Line 14, you'll see the comment, #Shuffles the first set of answer choices
. This is a common way to use comments in order to explain to the reader what that specific line of code does. That way, if someone opens up your code file, they can understand what's happening with each line of code. Not only does this help if they're trying to fix the code (like to fix a bug), but it's also helpful in case they want to reuse the code for another program. Not all lines of code (nor most lines of code) have a comment on the end like this one; usually just lines of code where the author thinks someone might need help understanding what the line does and why it exists.
On Line 16, the code line_number = 0
creates the new line_number
variable and sets it to 0
. This variable is used mostly by the init.py file to count the lines of the TXT file. The program uses this information to properly move on to the next question in the sequence and to find out when the game runs out of questions in the TXT file (because that's when the game ends).
On Line 17, the code number_of_questions = 12
creates the number_of_questions
variable, which sets the total number of questions in the game. This variable is mostly used by the init.py file to track when you run out of questions, in order to end the game. For this version of the game, we set the number of questions to 12.
On Line 19, you'll find the code display_intro_screen() #Displays the intro screen
. The display_intro_screen()
function runs a block of code that lives in the init.py file. The block of code displays the intro screen until the player clicks near the "Click here to start" text to start the game.
On Line 21, the code running = True
creates the running
variable and sets it to the True
value. This means that the program (the main part of the game) is now running.
On Line 23, you'll find the code comment, #This while block runs the game until the player closes the window
. This commented line describes what will be run in this while
block. The question screen will be shown, and then when you run out of questions, it displays the end screen, with a message from Mrs. Codala. The game ends when the player closes the window.
Line 23 is an example of including a code comment on its own line. This tells the programmers who are reading your file what the next section of code does, to give them more context. This information is helpful if they need to update the code.
On Line 24, the code while running:
starts a while
loop that runs the entire game. It displays each new question until the user closes the window or until the game runs out of questions. On Line 21, we set running
to the True
value, so all the code inside the while
loop continues running until the value of running
is changed to False
instead. That happens on Lines 28-29, when the player clicks the close button on the window. At the end of the file, on Line 94, if the game runs out of questions, then running is set to False
(in the init.py file). The while
loop stops running, so the screen doesn't refresh with new questions.
On Line 25, you'll find the code, display_question(question, ANSWER_CHOICES) #Displays the new question and the three answer choices
. As the comment describes, the display_question()
function displays the new question and the three answer choices on the screen. Because this is a loop, this code runs for each new question, and it changes the screen each time, displaying the new questions and answers. This function is defined in the init.py file.
On Line 26, the code EVENTS = pygame.event.get()
sets the EVENTS
variable to the list that came from pygame.event
module. The Pygame extension comes with modules like this one, that capture player clicks. Our code puts that information into the EVENTS
variable so that the program knows where the player clicked.
On Line 27, you'll see the code for event in EVENTS:
This is a nested for
loop, which means it is a for
loop within another loop. In this case, the for
loop is nested within our while
loop. The code that's in this for
block goes from Line 27 until the end of the file (Line 96). This block of code is going to run until there are no more events coming from the pygame.event.get()
function, which is stored in the EVENTS
list.
This loop can be thought of as a "for each" loop. It allows us to access every element in the EVENTS
list and assign it each time to the events
variable. This list runs once for each event that happens (such as a mouse click).
On Line 28, you'll find the code, if event.type == pygame.QUIT: #If the player clicks the Close button, it exits the game
. This if
statement checks to see if the user clicks the Close button on the window. If so, Line 29 runs, which closes the program and the window. You'll code like this in every Pygame-based Python game. Without these lines of code (Lines 28-29), the window wouldn't close when the player clicks the Close button on the window.
On Line 29, the code running = False
sets the variable running
to False
to exit the loop and close the game. The while
loop on Line 24 runs the game indefinitely, until running
is set to False
.
On Line 30, the code mouse_position = pygame.mouse.get_pos()
sets the variable mouse_position
to a Pygame function that captures the coordinates of where your mouse is on the screen. The get_pos()
function stands for "get the position."
On Line 31, you'll find the code, if event.type == pygame.MOUSEBUTTONDOWN: #If the player clicks the mouse
. This if
block (Lines 31-96) includes the rest of the code for the game. If the player doesn't click the mouse at that mouse coordinate position (determined on Line 30), then the while
loop runs again as the player moves the mouse to a new position.
On Line 32, you'll find the code comment, # Check to see if player has clicked on one of the possible answers
. This comment uses a full line to explain what's required for the if
block (from Lines 33-92) to run. This if
block only runs if the player clicks one of the three possible answers.
On Line 33, you'll find the code, if answer_1_rect.collidepoint(mouse_position) or answer_2_rect.collidepoint(mouse_position) or answer_3_rect.collidepoint(mouse_position):
This line of code sets up the three embedded if
statements on Lines 34, 53, and 72. This line of code makes sure the player clicks on one of the three possible answers. This ensures that nothing happens unless the player clicks one of the answers.
On Line 34, you'll find the code, if answer_1_rect.collidepoint(mouse_position): #If the player clicks the 1st answer on the top
. This is a nested if statement that only runs if the player clicks the top answer. You might notice that the hitbox for each answer is a little bigger than the text, so that you can "miss the text" a little with your mouse and still get the desired result (of clicking the answer).
On Line 35, you'll see the code, if ANSWER_CHOICES[0] == answer: #If it's the correct answer
. Python checks to see if the first/top answer that the player chose is the correct answer. If it is the correct answer, then Python runs the code below it that is indented (the code in this if
block, on Lines 36-43). If the answer isn't right, Python skips ahead to the else
statement on Line 44.
On Line 36, you'll find the comment, #TODO: Uncomment these lines to add interactive effect
. This comment instructs you to uncomment Lines 38-42. This is your first change to the code for this project!
NEW CODE: On Lines 38-42, you're going to remove the hashtags before those lines of code. For instructions, see the next section, "Write Your Own Code." So, we're skipping ahead to explain Line 43...
On Line 43, you'll see the code, display_codala(correct_a, "correct_text") # Displays the image of Mrs. Codala and her text for the correct answer
. Since this was the correct answer choice, Mrs. Codala will be displayed with her text bubble saying that the player got the correct answer!
On Line 44, you'll find the code, else: #If it's an incorrect answer
. If the player picked the first answer, and it's wrong, then the code block runs, including Lines 45-52. This code is going to display the image of Mrs. Codala with her text bubble saying that the player got the incorrect answer. You're going to write this code yourself!
Let's start this off with the code comment on Line 36, #TODO: Uncomment these lines to add interactive effect
. On Lines 38-42, you're going to remove the hashtags before those lines of code.
On Line 37, you'll find the comment, # Change the color from blue to green
. That's exactly what will happen when you make Lines 38-42 active! Because this block of code only runs if the user clicked the first answer (Line 34) and if it's correct (Line 35), then this block changes the blue text to green to visually show to the player that they got the answer right!
On Line 38, the code #answer_1_text = my_font.render(answer, True, (0,128,0))
is commented out, so that it doesn't run.
-
Delete the hashtag (the pound symbol) from the front of this line to load the text for the first question into the
answer_1_text
variable.
On Line 39, remove the hashtag from #screen.blit(answer_1_text, answer_1_rect)
to uncomment this line so that it runs. This line creates the image for the text and the rectangle to be displayed, but they aren't visible on the screen yet.
On Line 40, remove the hashtag from #pygame.display.update()
to uncomment and run the line of code. This line updates the display to show the question and the answer that was drawn on Line 39.
On Line 41, remove the hashtag from #pygame.event.get()
to uncomment it. This will get the next event in the "pygame.event" list.
On Line 42, remove the hashtag from #time.sleep(1)
to uncomment it. This line starts the timer to pause the game for one second, so that the player can see that the text changed color.
Lines 43-44 already have the code ready to go! But here's what's on those lines and what it does...
On Line 43, you'll see the code, display_codala(correct_a, "correct_text") # Displays the image of Mrs. Codala and her text for the correct answer
. Since this was the correct answer choice, Mrs. Codala will be displayed with her text bubble saying that the player got the correct answer!
On Line 44, you'll find the code, else: #If it's an incorrect answer
. If the player picked the first answer, and it's wrong, then the code block runs, including Lines 45-52. This code is going to display the image of Mrs. Codala with her text bubble saying that the player got the incorrect answer.
On Line 45, you'll see the comment #TODO: Add the interactive effect
. In the next lines of code, you will add the interactive effect described in the code comment on Line 46...
On Line 46, it says, # Change the color from blue to red
. This code block will change the color from blue to red when it displays the text. The color is changed on Line 47.
On Line 47, you're going to change the color of the text from blue to red. This is a little similar to the code on Line 38.
Type out the answer 1 variable:
answer_1_text
Next, on the same line, add a space, type in the =
equal symbol, add another space, and then type in this function:
my_font.render()
In the parentheses on that line, you're going to add in three parameters. In the parentheses, type in these three arguments, and include a comma after the first two:
ANSWER_CHOICES[0]
True
(255,0,0)
The first argument displays the text in the 0
position of the ANSWER_CHOICES[]
list, which is the first of the three answers. The second argument (True
) tells the function to display this text. the third argument (255,0,0)
tells the program to make the text color red.
The final line of code looks like this:
answer_1_text = my_font.render(ANSWER_CHOICES[0], True, (255,0,0))
Line 48 is the exact same as Line 39! Copy Line 39 here, at the same indentation level as Lines 45-47. The screen.blit()
function creates the image for the text and the rectangle to be displayed, but they aren't visible on the screen yet.
Line 49 is the exact same as Line 40! Copy Line 40 here, at the same indentation level as Lines 45-48. This line updates the display to show the question and the answer that was drawn on Line 48.
Line 50 is the exact same as Line 41! Copy Line 41 here, at the same indentation level as Lines 45-49. This Pygame function get the next event in the "pygame.event" list.
Line 51 is the exact same as Line 42! Copy Line 42 here, at the same indentation level as Lines 45-50. This line starts the timer to pause the game for one second, so that the player can see that the text changed color.
On Line 52, you'll see the code display_codala(incorrect_a, "incorrect_text") # Displays the image of Mrs. Codala and her text for the incorrect answer
. Because this was the incorrect answer choice, the Mrs. Codala is displayed with her text bubble saying that the player got the incorrect answer.
On Line 53, you'll see the code if answer_2_rect.collidepoint(mouse_position): # If the player clicks the 2nd answer
. Similar to Line 34, this if
block (Lines 54-71) runs if the player selected the middle answer (the second answer from the top).
On Line 54, you'll see the code if ANSWER_CHOICES[1] == answer:
If the answer that the player selected (the second answer) is correct, the following indented code will run (which is the rest of this if
block, Lines 55-62).
On Line 55, you'll find the comment, #TODO: Add the interactive effect
. This is similar to the section of code that you uncommented on Lines 38-42.
On Line 56, the code comment says, # Change the color from blue to green
. That's what you're going to do next: you'll change the text from blue to green.
Line 57 is almost the same as Line 38. The only difference is that the variable at the beginning of the line is different, because this line of code displays the text on the second line (for the second answer).
Type in the answer 2 variable:
answer_2_text
The rest of the line is the same as Line 38. You set the my_font.render()
function to the answer_2_text
variable. Just like in Line 38, (0,128,0)
is the RGB value for the green text.
Type out the rest of the line of code, based on what you see in Line 38.
On Line 58, make sure your code is indented at the same levels as Lines 55-57. Type in the screen.blit()
function. Inside the parentheses, type in the answer_2_text
variable, add a comma and a space, and then type in the answer_2_rect
variable. This is similar to Line 48, but the variables are for the second answer instead of the first answer. This function creates the image for the text and the rectangle to be displayed, but they aren't visible on the screen yet.
On Line 59, you're going to update the screen! Make sure you're still indented at the same level as Lines 55-58. Type or paste in the code you used on Line 49, to update the screen with the now-green text.
[Line 60 - To be deleted: pygame.event.get(). All the line numbers will need to be updated.]
Line 61 is the same as Line 51. Type or paste in the code from Line 51 to wait one second for the player to see that the color of the text changed from blue to green. You'd think that one second isn't long enough, but when you play the game, it's not too fast.
Line 62 is already written out for you! It displays the image of Mrs. Codala and the text (as part of that image) that tells the player they got the answer right! It's very similar to Line 43, but the correct_b
is different, because we have two different "correct" images and two different "incorrect" images. This gives the player a little variety.
Line 63 indents to the left (at the same level as the if statement on Line 54) and is our else:
statement. This statement runs if Line 54 isn't true... if the player selected Answer 2 and it's not the right answer. Then all the lines that are indented under Line 63 will run. What do those lines do? We're going to change the color of the text from blue to red (like we did with the else
block on Lines 44-52).
On Line 64, you'll find the indented comment, #TODO: Add the interactive effect
. This comment describes the code you'll write next, on Lines 66-70.
On Line 65, you'll find the comment, # Change the color from blue to red
. So there you go! Let's change the color from blue to red! Comments like these make it so that someone else can read your code and understand what's going on!
On Line 66, you're going to assign the my_font.render()
function to the answer_2_text
variable. It's very similar to Line 47, so take a look at that line first. Make sure you're indented at the same level as Lines 64-65.
-
Type in the
answer_2_text
variable. We're using this variable because we're rendering (or showing) the text for Answer 2. -
Add a space, type in the
=
equal symbol, and add another space. You're setting the new value of theanswer_2_text
variable. - Type in the
my_font.render()
function. It's not just a clever name! This function renders (or displays) the font (which includes the text and color) for Answer 2. - In the parentheses, you're going to pass in three arguments. Type them in:
-
ANSWER_CHOICES[1]
- This grabs the second answer from the list of answers in your text file. It's not the correct answer, because if it was, then it would have been displayed as the correct answer back in theif
block that starts on line 54. -
True
- This argument means that it's ready to render. It's always used in this function. -
(255,0,0)
- This is your new red color. It's the same argument that's used in Line 47, which also turns the text red.
-
Make sure you use commas between the arguments and end Line 66 with an end parenthesis. Here's what Line 66 should look like: answer_2_text = my_font.render(ANSWER_CHOICES[1], True, (255,0,0))
Next is Line 67! Copy (or paste) the code in from Line 58. Make sure the line is indented at the same level as Lines 64-66. The screen.blit()
function creates the image for the text and the rectangle to be displayed, but they aren't visible on the screen yet.
Next is Line 68! Copy (or paste) the code in from Line 59. Make sure the line is indented at the same level as Lines 64-67. This code will update the screen and change the color of the text from blue to red.
[Line 69 - To be deleted: pygame.event.get(). All the line numbers will need to be updated.]
On Line 70, the final step for this code block is to write out the code for a timer that lets the player see that the color of the text changed to red. The timer lasts for one second. Copy (or paste) the code in from Line 61. Make sure the line is indented at the same level as Lines 64-69.
Line 71 is already written for you. The display_codala()
function displays the image of Mrs. Codala, letting the reader know that the answer wasn't correct, as well as the text that displays on the screen, "That is incorrect."
Lines 72-73 set up the next if
block, to check if the player clicks the third answer and it is the correct answer.
Line 74 let's you know that you'll need to write Lines 76-80, to change the answer text from blue to green. This line contains the comment, #TODO: Add the interactive effect
.
Likewise, Line 75 has the comment, # Change the color from blue to green
. It's valuable to include comments like this in your code so that anyone reading your code will understand what the if
block is doing.
Line 76 is very similar to Line 57. Copy the code from Line 57, but change the variable at the beginning from answer_2_text
to answer_3_text
instead. The difference is because we're changing the color on the third answer instead of the second answer (like Line 57 does). The answer
argument in the function passes the line of text (into our answer_3_text
variable) that's the answer to the question from our text file. The (0,128,0)
function is what changes the color from blue to green.
Line 77 is very similar to Line 67. Copy the code from Line 67, but change the variables from answer_2_text
and answer_2_rect
to answer_3_text
and answer_3_rect
instead. The screen.blit()
function creates the image for the text and the rectangle to be displayed, but they aren't visible on the screen yet.
Adding in this code should feel very repetitive! Each time you add an if
block for conditional logic, you're changing the code a little! But the code is very similar in each if
block. That might seem strange to repeat your code so much, but this challenge is helping you get used to these repetitive tasks and the process of copying and changing code. Why? Because developers write repetitive code a lot!
Even if it feels like you're repeating the same thing and not learning much, each time you copy and change the code for a new set of logic, you get even more familiar with the process of writing out similar logical blocks of code like this!
Line 78 is the same as Line 68! Copy (or paste) the code in from Line 68. Make sure the line is indented at the same level as Lines 74-77. This code will update the screen and change the color of the text from blue to green. (That rhymes! I'm a poet and didn't know it, and I only had to mention this line of code to show it!)
[Line 79 - To be deleted: pygame.event.get(). All the line numbers will need to be updated.]
Line 80 is the exact same as Line 70! Copy (or paste) the code in from Line 70. Make sure the line is indented at the same level as Lines 74-79. This code adds a timer that lasts for one second of delay before running the next line of code. It lets the player see that the color of the text changed to green, which is the point of this challenge. It won't do us any good if we don't give the player a chance to see that we're changing the color of the answers!
Line 81 is already in the file for you! The code display_codala(correct_a, "correct_text")
will bring up the image of Mrs. Codala and the text to tell the player that they got the answer right!
On Line 82, you'll see the code else:
An else
block runs when the if
statement/block isn't true. So, if the if
statement on Line 73 isn't true (if the third answer is not correct), then this else
block runs.
In Python, an else
statement is tied to the last if
statement at the same indentation level. Let's look at an example.
If you write more than one if
statement and then an else
statement, then the else
statement only pairs with the last if statement:
x = 3
if x > 5:
print("Greater than 5")
if x < 5:
print("Less than 5")
else:
print("Not less than 5")
So, the else
statement is tied to the second if
statement, but not the first one. You'll also notice that the code isn't very useful or clear.
Sometimes you'll see the logic in code and recognize that it's not very good. Then, you'll either need to fix it in a small way or in a larger way, where you rewrite the code for more direct, clear, and sometimes simple logic. By logic, we mean the flow or steps you take to get the result you want in the program. Basically, programming is like a big puzzle that you're trying to solve! You want to get your result (or your feature to behave the way you intend) in the best way possible.
If you want to work through multiple if
statements before you run the else
statement, then you'll use an if
/elif
/else
chain. Here's an example of running a few if
conditions through a chain:
x = 10
if x > 15:
print("Greater than 15")
elif x > 5:
print("Greater than 5")
else:
print("5 or less")
The example above uses an if
block and an elif
block, before you get to the else
block. That code uses the else
statement to let you know that your number in the x
variable is "5 or less"
.
Back to the Challenge!
On Line 83, you'll see the comment, #TODO: Add the interactive effect
. That's what you're going to do, for the last section of code that you'll add in this challenge!
Line 84 holds the comment, # Change the color from blue to red
. Comments like this help you (and others) remember what a particular block of code is doing! That might not seem important (to document your code), but you have to revisit code a lot when you fix bugs, share code, or add features to your program. So, you want to make sure the person reading your code knows what everything does!
It's time to write some more code! This is your last code block, so let's get to it!
Line 85 is very similar to Line 66, which changed Answer 2 to red (because it was the wrong answer). This time, you're changing Answer 3 to red (because it's the wrong answer). It's time to copy some code and make two changes:
- Copy the code from Line 66 and write it on Line 85.
-
Change the variable from
answer_2_text
toanswer_3_text
instead. This is because you're changing the text for Answer 3 instead of Answer 2. As you can see, this code assigns themy_font.render()
function to our variable. -
Change the number
1
inANSWER_CHOICES[1]
to a2
instead, so that it now saysANSWER_CHOICES[2]
in the first argument (there are three arguments in the parentheses, separated by commas. This grabs the Answer 3 text in the third answer spot for the question. Remember that Python lists start at number0
, so that's why number2
is the third spot in theANSWER_CHOICES[]
list.
Those are your only two changes! The other two arguments in Line 85 are:
-
True
- This argument means that it's ready to render. It's always used in this function. -
(255,0,0)
- This is your new red color. It's the same argument that's used in Line 47, which also turns the text red.
Line 86 is the same as Line 77! Copy (or paste) the code in from Line 77. Make sure the line is indented at the same level as Lines 83-85. The screen.blit()
function creates the image for the text and the rectangle to be displayed, but they aren't visible on the screen yet.
Line 87 is the same as Line 78! Copy (or paste) the code in from Line 78. I won't tell you to make sure the code is indented with the other lines in this code block, because you probably have that down now! (Oh, wait, I think I just told you again.) This code will update the screen and change the color of the text from blue to red.
[Line 88 - To be deleted: pygame.event.get(). All the line numbers will need to be updated.]
Line 89 is the same as Line 80! Copy (or paste) the code in from Line 80. This code adds a timer that lasts for one second of delay before running the next line of code. It lets the player see that the color of the text changed from blue to red, so that the player has a chance to see that we changed the color of the answer.
You did it! You finished this code block!
Now we're going to explain the last section of code!
On Line 90, the code display_codala(incorrect_a, "incorrect_text")
will display the Mrs. Codala image and some screen text. Since this was the incorrect answer, the Mrs. Codala image and text will tell the player that they got the incorrect answer.
Line 91 shows the comment, #If it's the not the last question, we display the next question
. This commented line lets you know that if this isn't the last question, then it runs again! There are 12 questions in our TXT file and in our number_of_questions
variable (back on Line 17).
On Line 92, the code line_number, question, answer, ANSWER_CHOICES = move_to_next_question(TRIVIA, question, line_number, ANSWER_CHOICES)
sets up those variables for the next question.
On Line 93, you'll see the comment, # Checks if it's the last question and display end screen
. Be sure to remember to use code comments like this one to clarify any important steps in your code logic.
On Line 94, the code running = check_if_last_question(line_number, running, number_of_questions)
is a function that checks if your question number has reached the limit yet (which is 12, in our number_of_questions
variable on Line 17). If running
is false (we reached the 12th question), then the program will quit. If you haven't reached the 12th question yet, the program will ignore Lines 95-96 and continue on to the next question (going back up through our loop).
Line 95 checks if running is false. If it is, Line 96 runs, which displays our end screen! An image of Mrs. Codala opens up, which tells you that you finished the game!
If you want to make (and play) a version of the game that shows you your final score, try out the Level 3: Challenge 1 (if you haven't yet)!
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 3 (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 for how to build the Space Wars program: