about summary refs log tree commit diff stats
path: root/game.scm
diff options
context:
space:
mode:
Diffstat (limited to 'game.scm')
-rwxr-xr-xgame.scm136
1 files changed, 123 insertions, 13 deletions
diff --git a/game.scm b/game.scm index f3d67db..12809b7 100755 --- a/game.scm +++ b/game.scm
@@ -1,17 +1,23 @@
1#!/bin/sh 1#!/bin/sh
2#| -*- scheme -*- 2#| -*- scheme -*-
3exec csi -R r7rs -ss "$0" "$@" 3exec csi -R r7rs -ss "$0" "$@"
4game 4game --- 2023 lisp game jam
5(C) Case Duckworth <acdw@acdw.net>
6Distributed under the terms of the LATCRIFPL, v1.0.
7See COPYING for details.
5|# 8|#
6 9
7(import (scheme base) 10(import (scheme base)
11 (scheme time)
8 (scheme write) 12 (scheme write)
9 (chicken io) 13 (chicken io)
10 (chicken port) 14 (chicken port)
15 (chicken random)
11 (yolk common) 16 (yolk common)
12 (yolk xterm) 17 (yolk xterm)
13 (yolk erase) 18 (yolk erase)
14 (yolk cursor) 19 (yolk cursor)
20 (srfi 1)
15 (matchable) 21 (matchable)
16 (stty)) 22 (stty))
17 23
@@ -40,7 +46,9 @@ game
40 invisible-cursor) 46 invisible-cursor)
41 ;; Set up world 47 ;; Set up world
42 (world-init (world)) 48 (world-init (world))
43 (world-draw (world))) 49 (world-draw (world))
50 ;; Start snake
51 (snake-init (snake)))
44 52
45(define (game-cleanup) 53(define (game-cleanup)
46 ;; Restore terminal 54 ;; Restore terminal
@@ -76,6 +84,8 @@ game
76 (let ((c (integer->char (read-byte)))) 84 (let ((c (integer->char (read-byte))))
77 c)) 85 c))
78 86
87;;; Drawing stuff
88
79(define (draw . instructions) 89(define (draw . instructions)
80 (for-each (lambda (i) 90 (for-each (lambda (i)
81 (cond 91 (cond
@@ -84,7 +94,9 @@ game
84 (else (error "Don't know how to draw" i)))) 94 (else (error "Don't know how to draw" i))))
85 instructions)) 95 instructions))
86 96
87(define-record-type world 97;;; World
98
99(define-record-type <world>
88 (%make-world width height map) 100 (%make-world width height map)
89 world? 101 world?
90 (width world-width world-width-set!) 102 (width world-width world-width-set!)
@@ -146,9 +158,14 @@ game
146 (thing-place! w (wall x y))) 158 (thing-place! w (wall x y)))
147 (else))) 159 (else)))
148 world) 160 world)
149 (thing-place! world (me))) 161 (thing-place! world (me))
162 (thing-place! world (snake-head (snake)))
163 (for-each (lambda (t) (thing-place! world t))
164 (snake-tail (snake))))
165
166;;; Things
150 167
151(define-record-type thing 168(define-record-type <thing>
152 (make-thing name look x y z attrs) 169 (make-thing name look x y z attrs)
153 thing? 170 thing?
154 (name thing-name) 171 (name thing-name)
@@ -158,6 +175,20 @@ game
158 (z thing-z thing-z-set!) 175 (z thing-z thing-z-set!)
159 (attrs thing-attrs thing-attrs-set!)) 176 (attrs thing-attrs thing-attrs-set!))
160 177
178(define (thing-attr-get thing attr)
179 (let ((x (assoc attr (thing-attrs thing))))
180 (if x
181 (cdr x)
182 #f)))
183
184(define (thing-attr-set! thing attr val)
185 (let ((x (assoc attr (thing-attrs thing))))
186 (if x
187 (set-cdr! x val)
188 (thing-attrs-set! thing
189 (cons (cons attr val)
190 (thing-attrs thing))))))
191
161(define (thing-place! world thing) 192(define (thing-place! world thing)
162 (world-set! world (thing-x thing) (thing-y thing) thing)) 193 (world-set! world (thing-x thing) (thing-y thing) thing))
163 194
@@ -185,6 +216,18 @@ game
185 (set! new-y (- (world-height world) 1)))) 216 (set! new-y (- (world-height world) 1))))
186 (thing-move! world thing new-x new-y))))) 217 (thing-move! world thing new-x new-y)))))
187 218
219(define (thing-up! thing)
220 (thing-move-relative! (world) thing 0 -1))
221
222(define (thing-down! thing)
223 (thing-move-relative! (world) thing 0 1))
224
225(define (thing-right! thing)
226 (thing-move-relative! (world) thing 1 0))
227
228(define (thing-left! thing)
229 (thing-move-relative! (world) thing -1 0))
230
188(define (wall x y) 231(define (wall x y)
189 (make-thing 'wall "#" x y 100 '())) 232 (make-thing 'wall "#" x y 100 '()))
190 233
@@ -192,14 +235,81 @@ game
192 (make-parameter 235 (make-parameter
193 (make-thing 'me "@" 40 10 2 '()))) 236 (make-thing 'me "@" 40 10 2 '())))
194 237
195(define (up!) 238(define (up!) (thing-up! (me)))
196 (thing-move-relative! (world) (me) 0 -1)) 239(define (down!) (thing-down! (me)))
240(define (left!) (thing-left! (me)))
241(define (right!) (thing-right! (me)))
242
243;;; random
244
245(define random-int
246 (case-lambda
247 (() (pseudo-random-integer 100))
248 ((max) (pseudo-random-integer max))
249 ((min max)
250 (let ((max (if (> max min) max min))
251 (min (if (< min max) min max)))
252 (+ min (pseudo-random-integer (- max min)))))))
253
254(define (random-x world)
255 (random-int 1 (- (world-width world) 1)))
256
257(define (random-y world)
258 (random-int 1 (- (world-height world) 1)))
259
260;;; snake
261
262(define-record-type <snake>
263 (make-snake length head tail)
264 snake?
265 (length snake-length snake-length-set!)
266 (head snake-head snake-head-set!)
267 (tail snake-tail snake-tail-set!))
268
269(define snake
270 (make-parameter
271 (make-snake 5
272 (make-thing 'snake-head "<" 60 20 2
273 '((dir . (-1 0))))
274 (list (make-thing 'snake-tail "-" 61 20 2 '())
275 (make-thing 'snake-tail "-" 62 20 2 '())
276 (make-thing 'snake-tail "-" 63 20 2 '())
277 (make-thing 'snake-tail "-" 64 20 2 '())
278 (make-thing 'snake-tail "-" 65 20 2 '())))))
279
280(define (snake-x snake)
281 (thing-x (snake-head snake)))
282
283(define (snake-y snake)
284 (thing-y (snake-head snake)))
285
286(define (snake-z snake)
287 (thing-z (snake-head snake)))
197 288
198(define (down!) 289(define (snake-direction snake)
199 (thing-move-relative! (world) (me) 0 1)) 290 (thing-attr-get (snake-head snake) 'dir))
200 291
201(define (right!) 292(define (snake-init snake)
202 (thing-move-relative! (world) (me) 1 0)) 293 (let loop (())))
203 294
204(define (left!) 295(define (snake-step snake world)
205 (thing-move-relative! (world) (me) -1 0)) 296 (let ((new-tail (make-thing 'snake-tail
297 (if (zero? (car (snake-direction snake)))
298 "|"
299 "-")
300 (snake-x snake)
301 (snake-y snake)
302 (snake-z snake)
303 '())))
304 (apply thing-move-relative!
305 world
306 (snake-head snake)
307 (snake-direction snake))
308 (for-each (lambda (t) (world-set! world (thing-x t) (thing-y t) #f))
309 (snake-tail snake))
310 (snake-tail-set! snake
311 (take (cons new-tail
312 (snake-tail snake))
313 (snake-length snake)))
314 (for-each (lambda (t) (thing-place! world t))
315 (snake-tail snake))))