a goal changes one score, then waits

mid grug (level 2 of 6)explanationteaches: goal-line, serve-waitneeds: paddle-face

alreadyYou know the ball can cross a goal line and that the round lives in one Game value.

hereWhen the score changes, how the serve resets, and why the game waits before the next point.

Page: a goal changes one score, then waits

Job: explanation

Level: mid grug (level 2 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 panels: a court with a torn gap in the right wall where the ball has crossed, a score slip changing from 2 to 3 with the 3 in amber; then the ball resting at the centre above an hourglass marked 24 with a dashed arrow toward the left paddle
Two beats: the goal pays once; the serve resets and waits.

This page teaches that the goal check runs last and changes exactly two things, a score and the ball, so a point is paid once and the next round starts from a known place.

why

score_if_out at scoring.ts:4#goalcheck runs after both collision passes. The check at scoring.ts:9#WINDOW_W and the left edge decide which side missed. The scorer gains a point, and serve resets the ball. Because it runs last, a ball that a paddle face just saved can never score: the collision pass pushed it back inside before the goal line was consulted.

The reset has two phases. serve stores serving_to_right in state.ts and sets a wait counter; start_round at state.ts:44#start_round gives the ball its velocity once that count reaches zero. When the left side scores, scoring.ts:11#false passes a false flag and state.ts:46#leftbound gives the ball a negative vx, so it travels back toward the left paddle. A player gets a moment to find the ball before it moves.

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 score and the serve flag sit in the one Game value the goal check reads; a point changes that value once.

what breaks

Scoring before collision resolution would award a point to a ball that had just been saved by a paddle face. Counting a goal on every frame while the ball sits outside the window would give one serve a dozen points.

Serving straight into play makes the first frames hard to follow. A short wait gives a visible pause at the centre line and keeps the score change readable.

where it fits

step calls the scoring check last. one value holds the round describes the fields that change here. two collision passes, two answers explains the passes that run before it.

try it

Let the ball pass the right paddle and watch the left score change. The proof prints the decisive line, scoring.ts:4#goalcheck.

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

run grep -n 'export function score_if_out' scoring.ts

It prints:

4:export function score_if_out(game: state.Game): void { // goalcheck: runs after both collision passes

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

reference: the wait counter's arithmetic

SERVE_DELAY_STEPS at state.ts:21#24 is 24, and serve writes it into game.ball.wait together with a zero velocity and the centre position. Each step counts wait down by one; when it reaches zero, start_round at state.ts:44#start_round sets vx to the serve direction times BALL_SPEED and vy to BALL_SPEED * 0.35, then zeroes the counter.

The direction flag is the only memory of who missed. serving_to_right true sends the ball right; false sends it left, which is the leftbound comment at state.ts:46#leftbound. The flag also survives the restart key, which serves right again from advance.

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: two collision passes, two answers

goes on to: change one rule, follow the result

flowchart LR
  A0["two collision passes, two answers"]
  H["you are here: a goal changes one score, then waits"]
  B0["change one rule, follow the result"]
  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.

  • Pong - Wikipedia article

    Gives the original game's scoring and serve rules, the behaviour our goal check reproduces.

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 2 of 6Q1 · The ball leaves past the right edge. Who gains a point?

try first, then show answer

2. The left paddle

why

The right edge test at scoring.ts:9#WINDOW_W adds a point to the left score. A ball that leaves on the left adds a point to the right score.

level 2 of 6Q2 · When is the goal check made?

try first, then show answer

2. After both collision passes

why

score_if_out at scoring.ts:4#goalcheck is the last call in step. A paddle save has already changed the velocity before the goal lines are tested.

level 2 of 6Q3 · What does the wait counter do after a goal?

try first, then show answer

1. Holds the ball still before velocity is set

why

serve sets a wait counter, and start_round at state.ts:44#start_round gives the ball velocity when the counter reaches zero. The paddles stay where the player left them.

level 2 of 6Q4 · After the left side scores, which paddle does the ball travel toward?

try first, then show answer

2. Toward the left paddle

why

After the left side scores, scoring.ts:11#false clears the serving_to_right flag. start_round at state.ts:46#leftbound multiplies a negative direction by the ball speed, so the ball travels toward the left paddle. The ball starts at the centre with zero velocity until the wait ends.

level 4 of 6Q5 · The ball rests past the right edge for three frames after a serve bug. How many points does the left paddle gain per goal in the real game?

try first, then show answer

3. One; serve resets the ball into play at once

why

score_if_out at scoring.ts:4#goalcheck calls serve in the same branch that adds the point. serve moves the ball back to the centre, so the next frame finds the ball inside the window and the goal test stays false. One crossing pays one point.

level 4 of 6Q6 · A refactor runs score_if_out before the collision passes. What can happen now?

try first, then show answer

2. A ball a paddle just saved is counted as a goal

why

score_if_out at scoring.ts:4#goalcheck is the last call in step. Moving it before the passes means a ball the paddle pass would have pushed clear can reach the goal check still outside, so a save reads as a miss.

level 4 of 6Q7 · A teammate pays a point every time a paddle face bounces the ball. Name the field of Ball that the real goal test reads instead.

try first, then show answer

x

why

The paddle pass at update.ts:62#hface reverses vx and pushes the ball clear, and it adds no point. The goal test reads the field x of Ball against the edges at scoring.ts:9#WINDOW_W. It runs after both passes at scoring.ts:4#goalcheck, so a face that saved the ball has already moved it back inside.