one keyboard snapshot per frame

junior grug (level 1 of 6)explanationteaches: key-snapshot, key-directionneeds: game-aggregate

alreadyYou know the Game value holds the round and that one frame can run more than one simulation step.

hereWhy the keyboard is sampled once per frame into one Input value that every simulation step reads.

Page: one keyboard snapshot per frame

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.

A stone keyboard with one key pressed; a single thick arrow crosses a dashed frame boundary and ends at a bone slip titled Input holding three flag boxes
One read crosses the frame boundary; the snapshot holds three flags and nothing else.

This page teaches that the keyboard is sampled once per frame into one Input value, so every step inside the frame plays the same input.

why

The input reader samples the keys once and returns a snapshot. read_input at input.ts:13#read_input looks at the keys one time and returns a typed object with three flags: left up, left down, restart. main.ts:8#read_input calls it once before advance, and the whole frame runs on what it returned. direction_of at input.ts:21#direction_of folds the two movement flags into one number (up gives minus one, down gives plus one, both together give zero), and update.ts multiplies that number by paddle speed and the fixed step. Input knows nothing about positions; it imports no geometry from the game.

The restart flag rides a different check. The press check at input.ts:17#pressedge fires on the press edge, so holding R clears the score once per press.

Three uneven frame slabs pour their time through arrows into a row of equal slices; a fire cone marked cap stands over a torn tear at the row's right end where the tail of a long pause is dropped
One frame can hold several fixed slices; a key read inside each slice would sample one press more than once.

what breaks

Reading keys inside step would sample the keyboard again for every catch-up step the accumulator holds. A quick tap could move the paddle two steps on a slow frame and one step on a fast frame, which is the same monitor-dependent behaviour the fixed step exists to remove.

Using the held-key check for restart would clear the score on every frame the key stays down. The separate press check gives one restart per press.

where it fits

main.ts calls read_input before advance. update.ts receives the snapshot and reads no keys. draw.ts never sees it.

try it

Hold W and S together and watch the left paddle stay still. Press R once and watch both scores return to zero. The proof prints the decisive line, input.ts:13#read_input.

Run the source check from the repository root:

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

run grep -n 'export function read_input' input.ts

It prints:

13:export function read_input(keys: KeyReader): Input {

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

reference: what the snapshot can and cannot say

read_input at input.ts:13#read_input builds the value from exactly left_up, left_down and restart. It cannot carry a key repeat count, a mouse position or a timing: the Input type has no such fields, and read_input builds it from three key reads in one expression.

The two movement flags may both be true, which is why direction_of exists: both held keys cancel to a zero direction at input.ts:21#direction_of, and the paddle stands still for that frame. One flag alone gives the paddle its full speed times the step length.

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: one keyboard snapshot per frame"]
  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.

Useful Resources

Outside material, checked when this school was built.

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 · When does read_input look at the keyboard?

try first, then show answer

3. Once per drawn frame

why

main.ts:8#read_input calls the reader once, then passes the snapshot to advance. Several steps can share one snapshot.

level 5 of 6Q2 · read_input swaps the press-edge check for the held-key check. What breaks?

try first, then show answer

1. The score clears on every frame the key stays down

why

The press-edge check at input.ts:17#pressedge fires on the press edge. The held-key check at input.ts:15#UP reads the key on every frame it stays down, so the score would clear again on each frame of the hold instead of once per press.

level 1 of 6Q3 · Both W and S are held. What does direction_of return?

try first, then show answer

3. Zero

why

direction_of at input.ts:21#direction_of subtracts for up and adds for down. Both flags cancel to zero, so the paddle stays put.

level 3 of 6Q4 · direction_of returns zero because both keys are held. What does the frame do?

try first, then show answer

3. The paddle stands still

why

move_paddles at update.ts:43#keypair multiplies the direction by PADDLE_SPEED and the step. A zero direction makes that product zero, so the paddle stands still for the frame; one held key gives the paddle its full speed times the step length.

level 3 of 6Q5 · How many flags does the Input value carry?

try first, then show answer

3

why

read_input at input.ts:13#read_input builds the value with exactly three flags: left_up, left_down and restart. No other key reaches the snapshot.

level 5 of 6Q6 · What breaks if step called read_input itself instead of taking the snapshot?

try first, then show answer

3. A tap can move the paddle a different number of steps on a slow frame than on a fast one

why

main.ts:8#read_input reads the keyboard once before advance, and the catch-up loop in update.ts:11#advance spends the accumulator in steps. Reading keys inside those steps would sample the keyboard again for each one. A quick tap can move the paddle two steps on a slow frame and one on a fast frame, which is the monitor-dependent behaviour the fixed step removes.

level 4 of 6Q7 · One clerk reads the ticket at the start of a shift and hands it to every worker. A teammate lets each worker read again inside the shift. Name the Pong value that was the one shared reading.

try first, then show answer

Input

why

read_input at main.ts:8#read_input returns one Input value before advance, and the loop at update.ts:11#advance passes that same value to every step. A fresh read inside the loop would give the steps different flags. The shared reading is the three-flag Input value built at input.ts:13#read_input.