c/app/whack.c · 6018 字节 · 下载原文
/* whack.c —— 打地鼠(反应类:目标随机冒头,限时内点中)
* whack.c -- whack-a-mole (reaction game: a target pops up at random; hit it in time)
*
* 玩法:3×3 九个洞,每 `SHOW_TICKS` 拍有一个洞冒出地鼠;
* Rules: 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.
*
* 这一类游戏的关键是**"计时归谁管"**:地鼠的存活时间记在**拍数**上(`alive_ticks`),
* The key thing in this genre is **who owns the clock**: a mole's lifetime is counted in **ticks**
* (`alive_ticks`),
* 而不是 `time()` —— 定时器就是心跳,拍数与真实时间的关系由 `ui_timer_set` 定死,
* and not in `time()` -- the timer is the heartbeat and the tick-to-time relation is fixed by
* `ui_timer_set`,
* 这样"地鼠该待几拍"是可以算的、不随机器快慢漂。
* so "how many ticks a mole stays" is computable and does not drift with the machine.
*/
#include <vml_ui.h>
#include <stdlib.h>
#include <stdio.h>
#define MAX_FRAMES 6000
#define GRID 3
#define SHOW_TICKS 14 /* 一只地鼠活 14 拍 ≈ 0.7 秒(50ms 一拍) */
#define C_BG 0xFF101018
#define C_HOLE 0xFF1A1A24
#define C_MOLE 0xFFD9B44A
#define C_TEXT 0xFFE8E8F0
#define C_DIM 0xFF9AA0B0
#define C_MISS 0xFFE06C50
static int SW, SH, g_lang, g_frames;
static int cell, ox, oy;
static int mole_at, mole_ticks, wait_ticks;
static int score, lives, over, hit_flash;
static unsigned int seed = 20261122;
static int rnd(int n)
{
seed = seed * 1103515245u + 12345u;
return (int)((seed >> 16) % (unsigned int)n);
}
static void relayout(void)
{
int side;
SW = ui_scr_get_w();
SH = ui_scr_get_h();
if (SW <= 0) SW = 360;
if (SH <= 0) SH = 620;
{
int top = (SH < 420 || SW < 360) ? 64 : 130;
int bottom = (SH < 420 || SW < 360) ? 34 : 50;
side = SW - 24;
if (side > SH - top - bottom) side = SH - top - bottom;
if (side < 60) side = 60;
cell = side / GRID;
if (cell < 8) cell = 8;
ox = (SW - GRID * cell) / 2;
oy = top + (SH - top - bottom - GRID * cell) / 2;
if (oy < top) oy = top;
}
}
static void pick_hole(void)
{
mole_at = rnd(GRID * GRID);
mole_ticks = SHOW_TICKS;
}
static void new_game(void)
{
score = 0;
lives = 3;
over = 0;
hit_flash = 0;
mole_at = -1;
wait_ticks = 6;
}
static void draw(void)
{
int i;
char buf[12];
ui_2d_clear(C_BG);
for (i = 0; i < GRID * GRID; i++) {
int cx = ox + (i % GRID) * cell + cell / 2;
int cy = oy + (i / GRID) * cell + cell / 2;
int r = cell / 3;
ui_2d_ellipse(cx, cy + r / 3, r + cell / 12, r / 2 + cell / 20, C_HOLE, 1, 0);
if (i == mole_at) {
ui_2d_circle(cx, cy, r, hit_flash ? 0xFFFFFFFF : C_MOLE, 1, 0);
ui_2d_circle(cx - r / 3, cy - r / 4, r / 5, 0xFF101018, 1, 0);
ui_2d_circle(cx + r / 3, cy - r / 4, r / 5, 0xFF101018, 1, 0);
}
}
itoa(score, buf);
ui_2d_text(10, 24, buf, C_TEXT, 18, VML_ANCHOR_LEFT);
ui_2d_text(10, 48, g_lang == 0 ? "得分" : "score", C_DIM, 10, VML_ANCHOR_LEFT);
itoa(lives, buf);
ui_2d_text(SW - 10, 24, buf, C_MISS, 18, VML_ANCHOR_RIGHT);
ui_2d_text(SW - 10, 48, g_lang == 0 ? "命" : "lives", C_DIM, 10, VML_ANCHOR_RIGHT);
if (over)
ui_2d_text(SW / 2, oy + GRID * cell + 30, g_lang == 0 ? "结束,ESC 再来" : "Over - ESC to retry",
C_TEXT, 16, VML_ANCHOR_CENTER);
else
ui_2d_text(SW / 2, oy + GRID * cell + 30, g_lang == 0 ? "冒头就点它" : "Tap the mole when it pops",
C_DIM, 12, VML_ANCHOR_CENTER);
ui_2d_present();
}
int main(void)
{
int msg[4];
int tid;
int done = 0;
char buf[12];
g_lang = ui_scr_get_language();
relayout();
ui_win_open_ex("whack.c", 0, 0, VML_WIN_ROTATABLE, VML_WIN_NO_GAMEPAD);
relayout();
new_game();
draw();
tid = ui_timer_set(50, 1);
while (!done && ui_win_is_closed() == 0) {
int t = ui_msg_wait(msg, 0);
if (t == VML_MSG_NONE) continue;
if (t == VML_MSG_TOUCHDOWN || t == VML_MSG_MOUSEDOWN) {
int x = (msg[1] - ox) / cell, y = (msg[2] - oy) / cell;
if (!over && x >= 0 && y >= 0 && x < GRID && y < GRID) {
int idx = y * GRID + x;
if (idx == mole_at) {
score = score + 1;
mole_at = -1;
wait_ticks = 4 + rnd(4);
hit_flash = 1;
}
}
draw();
continue;
}
if (t == VML_MSG_KEYDOWN) {
if (msg[1] == VML_KEY_ESCAPE) { new_game(); draw(); }
continue;
}
if (t == VML_MSG_TIMER) {
g_frames = g_frames + 1;
if (g_frames >= MAX_FRAMES) done = 1;
if (!over) {
hit_flash = 0;
if (mole_at >= 0) {
mole_ticks = mole_ticks - 1;
if (mole_ticks <= 0) { /* 没点中:扣命 */
mole_at = -1;
lives = lives - 1;
wait_ticks = 8;
if (lives <= 0) { over = 1; done = 1; }
}
} else {
wait_ticks = wait_ticks - 1;
if (wait_ticks <= 0) pick_hole();
}
}
draw();
continue;
}
if (t == VML_MSG_WINDOWRESIZE) { relayout(); draw(); continue; }
if (t == VML_MSG_WINDOWCLOSE) break;
}
ui_timer_kill(tid);
ui_win_close();
itoa(score, buf);
printf("whack: score=%s lives=%d done\n", buf, lives);
return 0;
}