Activity: User Input and String Logic
Logical expressions like ||logic:if||
and ||logic:else||
can be used for much more than comparing numeric values. Data type values (such as Strings) can be compared and used to change the way the programs we write behave depending on the different conditions.
Some of the earliest computer games, text-based adventure games, used only text based responses to have the player interact with the story. The game prompts the user with a question, accepts the user’s answer, and calculates what response should be provided.
In this activity, students will:
||game:ask||
||game:ask for string with text||
- Use Boolean logic with Strings
Concept: User interaction
Allowing users to interact with your code is an important step in making an interesting and enjoyable game. Logical expressions play an important part in making your code react to that user input, even with something as simple as a “yes or no” question.
Example #1: Asking a question
- Review the code below
- Create the sample code and run the code
- Identify what condition makes you win (and what that means about what
||game:ask||
turns into)
if (game.ask("Do you want to win?")) {
game.splash("You win!")
}
In this simple game, the only options are to press ||controller:A||
to win, or ||controller:B||
to do nothing. This illustrates two important concepts:
- Built in methods can return boolean values, allowing us to easily create logical tests in our code.
- Tests can be based off user input - in this case, which button the user pressed.
Student Task #1: Option for failure!
- Start with the code from example #1
- Add an
||logic:else||
branch - use||game:splash||
to say “Bye” - Add an
||logic:else if||
statement which provides an “option for failure.” Use a||game:ask||
that asks “Do you want to lose?” - Make sure the player only gets the “You lost!” message if they respond “OK” to the prompt from step 3
Concept: Text input
Beyond asking questions with a binary response (for example, “yes or no” or “true or false”), we can request input from users and keep track of that information to enhance the player’s experience. We could ask for a user name and display the name in later in the game, such as in a leader board or a welcome message as in example #2.
Example #2: Taking in a user name
- Review the code below
- Create the sample code and run the code
- Identify how the users input affects the game
let name = game.askForString("What is your name?")
game.splash("Hello " + name + "!")
Prompts for names, like above, allows games to be more personal. The prompts can also enable users to make choices during game play, or make guesses from clues to solve a puzzle, or to use a password.
Student Task #2: Making a (secret?) password
- Start with the code from example #2.
- Create a new variable (
password
) - Set
password
to||game:ask for string||
with the prompt text “What is your password?” - Create an
||logic:if else||
block - Use the
||logic:=||
block to compare the ‘password’ from user input with the string “Arcade” - If those two are the same,
||game:splash||
“login successful” - Otherwise,
||game:splash||
“login failed” - Challenge: use the
||logic:or||
block to also compare your stored password with “*******“ and accept the password if the user’s input is equal to either “*******“ or “Arcade”
What did we learn?
- In task #2, you implemented a basic “password checker.” What is one possible downside of the way that the code was implemented?
- List at least 2 ways in which user input could be used in writing a text based game.