Debian Codegraph @

examples/snake.scm

raw
1 ;;
2 ;; snake game - sample for raw console manipulation
3 ;;
4
5 (use gauche.termios)
6 (use gauche.array)
7 (use util.match)
8 (use srfi.27)
9 (use text.console)
10
11 (define (main args)
12 (call-with-console (guard (e [else (exit 1 (condition-message e))])
13 (make-default-console))
14 game)
15 0)
16
17 ;; snake : (<dir> <head> <tail> ...)
18 ;; <head> : <pt>
19 ;; <tail> : <pt>
20 ;; <pt> : (<x> . <y>)
21 ;; <dir> : N | E | W | S
22 (define (snake-dir snake) (car snake))
23 (define (snake-body snake) (cdr snake))
24 (define (snake-head snake) (car (snake-body snake)))
25 (define (snake-tail snake) (cdr (snake-body snake)))
26
27 ;; NB: For the time being, we avoid writing the wall to the last row;
28 ;; Writing on the bottom-right corner causes the screen to scroll in
29 ;; <windows-console>. We can avoid it by SetConsoleMode but currently we're
30 ;; not sure how to handle it in the console abstraction layer (we don't want
31 ;; the wrap-to-next-line behavior off by default).
32 (define (game con)
33 (hide-cursor con)
34 (clear-screen con)
35 (random-source-randomize! default-random-source)
36 (receive (row col) (query-screen-size con)
37 (let* ([field (new-field (- row 1) col)]
38 [snake (new-snake field)])
39 (render-field con field)
40 (run-game con field snake #f (new-food field snake) 0))))
41
42 (define (collide-wall? field x y) (array-ref field y x))
43
44 (define (collide-snake? snake x y)
45 (any (^p (and (eqv? (car p) x) (eqv? (cdr p) y))) (cdr snake)))
46
47 (define (new-field row col)
48 (rlet1 field (make-array (shape 0 row 0 col) #f)
49 (dotimes [k col]
50 (array-set! field 0 k #t)
51 (array-set! field (- row 1) k #t))
52 (dotimes [k row]
53 (array-set! field k 0 #t)
54 (array-set! field k (- col 1) #t))
55 (dotimes [k (* (quotient row 5) 2)]
56 (array-set! field k (ash col -1) #t)
57 (array-set! field (- row k 1) (ash col -1) #t))))
58
59 (define (new-snake field)
60 (let ([row (array-end field 0)]
61 [col (array-end field 1)])
62 (let* ([t (cons (random-integer col) (random-integer row))]
63 [dir (~ '(N E W S) (random-integer 4))]
64 [h (next-point t dir)])
65 (if (or (collide-wall? field (car t) (cdr t))
66 (collide-wall? field (car h) (cdr h)))
67 (new-snake field)
68 `(,dir ,h ,t)))))
69
70 (define (update-snake snake dir new-head extend?)
71 `(,dir ,new-head
72 ,@(if extend? (snake-body snake) (drop-right (snake-body snake) 1))))
73
74 (define (new-food field snake) ; returns food location (x . y)
75 (let ([row (array-end field 0)]
76 [col (array-end field 1)])
77 (let ([x (random-integer col)]
78 [y (random-integer row)])
79 (if (or (collide-wall? field x y)
80 (collide-snake? snake x y))
81 (new-food field snake)
82 (cons x y)))))
83
84 (define (next-point point dir)
85 (match-let1 (x . y) point
86 (cons (case dir [(E) (+ x 1)] [(W) (- x 1)] [else x])
87 (case dir [(S) (+ y 1)] [(N) (- y 1)] [else y]))))
88
89 (define (find-food? snake dir food)
90 (equal? food (next-point (snake-head snake) dir)))
91
92 (define (run-game con field snake prev-snake food score)
93 (render con field snake prev-snake food score)
94 ;; trick - since height of character is larger than width, if we use the
95 ;; same interval, it would look like the snake runs faster in N-S direction
96 ;; than E-W direction.
97 (case (snake-dir snake)
98 [(N S) (sys-nanosleep #e18e7)]
99 [(E W) (sys-nanosleep #e9e7)])
100 (let1 dir (or (and-let* ([newdir (get-dir con)])
101 (case (snake-dir snake) ; do now allow to turn 180 deg.
102 [(N) (and (not (eq? newdir 'S)) newdir)]
103 [(E) (and (not (eq? newdir 'W)) newdir)]
104 [(S) (and (not (eq? newdir 'N)) newdir)]
105 [(W) (and (not (eq? newdir 'E)) newdir)]))
106 (snake-dir snake))
107 (match-let1 (and (x . y) hd) (next-point (snake-head snake) dir)
108 (cond [(or (collide-wall? field x y)
109 (collide-snake? snake x y))
110 (render con field (update-snake snake dir hd #f) snake food score)
111 (game-over con field)]
112 [(find-food? snake dir food)
113 (let1 snake. (update-snake snake dir hd #t)
114 (run-game con field snake. snake
115 (new-food field snake.) (+ score 1)))]
116 [else
117 (run-game con field (update-snake snake dir hd #f) snake
118 food score)]))))
119
120 (define (get-dir con) ;returns W, S, N, E or #f
121 (and (chready? con)
122 (case (getch con)
123 [(#\h KEY_LEFT) 'W]
124 [(#\j KEY_DOWN) 'S]
125 [(#\k KEY_UP) 'N]
126 [(#\l KEY_RIGHT) 'E]
127 [else #f])))
128
129 (define (render con field snake prev-snake food score)
130 (set-character-attribute con '(green black bright))
131 (render-snake con snake prev-snake)
132 (set-character-attribute con '(magenta black bright))
133 (render-point con food #\o)
134 (set-character-attribute con '(white black))
135 (move-cursor-to con
136 (array-length field 0) ;the bottom row
137 5)
138 (putstr con (format "S C O R E : ~3d" score)))
139
140 (define (render-field con field)
141 (set-character-attribute con '(black black))
142 (clear-screen con)
143 (set-character-attribute con '(cyan blue))
144 ($ array-for-each-index field
145 (^[y x]
146 (when (array-ref field y x)
147 (move-cursor-to con y x)
148 (putch con #\#)))))
149
150 (define (render-snake con snake prev-snake)
151 ;; We only need to draw difference of snake and prev-snake.
152 ;; NB: prev-snake can be #f for the initial state.
153 (render-point con (snake-head snake) #\@)
154 (if prev-snake
155 (begin
156 (when (= (length (snake-body snake)) (length (snake-body prev-snake)))
157 (render-point con (last (snake-tail prev-snake)) #\space))
158 (render-point con (snake-head prev-snake) #\*))
159 (dolist [pt (snake-tail snake)]
160 (render-point con pt #\*))))
161
162 (define (render-point con pt ch)
163 (match-let1 (x . y) pt
164 (move-cursor-to con y x)
165 (putch con ch)))
166
167 (define (game-over con field)
168 (let ([height (array-length field 0)]
169 [width (array-length field 1)])
170 (move-cursor-to con (ash height -1) (- (ash width -1) 5))
171 (set-character-attribute con '(white black reverse))
172 (putstr con "Game over!")
173 ;; Exit game with any keypress; however, we don't want to pick
174 ;; the keypress right after game over, so wait for a sec, discard
175 ;; whatever keys pressed during that, then wait for input.
176 (sys-sleep 1)
177 (while (chready? con) (getch con))
178 (getch con)
179 (reset-terminal con)))