← 都来码 / Dolaima← 例程索引scheme/app/whack.scm 原文

scheme/app/whack.scm · 6400 字节 · 下载原文

#include <vml_ui.scm>
; 打地鼠 —— 用 **Scheme** 写的手机小游戏
; Whack-a-mole -- a small phone game written in **Scheme**
;
; 玩法:3×3 的洞里随机冒地鼠,30 秒内点得越多分越高;点空扣分。
; ⚠ `(make-vector n v)` **不会填初始值** ⇒ 一律再用循环 `vector-set!` 填一遍。
;
; 跑法 —— 手机:vml run examples/scheme/whack.scm
; 跑法 —— 桌面:vmlcli Examples/scheme/whack.scm --screen 480x640 --frames /tmp/fr

(define c_bg -15724520)
(define c_txt -1513232)
(define c_dim -6643536)
(define c_ok -13730510)
(define c_no -2073552)
(define c_hole -14408660)
(define c_gold -2516442)

(define (digit n)
  (if (= n 0) "0" (if (= n 1) "1" (if (= n 2) "2" (if (= n 3) "3"
  (if (= n 4) "4" (if (= n 5) "5" (if (= n 6) "6" (if (= n 7) "7"
  (if (= n 8) "8" "9"))))))))))

(define w 360)
(define h 620)
(define cellw 60)
(define ox 60)
(define oy 200)
(define s 240)
(define margin 6)
(define sf 16)
(define sfs 9)
(define hintfs 9)
(define statusY 39)
(define boardTop 56)
(define hintY 230)
(define inset 5)
(define tx 0)
(define ty 0)
(define score 0)
(define ticks 0)
(define move 0)
(define over 0)
(define frames 0)
(define tid 0)
(define quit 0)
(define t 0)
(define ax 0)
(define ay 0)
(define key 0)
(define i 0)
(define j 0)
(define rem 0)
(define hole (make-vector 9 0))

(set! i 0)
(do ()
    ((> i 8))
  (vector-set! hole i 0)
  (set! i (+ i 1)))
(vector-set! hole (ui_math_rand 9) 1)

(ui_win_open "打地鼠" 0 0)
(ui_scr_set_keep_on 1)
(set! w (ui_scr_get_w))
(set! h (ui_scr_get_h))
(if (< w 1) (set! w 360) 0)
(if (< h 1) (set! h 620) 0)
; —— 布局按实际画布现算:顶部得分/倒计时 / 中部 3×3 洞 / 底部提示,字号随 min(w,h) 缩放 ——
(set! s (if (< w h) w h))
(set! margin (/ s 40))
(if (< margin 4) (set! margin 4) 0)
(if (> margin 24) (set! margin 24) 0)
(set! sf (/ s 15))
(if (< sf 14) (set! sf 14) 0)
(if (> sf 44) (set! sf 44) 0)
(set! sfs (/ s 26))
(if (< sfs 8) (set! sfs 8) 0)
(if (> sfs 22) (set! sfs 22) 0)
(set! hintfs sfs)
(set! statusY (+ margin sf sfs 8))
(set! boardTop (+ statusY sfs 8))
(set! hintY (- h hintfs))
(set! cellw (/ (- w (* 2 margin)) 3))
(if (< (/ (- (- hintY hintfs margin) boardTop) 3) cellw)
    (set! cellw (/ (- (- hintY hintfs margin) boardTop) 3)) 0)
(if (< cellw 30) (set! cellw 30) 0)
(set! ox (/ (- w (* cellw 3)) 2))
(set! oy boardTop)
(set! inset (/ cellw 10))
(if (< inset 2) (set! inset 2) 0)
(if (> inset 12) (set! inset 12) 0)
(set! tid (ui_timer_set 100 0))

(do ()
    ((= quit 1))

  (ui_2d_clear c_bg)
  (set! i 0)
  (do ()
      ((> i 8))
    (set! tx (+ ox (* (- i (* (/ i 3) 3)) cellw) inset))
    (set! ty (+ oy (* (/ i 3) cellw) inset))
    (if (= (vector-ref hole i) 1)
        (begin
          (ui_2d_rect tx ty (- cellw inset inset) (- cellw inset inset) c_ok 1 0 10)
          (ui_2d_circle (+ tx (/ (- cellw inset inset) 2)) (+ ty (/ (- cellw inset inset) 2))
                        (/ (- cellw inset inset) 4) c_gold 1 0))
        (ui_2d_rect tx ty (- cellw inset inset) (- cellw inset inset) c_hole 1 0 10))
    (set! i (+ i 1)))

  (set! tx margin)
  (if (> score 99)
      (begin (ui_2d_text tx (+ margin sf) (digit (/ score 100)) c_txt sf 0) (set! tx (+ tx (/ (* sf 3) 5)))) 0)
  (if (> score 9)
      (begin (ui_2d_text tx (+ margin sf) (digit (- (/ score 10) (* (/ score 100) 10))) c_txt sf 0)
             (set! tx (+ tx (/ (* sf 3) 5))))
      (begin (ui_2d_text tx (+ margin sf) (digit 0) c_txt sf 0) (set! tx (+ tx (/ (* sf 3) 5)))))
  (ui_2d_text tx (+ margin sf) (digit (- score (* (/ score 10) 10))) c_txt sf 0)
  (ui_2d_text margin (+ margin sf sfs 3) "得分" c_dim sfs 0)
  (set! rem (- 300 ticks))
  (if (< rem 0) (set! rem 0) 0)
  (set! tx (- w margin (/ (* sf 6) 5)))
  (ui_2d_text tx (+ margin sf) (digit (/ (/ rem 10) 10)) c_ok sf 0)
  (ui_2d_text (+ tx (/ (* sf 3) 5)) (+ margin sf) (digit (- (/ rem 10) (* (/ (/ rem 10) 10) 10))) c_ok sf 0)
  (ui_2d_text (- w margin (* sfs 3)) (+ margin sf sfs) "剩余秒" c_dim sfs 0)
  (if (= over 1)
      (ui_2d_text (- (/ w 2) (* sfs 5)) statusY "时间到!ESC 再来" c_no sfs 0)
      (ui_2d_text (- (/ w 2) (* sfs 4)) statusY "点冒头的绿色地鼠" c_dim sfs 0))
  (set! tx (- (/ w 2) (* hintfs 7)))
  (if (< tx margin) (set! tx margin) 0)
  (ui_2d_text tx hintY "30 秒计分 · ESC 重开" c_dim hintfs 0)
  (ui_2d_present)

  (set! t (ui_msg_fetch_wait 0))
  (if (= t 10) (set! quit 1) 0)
  (if (= t 6)
      (begin
        (set! ax (ui_msg_get_a))
        (set! ay (ui_msg_get_b))
        (if (= over 0)
            (if (>= ax ox)
                (if (>= ay oy)
                    (if (< ax (+ ox (* cellw 3)))
                        (if (< ay (+ oy (* cellw 3)))
                            (begin
                              (set! j (+ (* (/ (- ay oy) cellw) 3) (/ (- ax ox) cellw)))
                              (if (= (vector-ref hole j) 1)
                                  (begin
                                    (set! score (+ score 10))
                                    (set! i 0)
                                    (do () ((> i 8)) (vector-set! hole i 0) (set! i (+ i 1)))
                                    (vector-set! hole (ui_math_rand 9) 1))
                                  (set! score (- score 5))))
                            0)
                        0)
                    0)
                0)
            0))
      0)
  (if (= t 1)
      (begin
        (set! key (ui_msg_get_a))
        (if (= key 27)
            (begin
              (set! score 0) (set! ticks 0) (set! over 0) (set! move 0)
              (set! i 0)
              (do () ((> i 8)) (vector-set! hole i 0) (set! i (+ i 1)))
              (vector-set! hole (ui_math_rand 9) 1))
            0))
      0)
  (if (= t 9)
      (begin
        (set! frames (+ frames 1))
        (if (= over 0)
            (begin
              (set! ticks (+ ticks 1))
              (set! move (+ move 1))
              (if (>= move 6)
                  (begin
                    (set! move 0)
                    (set! i 0)
                    (do () ((> i 8)) (vector-set! hole i 0) (set! i (+ i 1)))
                    (vector-set! hole (ui_math_rand 9) 1))
                  0)
              (if (> ticks 299) (set! over 1) 0))
            0)
        (if (> frames 30000) (set! quit 1) 0))
      0))

(ui_timer_kill tid)
(ui_scr_set_keep_on 0)
(ui_win_close)