In this activity we'll build an adventure game, where you can choose your own path through a
story.
Here's an example of the type of game we'll build:
The Cave of Time
.
write "Everyone loves turtles."Put what you want to say in quotes.
do ht # Well... everyone *secretly* loves turtles.
get_ready_for_school = ->
# indent two spaces to put the next lines on
# the to-do list for this function
write "Get dressed"
write "Eat breakfast"
write "Brush teeth"
# now stop indenting!
If you run that by itself (try it!) nothing will happen yet. Here's why:
you've only written down what should be on the checklist -- and in order to actually
do those things, you can:
do get_ready_for_schoolOr to do it 5 times, once for each day of the week:
do get_ready_for_school do get_ready_for_school do get_ready_for_school do get_ready_for_school do get_ready_for_school
# Ask a question and print a response.
write "Is it a weekday?"
menu
"Yes, it is!": -> write "time to get ready for school!"
"No, it's not!": -> write "time to sleep in!"
We can even use this choice to decide which function to
do:
# Write checklists for weekdays and weekends get_ready_for_school = -> write "Get dressed" write "Eat breakfast" write "Brush teeth" sleep_in = -> write "zzzzz" # Ask which it is, and follow the right checklist write "Is it a weekday?" menu "Yes, it is!": get_ready_for_school "No, it's not!": sleep_in
page1 = ->
write "Help, I've lost my pet turtle! Can you help me find it?"
menu
"Go look inside the house": page2
"Go look in the backyard": page3
page2 = ->
do cs
write "No turtles here! I guess they don't hang out inside."
menu
"Look outside instead": page3
page3 = ->
do cs
do st # Show turtle!
write "Hey, it's my turtle! Thanks!"
write "You win! Hurrah!"
do page1
All right! Go make a game!
# magic_word is a variable. # The magic word is abracadabra! magic_word = "abracadabra" write "What's the magic word?" write magic_word # A variable can vary (which means "change") -- # that's where the word variable comes from! # Let's change it to something else! magic_word = "freckles" write "What's the magic word?" write magic_wordYou can save whatever you like in a variable. We saved a word here, but next we'll use some special values, true and false. Because they're special, you don't need to put true and false in quotes like you do for regular words.
# say_hello is a variable, set to the special value "false" say_hello = false if say_hello # Indent 2 spaces, just like for functions and menus. # The next line won't happen because write_hello is set # to "false" (which means "no") write "hello, world!" # write_something is a variable, set to the special value "false" say_goodbye = true if say_goodbye # This one will happen, because say_goodbye is set # to "true" (which means "yes") write "goodbye!"Using these two things, we can remember when you find the Key -- then later, we can check whether you already found the key, and then let you open the door.
# This is a variable! You can name variables whatever you like.
found_the_key = false
under_the_hidden_tree = ->
do cs
write "Finally, after a long walk in the forest, you "
write "find yourself under an old, mossy oak tree."
write "The sunlight glimmers against something "
write "hidden in its gnarled roots..."
menu
"Look closer at the roots": tree_roots
"Return to the castle": castle
tree_roots = ->
do cs
write "You dig among the roots with your fingers and find an old key."
write "It's dirty and rusty, but it looks like it would still work."
# Here's where we make sure to remember you found the key!
found_the_key = true
menu
"Go back to the castle": castle
castle = ->
do cs
write "You walk to the castle and go inside to "
write "look at the treasury door."
# If you found the key, then you get to unlock the door.
if found_the_key
menu
"Unlock the door": treasure_room
# If you haven't found it yet...
if not found_the_key
write "If only you had the key, you might be able to save the kingdom..."
menu "Search for the key": under_the_hidden_tree
treasure_room = ->
write "The key fits! You open the door, and among "
write "the treasure you see a Crown..."
# Start the game
do castle
sphinx_start = ->
do cs
write "You've found the Sphinx! "
write "A huge lion with the head of a person, she looks at you and growls:"
write "\"You can only pass if you can answer my riddle. Do you want to hear it?\""
menu
"Say \"Yes, tell me!\"": sphinx_riddle
"Run away!": run_away
sphinx_riddle = ->
write "She asks: \"What creature has one voice, but first walks on four feet, then"
write "on two feet, and finally on three feet?\""
write "Hmmm....."
menu
"Say \"The answer is a caterpillar!\"": sphinx_wrong
"Say \"The answer is the Nile River!\"": sphinx_wrong
"Say \"The answer is a person!\"": sphinx_right
"Say \"The answer is nothing!\"": sphinx_wrong
sphinx_right = ->
write "The sphinx frowns and replies: "
write "\"Yes. A person walks on four feet when they crawl as a baby, "
write "then on two as an adult,"
write "and finally on three when they are old and walk with a cane. "
write "But how did you know...? "
write "Very well, you may enter.\" "
sphinx_wrong = ->
write "The sphinx frowns and replies:"
write "\"No, that is wrong. You may not enter the city.\""
menu
"Say \"Wait, wait, let me try again!\"": sphinx_riddle
"Run away": run_away
run_away = ->
write "You flee!"
# Start the game
do sphinx_start
