scheme/app/maze.scm · 5910 字节 · 下载原文
#include <vml_ui.scm>
; 迷宫 —— 用 **Scheme** 写的手机小游戏(探索)
; Maze -- a small phone game written in **Scheme** (exploration)
;
; 玩法:方向键 / 点屏幕四边移动,走到右下角红格即通关;ESC 重开。
; 公式化蛇形迷宫(偶数行整行通道、奇数行交替在左右端开口)。
; `is-wall` 是纯算术函数(不调库),可以定义。
;
; 跑法 —— 手机:vml run examples/scheme/maze.scm
; 跑法 —— 桌面:vmlcli Examples/scheme/maze.scm --screen 480x640 --frames /tmp/fr
(define c_bg -15724520)
(define c_txt -1513232)
(define c_dim -6643536)
(define c_ok -10044566)
(define c_wall -15066590)
(define c_path -14399438)
(define c_goal -2069424)
(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 (is-wall cx cy)
(if (or (< cx 0) (< cy 0) (> cx 20) (> cy 20)) 1
(if (or (= cy 0) (= cy 20)) 0
(if (= (- cy (* (/ cy 2) 2)) 0) 0
(if (= (- (/ cy 2) (* (/ (/ cy 2) 2) 2)) 0)
(if (= cx 20) 0 1)
(if (= cx 0) 0 1))))))
(define w 360)
(define h 620)
(define cellw 3)
(define ox 0)
(define oy 110)
(define s 240)
(define margin 6)
(define hudfs 16)
(define sfs 9)
(define hintfs 9)
(define hintY 231)
(define boardTop 47)
(define boardBottom 216)
(define step 9)
(define tx 0)
(define px 1)
(define py 0)
(define won 0)
(define steps 0)
(define frames 0)
(define tid 0)
(define quit 0)
(define t 0)
(define ax 0)
(define ay 0)
(define key 0)
(define nx 0)
(define ny 0)
(define dl 0)
(define dr 0)
(define dt 0)
(define db 0)
(define i 0)
(define j 0)
(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)
; —— 布局按实际画布现算:顶部步数 / 中部 21×21 迷宫 / 底部提示,字号随 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! hudfs (/ s 15))
(if (< hudfs 14) (set! hudfs 14) 0)
(if (> hudfs 40) (set! hudfs 40) 0)
(set! sfs (/ s 26))
(if (< sfs 8) (set! sfs 8) 0)
(if (> sfs 22) (set! sfs 22) 0)
(set! hintfs sfs)
(set! hintY (- h hintfs))
(set! boardTop (+ margin hudfs sfs 16))
(set! boardBottom (- hintY hintfs margin))
(set! cellw (/ (- w (* 2 margin)) 21))
(if (< (/ (- boardBottom boardTop) 21) cellw) (set! cellw (/ (- boardBottom boardTop) 21)) 0)
(if (< cellw 3) (set! cellw 3) 0)
(set! ox (/ (- w (* 21 cellw)) 2))
(set! oy boardTop)
(set! step (/ (* hudfs 3) 5))
(set! tid (ui_timer_set 120 0))
(do ()
((= quit 1))
(ui_2d_clear c_bg)
(set! i 0)
(do ()
((> i 20))
(set! j 0)
(do ()
((> j 20))
(if (= (is-wall i j) 1)
(ui_2d_rect (+ ox (* i cellw)) (+ oy (* j cellw)) cellw cellw c_wall 1 0 0)
(ui_2d_rect (+ ox (* i cellw)) (+ oy (* j cellw)) cellw cellw c_path 1 0 0))
(set! j (+ j 1)))
(set! i (+ i 1)))
(ui_2d_rect (+ ox (* 19 cellw) 1) (+ oy (* 20 cellw) 1) (- cellw 2) (- cellw 2) c_goal 1 0 2)
(ui_2d_rect (+ ox (* px cellw) 1) (+ oy (* py cellw) 1) (- cellw 2) (- cellw 2) c_ok 1 0 2)
(set! tx margin)
(if (> steps 99)
(begin (ui_2d_text tx (+ margin hudfs) (digit (/ steps 100)) c_txt hudfs 0) (set! tx (+ tx step))) 0)
(if (> steps 9)
(begin (ui_2d_text tx (+ margin hudfs) (digit (- (/ steps 10) (* (/ steps 100) 10))) c_txt hudfs 0)
(set! tx (+ tx step)))
(begin (ui_2d_text tx (+ margin hudfs) (digit 0) c_txt hudfs 0) (set! tx (+ tx step))))
(ui_2d_text tx (+ margin hudfs) (digit (- steps (* (/ steps 10) 10))) c_txt hudfs 0)
(ui_2d_text margin (+ margin hudfs sfs 3) "步数" c_dim sfs 0)
(if (= won 1)
(ui_2d_text (- (/ w 2) (* hintfs 5)) hintY "走出来了!点一下重开" c_ok hintfs 0)
(ui_2d_text (- (/ w 2) (* hintfs 5)) hintY "方向键 / 点屏幕四边" c_dim hintfs 0))
(ui_2d_present)
(set! t (ui_msg_fetch_wait 0))
(if (= t 10) (set! quit 1) 0)
(if (= t 1)
(begin
(set! key (ui_msg_get_a))
(if (= key 27) (begin (set! px 1) (set! py 0) (set! won 0) (set! steps 0)) 0)
(set! nx px)
(set! ny py)
(if (= key 37) (set! nx (- px 1)) 0)
(if (= key 39) (set! nx (+ px 1)) 0)
(if (= key 38) (set! ny (- py 1)) 0)
(if (= key 40) (set! ny (+ py 1)) 0)
(if (= won 0)
(if (= (is-wall nx ny) 0)
(begin
(set! px nx)
(set! py ny)
(set! steps (+ steps 1))
(if (= px 19) (if (= py 20) (set! won 1) 0) 0))
0)
0))
0)
(if (= t 6)
(begin
(set! ax (ui_msg_get_a))
(set! ay (ui_msg_get_b))
(if (= won 1)
(begin (set! px 1) (set! py 0) (set! won 0) (set! steps 0))
(begin
(set! dl ax)
(set! dr (- w ax))
(set! dt ay)
(set! db (- h ay))
(set! nx px)
(set! ny py)
(if (<= dl dr) (if (<= dl dt) (if (<= dl db) (set! nx (- px 1)) 0) 0) 0)
(if (> dl dr) (if (<= dr dt) (if (<= dr db) (set! nx (+ px 1)) 0) 0) 0)
(if (> dl dr) (if (> dr dt) (if (<= dt db) (set! ny (- py 1)) 0) 0) 0)
(if (> dl dr) (if (> dr dt) (if (> dt db) (set! ny (+ py 1)) 0) 0) 0)
(if (= (is-wall nx ny) 0)
(begin
(set! px nx)
(set! py ny)
(set! steps (+ steps 1))
(if (= px 19) (if (= py 20) (set! won 1) 0) 0))
0)))
0)
(if (= t 9) (begin (set! frames (+ frames 1)) (if (> frames 12000) (set! quit 1) 0)) 0)))
(ui_timer_kill tid)
(ui_scr_set_keep_on 0)
(ui_win_close)