java/app/whack.java · 10365 字节 · 下载原文
// 打地鼠 —— 用 **Java** 写的手机小游戏
// Whack-a-mole — a small phone game written in **Java**
//
// 玩法:3×3 九个洞,每 SHOW_TICKS 拍有一个洞冒出地鼠;
// Gameplay: nine holes in a 3x3; every SHOW_TICKS ticks one hole pops a mole;
// 在它缩回去之前点中 +1 分,超时没点中扣一条命;三条命用完结束。
// tapping it before it ducks back scores +1; missing it costs a life; three lives ends the run.
//
// 与 `Examples/c/app/whack.c` / `Examples/python/app/whack.py` 同一套玩法与布局。
// Same rules and layout as `Examples/c/app/whack.c` / `Examples/python/app/whack.py`.
//
// ◆ 这一类游戏的关键是「计时归谁管」:地鼠的存活时间记在**拍数**上(mole_ticks),
// ◆ The key thing in this genre is **who owns the clock**: a mole's lifetime is counted in
// **ticks**, not wall time — the timer is the heartbeat, so "how many ticks a mole stays"
// is exact and does not drift with the machine.
// 而不是 time();拍数与真实时间的关系由 ui_timer_set 定死。
// The tick-to-real-time ratio is fixed by ui_timer_set.
//
// ◆ Java 前端实测约束(详见 memory.java 文件头)
// ◆ Java front-end conventions (see memory.java)
// ① `import vml.ui.*;` 只带函数不带常量 ⇒ 消息类型 / 锚点就地 `static final int`。
// ① The import brings functions but no constants ⇒ declare them as `static final int`.
// ② 屏幕坐标 → 格子先判负再除(Java 的 `/` 向零截断,棋盘外会被算进第一格)。
// ② Screen point → cell must test negatives before dividing (Java's `/` truncates toward zero).
//
// 跑法 / How to run:
// 手机 / phone: vml run examples/java/app/whack.java
// 桌面 / desktop: vmlcli Examples/java/app/whack.java --screen 411x726 --frames /tmp/fr
import vml.ui.*;
public class Whack {
// ── 消息类型 / 键码(源:Lib/c/vml_ui.h)──
// ── Message types / key codes (source: Lib/c/vml_ui.h) ──
static final int MSG_KEYDOWN = 1;
static final int MSG_MOUSEDOWN = 4;
static final int MSG_TOUCHDOWN = 6;
static final int MSG_TIMER = 9;
static final int MSG_CLOSE = 10;
static final int MSG_RESIZE = 11;
static final int MSG_ORIENT = 12;
static final int KEY_ESC = 27;
// ── 文字锚点(与 vml_ui.h 同值)──
static final int ANCHOR_LEFT = 0;
static final int ANCHOR_CENTER = 1;
static final int ANCHOR_RIGHT = 2;
// ── 配色:0xAARRGGBB ──
static final int C_BG = 0xFF101018;
static final int C_HOLE = 0xFF1A1A24;
static final int C_MOLE = 0xFFD9B44A;
static final int C_TEXT = 0xFFE8E8F0;
static final int C_DIM = 0xFF9AA0B0;
static final int C_MISS = 0xFFE06C50;
static final int C_EYE = 0xFF101018;
static final int C_FLASH = 0xFFFFFFFF;
static final int GRID = 3;
static final int SHOW_TICKS = 14; // 一只地鼠活 14 拍 ≈ 0.7 秒(50ms 一拍)
static final int MAX_FRAMES = 6000;
static final int TIMER_MS = 50;
/* 状态数组 S 的下标
The S slot map
0 地鼠在哪个洞(-1 = 没有) 1 地鼠剩余拍数
0 which hole holds a mole (-1 = none); 1 ticks the mole has left
2 下一只地鼠的等待拍数 3 得分
2 ticks to wait for the next mole; 3 score
4 剩余命数 5 是否结束
4 lives left; 5 game over?
6 点中闪光(1 = 本拍亮白) 7 随机种子
6 hit flash (1 = white this tick); 7 RNG seed
8 屏宽 9 屏高 10 格边长
8 screen width; 9 screen height; 10 cell size
11 棋盘左沿 12 棋盘上沿 13 界面语言(0 中 1 英)
11 board left; 12 board top; 13 UI language (0 Chinese, 1 English)
14 帧号 15..19 余量
14 frame counter; 15..19 slack */
static int[] S = new int[20];
// ── 小工具 ──
// ── Small helpers ──
// 16 位 LCG(32 位状态会溢出退化,见 memory.java)
// 16-bit LCG (a 32-bit state overflows and degenerates; see memory.java).
static int whRnd(int n) {
S[7] = (S[7] * 1103 + 12345) % 32768;
return (S[7] / 256) % n;
}
static String whTr(String zh, String en) {
if (S[13] == 0) { return zh; }
return en;
}
// ── 排版 ──
static void whRelayout() {
int sw = ui_scr_get_w();
int sh = ui_scr_get_h();
if (sw <= 0) { sw = 360; }
if (sh <= 0) { sh = 620; }
S[8] = sw;
S[9] = sh;
// 矮屏压紧页眉/页脚,3×3 洞位从 60px 放大到约 153px。
// Tighten header/footer on short screens; the 3x3 holes grow from ~60px to ~153px.
int top = 130;
int foot = 30;
if (sh < 480) { top = 60; }
int side = sw - 24;
int avail = sh - top - foot;
if (avail < side) { side = avail; }
int cell = side / GRID;
if (cell < 8) { cell = 8; }
S[10] = cell;
S[11] = (sw - GRID * cell) / 2;
S[12] = top + (avail - GRID * cell) / 2;
if (S[12] < top) { S[12] = top; }
}
static void whPickHole() {
S[0] = whRnd(GRID * GRID);
S[1] = SHOW_TICKS;
}
static void whNewGame() {
S[3] = 0;
S[4] = 3;
S[5] = 0;
S[6] = 0;
S[0] = -1;
S[1] = 0;
S[2] = 6;
}
// ── 绘制 ──
static void whDrawHole(int i) {
int cell = S[10];
int cx = S[11] + (i % GRID) * cell + cell / 2;
int cy = S[12] + (i / GRID) * cell + cell / 2;
int r = cell / 3;
// 洞口:一个压扁的椭圆(地鼠从左下方"钻出来"的透视)
// The hole: a squashed ellipse (the mole rises from behind its lower edge).
ui_2d_ellipse(cx, cy + r / 3, r + cell / 12, r / 2 + cell / 20, C_HOLE, 1, 0);
if (i == S[0]) {
int mc = C_MOLE;
if (S[6] == 1) { mc = C_FLASH; }
ui_2d_circle(cx, cy, r, mc, 1, 0);
ui_2d_circle(cx - r / 3, cy - r / 4, r / 5, C_EYE, 1, 0);
ui_2d_circle(cx + r / 3, cy - r / 4, r / 5, C_EYE, 1, 0);
}
}
static void whDraw() {
int i = 0;
ui_2d_clear(C_BG);
while (i < GRID * GRID) {
whDrawHole(i);
i = i + 1;
}
ui_2d_text(10, 24, "" + S[3], C_TEXT, 18, ANCHOR_LEFT);
ui_2d_text(10, 48, whTr("得分", "score"), C_DIM, 10, ANCHOR_LEFT);
ui_2d_text(S[8] - 10, 24, "" + S[4], C_MISS, 18, ANCHOR_RIGHT);
ui_2d_text(S[8] - 10, 48, whTr("命", "lives"), C_DIM, 10, ANCHOR_RIGHT);
int msgY = S[12] + GRID * S[10] + 30;
if (msgY > S[9] - 10) { msgY = S[9] - 10; }
if (S[5] == 1) {
ui_2d_text(S[8] / 2, msgY, whTr("结束,点一下再来", "Over - tap to retry"),
C_TEXT, 16, ANCHOR_CENTER);
} else {
ui_2d_text(S[8] / 2, msgY, whTr("冒头就点它", "Tap the mole when it pops"),
C_DIM, 12, ANCHOR_CENTER);
}
ui_2d_present();
}
// 点屏幕:命中地鼠 +1 分;结束后点任意处重开
// Screen tap: a hit scores +1; after game over, any tap restarts.
static void whTap(int tx, int ty) {
if (S[5] == 1) {
whNewGame();
return;
}
// ⚠ 先判负再除(Java 的 / 向零截断)
// ⚠ Test negatives before dividing (Java's / truncates toward zero).
int relX = tx - S[11];
int relY = ty - S[12];
if (relX < 0 || relY < 0) { return; }
int x = relX / S[10];
int y = relY / S[10];
if (x >= GRID || y >= GRID) { return; }
int idx = y * GRID + x;
if (idx == S[0]) {
S[3] = S[3] + 1;
S[0] = -1;
S[1] = 0;
S[2] = 4 + whRnd(4);
S[6] = 1;
}
}
public static void main(String[] args) {
int t = 0;
int tid = 0;
S[13] = ui_scr_get_language();
S[7] = 20267;
whRelayout();
ui_win_open_ex(whTr("打地鼠", "Whack (java)"), 0, 0, 1, 0);
ui_scr_set_keep_on(1);
whRelayout();
whNewGame();
whDraw();
tid = ui_timer_set(TIMER_MS, 1);
while (ui_win_is_closed() == 0) {
t = ui_msg_fetch_wait(TIMER_MS);
if (t == MSG_CLOSE) { break; }
if (t == MSG_TOUCHDOWN || t == MSG_MOUSEDOWN) {
whTap(ui_msg_get_a(), ui_msg_get_b());
whDraw();
continue;
}
if (t == MSG_KEYDOWN) {
if (ui_msg_get_a() == KEY_ESC) {
whNewGame();
whDraw();
}
continue;
}
if (t == MSG_RESIZE || t == MSG_ORIENT) {
whRelayout();
whDraw();
continue;
}
if (t == MSG_TIMER) {
S[14] = S[14] + 1;
if (S[14] >= MAX_FRAMES) { break; }
if (S[5] == 0) {
S[6] = 0;
if (S[0] >= 0) {
S[1] = S[1] - 1;
if (S[1] <= 0) {
// 没点中:扣命 / missed: lose a life
S[0] = -1;
S[4] = S[4] - 1;
S[2] = 8;
if (S[4] <= 0) { S[5] = 1; }
}
} else {
S[2] = S[2] - 1;
if (S[2] <= 0) { whPickHole(); }
}
}
whDraw();
continue;
}
}
ui_timer_kill(tid);
ui_scr_set_keep_on(0);
ui_win_close();
System.out.print("whack: score="); System.out.println(S[3]);
System.out.print("whack: lives="); System.out.println(S[4]);
System.out.print("whack: over="); System.out.println(S[5]);
System.out.print("whack: frames="); System.out.println(S[14]);
System.out.println("whack: done");
}
}