r/app/whack.r · 4570 字节 · 下载原文
#include <vml_ui.r>
# 打地鼠 —— 用 **R** 写的手机小游戏
# Whack-a-mole -- a small phone game written in **R**
#
# 玩法:3×3 的洞里随机冒地鼠,30 秒内点得越多分越高;点空扣分。
#
# 跑法 —— 手机:vml run examples/r/whack.r
# 跑法 —— 桌面:vmlcli Examples/r/whack.r --screen 480x640 --frames /tmp/fr
c_bg <- -15724520
c_txt <- -1513232
c_dim <- -6643536
c_ok <- -11409298
c_no <- -2069424
c_hole <- -14408660
c_gold <- -2516442
new_mole <- function(hv) {
nv <- rep(0, 9)
nv[ui_math_rand(9) + 1] <- 1
return(nv)
}
k <- ui_win_open("打地鼠", 0, 0)
k <- ui_scr_set_keep_on(1)
w <- ui_scr_get_w()
h <- ui_scr_get_h()
if (w <= 0) { w <- 360 }
if (h <= 0) { h <- 620 }
# 布局按画布现算:字号随 min(W,H) 缩放,九宫格吃掉中间剩余高度。
mn <- w
if (h < mn) { mn <- h }
scn <- as.integer(mn * 100 / 360)
if (scn < 55) { scn <- 55 }
if (scn > 240) { scn <- 240 }
pad <- as.integer(mn / 20)
if (pad < 6) { pad <- 6 }
if (pad > 24) { pad <- 24 }
f_stat <- as.integer(26 * scn / 100)
if (f_stat < 8) { f_stat <- 8 }
f_small <- as.integer(12 * scn / 100)
if (f_small < 8) { f_small <- 8 }
f_hint <- as.integer(11 * scn / 100)
if (f_hint < 8) { f_hint <- 8 }
stat_y <- pad + as.integer(f_stat / 2)
lab_y <- pad + f_stat + as.integer(f_small / 2)
msg_y <- pad + f_stat + f_small + as.integer(f_small / 2) + as.integer(pad / 2)
gtop <- pad + f_stat + f_small + f_small + pad
avail <- h - gtop - f_hint - pad
cellw <- as.integer((w - 2 * pad) / 3)
ch2 <- as.integer(avail / 3)
if (ch2 < cellw) { cellw <- ch2 }
if (cellw < 20) { cellw <- 20 }
ox <- as.integer((w - cellw * 3) / 2)
oy <- gtop
m <- as.integer(cellw / 10)
if (m < 2) { m <- 2 }
hinty <- h - pad - as.integer(f_hint / 2)
score <- 0
ticks <- 0
move <- 0
over <- 0
frames <- 0
hole <- new_mole(0)
tid <- ui_timer_set(100, 0)
while (frames < 30000) {
k <- ui_2d_clear(c_bg)
i <- 1
while (i <= 9) {
r <- as.integer((i - 1) / 3)
c <- i - 1 - r * 3
tx <- ox + c * cellw + m
ty <- oy + r * cellw + m
if (hole[i] == 1) {
k <- ui_2d_rect(tx, ty, cellw - 2 * m, cellw - 2 * m, c_ok, 1, 0, 10)
k <- ui_2d_circle(tx + as.integer((cellw - 2 * m) / 2), ty + as.integer((cellw - 2 * m) / 2), as.integer((cellw - 2 * m) / 4), c_gold, 1, 0)
} else {
k <- ui_2d_rect(tx, ty, cellw - 2 * m, cellw - 2 * m, c_hole, 1, 0, 10)
}
i <- i + 1
}
k <- ui_2d_text(ox, stat_y, sprintf("%d", score), c_txt, f_stat, 0)
k <- ui_2d_text(ox, lab_y, "得分", c_dim, f_small, 0)
rem <- 300 - ticks
if (rem < 0) { rem <- 0 }
rx <- w - pad - as.integer(f_stat * 18 / 10)
if (rx < 0) { rx <- 0 }
k <- ui_2d_text(rx, stat_y, sprintf("%d", as.integer(rem / 10)), c_ok, f_stat, 0)
k <- ui_2d_text(w - pad - as.integer(f_small * 3), lab_y, "剩余秒", c_dim, f_small, 0)
if (over == 1) {
k <- ui_2d_text(ox, msg_y, "时间到!ESC 再来", c_no, f_small, 0)
} else {
k <- ui_2d_text(ox, msg_y, "点冒头的绿色地鼠", c_dim, f_small, 0)
}
k <- ui_2d_text(pad, hinty, "30 秒计分 · ESC 重开", c_dim, f_hint, 0)
k <- ui_2d_present()
msg <- ui_msg_fetch_wait(0)
if (msg == 10) { frames <- 30000 }
if (msg == 6) {
ax <- ui_msg_get_a()
ay <- ui_msg_get_b()
if (over == 0) {
if (ax >= ox) {
if (ay >= oy) {
c <- as.integer((ax - ox) / cellw)
r <- as.integer((ay - oy) / cellw)
if (c < 3) {
if (r < 3) {
j <- r * 3 + c + 1
if (hole[j] == 1) {
score <- score + 10
hole <- new_mole(0)
} else {
score <- score - 5
}
}
}
}
}
}
}
if (msg == 1) {
key <- ui_msg_get_a()
if (key == 27) {
score <- 0; ticks <- 0; over <- 0; move <- 0
hole <- new_mole(0)
}
}
if (msg == 9) {
frames <- frames + 1
if (over == 0) {
ticks <- ticks + 1
move <- move + 1
if (move >= 6) { move <- 0; hole <- new_mole(0) }
if (ticks >= 300) { over <- 1 }
}
}
}
k <- ui_timer_kill(tid)
k <- ui_scr_set_keep_on(0)
k <- ui_win_close()