Mystery Number
This is a math game where the player tries to guess the value of a mystery number -- a number between 1 and 100 chosen randomly by the computer.
The stack has three cards: a splash/title card, a directions card, and a card with the game code. All of the script for the game is contained in this third card in two places: on the card script and in a text field titled "guess."
Downloading a Project Starter Stack
To save some time, I have prepared a stack with the three cards and all of the objects needed except for the following:
- All of the scripts
- The text field "guess" that will go on the third card.
Click here to download this project starter stack. (If you get a "Page not found" message, then right-click on the link and choose "Save link as" and to download the file.)
Adding the Text Field "Guess" to Card Three
During the workshop, we will do the following together:
- After you open the starter stack, navigate to card three and drag a text field to the center of the screen. Title it "guess."
- Double-click on this text field to open the inspector window for it.
- Under "Basic Properties," name this object as "guess" (without the quotes). This is crucial as the scripts we will be using depend on its name being exactly correct.
- Turn off the vertical scroll bar.
- I suggest you increase the font size to at least 24 (found under "Text Formatting").
- Resize this text field as necessary.
Adding Scripts: "on OpenCard" Script
(Click here for a text file with these scripts.)
Be sure you are on card 3, then choose to go to the Card Script (under the "Object" file menu).
Copy and paste this script:
global varMystery, varGuess, varTries
on opencard
show field "guess"
show field "directions"
show field "guesses"
put random (100) into varMystery
//turn the following line off (by turning it into a comment)
//after you are sure everything is working
put varMystery into message
put 0 into varTries
put varTries into line 2 of field "guesses"
hide field "too high"
hide field "too low"
hide field "correct"
hide button "Play Again"
focus on card field "guess"
end opencard
Adding Scripts: Script to Add to the "Guess" Text Field
Click once on the text field "guess" to select it (be sure you are in edit mode). Then, open the script window for this object by clicking on the "Edit Script" button (a picture of a scroll) in the toolbar near the top of the window. (CTRL/CMD-E is the short-cut to opening the script window.)
Copy and paste this script:
global varMystery, varGuess, varTries
on returninfield
add 1 to varTries
put varTries into line 2 of field "guesses"
put line 1 of me into varGuess
if varGuess > varMystery then
show field "too high"
end if
if varGuess< varMystery then
show field "too low"
end if
if varGuess = varMystery then
hide field "guess"
hide field "directions"
hide field "guesses"
if varTries>1 then
put "You guessed "&varMystery&" in "&varTries&" guesses!" into line 1 of field "correct"
end if
if varTries=1 then
put "You guessed "&varMystery&" in "&varTries&" guess!" into line 1 of field "correct"
end if
show field "correct"
show button "Play Again"
end if
wait 1 second
hide card field "too high"
hide card field "too low"
put empty into me
end returninfield
Final Version
Click here to download the final version of the Mystery Number stack. (If you get a "Page not found" message, then right-click on the link and choose "Save link as" and to download the file.)
Enhanced Version: Using Images as Feedback in a Tournament
Click here to download this enhanced version of the project stack. (If you get a "Page not found" message, then right-click on the link and choose "Save link as" and to download the file.)
If you understand how Mystery Number works, you may enjoy taking a look at an enhanced version where the player is playing the game in a "tournament" consisting of 3 rounds. Each round is achieved when the mystery number is guessed. The tournament is won after three rounds.
Successfully completing a tournament round is communicated via text and also visually with a small star graphic. Each time a round is completed, a yellow star is shown at the top of the screen.
When the tournament is won, the total number of guesses used in the tournament is shown. Thus, we have a new score based on this total number of guesses over the duration of the tournament.
Below is a summary of the enhancements.
Title Card Script
Insert the following new script on the card "title":
global varStar, varTournamentTries
on OpenCard
put 0 into varStar
put 0 into varTournamentTries
//graphics
hide image "star1" on card "game"
hide image "star2" on card "game"
hide image "star3" on card "game"
end OpenCard
On the card "game" there are six images: 3 star outlines, and 3 yellow stars. When the game starts, the yellow stars are hidden using this script.
Also notice two new global variables are declared and initial values set to 0: varStar and varTournamentTries
varStar will be used to determine when to show one of the yellow star. varTournament is used to aggregate all of the guesses taken over the duration of the tournament.
Game Card Script
Insert the following script on the card "game":
global varMystery, varGuess, varTries, varStar
on opencard
show field "guess"
show field "directions"
show field "guesses"
put random (100) into varMystery
//turn the following line off (by turning it into a comment)
//after you are sure everything is working
put varMystery into message
put 0 into varTries
put varTries into line 2 of field "guesses"
hide field "too high"
hide field "too low"
hide field "correct"
hide field "wonTournament"
hide button "Play Again"
focus on card field "guess"
end opencard
The only important change here is that the script hides a new field on the card titled "wonTournament." This field will only appear when the tournament has been won.
Script to Add to the "Guess" Text Field
Here is the enhanced for of the script for the text field "guess":
global varMystery, varGuess, varTries, varTournamentTries, varStar
on returninfield
add 1 to varTries
put varTries into line 2 of field "guesses"
put line 1 of me into varGuess
if varGuess > varMystery then
show field "too high"
end if
if varGuess< varMystery then
show field "too low"
end if
if varGuess = varMystery then
add varTries to varTournamentTries
hide field "guess"
hide field "directions"
hide field "guesses"
if varTries>1 then
put "You guessed "&varMystery&" in "&varTries&" guesses!" into line 1 of field "correct"
end if
if varTries=1 then
put "You guessed "&varMystery&" in "&varTries&" guess!" into line 1 of field "correct"
end if
show field "correct"
add 1 to varStar
if varStar = 1 then show image "star1"
if varStar = 2 then show image "star2"
if varStar = 3 then show image "star3"
if varStar <3 then show button "Play Again"
if varStar =3 then
wait 2 seconds
put "You won the tournament in "&varTournamentTries&" tries!" into field "wonTournament"
show field "wonTournament"
show button "New Tournament"
end if
end if
wait 1 second
hide card field "too high"
hide card field "too low"
put empty into me
end returninfield
Script for New Button "New Tournament"
This is a new button that only appears when the tournament has been won. Here is the script:
global varStar, varTournamentTries
on mouseUp
put 0 into varStar
put 0 into varTournamentTries
hide field "wonTournament"
hide me
//graphics
hide image "star1" on card "game"
hide image "star2" on card "game"
hide image "star3" on card "game"
openCard
end mouseUp
Notice that the last line is "openCard," which does just that -- it opens the current card which triggers the script on the card.