Sub topic 1
What Emergent Gameplay Is
Prerequisites
This chapter assumes you can write Python loops, functions, dictionaries and sets. You should also understand the basic game loop idea, where state changes in discrete ticks or turns. You do not need prior knowledge of cellular automata.
What emergent gameplay is
Emergent gameplay is behavior that no single rule describes, but that appears when several simple rules act on shared state. The designer writes the rules. The rules then produce situations the designer never scripted.
A scripted behavior is written out directly, such as "when the player enters the room, the door opens." An emergent behavior has no line of code that describes it. It comes from many rules interacting over time.
Simple rules as building blocks
A simple rule is a small, local statement such as "fire spreads to adjacent grass" or "a unit with low health retreats." Each rule is easy to read and test alone. The complexity comes from combining them. A handful of one-line rules can produce many distinct situations when they act together.
Local interaction and shared state
Shared state is the data that all rules read and write, such as a grid of tiles or a set of unit positions. Local interaction means each rule looks only at a nearby part of that state. Large patterns grow from many small local actions, and no single rule needs to know that the pattern exists.
A reference case: Conway's Game of Life
Conway's Game of Life is a cellular automaton, which is a grid of cells that are each either alive or dead. On every tick, each cell counts its eight neighbors. A dead cell with exactly 3 live neighbors becomes alive. A live cell with 2 or 3 live neighbors stays alive. All other cells end up dead.
from collections import Counter
def life_step(live):
"""Advance Conway's Game of Life one generation.
live is a set of (x, y) cells that are alive."""
counts = Counter(
(x + dx, y + dy)
for (x, y) in live
for dx in (-1, 0, 1)
for dy in (-1, 0, 1)
if (dx, dy) != (0, 0)
)
return {
cell
for cell, n in counts.items()
if n == 3 or (n == 2 and cell in live)
}
glider = {(1, 0), (2, 1), (0, 2), (1, 2), (2, 2)}
state = glider
for _ in range(4):
state = life_step(state)
print(sorted(state)) # the same glider shape, shifted by (1, 1)The code contains two counting rules and no movement logic. Yet the five-cell glider shape reappears after four ticks, shifted by (1, 1). The movement is emergent.
Exercise
In the life_step code, suppose the survival condition (n == 2 and cell in live) is removed, so only n == 3 keeps a cell alive. What happens to the glider?
Show answer
Live cells with two neighbors would die on the next tick. The glider would not keep its shape and would break apart instead of moving intact.