-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal Game.rkt~
341 lines (279 loc) · 10.4 KB
/
Final Game.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
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-advanced-reader.ss" "lang")((modname |Final Game|) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #t #t none #f () #f)))
;; Call (game) to start the game
;; In this game a stationary player has to shoot a stationary target but there will be a horizontal wind that the player must account for, which changes randomly every time the target is hit.
;; Make methods for some of the structs
(require 2htdp/image)
(require 2htdp/universe)
(require "struct-inheritance.rkt")
;; Called on each tick to update the lifetime of the missile
;; and destroy it if the lifetime is zero.
(define (update-missile! m)
(begin
(if (= 0 (missile-lifetime m))
(destroy! m)
(set-missile-lifetime! m (- (missile-lifetime m) 1)))
(set-game-object-velocity! m (posn-+ wind-velocity (game-object-velocity m)))))
;; Called when the left arrow key is pressed
(define (on-left-press)
(set-game-object-rotational-velocity! the-player (- 2)))
;; Called when the left arrow key is released
(define (on-left-release)
(set-game-object-rotational-velocity! the-player 0))
;; Called when the right arrow key is pressed
(define (on-right-press)
(set-game-object-rotational-velocity! the-player 2))
;; Called when the right arrow key is released
(define (on-right-release)
(set-game-object-rotational-velocity! the-player 0))
;; Called when the space bar is pressed.
;; There's no action to take when the space bar is released.
(define (on-space-press)
(fire-missile!))
;;;
;;; Turnable constants
;;;
(define window-width 800)
(define window-height 600)
(define frame-rate 30)
(define inter-frame-interval (/ 1.0 frame-rate))
(define score 0)
(define wind-velocity (make-posn 0 0))
;;;
;;; Type definitions
;;;
(define-struct game-object
([position #:mutable]
[velocity #:mutable]
[orientation #:mutable]
[rotational-velocity #:mutable]
radius))
(define-struct (player game-object) ())
(define-struct (missile game-object)
([lifetime #:mutable]))
(define-struct (target game-object)())
;;;
;;; Tracking game objects
;;;
(define all-game-objects '())
(define (destroy! object)
(set! all-game-objects
(remove object all-game-objects)))
(define the-player '())
;;;
;;; Object creation
;;;
(define (new-target)
(make-target (make-posn (/ window-width 2) 25)
(make-posn 0 0)
0
0
20))
(define (fire-missile!)
(local [(define forward (forward-direction the-player))]
(set! all-game-objects
(cons (make-missile (posn-+ (game-object-position the-player)
(posn-* (+ (game-object-radius the-player) 5)
forward))
(posn-* 180 forward)
0
0
2
100)
all-game-objects))))
;;;
;;; Driver loop
;;;
(define (game)
(begin (set! the-player
(make-player (make-posn (/ window-width 2)
(- window-height 25))
(make-posn 0 0)
4.7
0
20))
(set! all-game-objects
(cons the-player
all-game-objects))
(set! all-game-objects
(cons (new-target)
all-game-objects))
(big-bang all-game-objects
(on-key (λ (ignore key)
(begin (on-key-press key)
all-game-objects)))
(on-release (λ (ignore key)
(begin (on-key-release key)
all-game-objects)))
(on-tick (lambda (game-objects)
(begin (for-each update! game-objects)
(update-physics!)
all-game-objects))
inter-frame-interval)
(to-draw (lambda (game-objects)
(place-image (text (string-append "SCORE: " (number->string score)) 30 "purple")
70
30
(place-image (text (string-append "WIND: " (number->string (round (* 10 (posn-x wind-velocity))))) 20 "purple")
70
70
(foldl (lambda (object scene)
(place-image (rotate (radians->rotation (game-object-orientation object))
(render object))
(posn-x (game-object-position object))
(posn-y (game-object-position object))
scene))
(rectangle window-width window-height "solid" "white")
game-objects))))
800
600))))
;;;
;;; Event dispatch
;;;
(define (on-key-press key)
(cond [(equal? key "left")
(on-left-press)]
[(equal? key "right")
(on-right-press)]
[(equal? key " ")
(on-space-press)]
[else null]))
(define (on-key-release key)
(cond [(equal? key "left")
(on-left-release)]
[(equal? key "right")
(on-right-release)]
[else null]))
;;;
;;; Rendering (drawing on the screen)
;;;
(define (render object)
(cond [(player? object)
(isosceles-triangle 30 40 "solid" "aquamarine")]
[(missile? object)
(circle 2 "solid" "black")]
[(target? object)
(circle 20 "solid" (random-color))]))
(define radians->rotation-coefficient
(/ -360.0
(* 2 pi)))
(define (radians->rotation radians)
(+ (* radians radians->rotation-coefficient)
-90))
;;;
;;; State update
;;;
(define (update! object)
(when (missile? object)
(update-missile! object)))
(define (update-physics!)
(begin (for-each (λ (object)
(begin (set-game-object-orientation! object
(+ (game-object-orientation object)
(* inter-frame-interval
(game-object-rotational-velocity object))))
(set-game-object-position! object
(local [(define new-position
(posn-+ (posn-* inter-frame-interval
(game-object-velocity object))
(game-object-position object)))]
(make-posn (wrap (posn-x new-position) window-width)
(wrap (posn-y new-position) window-height))))))
all-game-objects)
(handle-collisions all-game-objects)))
;;;
;;; Collision handling
;;;
(define (handle-collisions objects)
(unless (empty? objects)
(local [(define head (first objects))
(define tail (rest objects))]
(begin (for-each (λ (object)
(when (collided? head object)
(handle-collision head object)))
tail)
(handle-collisions tail)))))
(define (collided? a b)
(< (distance-squared (game-object-position a)
(game-object-position b))
(squared (+ (game-object-radius a)
(game-object-radius b)))))
(define (handle-collision a b)
(cond [(and (target? a)
(missile? b))
(begin (destroy! b)
(set! score (+ score 1))
(set! wind-velocity (random-wind)))]
[(and (target? b)
(missile? a))
(begin (destroy! a)
(set! score (+ score 1))
(set! wind-velocity (random-wind)))]
[(and (missile? a)
(missile? b))
(begin
(destroy! a)
(destroy! b))]
[(and (player? a)
(missile? b))
(destroy! b)]
[(and (missile? a)
(player? b))
(destroy! a)]))
;;;
;;; Vector arithmetic
;;;
(define (posn-+ a b)
(make-posn (+ (posn-x a)
(posn-x b))
(+ (posn-y a)
(posn-y b))))
(define (posn-* k p)
(make-posn (* k (posn-x p))
(* k (posn-y p))))
(define (distance-squared p1 p2)
(+ (squared (- (posn-x p1)
(posn-x p2)))
(squared (- (posn-y p1)
(posn-y p2)))))
(define (forward-direction object)
(local [(define o (game-object-orientation object))]
(make-posn (cos o)
(sin o))))
;;;
;;; Randomization
;;;
(define random-color
(local [(define colors
(list (color 255 0 0)
(color 0 255 0)
(color 0 0 255)
(color 128 128 0)
(color 128 0 129)
(color 0 128 128)))]
(λ () (random-element colors))))
(define (random-element list)
(list-ref list
(random (length list))))
(define (random-float min max)
(+ min
(* (random)
(- max min))))
(define (random-velocity)
(make-posn (random-float -10 10)
(random-float -10 10)))
(define (random-wind)
(make-posn (- (* (random) 3) 1.5) 0))
;;;
;;; Other arithmetic utilities
;;;
(define (wrap number limit)
(cond [(< number 0)
(+ number limit)]
[(> number limit)
(- number limit)]
[else
number]))
(define (squared x)
(* x x))