Modularity in Game Development
- 105Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What is modularity in programming?
Breaking a large problem or program into smaller, related components that can be worked on separately.
Or swipe the card
105 cards
What is modularity in programming?
Breaking a large problem or program into smaller, related components that can be worked on separately.
How could a cozy role-playing game be decomposed into major activity categories?
Into categories such as farming, fishing, mining, and crafting.
How can the farming component of a game be broken down further?
Into specific actions such as planting, fertilizing, watering, harvesting, and trampling.
Why is solving one subproblem at a time easier than building an entire game at once?
Each smaller task is easier to understand and tackle in isolation than the larger system containing many interacting parts.
What is an example of a small farming subproblem that could be implemented independently?
Writing a harvest task that returns a random number of vegetables based on how well the player cared for the plant.
How does modularity support collaboration between programmers?
Different programmers or teams can own separate components, such as farming and fishing, or separate tasks within one component.
Why do modular components make testing and debugging easier?
A small, focused component is easier to check and diagnose than an entire large program.
How can testing components individually increase confidence in a complete program?
If the individual components have been tested and work, there is more reason to expect the combined program to work.
How does organizing code by task improve readability?
It makes related instructions easier to locate and work with than one large, unorganized block of code.
What is the usual relationship between modules and functions in an organized program?
Modules group related functionality, and functions within them implement specific tasks.
What is a function?
A named block of code that performs a specific task.
How do functions reduce repeated code?
Define the task once, then call the function whenever the task is needed instead of rewriting its instructions.
What are the typical inputs and output of a function?
A function can take input values, operate on them, and return an output value.
How do input parameters make a function more general-purpose?
They let the same task work with different values, such as rounding different numbers or choosing different potion colors.
What keyword begins a Python function definition, and what does it mean?
def, short for “define.”What elements make up a Python function header?
The
defkeyword, the function name, parentheses containing any parameter names, and a colon.What are parameters in a function definition?
Names for the inputs a function accepts; they act as variables accessible within the function.
What is the function body, and how is it marked in Python?
The instructions that perform the function’s task; they are indented under the function header.
What does the
returnkeyword do?It ends the function and sends the value following it back as the function’s output.
How do arguments differ from parameters?
Arguments are the values supplied in a function call; parameters are the names in the function definition that receive those values.
What is the usual order for defining and using a function in a Python program?
Define the function before the program reaches a call to it, so Python has learned the task before trying to execute it.
How does a function call use its arguments?
It binds each argument to its corresponding parameter; the first argument is assigned to the first parameter, and so on.
How can a
drink_potionfunction simplify an adventure game?Replace repeated potion conditionals with calls that pass in the selected potion color and receive the affected health points.
Why is a single
drink_potiondefinition easier to update than duplicated potion conditionals?A new potion type, such as a blue potion, can be added in one place instead of updating every repeated conditional.
Why should a function return a consistent data type?
Callers can then know what type of value to expect; for example, a rank function should return strings for numeric ranks as well as face cards.
What does it mean for a function definition to be executed?
Python learns the function’s instructions and associates them with its name; it does not run the function body at that point.
Conceptually, what does Python store when it encounters a function definition?
The function’s instructions, its name, and information about how many parameters it takes.
What happens when Python reaches a function call?
It finds the function’s instructions, sets up the call’s inputs, executes the body, and returns to the call site with the result.
What is a stack frame?
An isolated region of short-term memory used for a particular function call and its local variables.
What happens to arguments when a function call begins?
Their values are copied into the new stack frame and assigned to the corresponding parameter names.
What happens to a function’s stack frame when the function returns?
The frame is popped off and its local memory is cleared; the return value is passed back to the call site.
What happens to a returned value if a function call’s result is not stored or used?
The function still computes and returns it, but the value is discarded after the call.
What is variable scope?
The region of a program in which a variable can be accessed.
What is global scope?
The scope for names defined at the top level of a program; those names are available throughout the program, subject to scope lookup rules.
What is local scope?
The scope belonging to a function call, where its parameters and variables are stored in that call’s stack frame.
What happens to local variables when a function call finishes?
They disappear when the function’s stack frame is cleared.
Why does assigning to a variable inside a function not ordinarily update a same-named global variable?
The assignment creates or changes a variable in the function’s local scope, separate from the global variable.
What is the reliable way for a function to communicate a calculated result to its caller?
Return the result, then use or assign the returned value in the caller’s scope.
What does Python’s
globalkeyword allow, and why should it usually be avoided?It allows a function to refer to a global variable for assignment, but relying on global state can cause name conflicts and make dependencies harder to understand.
How does Python search for a name used inside a function?
It searches the local scope first, then enclosing scopes such as global scope; if the name is not found, it raises a
NameError.Why is passing a needed value as a function parameter safer than relying on a global variable?
The function’s callers must explicitly provide the required information, rather than depending on a global variable that may not exist or be set correctly.
What is a useful way to think about a function’s scope boundaries?
Treat a function like an input-output machine: pass information in through arguments and send information out through its return value.
What happens when one function calls another function?
The computer pauses the caller, starts the called function in a new stack frame, and resumes the caller when the inner function returns.
What happens to the caller’s stack frame during an inner function call?
It remains in place with its state preserved because the caller has not finished executing.
How are multiple nested function calls managed?
Each call adds a new stack frame on top; each return removes the top frame and resumes the function below it.
How can information pass through a chain of nested function calls?
A function can receive information as an argument and pass it along as an argument to another function.
What problems can arise when one large function performs several different tasks?
It can be harder to read, test, and reuse because its name may not reveal all its responsibilities and its behavior is difficult to isolate.
In a stealth game, what determines the volume of a player’s footsteps?
The player’s movement type and the environment—for example, crouching in grass is quiet, while running on metal is loud.
How can the calculation of movement-based footstep volume be factored into a function?
Use a function that takes a base volume and movement type and returns the adjusted volume.
How can environment-based volume adjustment be factored into a function?
Use a function that takes a volume and an environment type and returns the adjusted volume.
How can a wrapper function calculate a player’s footstep volume?
It can call the movement-volume function and then the environment-volume function, returning the resulting volume.
Why is it useful to separate a detection check from the volume calculation?
A separate function can take a volume and return a detection Boolean, making the check independently reusable.
How can splitting stealth calculations into small functions improve testing?
Each focused function’s output can be checked directly, such as verifying that crouching lowers volume and running raises it.
How can small functions make a new voice-detection feature easier to build?
Reuse the environment adjustment and pass the calculated voice volume to the independent detection function.
What is a Python module?
A Python file with a
.pyextension that contains code, often grouped around related functionality.How do you make functions in another module available to a program?
Import the module, then refer to its functions with the module name as a prefix, such as
arcade.get_score.What is a program’s entry point?
The single file chosen to begin execution; by convention, a Python program’s main file is often named
main.py.How can code in a module be executed as part of a larger program?
It must be reached through the chain of execution that starts at the program’s entry point, for example through imports and function calls.
What does importing a module do conceptually?
It executes the module’s top-level instructions and makes its defined names available under that module’s namespace.
What is a module namespace?
A scope associated with a module that contains the names defined in that file.
Why must a function from an imported module usually be prefixed with the module name?
The function is stored in the module’s namespace, so the prefix tells Python which scope to search.
How do module namespaces help programmers avoid name conflicts?
Different modules can define the same function name because each name belongs to a separate module scope.
Why should modules generally avoid top-level statements that produce output?
Top-level statements run when the module is imported, which can create unexpected output and make its source difficult to trace.
What are three benefits of organizing functions into modules?
Modules make code easier to navigate, allow related functionality to be reused in multiple places, and help programmers focus on one part of a program at a time.
What are the two parts of a playing card in the described card program?
Its suit and rank.
How can a function draw a hand containing a chosen number of cards?
Take the hand size as a parameter, use a loop to draw that many cards, accumulate them, and return the hand.
How can a newline character make a returned hand of cards easier to read?
The
\ncharacter represents a line break, so cards can be displayed on separate lines.How can a card-drawing function choose a suit with equal likelihood?
Randomly choose one of four options, each corresponding to hearts, diamonds, spades, or clubs.
How many possible ranks does a standard playing card have, and what are they?
Thirteen: ace, two through ten, jack, queen, and king.
In the described rank-generation approach, how are random values mapped to face ranks?
The values 1, 11, 12, and 13 map to ace, jack, queen, and king; other values represent their numeric ranks.
Why should a rank-generation function convert numeric ranks to strings before returning them?
So every possible rank has the same return type and can be combined consistently with string values.
How can a card-drawing program support games that exclude face cards?
Use a Boolean parameter to choose whether face cards are enabled; when disabled, generate only numeric ranks from two through ten.
How can an option such as enabling face cards reach the rank-generation function?
Pass it through each relevant function: from the hand-drawing function to the card-drawing function, then to the rank function.
What limitation does the described random card-drawing approach have regarding deck state?
It does not track cards already drawn, so the same card can be drawn more than once.
When might allowing duplicate cards be useful, and when might it be a bug?
It can suit games using multiple decks or allowing card counting to be avoided; it is a bug when rules require exactly one of each card.
What are the rules of the described version of Nim?
Players take turns removing one or two stones from a pile; whoever removes the last stone loses.
How are the user and computer turns separated in the Nim program?
Functions in a
stonesmodule handle choosing stones, while the main program manages the game loop and pile.How does the Nim program validate the user’s stone choice?
It repeats the input prompt in a loop until the user enters one or two.
What condition controls the main Nim game loop?
The loop continues while the pile contains more than zero stones.
How does a Boolean turn variable help alternate Nim players?
One Boolean value selects the user’s turn and the other selects the computer’s; the value is switched after each turn.
Why does the Nim game check whether stones remain before starting another turn?
The game must end as soon as the pile is empty, rather than giving the other player an extra turn.
How can the program determine who took the last stone after the loop ends?
Because the turn variable is switched at the end of each iteration, its value after the loop indicates the other player took the last turn.
What is the purpose of a separate
showfunction in the Nim program?It takes the number of stones and returns a picture of the pile, keeping display changes separate from the main game logic.
Why might the starting pile size be randomized?
A fixed starting size can make repeated games predictable; randomizing it makes the game less repetitive.
What losing position does the described Nim strategy try to leave for the opponent?
A pile of four stones, from which the opponent must take one or two and leave the next player in a favorable position.
According to the described strategy, how many stones should the computer take from piles of one, two, or five?
One stone.
According to the described strategy, how many stones should the computer take from piles of three or six?
Two stones.
Why should the computer take one stone from a pile of five?
That leaves four stones for the opponent, a position the strategy aims to create.
Why should the computer take two stones from a pile of six?
That leaves four stones for the opponent, a position the strategy aims to create.
What does the described Nim computer do for pile sizes not covered by its listed cases?
It chooses a number randomly by default; the described algorithm is not fully optimized for every pile size.
What is a unit test?
A test of one unit of functionality at a time, usually a function.
How does a unit test express a function’s requirements?
It calls the function with specified inputs and asserts that the result matches an expected value.
When does a unit test pass, and when does it fail?
It passes when the actual result matches the expected result; it fails when they differ.
How can automated unit tests protect a codebase when programmers make changes?
Teams can run tests after changes and block submission until related tests pass.
Why are unit tests described as a living contract?
They encode expected behavior; a change must either preserve that behavior or update the tests when the requirements intentionally change.
Why is testing a function with only one input case often insufficient?
A function that always returns the same value could pass one test without implementing the intended logic.
What is code coverage in the context of choosing unit tests?
A goal of having tests execute every possible path through a function, using representative inputs for different cases.
Which input classes should be tested for the described route-score function?
Inputs with fewer than or equal to two trains, three trains, four trains, and greater than or equal to five trains.
What should programmers do when a bug reveals a missing test case?
Add a test for that specific failure so the same bug is less likely to recur.
Which Python testing frameworks are mentioned?
The
unittestmodule and thepytestmodule.What information can a failing test report provide?
The name of the failing test and the assertion that failed, including the actual result and expected result.
Why is memorizing test-framework syntax less important than identifying good test cases?
Test code often contains repeated boilerplate, while choosing cases that exercise meaningful behavior is the key testing skill.
What is a practical testing habit for a beginner who is not automating unit tests?
Call each new or changed function with several different arguments and check that its results make sense.
When using a stack trace to find the source of an error, where should you generally focus first?
The bottom, where the trace identifies the actual error and the line that caused it; the rest shows the path of execution.
How can testing a racing game’s display function in isolation help diagnose missing cars?
Try cases where either car is ahead, both share a position, and a car has passed the track’s end to identify which situations fail.

Ask anything
We've read this whole study set, so questions about it get the best answers.