Learn about multi-dimensional arrays. We'll use one for our Trivia questions and answers.
Use a custom Code Block to define a function to add questions to your Trivia game.
Dynamically present questions and multiple choice answers in random order.
Let user input an answer choice and then inform if their choice is correct. Present the score at the end.
NOTE: This is an example of a complex program that won't run until completed. BUT, it will be easily re-usable to create new Trivia games!
We've used variables before - they hold ONE object of information, such as score
score = 5
A "list" stores MULTIPLE objects of information, such as names
names = ("John", "Suzie", "Mark", "Lisa")
An array is a special type of list that contains objects of the same type (all text, all numbers, etc.)
When you have multiple lists that are related, it's call a multi-dimensional array.
names = ("John", "Suzie", "Mark", "Lisa")
scores = (92, 78, 83, 65)
We're going to use questions and answers from this 2-dimensional array in our Trivia game:
Question list in our 2-dimensional array:
Answer1 list in our 2-dimensional array:
Answer2 list in our 2-dimensional array:
Answer3 list in our 2-dimensional array:
CorrectAnswer list in our 2-dimensional array:
One Question and Answers set across our 2-dimensional array:
Correct Answer example across our 2-dimensional array:
Go To: http://scratch.mit.edu/
From the Scratch menu, click "Create" to start a new project
We don't need Scratch the Cat!
Select the "Stage" and its "Backdrops" tab. Use the Paint icon to create a New backdrop for your "Game Over" screen. Use the Text tool to type the message; leave some space to display the number of questions and score.
Use Paint icon to create a new Sprite. Use the Text tool to type a letter "Q".
Select the Set Costume Center icon. Click on the center of your letter "Q" to set its center point.
Select the Sprite's Information icon. Name the Sprite "Q" and uncheck the show setting to hide it.
Create 4 more Sprites. Set their center points. Name the Sprites as you see, below. Don't forget to uncheck the show setting for each to hide them!
Select the Stage. Use the Make a Variable button to create the 5 variables, seen below.
Select the Stage. Use the Make a List button to create the 6 lists, seen below.
Under the Data scripts tab, use blocks to build the rest of the Add function to add each question/answer set to your Trivia array.
Setting up a clean slate for the Trivia with Data script blocks:
Adding the questions/answers with our custom Add block under the More blocks area:
Add script blocks to kick off the questions and wait for user to complete before showing Game Over screen and score:
Select Stage. Select "backdrop2" so that we can place our Variables.
Temporarily show the "Score" and "questionNumber" variables. Make sure they are in "large readout" format. Position them in the correct places over "backdrop2".
Add a few scripts for the set up. Use a repeat loop to get a random question from our number of questions (questionNumber variable).
Add the selected question to our askedQuestion list, so we don't ask it again. Set the correctAnswer to check for. Then, ask the question and wait for the user's answer.
Use an if-then-else loop to evaluate for the user's answer and do the proper actions.
Position the Sprite. Listen to broadcasts. Say the item # from the Answer1 array matching the question # of the getQuestion variable.
Position the Sprite. Listen to broadcasts. Say the item # from the Answer2 array matching the question # of the getQuestion variable.
Position the Sprite. Listen to broadcasts. Say the item # from the Answer3 array matching the question # of the getQuestion variable.
Add a few sounds from the Library to use for correct and incorrect answers.
Add scripts for set up and to Listen to broadcasts. Do actions when user answers correct or incorrect.
You've created a Trivia game using a multi-dimensional array and a custom function Code Block! Now, here are a few Challenges:
Come up with your own set of questions and answers on a specific topic and share it on Scratch for others to try.
Think of how to use arrays and custom function Code Block techniques in your other projects.
If you got stuck anywhere, see the completed project with comments here: http://scratch.mit.edu/projects/56598724/!
/