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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user