feat(samples): add Pong and refresh test projects
Add the Studio-created Pong project with a short match against CPU, pause, score and restart controls. Refresh legacy samples for current PBS input, RGBA colors and preloaded assets while preserving existing identities. Run convenience launchers against the current build and configured runtime; keep local caches and native assistant configuration ignored. Validation: shell syntax, JSON, registry identity/image preservation and whitespace checks passed. Pong ran in Studio as confirmed by the user. Build/pack/runtime validation of the refreshed samples remains pending.
This commit is contained in:
@@ -0,0 +1,261 @@
|
||||
import { Color } from @core:color;
|
||||
import { Gfx } from @sdk:gfx;
|
||||
import { Pad, Button } from @sdk:input;
|
||||
|
||||
// One logical frame is one game tick. Ball coordinates use quarter pixels.
|
||||
declare const SCALE: int = 4;
|
||||
declare const FIELD_TOP: int = 72;
|
||||
declare const FIELD_BOTTOM: int = 238;
|
||||
declare const PADDLE_H: int = 36;
|
||||
declare const BALL_SIZE: int = 6;
|
||||
declare const MENU: int = 0;
|
||||
declare const SERVING: int = 1;
|
||||
declare const PLAYING: int = 2;
|
||||
declare const PAUSED: int = 3;
|
||||
declare const FINISHED: int = 4;
|
||||
|
||||
declare global phase: int = 0;
|
||||
declare global resume_phase: int = 0;
|
||||
declare global player_y: int = 137;
|
||||
declare global cpu_y: int = 137;
|
||||
declare global player_score: int = 0;
|
||||
declare global cpu_score: int = 0;
|
||||
declare global ball_x: int = 948;
|
||||
declare global ball_y: int = 608;
|
||||
declare global ball_vx: int = 4;
|
||||
declare global ball_vy: int = 2;
|
||||
declare global serve_ticks: int = 60;
|
||||
declare global hit_flash: int = 0;
|
||||
|
||||
fn clamp(value: int, low: int, high: int) -> int {
|
||||
if value < low { return low; }
|
||||
if value > high { return high; }
|
||||
return value;
|
||||
}
|
||||
|
||||
fn abs_i(value: int) -> int {
|
||||
if value < 0 { return -value; }
|
||||
return value;
|
||||
}
|
||||
|
||||
fn prepare_serve(direction: int) -> void {
|
||||
player_y = 137;
|
||||
cpu_y = 137;
|
||||
ball_x = 237 * SCALE;
|
||||
ball_y = 152 * SCALE;
|
||||
ball_vx = direction * 4;
|
||||
ball_vy = if (player_score + cpu_score) % 2 == 0 { 2 } else { -3 };
|
||||
serve_ticks = 60;
|
||||
hit_flash = 0;
|
||||
phase = SERVING;
|
||||
}
|
||||
|
||||
fn new_match() -> void {
|
||||
player_score = 0;
|
||||
cpu_score = 0;
|
||||
prepare_serve(1);
|
||||
}
|
||||
|
||||
[Init]
|
||||
fn init() -> void {
|
||||
new_match();
|
||||
phase = MENU;
|
||||
resume_phase = MENU;
|
||||
}
|
||||
|
||||
fn move_player(up: bool, down: bool) -> void {
|
||||
if up && !down { player_y = player_y - 4; }
|
||||
if down && !up { player_y = player_y + 4; }
|
||||
player_y = clamp(player_y, FIELD_TOP, FIELD_BOTTOM - PADDLE_H);
|
||||
}
|
||||
|
||||
fn move_cpu() -> void {
|
||||
// The CPU reacts only in its half and is slower than the human paddle.
|
||||
let target: int = 137;
|
||||
if ball_vx > 0 && ball_x > 240 * SCALE {
|
||||
target = ball_y / SCALE + BALL_SIZE / 2 - PADDLE_H / 2;
|
||||
}
|
||||
if target > cpu_y + 2 { cpu_y = cpu_y + 2; }
|
||||
if target < cpu_y - 2 { cpu_y = cpu_y - 2; }
|
||||
cpu_y = clamp(cpu_y, FIELD_TOP, FIELD_BOTTOM - PADDLE_H);
|
||||
}
|
||||
|
||||
fn bounce(paddle_y: int, direction: int) -> void {
|
||||
let speed: int = clamp(abs_i(ball_vx) + 1, 4, 7);
|
||||
ball_vx = direction * speed;
|
||||
let offset: int = ball_y / SCALE + BALL_SIZE / 2 - paddle_y - PADDLE_H / 2;
|
||||
ball_vy = clamp(offset / 5, -4, 4);
|
||||
if ball_vy == 0 {
|
||||
ball_vy = if (player_score + cpu_score) % 2 == 0 { -1 } else { 1 };
|
||||
}
|
||||
hit_flash = 6;
|
||||
}
|
||||
|
||||
fn point(player_won: bool) -> void {
|
||||
if player_won { player_score = player_score + 1; }
|
||||
else { cpu_score = cpu_score + 1; }
|
||||
if player_score == 5 || cpu_score == 5 {
|
||||
phase = FINISHED;
|
||||
return;
|
||||
}
|
||||
// The next ball travels toward the side that lost the point.
|
||||
prepare_serve(if player_won { 1 } else { -1 });
|
||||
}
|
||||
|
||||
fn move_ball() -> void {
|
||||
// At max speed each substep travels 1.75 px, below the paddle thickness.
|
||||
for substep: int from 0 until 4 {
|
||||
ball_x = ball_x + ball_vx;
|
||||
ball_y = ball_y + ball_vy;
|
||||
|
||||
if ball_y < FIELD_TOP * SCALE {
|
||||
ball_y = FIELD_TOP * SCALE;
|
||||
ball_vy = abs_i(ball_vy);
|
||||
}
|
||||
if ball_y > (FIELD_BOTTOM - BALL_SIZE) * SCALE {
|
||||
ball_y = (FIELD_BOTTOM - BALL_SIZE) * SCALE;
|
||||
ball_vy = -abs_i(ball_vy);
|
||||
}
|
||||
|
||||
let x: int = ball_x / SCALE;
|
||||
let y: int = ball_y / SCALE;
|
||||
if ball_vx < 0 && x <= 50 && x + BALL_SIZE >= 42 {
|
||||
if y + BALL_SIZE > player_y && y < player_y + PADDLE_H {
|
||||
ball_x = 50 * SCALE;
|
||||
bounce(player_y, 1);
|
||||
}
|
||||
}
|
||||
if ball_vx > 0 && x + BALL_SIZE >= 430 && x <= 438 {
|
||||
if y + BALL_SIZE > cpu_y && y < cpu_y + PADDLE_H {
|
||||
ball_x = (430 - BALL_SIZE) * SCALE;
|
||||
bounce(cpu_y, -1);
|
||||
}
|
||||
}
|
||||
|
||||
if ball_x + BALL_SIZE * SCALE < 20 * SCALE {
|
||||
point(false);
|
||||
return;
|
||||
}
|
||||
if ball_x > 460 * SCALE {
|
||||
point(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update(up: bool, down: bool, confirm: bool, start: bool, back: bool) -> void {
|
||||
// Exactly one state branch runs per frame, including on input edges.
|
||||
if phase == MENU {
|
||||
if confirm || start { new_match(); }
|
||||
return;
|
||||
}
|
||||
if phase == FINISHED {
|
||||
if back { phase = MENU; }
|
||||
else if confirm || start { new_match(); }
|
||||
return;
|
||||
}
|
||||
if phase == PAUSED {
|
||||
if back { phase = MENU; }
|
||||
else if confirm || start { phase = resume_phase; }
|
||||
return;
|
||||
}
|
||||
if phase == SERVING {
|
||||
if back { resume_phase = SERVING; phase = PAUSED; return; }
|
||||
move_player(up, down);
|
||||
serve_ticks = serve_ticks - 1;
|
||||
if confirm || start || serve_ticks <= 0 { phase = PLAYING; }
|
||||
return;
|
||||
}
|
||||
if start || back {
|
||||
resume_phase = PLAYING;
|
||||
phase = PAUSED;
|
||||
return;
|
||||
}
|
||||
move_player(up, down);
|
||||
move_cpu();
|
||||
if hit_flash > 0 { hit_flash = hit_flash - 1; }
|
||||
move_ball();
|
||||
}
|
||||
|
||||
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); }
|
||||
if value != 0 && value != 1 { Gfx.fill_rect(x + 3, y + 13, 12, 3, color); }
|
||||
if value != 1 && value != 4 { Gfx.fill_rect(x + 3, y + 26, 12, 3, color); }
|
||||
if value == 0 || value == 4 || value == 5 { Gfx.fill_rect(x, y + 3, 3, 10, color); }
|
||||
if value != 5 { Gfx.fill_rect(x + 15, y + 3, 3, 10, color); }
|
||||
if value == 0 || value == 2 { Gfx.fill_rect(x, y + 16, 3, 10, color); }
|
||||
if value != 2 { Gfx.fill_rect(x + 15, y + 16, 3, 10, color); }
|
||||
}
|
||||
|
||||
fn draw_overlay(title: str, line_one: str, line_two: str) -> void {
|
||||
Gfx.draw_square(112, 111, 256, 90, Color.rgb(57, 81, 106), Color.rgb(12, 22, 39));
|
||||
Gfx.fill_rect(112, 111, 3, 90, Color.rgb(77, 237, 205));
|
||||
Gfx.draw_text(136, 125, title, Color.rgb(236, 244, 250));
|
||||
Gfx.draw_text(136, 150, line_one, Color.rgb(155, 176, 198));
|
||||
Gfx.draw_text(136, 174, line_two, Color.rgb(77, 237, 205));
|
||||
}
|
||||
|
||||
fn draw() -> void {
|
||||
let white = Color.rgb(236, 244, 250);
|
||||
let muted = Color.rgb(110, 135, 162);
|
||||
let mint = Color.rgb(77, 237, 205);
|
||||
let coral = Color.rgb(255, 132, 119);
|
||||
Gfx.clear(Color.rgb(7, 14, 26));
|
||||
Gfx.draw_text(24, 17, "PONG", white);
|
||||
Gfx.draw_text(24, 32, "PROMETEU", muted);
|
||||
Gfx.draw_text(370, 17, "FIRST TO 5", muted);
|
||||
Gfx.draw_text(138, 34, "YOU", mint);
|
||||
Gfx.draw_text(313, 34, "CPU", coral);
|
||||
draw_digit(player_score, 190, 19, mint);
|
||||
draw_digit(cpu_score, 272, 19, coral);
|
||||
Gfx.fill_rect(237, 28, 3, 3, muted);
|
||||
Gfx.fill_rect(237, 39, 3, 3, muted);
|
||||
|
||||
Gfx.draw_square(20, 66, 440, 178, Color.rgb(37, 57, 79), Color.rgb(13, 23, 40));
|
||||
for y: int from 76 until 238 step 13 {
|
||||
Gfx.fill_rect(239, y, 2, 6, Color.rgb(43, 61, 82));
|
||||
}
|
||||
Gfx.fill_rect(40, player_y + 3, 10, PADDLE_H, Color.rgb(20, 58, 60));
|
||||
Gfx.fill_rect(430, cpu_y + 3, 10, PADDLE_H, Color.rgb(65, 37, 46));
|
||||
Gfx.fill_rect(42, player_y, 8, PADDLE_H, mint);
|
||||
Gfx.fill_rect(430, cpu_y, 8, PADDLE_H, coral);
|
||||
|
||||
if phase == PLAYING || phase == SERVING || phase == PAUSED {
|
||||
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); }
|
||||
}
|
||||
|
||||
Gfx.draw_text(24, 254, "UP / DOWN MOVE", muted);
|
||||
Gfx.draw_text(344, 254, "START PAUSE", muted);
|
||||
if phase == MENU {
|
||||
draw_overlay("LET'S PLAY PONG", "YOU VS CPU / FIRST TO 5", "A / START - PLAY");
|
||||
} else if phase == SERVING {
|
||||
let countdown = if serve_ticks > 40 { "3" } else if serve_ticks > 20 { "2" } else { "1" };
|
||||
Gfx.draw_text(220, 111, "READY", mint);
|
||||
Gfx.draw_text(236, 128, countdown, white);
|
||||
Gfx.draw_text(190, 188, "A - SERVE NOW", muted);
|
||||
} else if phase == PAUSED {
|
||||
draw_overlay("PAUSED", "A / START - RESUME", "B - MAIN MENU");
|
||||
} else if phase == FINISHED {
|
||||
let title = if player_score == 5 { "YOU WIN!" } else { "CPU WINS THIS MATCH" };
|
||||
draw_overlay(title, "A / START - PLAY AGAIN", "B - MAIN MENU");
|
||||
}
|
||||
}
|
||||
|
||||
[Frame]
|
||||
fn frame() -> void {
|
||||
let up = Pad.down(Button.Up);
|
||||
let down = Pad.down(Button.Down);
|
||||
let confirm = Pad.pressed(Button.A);
|
||||
let start = Pad.pressed(Button.Start);
|
||||
let back = Pad.pressed(Button.B);
|
||||
update(up, down, confirm, start, back);
|
||||
draw();
|
||||
}
|
||||
Reference in New Issue
Block a user