Computer Science
Grade 5
20 min
List-Based Games
List-Based Games
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Design a 2D game board using a nested list.
Write a loop to search for a specific item within a list.
Modify a list during gameplay by adding or removing items.
Use a list to represent and manage a player's inventory.
Access and change specific elements in a nested list using double indices.
Combine loops and complex conditionals to create interactive game logic.
Have you ever wanted to build your own treasure map or a backpack for a game character that can actually hold items? πΊοΈπ Let's learn how lists can make that happen!
In this lesson, we'll explore advanced ways to use lists to build more exciting games. We will learn how to create game boards, search for secret items, and manage a player's inventory. These powerful skills will...
2
Key Concepts & Vocabulary
TermDefinitionExample
Nested List (or 2D List)A list that contains other lists as its elements. It's like having rows of boxes, where each box is another list.A tic-tac-toe board can be a nested list: `board = [ ['X', 'O', ''], ['O', 'X', ''], ['', '', 'X'] ]`
IndexThe position of an item in a list. Remember, computers start counting from 0, not 1!In the list `inventory = ['sword', 'shield', 'potion']`, the index of 'sword' is 0 and the index of 'potion' is 2.
Double IndexingUsing two sets of square brackets to access an item inside a nested list. The first index picks the inner list (the row), and the second index picks the item in that list (th...
3
Core Syntax & Patterns
Pattern: Searching a List
for item in myList:
if item == target:
// Found it!
Use a 'for' loop to check every item in a list. Inside the loop, use an 'if' statement to see if the current item is the one you're searching for.
Pattern: Accessing a Nested List
nestedList[row_index][column_index]
To get or change an element in a grid-like nested list, use two indices. The first selects the row (the inner list) and the second selects the column (the item within that inner list).
Syntax: Modifying Lists
myList.append(newItem)
myList.remove(itemToRemove)
Use the `.append()` command to add an item to the very end of a list. Use the `.remove()` command to find and remove the first occurrence of a specific item from the list.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A crafting recipe requires one 'wood' and two 'stone'. A player's inventory is `["stone", "wood", "stone", "iron", "stone"]`. When coding the logic to check and remove these items, what is a major challenge?
A.The computer can't count how many 'stone' items there are.
B.Removing one 'stone' might accidentally remove all of them with a simple command.
C.The logic must first confirm at least one 'wood' exists, then confirm the count of 'stone' is at least two, and then carefully remove only one 'wood' and exactly two 'stone's.
D.The list is too long for the computer to check.
Challenging
A list of high scores is `[250, 100, 500, 300]`. To sort this list from highest to lowest without a built-in sort command, a program might repeatedly step through the list, compare adjacent items, and swap them if they are in the wrong order. What is this logical process called?
A.An inventory system.
B.sorting algorithm.
C.random number generator.
D.conditional check.
Challenging
A game uses a binary list `[0, 1, 1, 0]` to represent four switches. To solve a puzzle, the player must arrange the switches to match the binary representation of the number 10. What should the final list look like?
A.`[1, 0, 1, 0]`
B.`[0, 1, 1, 0]`
C.`[1, 0, 0, 1]`
D.`[1, 1, 1, 1]`
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free