one value holds the round

junior grug (level 1 of 6)explanationstatus: verifiedclass: source-factteaches: game-aggregate, state-fields

alreadyYou know what a game loop does and can read a small TypeScript type with named fields.

hereWhich fields live in the one Game value and why keeping the round in one place makes the rest of the game easier to reason about.

Page: one value holds the round

Job: explanation

Level: junior grug (level 1 of 6)

Source lines:

why grug give these 3 tool notes
explanation

this page says why the code has this shape and what a reader must be careful of.

A timber crate named Game with its lid lifted; two paddle slabs, a ball, a clock and a small flag float above it, each tied to the crate mouth by a dashed lift line
The round lifted out of its one container: each piece is a field of the same Game value.

This page teaches that the whole game state sits in one Game value, so a reader can see what survives between frames without following a global.

why

The game keeps its whole memory in one Game value. state.ts:6#aggregate declares that value: two paddles, a ball, an accumulator and a serve direction. init_game at state.ts:23#init_game builds it and returns it, so the starting position comes back as a return value and the frame loop owns it. The fields are plain numbers and flags: a paddle holds an x and a score, a ball holds a position, a velocity and a wait counter. That plainness is the point. update.ts changes one field at a time and draw.ts reads the same fields without owning them, because both receive that one object.

The picture is the same fact drawn out: nothing lives outside the crate. When a later lesson says “the ball's x”, the answer to “where is it kept” is always this one type.

Two timber benches face a shared paper slab named Game: at the left bench a grug writes on the slab along a heavy amber arrow fed by the Input slip; at the right bench a grug reads through a magnifier along a thin dashed arrow, and a fire arrow from his bench is crossed out and labelled no write
One slab, one writer and one reader: a split state would give them two places to look for the round.

what breaks

A module-level Game would let any file change the score at any moment, and a reader would have to trace writes across files to know what a frame sees. Splitting the round across several types would make a serve touch three values and leave no single place to look for the truth.

With a zeroed Game and no init_game, the ball sits at the top left with zero velocity and a wait count of zero, so play never starts. The initialiser centres both paddles and calls serve, which centres the ball and sets the 24-step wait.

where it fits

This page is the floor of the school. one keyboard snapshot per frame produces a value that goes in, time arrives in equal slices changes this one, and draw reads, update writes reads it. The decision behind the shape is written in why the state is one plain object.

try it

Open the type and count the fields that change during a round. The proof prints the decisive line, state.ts:6#aggregate.

Run the source check from the repository root:

grep -n 'export type Game' state.ts
source anchor: this line is in the file

run grep -n 'export type Game' state.ts

It prints:

6:export type Game = { // aggregate: one value holds the whole round

The cited file state.ts ships with this site at the same path. Run the command from the school folder, or open state.ts.

reference: every field, and who moves it

Game at state.ts:6#aggregate carries five fields. left and right are Paddle values with an x, a y and a score. ball is a Ball with x, y, vx, vy and wait, where wait is the serve counter named at state.ts:5#waitfield. accumulator holds frame time not yet spent, and serving_to_right names the direction of the next serve.

Who moves what: advance in update.ts adds to the accumulator and spends it in steps. move_paddles writes the paddle y values. The collision passes in update.ts:62#hface and update.ts:73#vface write the ball. score_if_out in scoring.ts:4#goalcheck writes a score and calls serve. draw_game writes none of them.

reference: every connection to this page
where this fits

This is reference. The next page in the reading order is at the bottom of the page.

goes on to: one keyboard snapshot per frame, draw reads, update writes, time arrives in equal slices, why the state is one plain object

1 more onward page(s) are in the full path.

flowchart LR
  H["you are here: one value holds the round"]
  B0["one keyboard snapshot per frame"]
  B1["draw reads, update writes"]
  B2["time arrives in equal slices"]
  B3["why the state is one plain object"]
  H --> B0
  H --> B1
  H --> B2
  H --> B3

this badge records quiz answers with a difficulty from 1 to 6. it does not measure independence or responsibility.

grill: check what you know

Grug asks. Say the answer out loud, then tap. Grug remembers what you miss and asks again in the pop quiz.

the number is a question's difficulty from 1 to 6. a coloured chip marks a question that asks for a change, an effect or a judgment, declared in the quiz row. the rung badge records quiz answers: two right at a level and the levels below it, with a declared transfer question at level 5 or 6. it does not certify independence or responsibility. the questions are level 1 of 6 recall a fact; level 2 of 6 recall a detail; level 3 of 6 trace a path; level 4 of 6 trace a longer path; level 5 of 6 predict or judge; level 6 of 6 judge a transfer.

level 1 of 6Q1 · Where does one frame keep the ball position and both scores?

try first, then show answer

2. A single Game value

why

Game in state.ts:6#aggregate holds both paddles, the ball and the accumulator. A canvas texture holds pixels, and draw never writes scores.

level 1 of 6Q2 · What does init_game hand back to the frame loop?

try first, then show answer

2. A Game value with both paddles centred

why

init_game at state.ts:23#init_game places both paddles at the middle, calls serve and returns the value. It takes no arguments and returns no window handle.

level 4 of 6Q3 · The ball's wait field is deleted. Which behaviour disappears?

try first, then show answer

3. The centre pause after a goal

why

wait at state.ts:5#waitfield is the serve counter: serve writes it and each step counts it down, and start_round gives the ball its velocity once it reaches zero. Without the field, the centre pause is gone. The paddle's score lives in the paddle type, and the wall bounce in update.ts reads only the ball's y, so neither touches wait.

level 5 of 6Q4 · What happens if the game starts at zero values with no init_game?

try first, then show answer

2. Paddles and ball start at the top left corner

why

init_game sets both paddles and the ball before the loop. Without it, a zeroed Game leaves every coordinate at zero, so the top left corner is the starting place.

level 4 of 6Q5 · serving_to_right is deleted and nothing replaces it. What can the game no longer decide?

try first, then show answer

2. Which way the next serve travels

why

serving_to_right at state.ts:11#sideflag stores the direction of the next serve. serve writes it and start_round reads it to set the sign of vx. The wait length lives in the wait counter and the score belongs to the scorer, so neither needs the flag.

level 4 of 6Q6 · draw_game starts writing the ball position each frame. What does the one-value boundary lose?

try first, then show answer

1. The simulation stops being the only writer, so the same input can give different games

why

The type at state.ts:6#aggregate exists so update owns the writes and draw owns the reads. A draw function writing the ball position makes the frame rate a second input to the simulation, and the same keyboard input stops producing the same game.

level 4 of 6Q7 · A board game keeps its board, current player and move count in one record, and only its rules file writes that record. Name the Pong value that plays the same role.

try first, then show answer

Game

why

Game at state.ts:6#aggregate is the one value that holds the round. advance in update.ts writes its fields and draw_game in draw.ts reads them. A board game whose rules alone write one record has the same boundary: the record holds what a turn changes, and the renderer reads that same record.