Computer Science Grade 7 20 min

Creating Interactive Stories

Creating Interactive Stories

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Design a story that uses variables to track player state, like score or health. Implement an inventory system using a list to store and check for items. Create reusable story scenes or events by defining and calling functions. Construct complex choices using nested conditional statements (if/else inside another if/else). Use boolean flag variables to track whether a key event has happened in the story. Plan an interactive story by mapping out variables, functions, and branching paths before coding. Ever played a game where the story remembers you found a secret key and lets you open a special door later? 🔑 Let's learn how to give our stories a memory! In this lesson, we'll move beyond simple branching stories. You'll learn advanced techni...
2

Key Concepts & Vocabulary

TermDefinitionExample State VariableA variable that stores information about the current status or 'state' of the story. It acts like the story's memory.A variable named `player_health = 100` tracks how much health the player has. If they fall into a trap, the code might change it to `player_health = 80`. InventoryA list or array that holds all the items a player has collected during the story.An inventory list could look like this: `inventory = ["rusty key", "glowing mushroom"]`. We can add or remove items from this list. FunctionA named, reusable block of code that performs a specific action. You can 'call' a function whenever you need to run that code.A function `show_forest_path()` could contain all the code to describe the forest and ask t...
3

Core Syntax & Patterns

The State-Tracking Pattern 1. Initialize variables at the start. 2. Modify variables inside choices. 3. Check variables to decide outcomes. Use this pattern to give your story memory. Create variables for things like score, health, or lives at the very beginning of your program. Inside your `if/else` blocks, change these variables based on the player's actions. Use `if` statements to check their values to unlock new paths or endings. The Inventory Management Pattern 1. Create an empty list: `inventory = []` 2. Add items: `inventory.append("item")` 3. Check for items: `if "item" in inventory:` Use this pattern to let players collect and use items. Start with an empty list. When the player finds something, use the `append` command to add it. To see if...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
A player is in a 'Haunted Library' scene. If they have a 'lantern' (from inventory), they can read a book. Reading the book for the first time gives 10 score points and sets a flag `has_read_book` to True. If they try to read it again, they get a message saying 'You already read this.' Which logic best describes the implementation?
A.Check for the lantern. If present, add 10 to score. Set `has_read_book` to True.
B.Check for the lantern. If present, check if `has_read_book` is False. If it is, add 10 to score and set the flag to True. Otherwise, show the 'already read' message.
C.Check if `has_read_book` is True. If so, add 10 to score. Then check for the lantern.
D.Add 10 to score. Then, inside an `if` statement, check for the lantern and set `has_read_book` to True.
Challenging
Review this code for opening a chest: `if "gold_key" in inventory: if has_opened_chest = False: print("You get 100 gold!"); score + 100; has_opened_chest = True`. Which choice best identifies the multiple errors based on the tutorial's 'Common Pitfalls'?
A.The `if` statements are nested incorrectly and `score` is not initialized.
B.It uses assignment (`=`) instead of comparison (`==`), and it doesn't actually update the `score` variable.
C.The inventory check is wrong, and the `has_opened_chest` variable should be a number.
D.It's missing an `else` statement, and the print message is too long.
Challenging
You are planning a story with a 'time travel' mechanic. An action in the 'Past' (like planting a tree) should change the 'Future' (a large tree now exists). What combination of advanced concepts is BEST suited to implement this?
A.single state variable for `score` and one function for the whole story.
B.An inventory list to hold 'time' and a nested conditional to check the year.
C.flag variable (e.g., `tree_planted = False`) and two separate functions (`visit_past()` and `visit_future()`) that both check the flag's state.
D.single, very long `if/elif/else` chain to handle all possibilities without using functions or flags.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Topics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.