csharp/app/angrybirds.cs · 32757 字节 · 下载原文
// 愤怒的小鸟(简化版)—— 用 **C#** 写的手机小游戏
// Angry Birds (simplified) — a small phone game written in **C#**
//
// 玩法:**按住小鸟往后拉**给弹弓蓄力,松手发射;目标是打掉右边的三只绿猪。
// Gameplay: **drag the bird back** to load the slingshot, release to fire; the goal is to
// 木头可以被打碎,猪被打中就消失。每局三只鸟,鸟落地/飞出屏幕就算用完一只。
// knock out the three green pigs on the right. Wood breaks when hit; a hit pig is gone.
// Three birds per round; a bird that lands or leaves the screen is spent.
// 全部打掉过关;鸟用完还没打完就算失败。过关/失败后**点任意处**重来。
// Clear them all to win; running out of birds loses. After either, **tap anywhere** to restart.
// 键盘:方向键调整拉弓方向,空格发射,R 重开,ESC 退出。
// Keyboard: arrows move the pull point, Space fires, R restarts, ESC quits.
//
// ◆ 这个示例的重点:**抛物线 + 三种碰撞 + 回合状态机**
// ◆ What this sample focuses on: **projectile motion + three collision kinds + a round state machine**
//
// ① 运动仍是**墙钟欠账补步**(用户 2026-10-04 口径):定时器只负责唤醒,
// ① Motion still steps from **wall-clock debt** (user rule of 2026-10-04): the timer only wakes
// 每帧该走几步 = 已经过去的毫秒数 ÷ 每步毫秒数。
// the loop; steps per frame = elapsed milliseconds / milliseconds per step.
//
// ② 位置与速度都是「像素 ×10」的整数(结果可复现,demo 判据靠这一点):
// ② Positions and velocities are integers in "pixels x10" (reproducible — the demo checkpoints
// 重力每步给 vy 加一个固定量,抛物线自然就出来了,不写任何三角函数。
// rely on it). Gravity adds a fixed amount to vy each step, which produces the parabola without
// any trigonometry.
//
// ③ 三种碰撞按**固定顺序**判:地面 → 木块(AABB)→ 猪(圆与圆)。
// ③ The three collisions run in a **fixed order**: ground -> wood (AABB) -> pigs (circle vs circle).
// 先判地面,避免"贴地飞行的鸟"被木块抢走;先判木块,木块碎掉后鸟才继续往里飞。
// Ground goes first so a bird skimming the floor is not stolen by a block; wood before pigs so
// the bird keeps flying inward after the plank breaks.
//
// 数值的唯一真源仍是 `Lib/c/vml_ui.h`。
// The single source of truth for these numbers is still `Lib/c/vml_ui.h`.
//
// 跑法:手机 `vml run examples/csharp/app/angrybirds.cs`;
// How to run: phone `vml run examples/csharp/app/angrybirds.cs`;
// 桌面 `vmlcli Examples/csharp/app/angrybirds.cs --screen 411x726 --frames /tmp/fr`
// desktop `vmlcli Examples/csharp/app/angrybirds.cs --screen 411x726 --frames /tmp/fr`
// `--arg demo` 是无头验收模式:三段固定物理 + 80 拍渲染后自己退出。
// `--arg demo` is the headless acceptance mode: three fixed physics checks + 80 rendered ticks, then exit.
using Vml.Ui;
class AngryBirds
{
// ── 消息类型 / 键码(源:Lib/c/vml_ui.h)──
// ── Message types / key codes (source: Lib/c/vml_ui.h) ──
const int MSG_KEYDOWN = 1;
const int MSG_MOUSEDOWN = 4;
const int MSG_TOUCHDOWN = 6;
const int MSG_TOUCHMOVE = 7;
const int MSG_TOUCHUP = 8;
const int MSG_TIMER = 9;
const int MSG_CLOSE = 10;
const int MSG_RESIZE = 11;
const int MSG_ORIENT = 12;
const int KEY_ESC = 27;
const int KEY_ENTER = 13;
const int KEY_SPACE = 32;
const int KEY_LEFT = 37;
const int KEY_UP = 38;
const int KEY_RIGHT = 39;
const int KEY_DOWN = 40;
const int KEY_R = 82;
const int ANCHOR_LEFT = 0;
const int ANCHOR_CENTER = 1;
const int ANCHOR_RIGHT = 2;
const int VANCHOR_MIDDLE = 1;
const int LANG_ZH = 0;
const int WIN_PORTRAIT = 0;
const int WIN_NO_GAMEPAD = 0;
// ── 配色(0xAARRGGBB 的十进制负数形式)──
// ── Colors (decimal-negative 0xAARRGGBB) ──
const int SKY = -9517336; // 0xFF6EC6E8
const int SKY2 = -6365968; // 0xFF9EDCF0
const int GROUND = -10640566; // 0xFF5DA34A
const int DIRT = -7644629; // 0xFF8B5A2B
const int WOOD = -5211586; // 0xFFB07A3E
const int WOOD_D = -8760794; // 0xFF7A5226
const int PIG = -8405171; // 0xFF7FBF4D
const int PIG_S = -5711509; // 0xFFA8D96B
const int PIG_D = -12690390; // 0xFF3E5C2A
const int BIRD = -2077901; // 0xFFE04B33
const int BELLY = -867718; // 0xFFF2C27A
const int BEAK = -20448; // 0xFFFFB020
const int TEXT = -1513232; // 0xFFE8E8F0
const int MUTED = -6643536; // 0xFF9AA0B0
const int SLING = -9747930; // 0xFF6B4226
const int GHOST = -4729117; // 0xFFB7D6E3
const int WHITE = -1; // 0xFFFFFFFF
const int BLACK = -15066598; // 0xFF1A1A1A
const int GOLD = -16320; // 0xFFFFC040
const int SCRIM = -872415232; // 0xCC000000
// ── 规则 / 手感 ──
// ── Rules / feel ──
const int TICK_MS = 16; // 物理节拍(定时器只负责唤醒)
// physics beat (the timer only wakes the loop)
const int GRAV = 2; // 每步给 vy 加多少(px/拍 ×10 的 1/10)
// added to vy each step (a tenth of a px per tick x10)
const int BIRD_R = 9;
const int PIG_R = 14;
const int MAX_PULL = 62; // 拉弓最大位移(px)
// maximum pull distance in px
const int START_BIRDS = 3;
const int BLOCK_SCORE = 50;
const int PIG_SCORE = 500;
const int MAX_BLOCKS = 8;
const int MAX_PIGS = 4;
// ── 状态 ──
// ── State ──
static int sw;
static int sh;
static int lang;
static int groundY;
static int slingX;
static int slingY;
static int sceneK = 100; // 木屋/弹弓的缩放百分比(小屏缩小,大屏保持 100)
// Scale (percent) of the hut / slingshot (shrinks on small screens, stays 100 on large ones)
static int[] bk = new int[MAX_BLOCKS * 5]; // x, y, w, h, alive
static int blockCount;
static int[] pg = new int[MAX_PIGS * 3]; // x, y, alive(中心点)
// x, y, alive (centre points)
static int pigCount;
static int pigsLeft;
static int state; // 0 = 瞄准,1 = 飞行中,2 = 过关,3 = 失败
// 0 = aiming, 1 = flying, 2 = won, 3 = lost
static int birdsLeft;
static int score;
static int frameLimit;
static int frames;
static int acc;
static int lastMs;
static int bx10; // 鸟心 px ×10
// bird centre, px x10
static int by10;
static int vx10;
static int vy10;
static int dragging; // 手指按住中
// a finger is down
static int dragStartX; // 按下时的手指位置(**增量**拉弓的基准)
// finger position when the drag started (the base for **delta** pulling)
static int dragStartY;
static int pullX; // 拉弓位移(px,左 / 下)
// pull offset in px (left / down)
static int pullY;
static int touchX;
static int touchY;
// ── 小工具 ──
// ── Small helpers ──
static string T(string zh, string en)
{
if (lang == LANG_ZH) return zh;
return en;
}
static int clamp(int v, int lo, int hi)
{
if (v < lo) return lo;
if (v > hi) return hi;
return v;
}
static int absI(int v)
{
if (v < 0) return 0 - v;
return v;
}
// 逐位画数字(不拼字符串、不分配)
// Draw a number digit by digit (no strings, no allocation)
static int numDigits(int v)
{
if (v < 10) return 1;
if (v < 100) return 2;
if (v < 1000) return 3;
if (v < 10000) return 4;
return 5;
}
static int pow10(int n)
{
if (n <= 0) return 1;
if (n == 1) return 10;
if (n == 2) return 100;
if (n == 3) return 1000;
return 10000;
}
static string digitText(int d)
{
if (d == 0) return "0";
if (d == 1) return "1";
if (d == 2) return "2";
if (d == 3) return "3";
if (d == 4) return "4";
if (d == 5) return "5";
if (d == 6) return "6";
if (d == 7) return "7";
if (d == 8) return "8";
return "9";
}
static void drawNum(int x, int y, int v, int color, int size, int anchor)
{
int n = numDigits(v);
int dw = size * 14 / 25;
int total = n * dw;
int startX = x;
if (anchor == ANCHOR_CENTER) startX = x - total / 2;
if (anchor == ANCHOR_RIGHT) startX = x - total;
int i = 0;
while (i < n)
{
int d = (v / pow10(n - 1 - i)) % 10;
ui_2d_text(startX + i * dw, y, digitText(d), color, size, ANCHOR_LEFT);
i = i + 1;
}
}
// ── 关卡 ──
// ── Level ──
static void addBlock(int x, int y, int w, int h)
{
int i = blockCount * 5;
if (i + 4 >= bk.Length) return;
bk[i] = x;
bk[i + 1] = y;
bk[i + 2] = w;
bk[i + 3] = h;
bk[i + 4] = 1;
blockCount = blockCount + 1;
}
static void addPig(int x, int y)
{
int i = pigCount * 3;
if (i + 2 >= pg.Length) return;
pg[i] = x;
pg[i + 1] = y;
pg[i + 2] = 1;
pigCount = pigCount + 1;
}
static void layout()
{
// 地面高度随屏高走;小屏(如 240x240)少留一点地面,给木屋腾出竖直空间。
// Ground height follows the screen height; small screens (e.g. 240x240) leave less
// ground so the hut has vertical room.
int groundMargin = sh / 5;
if (groundMargin > 86) groundMargin = 86;
if (groundMargin < 48) groundMargin = 48;
groundY = sh - groundMargin;
// 木屋原本高 184px:画布放得下就保持原样,放不下就按可用高度等比缩小。
// The hut is 184px tall: keep it as-is when the canvas fits, otherwise scale it down
// proportionally to the available height.
int avail = groundY - 84;
sceneK = 100;
if (avail < 184)
{
sceneK = avail * 100 / 184;
if (sceneK < 45) sceneK = 45;
}
slingX = 62;
slingY = groundY - 104 * sceneK / 100;
if (slingY > groundY - 40) slingY = groundY - 40;
if (slingY < 84) slingY = 84;
}
// 木屋的基准 X:右边不出画布(小屏时左移)
// Base X of the hut: keep its right edge inside the canvas (shift left on small screens)
static int hutBase()
{
int base = sw * 56 / 100;
int right = base + (104 + 16) * sceneK / 100;
int limit = sw - 6;
if (right > limit) base = base - (right - limit);
if (base < 6) base = 6;
return base;
}
// 一座小木屋:三根立柱 + 两根横梁,三只猪分别站在地面和二层
// A little hut: three posts plus two beams, with three pigs on the ground and the upper floor
static void setupLevel()
{
blockCount = 0;
pigCount = 0;
int base = hutBase();
int gy = groundY;
int k = sceneK;
int pw = 16 * k / 100;
if (pw < 6) pw = 6;
addBlock(base, gy - 96 * k / 100, pw, 96 * k / 100); // 左立柱
// left post
addBlock(base + 52 * k / 100, gy - 96 * k / 100, pw, 96 * k / 100); // 中立柱
// middle post
addBlock(base + 104 * k / 100, gy - 96 * k / 100, pw, 96 * k / 100); // 右立柱
// right post
addBlock(base - 4 * k / 100, gy - 114 * k / 100, 128 * k / 100, 18 * k / 100); // 一层横梁
// lower beam
addBlock(base + 44 * k / 100, gy - 168 * k / 100, pw, 54 * k / 100); // 二层立柱
// upper post
addBlock(base + 20 * k / 100, gy - 184 * k / 100, 64 * k / 100, 16 * k / 100); // 二层横梁
// upper beam
addPig(base + 26 * k / 100, gy - PIG_R); // 地面猪 1
// ground pig 1
addPig(base + 78 * k / 100, gy - PIG_R); // 地面猪 2
// ground pig 2
addPig(base + 52 * k / 100, gy - 176 * k / 100 - PIG_R); // 二层猪
// upper pig
pigsLeft = pigCount;
}
static void resetRound()
{
score = 0;
birdsLeft = START_BIRDS;
setupLevel();
resetBird();
state = 0;
dragging = 0;
pullX = 0;
pullY = 0;
}
static void resetBird()
{
bx10 = slingX * 10;
by10 = (slingY - 10) * 10;
vx10 = 0;
vy10 = 0;
pullX = 0;
pullY = 0;
}
static void launch()
{
if (state != 0) return;
int p = absI(pullX) + absI(pullY);
if (p < 6) return;
vx10 = 0 - pullX * 14 / 10;
vy10 = 0 - pullY * 14 / 10;
state = 1;
dragging = 0;
ui_audio_beep(520, 60);
}
static void endShot()
{
vx10 = 0;
vy10 = 0;
if (pigsLeft <= 0)
{
state = 2;
ui_audio_beep(1047, 220);
return;
}
birdsLeft = birdsLeft - 1;
if (birdsLeft <= 0)
{
state = 3;
ui_audio_beep(131, 300);
return;
}
resetBird();
state = 0;
}
// ── 一次物理步进;返回 0 = 无事,1 = 撞木块,2 = 撞猪,3 = 落地/出界 ──
// ── One physics step; returns 0 = nothing, 1 = wood hit, 2 = pig hit, 3 = landed / out of bounds ──
static int tick()
{
if (state != 1) return 0;
bx10 = bx10 + vx10;
by10 = by10 + vy10;
vy10 = vy10 + GRAV;
int bx = bx10 / 10;
int by = by10 / 10;
int hit = 0;
// ① 地面(先判,免得贴地飞行的鸟被木块抢走)
// (1) Ground (first, so a floor-skimming bird is not stolen by a block)
if (by + BIRD_R >= groundY)
{
by10 = (groundY - BIRD_R) * 10;
endShot();
return 3;
}
// ② 木块(AABB 与鸟的外接方框相交)
// (2) Wood (AABB against the bird's bounding box)
int i = 0;
while (i < blockCount)
{
int o = i * 5;
if (bk[o + 4] != 0)
{
int x0 = bk[o];
int y0 = bk[o + 1];
int x1 = x0 + bk[o + 2];
int y1 = y0 + bk[o + 3];
if (bx + BIRD_R >= x0 && bx - BIRD_R <= x1 && by + BIRD_R >= y0 && by - BIRD_R <= y1)
{
bk[o + 4] = 0;
score = score + BLOCK_SCORE;
hit = 1;
// 从左边撞上来就退到木板左面,再按比例反弹(别卡在木头里)
// From the left, back off to the plank's left face and bounce proportionally (never stick)
if (vx10 > 0)
{
bx10 = (x0 - BIRD_R - 1) * 10;
vx10 = 0 - vx10 / 3;
vy10 = vy10 / 2;
}
else if (vx10 < 0)
{
bx10 = (x1 + BIRD_R + 1) * 10;
vx10 = 0 - vx10 / 3;
vy10 = vy10 / 2;
}
else
{
vy10 = 0 - vy10 / 3;
}
i = blockCount;
}
}
i = i + 1;
}
// ③ 猪(圆与圆)
// (3) Pigs (circle vs circle)
i = 0;
while (i < pigCount)
{
int o2 = i * 3;
if (pg[o2 + 2] != 0)
{
int dx = bx - pg[o2];
int dy = by - pg[o2 + 1];
int rr = BIRD_R + PIG_R;
if (dx * dx + dy * dy <= rr * rr)
{
pg[o2 + 2] = 0;
pigsLeft = pigsLeft - 1;
score = score + PIG_SCORE;
hit = 2;
vx10 = vx10 / 2;
vy10 = vy10 / 2;
}
}
i = i + 1;
}
// ④ 飞出屏幕
// (4) Out of the screen
if (bx10 > (sw + 60) * 10 || bx10 < (0 - 60) * 10 || by10 < (0 - 200) * 10)
{
endShot();
return 3;
}
return hit;
}
// ── 瞄准 ──
// ── Aiming ──
/// <summary>
/// 按**手指的位移**(不是手指的绝对坐标)设置拉弓量。
/// ⚠ 2026-10-25 修「操作很不灵」:原先按 `x - slingX` 算,手指**在弹弓上方**时
/// `dy` 是负的、被夹成 0 ⇒ 只能水平发射(没有抛物线);手指落在弹弓右侧时
/// `dx` 也被夹成 0 ⇒ 完全拉不动。改成从**按下点**起算位移:
/// 往左 = 拉、往下 = 抬高发射角,从屏幕哪里按下都一样。
/// </summary>
static void setPullDelta(int dx, int dy)
{
// 只允许往左/往下拉(屏幕 y 向下增);长度夹到 MAX_PULL
// Only pull left/down (screen y grows downward); clamp the length to MAX_PULL
if (dx > 0) dx = 0;
if (dy < 0) dy = 0;
int d2 = dx * dx + dy * dy;
if (d2 > MAX_PULL * MAX_PULL)
{
int d = 1;
while (d * d < d2) d = d + 1;
dx = dx * MAX_PULL / d;
dy = dy * MAX_PULL / d;
}
pullX = dx;
pullY = dy;
}
static void nudgePull(int ddx, int ddy)
{
setPullDelta(pullX + ddx, pullY + ddy);
}
/// <summary>
/// 每拍**轮询**一次手指:拖动是连续量,事件队列会积压 / 丢 UP。
/// ⚠ 之前主循环把触摸类消息 `ui_msg_drop(1)` 全丢掉 —— 连 **TOUCHUP 也被丢**,
/// 于是「松手发射」经常根本不触发(用户说的「操作很不灵」)。
/// 现在以轮询为准:`ui_touch_is_down()` 从 1 变 0 就是松手,UP 消息丢了也能发射。
/// </summary>
static void pollAim()
{
if (state != 0) return;
if (ui_touch_fetch(0) == 0) return;
int px = ui_touch_get_x();
int py = ui_touch_get_y();
if (ui_touch_is_down() != 0)
{
if (dragging == 0)
{
dragging = 1;
dragStartX = px;
dragStartY = py;
pullX = 0;
pullY = 0;
}
setPullDelta(px - dragStartX, py - dragStartY);
}
else if (dragging != 0)
{
dragging = 0;
launch();
}
}
// ── 绘制 ──
// ── Drawing ──
static void drawCloud(int x, int y, int s)
{
ui_2d_circle(x, y, s, WHITE, 1, 0);
ui_2d_circle(x + s, y + s / 3, s * 3 / 4, WHITE, 1, 0);
ui_2d_circle(x - s, y + s / 3, s * 3 / 4, WHITE, 1, 0);
}
static void drawPig(int cx, int cy)
{
ui_2d_circle(cx, cy, PIG_R, PIG_D, 1, 0);
ui_2d_circle(cx, cy, PIG_R - 2, PIG, 1, 0);
ui_2d_circle(cx + 1, cy + 3, 6, PIG_S, 1, 0); // 鼻子
// snout
ui_2d_circle(cx - 1, cy + 3, 1, PIG_D, 1, 0);
ui_2d_circle(cx + 3, cy + 3, 1, PIG_D, 1, 0);
ui_2d_circle(cx - 5, cy - 4, 3, WHITE, 1, 0); // 眼白
// eye whites
ui_2d_circle(cx + 5, cy - 4, 3, WHITE, 1, 0);
ui_2d_circle(cx - 4, cy - 4, 1, BLACK, 1, 0); // 瞳孔
// pupils
ui_2d_circle(cx + 6, cy - 4, 1, BLACK, 1, 0);
}
static void drawBird(int cx, int cy)
{
ui_2d_circle(cx, cy, BIRD_R, BIRD, 1, 0);
ui_2d_circle(cx - 1, cy + 4, BIRD_R / 2, BELLY, 1, 0);
ui_2d_circle(cx + 4, cy - 3, 3, WHITE, 1, 0);
ui_2d_circle(cx + 5, cy - 3, 1, BLACK, 1, 0);
// 尖嘴:一个小三角用两条线近似
// Beak: a small triangle approximated with two lines
ui_2d_line(cx + BIRD_R - 1, cy, cx + BIRD_R + 7, cy + 2, BEAK, 3);
ui_2d_line(cx + BIRD_R + 7, cy + 2, cx + BIRD_R - 1, cy + 5, BEAK, 3);
ui_2d_line(cx + BIRD_R - 1, cy + 5, cx + BIRD_R - 1, cy, BEAK, 2);
}
// 拉弓时的抛物线预览(只画点,不真算碰撞)
// Parabola preview while aiming (dots only, no real collision test)
static void drawTrajectory()
{
if (state != 0) return;
int p = absI(pullX) + absI(pullY);
if (p < 6) return;
int px10 = (slingX + pullX) * 10;
int py10 = (slingY + pullY - 10) * 10;
int pvx = 0 - pullX * 14 / 10;
int pvy = 0 - pullY * 14 / 10;
int i = 0;
while (i < 26)
{
px10 = px10 + pvx;
py10 = py10 + pvy;
pvy = pvy + GRAV;
i = i + 1;
if (i % 3 == 0)
{
int dotR = 3;
if (i > 18) dotR = 2;
ui_2d_circle(px10 / 10, py10 / 10, dotR, GHOST, 1, 0);
}
}
}
static void draw()
{
ui_2d_clear(SKY);
ui_2d_rect(0, sh / 3, sw, sh / 3, SKY2, 1, 0, 0);
drawCloud(sw / 5, 96, 16);
drawCloud(sw * 3 / 5, 64, 12);
drawCloud(sw * 4 / 5, 130, 10);
// 地面
// Ground
ui_2d_rect(0, groundY, sw, sh - groundY, GROUND, 1, 0, 0);
ui_2d_rect(0, groundY, sw, 6, DIRT, 1, 0, 0);
// 木块
// Wooden blocks
int i = 0;
while (i < blockCount)
{
int o = i * 5;
if (bk[o + 4] != 0)
{
ui_2d_rect(bk[o], bk[o + 1], bk[o + 2], bk[o + 3], WOOD, 1, 0, 3);
ui_2d_rect(bk[o], bk[o + 1], bk[o + 2], 4, WOOD_D, 1, 0, 2);
}
i = i + 1;
}
// 猪
// Pigs
i = 0;
while (i < pigCount)
{
int o = i * 3;
if (pg[o + 2] != 0) drawPig(pg[o], pg[o + 1]);
i = i + 1;
}
// 弹弓:两根立柱 + 两条皮筋
// Slingshot: two posts plus two bands
ui_2d_line(slingX, slingY, slingX, groundY, SLING, 7);
ui_2d_line(slingX, slingY, slingX - 11, slingY - 22, SLING, 6);
ui_2d_line(slingX, slingY, slingX + 11, slingY - 22, SLING, 6);
if (state == 0)
{
int bxp = slingX + pullX;
int byp = slingY + pullY - 10;
ui_2d_line(slingX - 11, slingY - 22, bxp, byp, DIRT, 4);
ui_2d_line(slingX + 11, slingY - 22, bxp, byp, DIRT, 4);
drawTrajectory();
drawBird(bxp, byp);
}
else if (state == 1)
{
drawBird(bx10 / 10, by10 / 10);
}
// HUD
// HUD
ui_2d_text(14, 30, T("得分", "Score"), MUTED, 14, ANCHOR_LEFT);
drawNum(14, 56, score, TEXT, 22, ANCHOR_LEFT);
ui_2d_text(sw - 14, 30, T("剩余鸟", "Birds"), MUTED, 14, ANCHOR_RIGHT);
int b = 0;
while (b < birdsLeft)
{
ui_2d_circle(sw - 22 - b * 22, 52, 8, BIRD, 1, 0);
b = b + 1;
}
ui_2d_text(sw / 2, 30, T("剩余猪", "Pigs"), MUTED, 14, ANCHOR_CENTER);
drawNum(sw / 2, 56, pigsLeft, TEXT, 22, ANCHOR_CENTER);
if (state == 0 && dragging != 0)
{
ui_2d_text(sw / 2, sh - 26, T("松手发射", "Release to fire"), MUTED, 13, ANCHOR_CENTER);
}
else if (state == 0)
{
ui_2d_text(sw / 2, sh - 26, T("按住小鸟往后拉 · 松手发射", "Drag the bird back, release to fire"), MUTED, 13, ANCHOR_CENTER);
}
if (state == 2)
{
ui_2d_rect(0, 0, sw, sh, SCRIM, 1, 0, 0);
ui_2d_text(sw / 2, sh / 2 - 30, T("过关!", "Cleared!"), GOLD, 32, ANCHOR_CENTER);
drawNum(sw / 2, sh / 2 + 8, score, GOLD, 24, ANCHOR_CENTER);
ui_2d_text(sw / 2, sh / 2 + 48, T("点任意处再来一局", "Tap anywhere for another round"), TEXT, 15, ANCHOR_CENTER);
}
if (state == 3)
{
ui_2d_rect(0, 0, sw, sh, SCRIM, 1, 0, 0);
ui_2d_text(sw / 2, sh / 2 - 30, T("鸟用完了", "Out of birds"), BIRD, 30, ANCHOR_CENTER);
drawNum(sw / 2, sh / 2 + 8, score, GOLD, 24, ANCHOR_CENTER);
ui_2d_text(sw / 2, sh / 2 + 48, T("点任意处再来一局", "Tap anywhere for another round"), TEXT, 15, ANCHOR_CENTER);
}
ui_2d_present();
}
// ── 无头验收:三段固定物理,结果完全可复现 ──
// ── Headless acceptance: three fixed physics checks with a fully reproducible result ──
static void demoChecks()
{
int i = 0;
int base = hutBase();
int gy = groundY;
// ① 木块:鸟从左侧水平飞向第一根立柱(y 在立柱高度范围内)⇒ 立柱消失、+50
// (1) Wood: the bird flies level into the first post from the left (y within the post) ⇒ the
// post is gone and the score gains 50
resetRound();
state = 1;
bx10 = (base - 60) * 10;
by10 = (gy - 60) * 10;
vx10 = 60;
vy10 = 0;
i = 0;
while (i < 60 && bk[4] != 0) { tick(); i = i + 1; }
print_str("ab: wood-dead=");
print_int(bk[4]);
print_str(" score=");
println_int(score);
// ② 猪:**清掉木块**、鸟从左侧稍稍上抛飞向第一只猪 ⇒ 猪消失、+500
// (2) Pig: **clear the wood** so the post is not in the way, then the bird arcs slightly
// upward from the left into the first pig ⇒ the pig is gone, +500
resetRound();
blockCount = 0;
state = 1;
bx10 = (base - 60) * 10;
by10 = (gy - 30) * 10;
vx10 = 60;
vy10 = -6;
i = 0;
while (i < 60 && pg[2] != 0) { tick(); i = i + 1; }
print_str("ab: pig-dead=");
print_int(pg[2]);
print_str(" pigs=");
print_int(pigsLeft);
print_str(" score=");
println_int(score);
// ③ 落地:鸟在半空静止 ⇒ 受重力落到地面、这一只鸟结束(state 回到瞄准)
// (3) Landing: the bird sits still in mid-air ⇒ gravity brings it down and the shot ends
// (state returns to aiming)
resetRound();
state = 1;
bx10 = (slingX + 40) * 10;
by10 = (gy - 140) * 10;
vx10 = 0;
vy10 = 0;
i = 0;
while (i < 120 && state == 1) { tick(); i = i + 1; }
print_str("ab: land-state=");
print_int(state);
print_str(" birds=");
println_int(birdsLeft);
// ④ 拉弓方向:左下拉 40/40 ⇒ 速度必须是右上(vx=+56, vy=-56)
// (4) Launch direction: a 40/40 pull to the lower-left => velocity up-right (vx=+56, vy=-56)
resetRound();
setPullDelta(-40, 40);
launch();
print_str("ab: launch=");
print_int(vx10);
print_str("/");
print_int(vy10);
print_str(" state=");
println_int(state);
}
static void Main()
{
lang = ui_scr_get_language();
frameLimit = 0;
if (ui_arg_get_count() > 1) frameLimit = 80;
ui_win_open_ex(T("愤怒的小鸟", "Angry Birds"), 0, 0, WIN_PORTRAIT, WIN_NO_GAMEPAD);
ui_scr_set_keep_on(1);
sw = ui_scr_get_w();
sh = ui_scr_get_h();
if (sw <= 0) sw = 411;
if (sh <= 0) sh = 726;
layout();
if (frameLimit > 0) demoChecks();
resetRound();
draw();
int tid = ui_timer_set(TICK_MS, 0);
acc = 0;
lastMs = ui_time_get_ms();
while (ui_win_is_closed() == 0)
{
int t = ui_msg_fetch_wait(TICK_MS);
// 这一帧该走几步:按墙钟欠账
// Steps this frame: by wall-clock debt
int now = ui_time_get_ms();
acc = acc + (now - lastMs);
lastMs = now;
if (acc > TICK_MS * 8) acc = 0;
if (acc >= TICK_MS)
{
while (acc >= TICK_MS)
{
acc = acc - TICK_MS;
frames = frames + 1;
int ev = tick();
// 一次事件只发一个音
// one sound per event
if (ev == 1) ui_audio_beep(880, 30);
if (ev == 2) ui_audio_beep(1047, 60);
}
pollAim();
draw();
}
if (t == MSG_CLOSE) break;
if (t == MSG_RESIZE || t == MSG_ORIENT)
{
sw = ui_scr_get_w();
sh = ui_scr_get_h();
if (sw <= 0) sw = 411;
if (sh <= 0) sh = 726;
layout();
resetRound();
draw();
continue;
}
if (t == MSG_KEYDOWN)
{
int k = ui_msg_get_a();
if (k == KEY_ESC) break;
if (k == KEY_R || (k == KEY_ENTER && state >= 2))
{
resetRound();
draw();
continue;
}
if (state == 0)
{
if (k == KEY_LEFT) { nudgePull(-8, 0); draw(); }
if (k == KEY_RIGHT) { nudgePull(8, 0); draw(); }
if (k == KEY_UP) { nudgePull(0, -8); draw(); }
if (k == KEY_DOWN) { nudgePull(0, 8); draw(); }
if (k == KEY_SPACE) { launch(); draw(); }
}
continue;
}
if (t == MSG_TOUCHDOWN || t == MSG_MOUSEDOWN)
{
if (state >= 2)
{
resetRound();
ui_audio_beep(523, 60);
draw();
}
else if (state == 0)
{
dragging = 1;
dragStartX = ui_msg_get_a();
dragStartY = ui_msg_get_b();
pullX = 0;
pullY = 0;
draw();
}
continue;
}
if (t == MSG_TOUCHMOVE)
{
// ⚠ **不要再 `ui_msg_drop(1)`**:那一 drop 连 TOUCHUP 一起丢,
// 「松手发射」会经常不触发。拖动改由 `pollAim()` 轮询兜底。
if (dragging != 0 && state == 0)
{
touchX = ui_msg_get_a();
touchY = ui_msg_get_b();
setPullDelta(touchX - dragStartX, touchY - dragStartY);
draw();
}
continue;
}
if (t == MSG_TOUCHUP)
{
if (dragging != 0 && state == 0)
{
dragging = 0;
launch();
draw();
}
continue;
}
if (t == MSG_TIMER)
{
// 定时器只负责唤醒(推进在上面按墙钟欠账走)
// The timer only wakes the loop (advance happens above, by wall-clock debt)
if (frameLimit > 0 && frames >= frameLimit) break;
continue;
}
}
ui_timer_kill(tid);
ui_scr_set_keep_on(0);
ui_win_close();
// 给无头验证留确定性判据
// Deterministic checkpoints for headless verification
print_str("ab: score-final=");
println_int(score);
print_str("ab: pigs-final=");
println_int(pigsLeft);
print_str("ab: birds-final=");
println_int(birdsLeft);
print_str("ab: state-final=");
println_int(state);
print_str("ab: frames=");
println_int(frames);
println_str("ab: done");
}
}