Sub topic 2
Simple Rules as Building Blocks
Assume you know loops, functions, dictionaries, and sets. You should also be familiar with a game loop that updates game state in discrete ticks or turns. This chapter uses those ideas to show how small rules can create behavior that no single rule describes.
Start with rules, not events
A scripted system describes important outcomes directly:
- “When the player presses the switch, open the gate.”
- “At 30 seconds, spawn an enemy.”
- “If the player enters this room, start the ambush.”
These events are easy to understand, but each new situation often needs another special case. An emergent system describes reusable conditions instead:
- Fire spreads to a neighboring flammable tile.
- A unit attacks a nearby enemy.
- A resource produces income each turn.
- A door opens when its power value is above zero.
A rule is a small statement that changes state when its condition is true. State is the information that describes the current game, such as positions, health, terrain, or inventory.
The important design choice is to keep rules general. A rule should refer to properties such as flammable, wet, or nearby, rather than to one named object or one exact story event.
Use local rules as building blocks
A local rule examines a small part of the state. It might inspect one cell and its neighbors, two units within attack range, or one player's current resources.
Local rules are useful because they compose. The same rule can apply in many places and combine with rules created for other purposes.
Consider a tile system:
| Tile property | Example rule | Possible result |
|---|---|---|
| `flammable` | Fire ignites a neighboring flammable tile | Fire spreads |
| `wet` | Water removes fire from a neighboring tile | Fire stops |
| `solid` | A solid tile blocks movement | Routes change |
| `conductive` | Energy passes through a neighboring conductive tile | Machines activate |
No rule needs to know the complete level. It only needs the properties of the tiles around it. A level designer can then create new situations by arranging tiles with different properties.
FLAMMABLE = {"grass", "wood"}
NEIGHBORS = ((1, 0), (-1, 0), (0, 1), (0, -1))
def spread_fire(grid):
"""Return a new grid after one fire-spread step."""
height = len(grid)
width = len(grid[0])
result = [row[:] for row in grid]
for y in range(height):
for x in range(width):
if grid[y][x] != "fire":
continue
result[y][x] = "ash"
for dx, dy in NEIGHBORS:
nx, ny = x + dx, y + dy
inside = 0 <= nx < width and 0 <= ny < height
if inside and grid[ny][nx] in FLAMMABLE:
result[ny][nx] = "fire"
return result
grid = [["fire", "grass", "grass", "water", "grass"]]
for _ in range(3):
grid = spread_fire(grid)
print(grid)The function contains no special case for “water stops fire.” Water stops fire because "water" is not in FLAMMABLE. A new nonflammable tile, such as "stone", also blocks the spread without changing the function.
Separate rules from content
A rule defines behavior. Content supplies the things that behavior acts on.
For example, the fire rule can operate on a small test row, a forest map, or a ship made of wooden rooms. The rule stays the same while the content changes.
This separation gives you more combinations:
- Flammable grass beside water creates a firebreak.
- Flammable wood beside oil creates a fast-burning area.
- A solid wall blocks movement but may still conduct energy.
- A wet wooden tile can resist fire while remaining solid.
When properties are independent, designers can combine them without writing a new rule for every combination. This is sometimes called orthogonal design: each rule or property addresses a separate concern and can intersect with others.
Track shared state carefully
Emergence requires rules to affect state that other rules can observe. If fire changes a tile to ash, a movement rule may later treat that tile as empty. If a machine consumes fuel, an economy rule may produce less power on the next turn.
The order of updates matters. A reliable game loop often reads the old state, calculates all changes, and then applies them together. This prevents one object processed early in a loop from receiving an unfair advantage over an identical object processed later.
Start with a few rules whose effects are easy to observe. Add complexity only when a playtest shows that the current system cannot produce a desired situation. Every added rule increases the number of interactions you must test.
Exercise
A fire rule ignites every neighboring tile whose type is in FLAMMABLE. What happens if you add "oil" to FLAMMABLE without changing the rule?
Show answer
Oil becomes part of the existing fire behavior. Fire can spread into oil, so the rule creates a new interaction through content rather than a special-case branch.