draw reads, update writes

junior grug (level 1 of 6)explanationteaches: draw-only, score-textneeds: state-fields

alreadyYou know the Game value carries the round and that main.ts drives draw and update.

hereWhy draw reads the same state without changing it, and how the score text is produced from that state.

Page: draw reads, update writes

Job: explanation

Level: junior grug (level 1 of 6)

Source lines:

why grug give these 3 tool notes why grug give the proof why grug give the grill why grug give the why under an answer
explanation

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

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
Two benches, one shared Game slab.

This page teaches the read/write split that keeps the simulation true: draw returns commands for the renderer, reads what update produced, and changes no field.

why

draw_game at draw.ts:9#draw_game returns a typed list of drawing commands: the net, paddles, ball, scores and hint. The functions that paint state take a value from Game and write no field. draw_net and draw_hint take no argument, and score_text returns a string for the renderer. The file changes no positions and no scores. main.ts:10#draw_game calls it after advance, so the frame shows the state update just produced; the slab in the picture is read at the right bench at exactly the moment the left bench finished writing it.

The score needs text for the renderer. score_text at draw.ts:44#textout returns a string for each digit, and the catch-all case at draw.ts:46#over prints a plus sign after nine. That conversion lives in the draw file because the screen needs text and the state holds a number.

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 dashed arrow leaves the crate for the screen; nothing in this drawing writes back from the read side.

what breaks

A draw function that advances the ball would make the simulation run faster on a machine with a higher frame rate. The same input would give different games, and a reader could not find the physics by reading update.ts.

Passing a renderer text type into the state would tie the game to the renderer. Keeping the score as a number lets update add points with plain arithmetic.

where it fits

main.ts calls draw_game after advance in the same frame function. one value holds the round describes the values this page reads. Nothing in this page feeds input back into update.

try it

Change the paddle colour in draw_paddle and rebuild. The game plays the same way because colours never enter the state. The proof prints the decisive line, draw.ts:9#draw_game.

Run the source check from the repository root:

grep -n 'export function draw_game' draw.ts
source anchor: this line is in the file

run grep -n 'export function draw_game' draw.ts

It prints:

9:export function draw_game(game: Game): DrawCommand[] { // draw_game: read the round, paint the frame

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

reference: the read-only side of the boundary

draw_game takes Game like advance does, but every command it returns is a fresh value: draw_paddle takes a Paddle, draw_ball takes a Ball, and the score number travels to the screen through score_text's returned string. A read side this thin is why the boundary holds: nothing in draw.ts writes a field of the game.

The frame ordering in main.ts is the other half of the contract: sample input, advance, draw. A frame that drew before advancing would show a state one step older than the input just spent, which is the preview-equals-commit failure the loop's order prevents.

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.

learn first: one value holds the round

goes on to: who owns what

flowchart LR
  A0["one value holds the round"]
  H["you are here: draw reads, update writes"]
  B0["who owns what"]
  A0 --> H
  H --> B0

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 5 of 6Q1 · What does draw_game change in the game?

try first, then show answer

1. Nothing in the game

why

draw_game at draw.ts:9#draw_game returns drawing commands and writes no field of Game.

level 1 of 6Q2 · Why can drawing run at a different rate from physics?

try first, then show answer

2. It only reads the state after update

why

main.ts:9#clock changes state through measured time, and main.ts:10#draw_game paints the result. Drawing can repeat a frame without moving the ball.

level 1 of 6Q3 · What type does score_text return for the renderer?

try first, then show answer

1. A string

why

score_text is declared at draw.ts:44#textout with a string return. The renderer takes text; the state keeps a number.

level 1 of 6Q4 · What is printed for a score above nine?

try first, then show answer

3. A plus sign

why

The catch-all case at draw.ts:46#over returns a plus sign. The ten digit cases cover zero through nine.

level 3 of 6Q5 · main.ts calls draw_game before advance by mistake. What does the screen show?

try first, then show answer

3. The state one step older than the input just spent

why

main.ts:9#clock runs before main.ts:10#draw_game, so the frame shows the state the input just spent. Swapping the calls paints the previous step's state, one step behind the keys the player is holding.

level 5 of 6Q6 · draw_game starts changing the ball position. What symptom follows?

try first, then show answer

2. The frame rate changes the simulation speed, so two machines play two different games

why

main.ts:10#draw_game paints once per rendered frame, and draw_game at draw.ts:9#draw_game writes no field. A write inside it makes the simulation speed follow the frame rate, so two machines at different refresh rates play two different games. That is what draw-rate guards against.

level 4 of 6Q7 · A grug at the reading bench is handed a pen and writes the ball position once for every frame he paints. Name the Pong procedure that becomes a second writer of the ball.

try first, then show answer

draw_game

why

The loop calls advance before draw_game at main.ts:10#draw_game. draw_game at draw.ts:9#draw_game returns commands for the paddles, ball and scores without writing a field. A write there runs once per painted frame, so the ball would move at the frame rate instead of the step rate that advance sets at update.ts:11#advance.