feat(pong): color the ball by speed and add contact sparks

Keep the headless pixel references aligned with the speed-dependent ball
palette and verify the first-contact color change alongside the local bounce.

Validation: make -C tooling verify passed in 20261127T035822Z-ec5d9266.
This commit is contained in:
2026-11-27 09:57:36 +00:00
parent 82ceb746ad
commit 802a13a6aa
2 changed files with 66 additions and 8 deletions
@@ -132,7 +132,7 @@ class HeadlessTaskIntegrationTest {
tasks.headless(root,task,TaskTools.HeadlessAction.SET_INPUT,null,Map.of("start",false,"a",true));
advance("pong-native-serve");
tasks.headless(root,task,TaskTools.HeadlessAction.CLEAR_INPUT,null,Map.of());
int previous=-1;boolean rightPaddleBounce=false;
int previous=-1;boolean rightPaddleBounce=false,contactColorObserved=false;
for(int i=0;i<80&&!rightPaddleBounce;i++) {
String request="pong-native-contact-"+i;
tasks.advance(root,task,request,1);
@@ -140,12 +140,17 @@ class HeadlessTaskIntegrationTest {
assertEquals(AutomationOutcome.Kind.SUCCEEDED,frame.runtimeResult().kind());
var image=new p.studio.app.evidence.PngEvidenceCodec().decode(frame.runtimeResult().capture().png());
byte[] rgba=image.rgba();int left=640,right=-1;
// Independent observation of the white ball/flash in the arena;
// HUD is above it, and neither paddle has this color.
// Independent colors from Pong's speed contract: pure white at 6,
// pale yellow at 7 after the first paddle contact. HUD, paddles
// and contact sparks use different colors.
for(int y=72;y<328;y++)for(int x=60;x<615;x++) {
int offset=(y*640+x)*4;
if(Byte.toUnsignedInt(rgba[offset])==236&&Byte.toUnsignedInt(rgba[offset+1])==244
&&Byte.toUnsignedInt(rgba[offset+2])==250) {left=Math.min(left,x);right=Math.max(right,x);}
int blue=Byte.toUnsignedInt(rgba[offset+2]);
if(Byte.toUnsignedInt(rgba[offset])==255&&Byte.toUnsignedInt(rgba[offset+1])==255
&&(blue==255||blue==127)&&Byte.toUnsignedInt(rgba[offset+3])==255) {
left=Math.min(left,x);right=Math.max(right,x);
if(blue==127)contactColorObserved=true;
}
}
assertTrue(right>=left,"Playing ball must remain observable before right contact");
int center=(left+right)/2;
@@ -155,6 +160,7 @@ class HeadlessTaskIntegrationTest {
previous=center;
}
assertTrue(rightPaddleBounce,"Actual guest ball must bounce off the paddle in the expanded arena");
assertTrue(contactColorObserved,"First paddle contact must change the ball from white to pale yellow");
}
}
@@ -211,7 +217,7 @@ class HeadlessTaskIntegrationTest {
String[] names={"menu-accent","serve-ball","moved-paddle"};
// Fixed expectations from the fixture's draw/input contract, never from a native capture.
Evidence.Rect[] regions={new Evidence.Rect(193,159,1,80),new Evidence.Rect(317,197,6,6),new Evidence.Rect(42,72,8,36)};
int[][] colors={{77,237,205},{236,244,250},{77,237,205}};
int[][] colors={{77,237,205},{255,255,255},{77,237,205}};
long totalPngBytes=0;
for(int i=0;i<names.length;i++) {
if(i==1)tasks.headless(root,task,TaskTools.HeadlessAction.SET_INPUT,null,Map.of("start",true));
+54 -2
View File
@@ -27,6 +27,11 @@ declare global ball_vx: int = 6;
declare global ball_vy: int = 3;
declare global serve_ticks: int = 60;
declare global hit_flash: int = 0;
declare const SPARK_TICKS: int = 26;
declare global spark_ticks: int = 0;
declare global spark_x: int = 0;
declare global spark_y: int = 0;
declare global spark_direction: int = 1;
fn clamp(value: int, low: int, high: int) -> int {
if value < low { return low; }
@@ -39,6 +44,16 @@ fn abs_i(value: int) -> int {
return value;
}
fn ball_color() -> Color {
// Paddle contacts raise speed from 6 to 10: white, yellow, then red.
let speed: int = clamp(abs_i(ball_vx), 6, 10);
if speed == 6 { return Color.rgb(255, 255, 255); }
if speed == 7 { return Color.rgb(255, 255, 127); }
if speed == 8 { return Color.rgb(255, 255, 0); }
if speed == 9 { return Color.rgb(255, 127, 0); }
return Color.rgb(255, 0, 0);
}
fn prepare_serve(direction: int) -> void {
player_y = 182;
cpu_y = 182;
@@ -48,6 +63,7 @@ fn prepare_serve(direction: int) -> void {
ball_vy = if (player_score + cpu_score) % 2 == 0 { 3 } else { -4 };
serve_ticks = 60;
hit_flash = 0;
spark_ticks = 0;
phase = SERVING;
}
@@ -90,9 +106,16 @@ fn bounce(paddle_y: int, direction: int) -> void {
ball_vy = if (player_score + cpu_score) % 2 == 0 { -1 } else { 1 };
}
hit_flash = 6;
// Anchor the burst to the paddle contact, independently of the departing ball.
spark_x = ball_x / SCALE;
if direction < 0 { spark_x = spark_x + BALL_SIZE; }
spark_y = clamp(ball_y / SCALE + BALL_SIZE / 2, paddle_y, paddle_y + PADDLE_H - 1);
spark_direction = direction;
spark_ticks = SPARK_TICKS;
}
fn point(player_won: bool) -> void {
spark_ticks = 0;
if player_won { player_score = player_score + 1; }
else { cpu_score = cpu_score + 1; }
if player_score == 5 || cpu_score == 5 {
@@ -175,9 +198,36 @@ fn update(up: bool, down: bool, confirm: bool, start: bool, back: bool) -> void
move_player(up, down);
move_cpu();
if hit_flash > 0 { hit_flash = hit_flash - 1; }
if spark_ticks > 0 { spark_ticks = spark_ticks - 1; }
move_ball();
}
fn draw_sparks() -> void {
if spark_ticks <= 0 { return; }
let age: int = SPARK_TICKS - spark_ticks;
// A short, mirrored fan with varied speeds/lifetimes and a gentle gravity arc.
for i: int from 0 until 10 {
let lifetime: int = 14 + (i % 4) * 4;
if age < lifetime {
let x: int = spark_x + spark_direction * (age * (6 + (i % 4) * 3) / SCALE + 1);
let y: int = spark_y + age * (i * 3 - 14) / SCALE + age * age / 24;
let size: int = if age < lifetime / 2 { 2 } else { 1 };
let color = if age < 4 { Color.rgb(255, 250, 211) }
else if age < lifetime / 2 { Color.rgb(255, 218, 92) }
else if age < lifetime - 4 { Color.rgb(255, 137, 45) }
else { Color.rgb(151, 63, 28) };
// Keep sparks inside the arena, including contacts near the walls.
if x >= 22 && x + size <= 618 && y >= FIELD_TOP && y + size <= FIELD_BOTTOM {
Gfx.fill_rect(x, y, size, size, color);
let tail_x: int = x - spark_direction * 2;
if age > 0 && age < lifetime / 2 && tail_x >= 22 && tail_x + 1 <= 618 {
Gfx.fill_rect(tail_x, y, 1, 1, Color.rgb(184, 91, 33));
}
}
}
}
}
fn draw_digit(value: int, x: int, y: int, color: Color) -> void {
// Seven-segment score display. A match never needs digits above five.
if value != 1 && value != 4 { Gfx.fill_rect(x + 3, y, 12, 3, color); }
@@ -223,14 +273,16 @@ fn draw() -> void {
Gfx.fill_rect(590, cpu_y, 8, PADDLE_H, coral);
if phase == PLAYING || phase == SERVING || phase == PAUSED {
draw_sparks();
let x: int = ball_x / SCALE;
let y: int = ball_y / SCALE;
if phase == PLAYING {
Gfx.fill_rect(x - ball_vx * 2, y - ball_vy * 2, BALL_SIZE, BALL_SIZE, Color.rgb(28, 45, 66));
Gfx.fill_rect(x - ball_vx, y - ball_vy, BALL_SIZE, BALL_SIZE, Color.rgb(64, 89, 114));
}
Gfx.fill_rect(x, y, BALL_SIZE, BALL_SIZE, white);
if hit_flash > 0 { Gfx.draw_square(x - 2, y - 2, 10, 10, mint, white); }
let color = ball_color();
Gfx.fill_rect(x, y, BALL_SIZE, BALL_SIZE, color);
if hit_flash > 0 { Gfx.draw_square(x - 2, y - 2, 10, 10, mint, color); }
}
Gfx.draw_text(24, 344, "UP / DOWN MOVE", muted);