draw reads, update writes
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 answerthis page says why the code has this shape and what a reader must be careful of.
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.
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
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
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.
try first, then show answer
1. Nothing in the game
draw_game at draw.ts:9#draw_game returns drawing commands and writes no field of Game.
try first, then show answer
2. It only reads the state after update
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.
try first, then show answer
1. A string
score_text is declared at draw.ts:44#textout with a string return. The renderer takes text; the state keeps a number.
try first, then show answer
3. A plus sign
The catch-all case at draw.ts:46#over returns a plus sign. The ten digit cases cover zero through nine.
try first, then show answer
3. The state one step older than the input just spent
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.
try first, then show answer
2. The frame rate changes the simulation speed, so two machines play two different games
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.
try first, then show answer
draw_game
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.