/
derby.rkt
406 lines (304 loc) · 11.6 KB
/
derby.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#lang racket
(require r-cade)
(require racket/random)
;; ----------------------------------------------------
(define horse-sprite
#((#x001c #x003e #x007e #x7ff0 #x8fe0 #x0fe0 #x1ff8 #x2836 #xc809 #x1008 #x2000)
(#x000c #x001e #x003f #x3ff2 #xcfe0 #x0fe0 #x1fe0 #x1820 #x2850 #xc890 #x1110)
(#x0006 #x000f #x001f #x1ff8 #xeff0 #x0fe0 #x1fe0 #x2860 #x1850 #x0de0 #x0840)
(#x000c #x001e #x003f #x9ff2 #x6fe0 #x0fe0 #x0ff0 #x1c30 #x1418 #x22e4 #x2100)))
;; ----------------------------------------------------
(define jockey-sprite '(#x06 #x06 #x7c #xf6 #xf0 #x38 #x08 #x10 #x38))
;; ----------------------------------------------------
(define rail-sprite
#((#xff #xff #x18 #x18 #x18 #x18 #x18)
(#xff #xff)
(#xff #xff)
(#xff #xff)))
;; ----------------------------------------------------
(define bush-sprite '(#x34 #x7e #xff #xff #x7e))
;; ----------------------------------------------------
(define berries-sprite #((#x00 #x20 #x04 #x10)
(#x00 #x14 #x40 #x08)))
;; ----------------------------------------------------
(define ribbon-sprite '(#x3c #x7e #xff #xff #xff #xff #x7e #x3c #x18 #x3c #x3c #x66 #x66 #x66 #x66))
;; ----------------------------------------------------
(define trumpet (synth (sin 1)
(triangle-wave 0.2)
(noise-wave 0.15)))
;; ----------------------------------------------------
(define call-to-post (music "A3---D4---F---A---A-A--F---F-F---D---F---D---A3---------------"
#:tempo 900
#:voice (voice trumpet (envelope 0 1 0.7 0.7 0.2 0.8 0.2 0.8 0))))
;; ----------------------------------------------------
(define clop-sound (tone 50 0.05 (voice square-wave (envelope 0 0.2 0.15 0))))
;; ----------------------------------------------------
(define gun-sound (tone 600 1 (voice (synth (triangle-wave 0.7) (noise-wave 0.4)) z-envelope)))
;; ----------------------------------------------------
(define positions #("1st" "2nd" "3rd" "4th" "5th" "6th" "7th" "8th" "9th"))
;; ----------------------------------------------------
(struct horse [color
jockey
lane
aggro
min-stamina
(time #:mutable)
(stamina #:mutable)
(recover #:mutable)
(frame #:mutable)
(x #:mutable)
(vel #:mutable)
(fps #:mutable)
(position #:mutable)])
;; ----------------------------------------------------
(struct bush [x berries color])
;; ----------------------------------------------------
(define player #f)
(define recover-timer 0)
(define horses null)
(define bushes null)
(define start-x -36)
(define start-vel 16)
(define start-fps 10)
(define finish-line 640)
(define score 0)
(define ribbons null)
(define race-started #f)
;; ----------------------------------------------------
(define action-btn btn-z)
;; ----------------------------------------------------
(define (track-offset)
(max (inexact->exact (floor (- (* (horse-x player) 4) 32))) 0))
;; ----------------------------------------------------
(define (railing-offset)
(- (remainder (track-offset) 32)))
;; ----------------------------------------------------
(define (draw-railing)
(for ([segment (range (+ (quotient (width) 32) 2))])
(for ([sprite rail-sprite]
[offset (in-naturals)])
(let ([x (+ (railing-offset) (* segment 32) (* offset 8))])
(color (if (odd? offset) 8 7))
(draw x 19 sprite)
(draw x 103 sprite)))))
;; ----------------------------------------------------
(define (draw-finish-line)
(let ([x (+ (- (* finish-line 4) (track-offset)) 4)])
(color 8)
(rect x 26 0 77)))
;; ----------------------------------------------------
(define (draw-bushes)
(for ([b bushes])
(let ([x (+ (- (* (bush-x b) 4) (track-offset)) 4)])
(color 11)
(draw x 12 bush-sprite)
(color (bush-color b))
(draw x 12 (bush-berries b)))))
;; ----------------------------------------------------
(define (draw-track)
(color 4)
(rect 0 0 (width) 8 #:fill #t)
(color 15)
(rect 0 7 (width) 9 #:fill #t)
; back grass
(color 3)
(rect 0 16 (width) 4 #:fill #t)
; railing lane
(color 11)
(rect 0 20 (width) 8 #:fill #t)
; racing lanes
(for ([lane (range 9)])
(let ([y (+ 28 (* lane 8))])
(color (if (odd? lane) 11 3))
(rect 0 y (width) 8 #:fill #t)))
; railing lane
(color 11)
(rect 0 100 (width) 8 #:fill #t)
; front grass
(color 3)
(rect 0 108 (width) 4 #:fill #t))
;; ----------------------------------------------------
(define (horse-anim-frame h)
(bitwise-and (inexact->exact (floor (horse-frame h))) 3))
;; ----------------------------------------------------
(define (draw-horse h)
(let* ([frame (horse-anim-frame h)]
[y (+ 22 (* (horse-lane h) 8))]
[x (cond
[(< (horse-x h) 32) (horse-x h)]
[(eq? h player) 32]
[else (+ 32 (- (horse-x h) (horse-x player)))])])
(color (horse-color h))
(draw-ex x y (vector-ref horse-sprite frame))
(color (horse-jockey h))
(draw (+ x 5) (- y 3) jockey-sprite)))
;; ----------------------------------------------------
(define (draw-horses)
(for ([h horses])
(draw-horse h))
(draw-horse player))
;; ----------------------------------------------------
(define (draw-stamina)
(let ([stamina (horse-stamina player)])
(color 7)
(text 1 1 "Stamina:")
(rect 33 1 stamina 5 #:fill #t)
(color 8)
(rect (+ stamina 33) 1 (- 100 stamina) 5 #:fill #t)))
;; ----------------------------------------------------
(define (draw-place)
(color 7)
(text (- (width) 12) 1 (if (not race-started)
"-"
(vector-ref positions (horse-position player)))))
;; ----------------------------------------------------
(define (use-crop h)
(let ([ds (/ (horse-vel h) 5)])
(set-horse-stamina! h (max (- (horse-stamina h) ds) 0))
(set-horse-recover! h 0.25)) ; (/ (- 100 (horse-stamina h)) 50)))
; reset timer
(set-horse-time! h 0.0)
; increase velocity of player's horse
(let ([m (/ (min (horse-stamina h) 50) 50)])
(set-horse-vel! h (+ (horse-vel h) (* (frametime) 25 m)))
(set-horse-fps! h (+ (horse-fps h) (* (frametime) 20 m)))))
;; ----------------------------------------------------
(define (recover-stamina h)
(if (> (horse-recover h) 0)
(set-horse-recover! h (max (- (horse-recover h) (frametime)) 0.0))
(set-horse-stamina! h (min (+ (horse-stamina h) (* (frametime) 50)) 100))))
;; ----------------------------------------------------
(define (place-horses)
(let ([order (sort (cons player horses) > #:key horse-x)])
(for ([h (cons player horses)])
(unless (> (+ (horse-x h) 16) finish-line)
(set-horse-position! h (index-of order h eq?))))))
;; ----------------------------------------------------
(define (advance-horse h)
(set-horse-x! h (+ (horse-x h) (* (horse-vel h) (frametime))))
(set-horse-frame! h (+ (horse-frame h) (* (horse-fps h) (frametime))))
(set-horse-time! h (+ (horse-time h) (frametime)))
; slow down horse
(let ([decay (* (frametime) (if (eq? h player)
0.75
(- 0.90
(* (random) 0.2)
(* (length ribbons) 0.01))))])
(set-horse-fps! h (max (- (horse-fps h) decay) start-fps))
(set-horse-vel! h (max (- (horse-vel h) decay) start-vel))))
;; ----------------------------------------------------
(define (advance-horses)
(for ([h horses])
(advance-horse h)
; randomly use the crop
(when (> (horse-x h) 32)
(let ([r (horse-recover h)]
[m (horse-min-stamina h)]
[a (horse-aggro h)])
(if (or (and (> r 0.0) (> (horse-stamina h) m) (> (horse-time h) a))
(and (= r 0.0) (> (horse-stamina h) (- 100 m))))
(use-crop h)
(recover-stamina h)))))
(advance-horse player))
;; ----------------------------------------------------
(define (spawn-horse color jockey lane)
(let* ([level (+ (length ribbons) 1)]
[aggro (* 1/60 (+ 4 (* (random) 3)))]
[min-sta (apply min (for/list ([_ (range level)])
(* (random) 100)))])
(horse color jockey lane aggro min-sta 0.0 100 0.0 0 start-x start-vel start-fps 0)))
;; ----------------------------------------------------
(define (spawn-bushes [offset (random 10)])
(if (> offset (+ finish-line (width)))
null
(let ([berries (random-ref berries-sprite)])
(cons (bush offset berries (vector-ref #(8 12 14 10) (random 4)))
(spawn-bushes (+ offset (random 10) 4))))))
;; ----------------------------------------------------
(define (next-race)
(cls)
(play-music call-to-post #:loop #f)
; ready the next level...
(set! race-started #f)
(set! player (spawn-horse 9 10 8))
(set! bushes (spawn-bushes))
(set! horses (for/list ([lane (range 8)]
[color (list 1 4 6 8 10 12 13 14)]
[jockey (list 12 9 1 14 0 5 7 2)])
(spawn-horse color jockey lane))))
;; ----------------------------------------------------
(define (ribbon-color pos)
(vector-ref #(12 8 10 7 14 11) pos))
;; ----------------------------------------------------
(define (award-ribbon)
(cls)
; draw past ribbons at bottom
(for ([r ribbons] [n (in-naturals)])
(let-values ([(y x) (quotient/remainder n 14)])
(color (ribbon-color r))
(draw (+ 6 (* x 12)) (+ 48 (* y 18)) ribbon-sprite)))
; draw the ribbon and award $$
(let ([pos (horse-position player)])
(if (< pos 6)
(let ([winnings (vector-ref #(1000 500 400 250 100 50) pos)])
(color (ribbon-color pos))
(draw (- (/ (width) 2) 4) 10 ribbon-sprite)
; award $$ based on position
(set! score (+ score winnings))
(set! ribbons (cons pos ribbons))
; show current winnings
(color 7)
(text 50 30 (format "You won $~a !!" winnings))
(text 50 38 (format "Total winnings: $~a" score))
(text 40 104 "Press START for next race...")
; wait to advance to the next race
(wait btn-start)
(next-race))
; game over
(begin
(color 7)
(text 6 10 "GAME OVER")
(text 6 18 "Better luck next time!")
(text 6 34 (format "Final winnings: $~a" score))
(text 71 104 "My ribbons")
(wait)
(quit)))))
;; ----------------------------------------------------
(define (game-loop)
(draw-track)
(draw-bushes)
(draw-railing)
(draw-finish-line)
(draw-horses)
(draw-stamina)
(draw-place)
; advance horses
(advance-horses)
(place-horses)
; hoof sounds
(when (= (horse-anim-frame player) 1)
(play-sound clop-sound))
; gun sound
(unless race-started
(when (> (horse-x player) 32)
(play-sound gun-sound)
(set! race-started #t)))
; player controls
(when (> finish-line (horse-x player) 32)
(if (action-btn)
(begin
(use-crop player)
(set! action-btn (if (eq? action-btn btn-z) btn-x btn-z)))
(recover-stamina player)))
; end of race?
(when (> (horse-x player) (+ finish-line 100))
(award-ribbon))
; quit game?
(when (btn-quit)
(quit)))
;; ----------------------------------------------------
(define (play)
(run game-loop 180 112 #:init next-race #:scale 3 #:title "R-cade: Derby"))
;; ----------------------------------------------------
(module+ main
(play))