-
Notifications
You must be signed in to change notification settings - Fork 0
/
dodgeJB.bak
396 lines (297 loc) · 14.2 KB
/
dodgeJB.bak
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
#lang racket
(require test-engine/racket-tests
2htdp/image
2htdp/universe)
(require (prefix-in gui: racket/gui))
; We developed the Mondriaan program in class; we'll now expand it into
; a full blown arcade game.
;
; We'll reduce the number of rectangles flying across the screen, make
; sure they are properly removed after they fly off screen again, and
; add a player icon that can be controlled with the keyboard
; and that needs to dodge all rectangles.
; ------------------------ Data definitions ---------------------------
;; A GameState is one of:
;; - 'main-menu
;; - 'playing
;; - 'paused
;; - 'lost
;; Interpretation: used to remember what is going on in the game
(struct Rectangle [x y vx vy col wid hei] #:transparent)
; A Rectangle is a structured type: it contains
; - x, y: (Numbers) its position
; - vx, vy: (Numbers) its movement speed
; - col: its colour (a String)
; - wid, hei (Numbers): width and height of the rectangle.
(struct Player [cx cy keys] #:transparent)
; Player is a structured type which defines the position of the circle on the background
; It contains:
; - cx: a Number
; - cy: a Number
; - keys: a Keys, indicating which directional keys are currently pressed
(struct Keys [l r u d] #:transparent)
; Keys is a structured type. it contains:
; - l, r, u, d: to track which directional keys are currently pressed
(struct WorldState [score difficulty gamestate player reclst] #:transparent)
; The WorldState is a structured type: it contains
; - score: a Number, showing how many seconds have passed in a game
; - difficulty: a Number, affecting rectangle spawn rate, and movement speed of the rectangles and the player
; - gamestate: a GameState
; - player: a Player structured type
; - reclst: a list of currently shown rectangles
; ---------------------- Constant definitions -------------------------
(define WIDTH 800)
(define HEIGHT 800)
(define BG (empty-scene WIDTH HEIGHT))
(define PEN (pen "black" 5 "solid" "round" "round"))
(define COLOURS (list "red" "blue" "yellow" "green"))
(define NCOLOURS (length COLOURS))
(define PLAYER-WIDTH 24)
(define IMG-PLAYER (radial-star PLAYER-WIDTH 10 15 "outline" "darkslategray"))
(define PLAYZONE (Rectangle (/ WIDTH 2) (/ HEIGHT 2) 0 0 "white" (+ 200 WIDTH) (+ 200 HEIGHT)))
(define (PAUSE-TEXT ws)
(overlay (text/font "PAUSED" 32 "black" "Gill Sans" 'swiss 'normal 'bold #f)
(text/font (string-append "\n\n\n\n\nScore: "
(number->string (* (WorldState-difficulty ws)
(round (WorldState-score ws)))))
18 "black" "Lucida Console" 'swiss 'normal 'bold #f)))
(define START-STATE (WorldState 0
1
'main-menu
(Player (/ WIDTH 2)
(/ HEIGHT 2)
(Keys #f #f #f #f))
empty))
(define MENU-FILE "MenuScreen.png")
(define MENU-IMAGE (bitmap/file MENU-FILE))
(define LOST-FILE "LostScreen.png")
(define LOST-IMAGE (bitmap/file LOST-FILE))
; The following can be used for testing.
(define REC1 (Rectangle 400 300 2 1 "red" 40 20))
(define REC2 (Rectangle 200 100 -2 3 "yellow" 30 50))
(define REC3 (Rectangle 420 300 2 1 "blue" 40 20))
(define REC4 (Rectangle 1000 300 2 1 "black" 40 20))
(define REC5 (Rectangle 430 320 2 1 "green" 40 20))
; ------------------ Mondriaan -----------------------
; Number String Number Number -> Rectangle
; Given a number "dir" indicating one of the four screen edges, a colour,
; width and height, create a new rectangle at a random position along the
; given screen edge and with a random velocity towards the opposite side.
; dir: 0=left to right, 1=right to left, 2=top to bottom, 3=bottom to top
(define (random-rectangle-helper dir col wid hei)
(cond [(= dir 0) (Rectangle (- wid)
(random HEIGHT)
(/ (round (* 100 (+ 1 (* 2 (random))))) 100)
0
col wid hei)]
[(= dir 1) (Rectangle (+ WIDTH wid) (random HEIGHT) (/ (round (* 100 (- (+ 1 (* 2 (random)))))) 100) 0 col wid hei)]
[(= dir 2) (Rectangle (random WIDTH) (- hei) 0 (/ (round (* 100 (+ 1 (* 2 (random))))) 100) col wid hei)]
[(= dir 3) (Rectangle (random WIDTH) (+ HEIGHT hei) 0 (/ (round (* 100 (- (+ 1 (* 2 (random)))))) 100) col wid hei)]
[else (error "There are only four directions!")]))
; -> Rectangle
; Generate a random rectangle
(define (random-rectangle)
(random-rectangle-helper (random 4)
(list-ref COLOURS (random NCOLOURS))
(+ 50 (random 80))
(+ 50 (random 80))))
; Rectangle Image -> Image
; Draws a Rectangle on top of a given image
(define (draw-rectangle rec scene)
(place-image (overlay (rectangle (Rectangle-wid rec) (Rectangle-hei rec) "outline" PEN)
(rectangle (Rectangle-wid rec) (Rectangle-hei rec) "solid" (Rectangle-col rec)))
(Rectangle-x rec)
(Rectangle-y rec)
scene))
; (Listof Rectangle) -> Image
; Draws all the rectangles
(define (draw-all reclst)
(cond [(empty? reclst) BG]
[else (draw-rectangle (first reclst)
(draw-all (rest reclst)))]))
(check-expect (draw-all (list REC1 REC2))
(draw-rectangle REC1 (draw-rectangle REC2 BG)))
; WorldState Rectangle -> Rectangle
; Moves a single rectangle according to its speed vector.
(define (move-one ws rec)
(struct-copy Rectangle rec
[x (round (+ (Rectangle-x rec) (* (WorldState-difficulty ws) (Rectangle-vx rec))))]
[y (round (+ (Rectangle-y rec) (* (WorldState-difficulty ws) (Rectangle-vy rec))))]))
; WorldState (Listof Rectangle) -> (Listof Rectangle)
; Move all rectangles in the WorldState
(define (move-all ws reclst)
(cond [(empty? reclst) empty]
[else
(cons (move-one ws (first reclst))
(move-all ws (rest reclst)))]))
; ------------------ Player controls -----------------------
; Keys String Boolean -> Keys
; updates key presses
(define (update-keys keys key val)
(cond [(string=? key "d") (struct-copy Keys keys [r val])]
[(string=? key "a") (struct-copy Keys keys [l val])]
[(string=? key "w") (struct-copy Keys keys [u val])]
[(string=? key "s") (struct-copy Keys keys [d val])]
[else (struct-copy Keys keys)]))
; ------------------- Helper Functions ------------------------
; WorldState GameState -> WorldState
; Updates gamestate
(define (set-state ws gs)
(struct-copy WorldState ws
[gamestate gs]))
; WorldState GameState -> Boolean
; Returns a boolean based on which gamestate is active
(define (in-state ws gs)
(equal? (WorldState-gamestate ws) gs))
; ------------------- Collision Detection ------------------------
; Rectangle Rectangle -> Boolean
; returns #true when two rectangles overlap, #false otherwise
(define (overlaps? r1 r2)
(and (< (abs (- (Rectangle-x r1) (Rectangle-x r2)))
(+ (/ (Rectangle-wid r1) 2) (/ (Rectangle-wid r2) 2)))
(< (abs (- (Rectangle-y r1) (Rectangle-y r2)))
(+ (/ (Rectangle-hei r1) 2) (/ (Rectangle-hei r2) 2)))))
(check-expect (overlaps? REC1 REC2) #f)
(check-expect (overlaps? REC1 REC3) #t)
(check-expect (overlaps? REC3 REC5) #f)
; Rectangle -> Boolean
; Determine if the Rectangle is still in the play zone, or has flown off to eternity.
(define (visible? rec)
(overlaps? rec PLAYZONE))
(check-expect (visible? REC1) #t)
(check-expect (visible? REC4) #f)
; (ListOf Rectangle) -> (ListOf Rectangle)
; Removes all rectangles that are not visible anymore
(define (remove-invisible reclst)
(cond [(empty? reclst) empty]
[else (if (visible? (first reclst))
(cons (first reclst)
(remove-invisible (rest reclst)))
(remove-invisible (rest reclst)))]))
; Player Rectangle -> Boolean
; returns #true when the player overlaps with a single rectangle, #false otherwise
(define (collides? ply rec)
(and (< (abs (- (Rectangle-x rec) (Player-cx ply)))
(+ (/ (Rectangle-wid rec) 2) PLAYER-WIDTH -5))
(< (abs (- (Rectangle-y rec) (Player-cy ply)))
(+ (/ (Rectangle-hei rec) 2) PLAYER-WIDTH -5))))
; Player (ListOf Rectangle) -> Boolean
; returns #true when the player overlaps with any rectangle from a list, #false otherwise
(define (collision ply reclst)
(cond [(empty? reclst) #f]
[(collides? ply (first reclst)) #t]
[else
(collision ply (rest reclst))]))
; ------------------- On Tick ------------------------
; WorldState -> WorldState
; Updates Player position as well as moving all rectangles and adding new ones every now and then
(define (tick-playing ws)
(if (collision (WorldState-player ws) (WorldState-reclst ws))
(set-state ws 'lost)
(struct-copy WorldState ws
[score (+ 1/28 (WorldState-score ws))]
[player (struct-copy Player (WorldState-player ws)
[cx (+ (Player-cx (WorldState-player ws))
(if (Keys-l (Player-keys (WorldState-player ws))) (+ (* -1 (WorldState-difficulty ws)) -5) 0)
(if (Keys-r (Player-keys (WorldState-player ws))) (+ (WorldState-difficulty ws) +5) 0))]
[cy (+ (Player-cy (WorldState-player ws))
(if (Keys-u (Player-keys (WorldState-player ws))) (+ (* -1 (WorldState-difficulty ws)) -5) 0)
(if (Keys-d (Player-keys (WorldState-player ws))) (+ (WorldState-difficulty ws) +5) 0))])]
[reclst (cond [(< (random) (* (WorldState-difficulty ws) 0.05)) (cons (random-rectangle)
(move-all ws (remove-invisible (WorldState-reclst ws))))]
[else (move-all ws (WorldState-reclst ws))])])))
; WorldState -> WorldState
; Handles tick update in all states
(define (on-tick-handler ws)
(cond [(in-state ws 'playing) (tick-playing ws)]
[else ws]))
; ------------------- To Draw ------------------------
; WorldState -> Image
; Draws everything on the background for 'playing gamestate
(define (draw-playing ws)
(place-image IMG-PLAYER
(Player-cx (WorldState-player ws))
(Player-cy (WorldState-player ws))
(draw-all (WorldState-reclst ws))))
; WorldState -> Image
; Draw the paused state
(define (draw-paused ws)
(overlay (PAUSE-TEXT ws)
(square WIDTH 140 "white")
(draw-playing ws)))
; WorldState -> Image
; Draw the main menu state
(define (draw-menu ws)
(overlay (text/font (string-append "\n\n\n\n\n\n\n\n\n\n\n\n\n\n "
(number->string (WorldState-difficulty ws)))
24 "black" "Gill Sans" 'swiss 'normal 'bold #f)
(scale (/ WIDTH (image-width MENU-IMAGE)) MENU-IMAGE)))
; WorldState -> Image
; Draw the lost state
(define (draw-lost ws)
(overlay (text/font (string-append "\n\n\n\n\n\n\n\n\n\n\n\n\n\nScore: "
(number->string (* (WorldState-difficulty ws)
(round (WorldState-score ws)))))
20 "white" "Lucida Console" 'swiss 'normal 'bold #f)
LOST-IMAGE))
; WorldState -> Image
; Draws everything on the screen for given WorldState
(define (to-draw-handler ws)
(cond [(in-state ws 'playing) (draw-playing ws)]
[(in-state ws 'paused) (draw-paused ws)]
[(in-state ws 'main-menu) (draw-menu ws)]
[(in-state ws 'lost) (draw-lost ws)]))
; ------------------- On Key ------------------------
; WorldState String -> WorldState
; Handles key presses during 'playing GS
(define (key-playing ws key)
(if (equal? key "escape") (set-state ws 'paused)
(struct-copy WorldState ws
[player (struct-copy Player (WorldState-player ws)
[keys (update-keys (Player-keys (WorldState-player ws)) key #t)])])))
; WorldState String -> WorldState
; Handle key presses during 'paused GS
(define (key-paused ws key)
(cond [(equal? key "escape") (set-state ws 'main-menu)]
[(equal? key " ") (set-state ws 'playing)]
[else ws]))
; WorldState String -> WorldState
; Handle key presses during 'main-menu GS
(define (key-menu ws key)
(cond [(equal? key " ") (set-state (struct-copy WorldState START-STATE [difficulty (WorldState-difficulty ws)]) 'playing)]
[(equal? key "1") (struct-copy WorldState ws [difficulty 1])]
[(equal? key "2") (struct-copy WorldState ws [difficulty 2])]
[(equal? key "3") (struct-copy WorldState ws [difficulty 3])]
[else ws]))
; WorldState String -> WorldState
; Handle key presses during 'lost GS
(define (key-lost ws key)
(cond [(equal? key "escape") (set-state ws 'main-menu)]
[(equal? key " ") (set-state (struct-copy WorldState START-STATE [difficulty (WorldState-difficulty ws)]) 'playing)]
[else ws]))
; WorldState String -> WorldState
; Handles key presses at all times
(define (on-key-handler ws key)
(cond [(in-state ws 'playing) (key-playing ws key)]
[(in-state ws 'paused) (key-paused ws key)]
[(in-state ws 'main-menu) (key-menu ws key)]
[(in-state ws 'lost) (key-lost ws key)]
[else ws]))
; WorldState String -> WorldState
; Handles key release during 'playing GS
(define (on-release-handler ws key)
(if (in-state ws 'playing)
(struct-copy WorldState ws
[player (struct-copy Player (WorldState-player ws)
[keys (update-keys (Player-keys (WorldState-player ws)) key #f)])])
ws))
; ------------------- Big Bang ------------------------
; -> WorldState
; Starts the game
(define (start)
(big-bang START-STATE
[to-draw to-draw-handler]
[on-key on-key-handler]
[on-release on-release-handler]
[on-tick on-tick-handler]))
(test)