-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpyHunter.rkt
More file actions
2423 lines (2278 loc) · 96.1 KB
/
SpyHunter.rkt
File metadata and controls
2423 lines (2278 loc) · 96.1 KB
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#lang racket
(require 2htdp/image
test-engine/racket-tests
2htdp/universe
"constants.rkt"
"graphics.rkt")
#|
TODO reorder parameters wherever vehicles are constructed ;; done
TODO replace every x/y accessor with vehicle-x or vehicle-y ;; done
TODO write a vehicle->LTRB function and use it to eliminate duplicate uses of compute-ltrb
TODO replace velocity accessors
TODO look up struct-copy in the documentation and consider using it to simplify functions
TODO consider splitting render code into a separate file as I did with graphics.rkt
TODO consider splitting LTRB code into a separate file too
TODO make horizontal velocity change in collision response
TODO diagnose collision bug
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Constant data
;;; Images:
-- player's car ;;DONE
-crash image
-- helper truck ;;DONE
- oil slick icon ;;DONE
- smoke screen icon ;;DONE
-- small enemy ;;DONE
-crash image
-- bullet-proof enemy ;;DONE
-- Background ;;DONE
-- splashscreen ;;DONE
-- gameover ;;DONE
-- Bullet ;;DONE
-- oil slick ;;DONE
-- smoke screen ;;DONE
;;; others
-- velocity of friendly cars
-- number of smoke screens obtained from truck
-- duration of smoke screen
-- INITIAL time of oil slick
-- starting position of helper trucks
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Dynamic data
-- score
-- time left on oil slick (when oil slick activated, countdown starts. and it
stops when oil slick deactivated)
;;; Velocities
-- bullet velocity ;; UNLESS IT IS JUST AN IMAGE ABOVE CAR
-- enemy velocity
-- velocity of the background (because of the player's car)
-- velocity of helper truck
|#
;; a spy is a
;; (spy Num Num Nat Num Num)
(define-struct spy [x y vel osleft ssleft] #:transparent)
;; where x is the spy's x coordinate,
;; y is the car's y coordinate,
;; vel is the spy's velocity,
;; osleft is the number of oilslicks the spy has,
;; and ssleft is the number of smokescreens the spy has left.
(define spy1 (spy 300 400 0 0 0))
(define spy2 (spy 200 400 11 2 3))
(define starting-spy (spy 300 400 5 0 0))
;; A Vehicle is a
;; (vehicle Nat Int Int Int)
;; where x is the vehicle's x-coordinate
;; y is the vehicle's y-coordinate
;; dx is the vehicle's x velocity and
;; dy is the vehicle's y velocity
(struct vehicle [x y dx dy] #:transparent)
;; Vehicle subtypes:
;; a FriendlyCar is a (FriendlyCar Nat Int Int Int)
(struct FriendlyCar vehicle [])
;; a small-enemy is a (small-enemy Nat Int Int Int)
(struct small-enemy vehicle [])
;; a large-enemy is a (large-enemy Nat Int Int Int)
(struct large-enemy vehicle [])
;; a hlpr-truck is a
;; (hlpr-truck Nat Int Int Int)
(struct hlpr-truck vehicle [gadget loading-spy-car?])
;; gadget is a symbol representing which gadget the truck is carrying
;; gadgets --> 'smokescreen or 'oilslick
;; loading-spy-car? : #t iff the spy car is in position to be loaded.
;; a Listof[vehicles] (LOV) is either
;; empty
;; (cons vehicle LOV)
;; vehicle-type : vehicle -> VehicleType
;; produces a symbol identifying the kind of vehicle: one of these:
;; 'friendly 'small-enemy 'large-enemy 'truck
;; or signals an error if unknown
(define (vehicle-type v)
(cond [(FriendlyCar? v) 'friendly]
[(small-enemy? v) 'small-enemy]
[(large-enemy? v) 'large-enemy]
[(hlpr-truck? v) 'truck]
[else (error 'vehicle-type "unknown vehicle ~a" v)]))
;; AList[VehicleType, Int+]
(define VEHICLE-WIDTHS
'((friendly 33)
(small-enemy 33)
(large-enemy 46)
(truck 33)))
(define VEHICLE-HEIGHTS
'((friendly 53)
(small-enemy 53)
(large-enemy 62)
(truck 109)))
(define VEHICLE-IMAGES
`((friendly ,frnd)
(small-enemy ,sml-enemy)
(large-enemy ,lrg-enemy)
(truck ,truck)
)) ;; TODO complete this table
;; vehicle-width : vehicle -> Int+
(define (vehicle-width v) (vehicle-lookup v VEHICLE-WIDTHS))
(define (vehicle-height v) (vehicle-lookup v VEHICLE-HEIGHTS))
(define (vehicle-image v) (vehicle-lookup v VEHICLE-IMAGES))
;; vehicle-lookup : vehicle AList[VehicleType, x] --> x
(define (vehicle-lookup v table)
(cond [(assq (vehicle-type v) table)
=> second]
[else
(error 'vehicle-lookup "unknown vehicle")]))
;; a crash is a
;; (crash Num Num Num Num Image)
(struct crash [x y dx dy img] #:transparent) ;; wanted to make this a crash,
;; but I can't figure out how to deal with the image changing
;; oilslick is a
;; (make-os Num Num)
(struct os [x y] #:transparent)
;; where x is the oilslick's x coordinate and
;; y is the oilslick's y coordinate
;; smokescreen is a
;; (make-ss x y Num)
(struct ss [x y duration])
;; shot is a
;; (shot Num Num
(struct shot [x y] #:transparent)
;; where x is the shots x coordinate,
;; x is the shots y coordinate
(define shot1 (shot 150 380))
(define shot2 (shot 150 360))
(define shot3 (shot 150 340))
;; a List-of[Objects] (LOO) is one of:
;; empty
;; (cons object LOO)
(define LOO1 (list (os 300 400)
(ss 200 500 1)
(small-enemy 300 790 0 2)
(large-enemy 300 200 0 2)
(hlpr-truck 200 150 -2 3 'os #f)
(FriendlyCar 400 150 0 5)))
(define LOO2 (list (os 300 400)
(ss 200 500 1)
(small-enemy 300 790 0 2)
(large-enemy 300 200 0 2)
(hlpr-truck 200 150 -2 3 'os #f)
(FriendlyCar 400 150 0 5)
(crash 200 460 -5 25 sml-enemy)))
;; a Listof[Num] (LON) is either
;; empty
;; (cons Num LON)
#;(define (fun-for-LON ls)
(cond [(empty? ls) ...]
[(cons? ls) ...]))
;; a Listof[Shot] (LOS) is either
;; empty or
;; (cons shot LOS)
(define LOS1 (list shot1 shot2 shot3))
;; a Listof[enemy] LOE is either
;; empty
;; (cons small-enemy LOE) or
;; (cons large-enemy LOE)
;; a car is either a
;; FriendlyCar
;; small-enemy
;; spy or
;; large-enemy
;; a Listof[car] LOC is either
;; empty or
;; (cons car LOC)
;; a gadget is either a
;; ss or an os
;; a Listo-of[Gadget] (LOG) is either
;; empty or
;; (cons gadget LOG)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;world defintion
;; A SpyGame is a
;; (splash Image) or
;; (shg Nat Nat Nat Nat spy List-of[Object Lis-of[Shot] Num]) or
;; (gameover Nat)
(define-struct splash [bg] #:transparent)
;; where bg is an image set as the splashscreen
(define-struct shg [lives score spy objects shots dtop] #:transparent)
;; where lives is the number of lives a player has left,
;; score is the player's score,
;; objects is a list of all the objects in the game, and
;; shots is the list of bullets fired by spy
;; and dtop is the y coordinate of the top of the background
;; the top of the screen is at y=0
;; example shg
(define starting-shg (shg 3 0 starting-spy '() '() -10))
(define shg1 (shg 3 0 spy1 '() '() -10))
(define shg2 (shg 2 45 spy2 LOO1 LOS1 -20))
(define shg3 (shg 1 10 spy1 '() '() -1))
(define shg4 (shg 0 293 spy1 '() '() 900))
(define shg5 (shg 2 45 spy2 LOO1 LOS1 0))
(define shg6 (shg 1 95 spy2 '() '() 803))
(define-struct gameover [score] #:transparent)
;; where score is the score the player had when they died
;; fun-for-shg: shg --> ?
#; (define (fun-for-shg s)
(... (shg-lives s) ...
(shg-score s) ...
(shg-spy s) ...
(shg-objects s) ...
(shg-shots s) ...
(shg-dtop s) ...))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
; ;;; ;;; ;;; ; ;
; ; ; ; ;
; ; ;;; ; ; ;;; ;;; ;;; ;;; ; ;; ;;;
; ; ;; ;; ; ; ; ; ; ; ;; ;; ;; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ;;; ; ; ; ; ; ;;;
; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ;; ;; ; ; ; ; ; ; ;; ;; ; ; ; ;
; ;;; ;;; ;; ;; ;;;;; ;;; ;;;;; ;;; ; ; ;;;
;
;
;
;;;;; a LTRB is a (LTRB Number Number Number Number)
(struct LTRB [left-x top-y right-x bottom-y] #:transparent)
(define ltrb1 (LTRB 0 0 10 10)) ;; overlapping with ltrb3 and
;; overlapping on edge with ltrb4
(define ltrb2 (LTRB 10 20 15 400)) ;; not overlapping anything
(define ltrb3 (LTRB 5 0 35 20)) ;; overlapping with ltrb1 and ltrb4
(define ltrb4 (LTRB 10 3 15 15)) ;; completely inside ltrb3
(define ltrb5 (LTRB 9 18 13 34)) ;; intersects with top of ltrb2
(define ltrb6 (LTRB 10 3 15 15)) ;; coincides with ltrb4
(define ltrb7 (LTRB 50 20 70 100)) ;; doesn't touch anything
;; rectangles corresponding to their LTRB conternumbers
(define rect1 (rectangle 10 10 'solid 'red))
(define rect2 (rectangle 5 380 'solid 'red))
(define rect3 (rectangle 30 20 'solid 'red))
(define rect4 (rectangle 5 12 'solid 'red))
;;template for functions processing LTRB
#; (define (fun-for-LTRB LTRB) ;fun-for-LTRB LTRB --> ???
(... (LTRB-left-x LTRB) ...
(LTRB-top-y LTRB) ...
(LTRB-right-x LTRB) ...
(LTRB-bottom-y LTRB) ...))
;; compute-ltrb: Num Num img --> LTRB
;; makes an LTRB for an image. the numbers are the image-center's x and y coor.
(check-expect (compute-ltrb 5 5 rect1) ltrb1)
(check-expect (compute-ltrb 20 10 rect3) ltrb3)
(check-expect (compute-ltrb 10 10
(square 6 'solid 'red))
(LTRB 7 7 13 13))
(check-expect (compute-ltrb 0 0
(square 15 'solid 'red))
(LTRB -7.5 -7.5 7.5 7.5))
(define (compute-ltrb x y img)
(LTRB (- x (* 1/2 (image-width img)))
(- y (* 1/2 (image-height img)))
(+ x (* 1/2 (image-width img)))
(+ y (* 1/2 (image-height img)))))
;; inside?: LTRB LTRB --> Boolean
; determines if the first LTRB is contained completely inside of the second LTRB
(check-expect (inside? ltrb1 ltrb2) #f)
(check-expect (inside? ltrb1 ltrb3) #f)
(check-expect (inside? ltrb3 ltrb4) #f) ;; ltrb4 is inside 3
(check-expect (inside? ltrb4 ltrb3) #t)
(define (inside? l1 l2)
(and (< (LTRB-left-x l2) (LTRB-left-x l1) (LTRB-right-x l1) (LTRB-right-x l2))
(< (LTRB-top-y l2) (LTRB-top-y l1) (LTRB-bottom-y l1) (LTRB-bottom-y l2))
))
;; touching?: LTRB LTRB --> Boolean
;; determines if LTRBs are touching. touching includes sharing a side or inside
(check-expect (touching? ltrb1 ltrb4) #t)
(check-expect (touching? ltrb3 ltrb2) #t)
(check-expect (touching? ltrb2 ltrb6) #f)
(check-expect (touching? ltrb4 ltrb3) #t)
(check-expect (touching? ltrb3 ltrb4) #t)
(check-expect (touching? ltrb4 ltrb1) #t)
(check-expect (touching? ltrb1 ltrb2) #f)
(check-expect (touching? ltrb1 (LTRB 2 4 5 7)) #t)
(check-expect (touching? ltrb1 ltrb1) #t)
(check-expect (touching? ltrb1 (LTRB 5 10 199 22)) #t)
(check-expect (touching? ltrb2 (LTRB 5 30 10 60)) #t)
(check-expect (touching? ltrb2 (LTRB 14 10 17 20)) #t)
(check-expect (touching? ltrb2 (LTRB 1 5 10 401)) #t)
(check-expect (touching? (LTRB 1 5 10 380) ltrb2) #t)
(check-expect (touching? (LTRB 8 400 20 401) ltrb2) #t)
(check-expect (touching? (LTRB 5 20 30 50) (LTRB 10 10 20 20)) #t)
(define (touching? l1 l2)
(or (inside? l1 l2)
(inside? l2 l1)
(and (touching-y? l1 l2)
(touching-x? l1 l2))))
;helper
;; touching-y?: LTRB LTRB --> Boolean
;; determines if the LTRBs are touching in the one dimensional y axis
(check-expect (touching-y? ltrb1 ltrb2) #f)
(check-expect (touching-y? ltrb1 ltrb4) #t)
(check-expect (touching-y? ltrb1 ltrb3) #t)
(check-expect (touching-y? ltrb4 ltrb6) #t)
(check-expect (touching-y? ltrb5 ltrb2) #t)
(define (touching-y? l1 l2)
(or (<= (LTRB-top-y l1) (LTRB-top-y l2) (LTRB-bottom-y l1))
(<= (LTRB-top-y l2) (LTRB-top-y l1) (LTRB-bottom-y l2))))
;helper
;; touching-x?: LTRB LTRB --> Boolean
;; determines if the LTRBs are touching in the one dimensional x axis
(check-expect (touching-x? ltrb1 ltrb7) #f)
(check-expect (touching-x? ltrb1 ltrb4) #t)
(check-expect (touching-x? ltrb6 ltrb3) #t)
(check-expect (touching-x? ltrb3 ltrb6) #t)
(check-expect (touching-x? ltrb5 ltrb2) #t)
(define (touching-x? l1 l2)
(or (<= (LTRB-left-x l1) (LTRB-left-x l2) (LTRB-right-x l1))
(<= (LTRB-left-x l2) (LTRB-left-x l1) (LTRB-right-x l2))))
;; overlapping? LTRB LTRB --> Boolean
; determine if two LTRBs are overlapping
(check-expect (overlapping? ltrb1 ltrb2) #f)
(check-expect (overlapping? ltrb3 ltrb7) #f)
(check-expect (overlapping? ltrb5 ltrb2) #t)
(check-expect (overlapping? ltrb2 ltrb5) #t)
(check-expect (overlapping? ltrb1 ltrb3) #t)
(check-expect (overlapping? ltrb4 ltrb6) #t)
(define (overlapping? l1 l2)
(or (inside? l1 l2)
(inside? l2 l1)
(touching? l1 l2)))
;;;;; compute-LTRB/inset: x-pos y-pos Image Number --> LTRB
;;produce an LTRB with an inset of given Number of pixels
;On each of the LTRB's four sides
(check-expect (compute-LTRB/inset 10 10
(square 6 'solid 'red)
2)
(LTRB 9 9 11 11))
(check-expect (compute-LTRB/inset 0 0
(square 10 'solid 'red)
3)
(LTRB -2 -2 2 2))
(define (compute-LTRB/inset x y img num)
(LTRB (- x
(+ (* -1 num) (* 1/2 (image-width img))))
(- y
(+ (* -1 num) (* 1/2 (image-height img))))
(+ x
(+ (* -1 num) (* 1/2 (image-width img))))
(+ y
(+ (* -1 num) (* 1/2 (image-height img))))))
;; vehicle->ltrb: vechicle --> LTRB
;; computes an LTRB from any struct that is a vehicle
(check-expect (vehicle->ltrb (small-enemy 300 790 0 2))
(compute-ltrb 300 790 sml-enemy))
(check-expect (vehicle->ltrb (large-enemy 300 200 0 2))
(compute-ltrb 300 200 lrg-enemy))
(define (vehicle->ltrb v)
(compute-ltrb (vehicle-x v)
(vehicle-y v)
(vehicle-lookup v VEHICLE-IMAGES)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#|
;;Wishlist
-- key handler ;;; DONE!
- shoot
- deploy oilslick
- deploy smokescreen
- move left
- move right
- go forward
- accelerate
-- draw handler ;;; DONE!
-- tick handler
- AI
- place friendly cars offscreen ;;;DONE!
- place enemy cars offscreen ;;;DONE!
- place helpertrucks offscreen ;;;DONE!
- move these things ;;;DONE!
- friendly cars
- ss
- BG
- collisions
- LTRBs ;;;DONE!
- generate-ltrb
- inside?
- overlapping?
- is an enemy hit by shot? ;;;DONE!
- is an enemy or a spy off the road? ;;;DONE!
- is the spy inside a truck? ;;;DONE!
- is an enemy or spy touching? ;;;DONE!
- is an enemy touching an os ;;;DONE!
- is an enemy touching a ss ;;;DONE!
|#
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Render Handler
;; render: SpyGame --> Image
;; renders all visual components of a SpyGame into an image
;check-expects for splash
(check-expect (render (splash splash-image)) splash-image)
(check-expect (render (splash spy-car)) splash-image)
; check-expects for gameover
(check-expect (render (gameover 0))
(overlay (above (text "Spy Hunter" 36 'red)
(text "GAME OVER" 48 'red)
(text "0" 48 'green))
plain-bg))
(check-expect (render (gameover 3))
(overlay (above (text "Spy Hunter" 36 'red)
(text "GAME OVER" 48 'red)
(text "3" 48 'green))
plain-bg))
(check-expect (render (gameover 211))
(overlay (above (text "Spy Hunter" 36 'red)
(text "GAME OVER" 48 'red)
(text "211" 48 'green))
plain-bg))
(check-expect (render (gameover 2000874))
(overlay (above (text "Spy Hunter" 36 'red)
(text "GAME OVER" 48 'red)
(text "2000874" 48 'green))
plain-bg))
; check-expects for shg
(check-expect (render shg1)
(draw-score shg1
(draw-lives shg1
(draw-spy
(shg-spy shg1)
(draw-os
(shg-spy shg1)
(draw-ss
(shg-spy shg1)
(draw-bg shg1)))))))
(check-expect (render shg5)
(draw-score shg5
(draw-lives shg5
(draw-LOO
(shg-objects shg5)
(draw-spy
(shg-spy shg5)
(draw-os
(shg-spy shg5)
(draw-ss
(shg-spy shg5)
(draw-shot
(shg-shots shg5)
(draw-bg shg5)))))))))
(define (render sg)
(cond [(splash? sg) splash-image]
[(shg? sg) (draw-score sg
(draw-lives sg
(draw-LOO
(no-os (shg-objects sg))
(draw-spy
(shg-spy sg)
(draw-os
(shg-spy sg)
(draw-ss
(shg-spy sg)
(draw-LOO
(filter os? (shg-objects sg))
(draw-shot
(shg-shots sg)
(draw-bg sg)))))))))]
[(gameover? sg) (overlay (above (text "Spy Hunter" 36 'red)
(text "GAME OVER" 48 'red)
(text (number->string
(gameover-score sg)) 48 'green))
plain-bg)]))
;; no-os: LOO --> LOO
;; removes the oilslicks from the LOO
(define (no-os ls)
(cond [(empty? ls) empty]
[(cons? ls) (if (os? (first ls))
(no-os (rest ls))
(cons (first ls) (no-os (rest ls))))]))
;; draw-bg: shg --> Image
;; draws the bg with proper dimensions using the BG
(check-expect (draw-bg shg1) (above (crop 0 (- (image-height BG) 10) W 10
BG)
(crop 0 0 W (- H 10) BG)))
(check-expect (draw-bg shg4) (crop 0 900 W H BG))
(check-expect (draw-bg shg5) (crop 0 0 W H BG))
;helper function wrap: shg --> Num+
; finds the y value where you want the screen to start from
; the dtop parameter of an shg
(define (wrap-y sg) (remainder (+ (image-height BG) (shg-dtop sg))
(image-height BG)))
(define (draw-bg sg)
(if (>= (+ (wrap-y sg) H)
(image-height BG))
(above (crop 0 (wrap-y sg) W (- (image-height BG) (wrap-y sg)) BG)
(crop 0 0 W (- H (- (image-height BG) (wrap-y sg))) BG))
(crop 0 (shg-dtop sg) W H BG)))
;; draw-score: shg Image --> Image
;; draws the player's score in top right
(check-expect (draw-score shg1 plain-bg)
(place-image (text "0" 30 'white)
(* 7/8 W) 25
plain-bg))
(define (draw-score sg bg)
(place-image (text (number->string (shg-score sg)) 30 'white)
(* 7/8 W) 25
bg))
;; draw-livesleft: shg --> Image
;; draws a small car for each life the player has left
(define mini-car-spacer (rectangle (* 1/60 W) 0 'solid 'beige))
(check-expect (draw-livesleft shg1) (beside mini-car mini-car-spacer
mini-car mini-car-spacer
mini-car mini-car-spacer
(empty-scene 0 0)))
(check-expect (draw-livesleft shg2) (beside mini-car mini-car-spacer
mini-car mini-car-spacer
(empty-scene 0 0)))
(check-expect (draw-livesleft shg3) (beside mini-car mini-car-spacer
(empty-scene 0 0)))
(check-expect (draw-livesleft shg4) (empty-scene 0 0))
(define (draw-livesleft sg)
(cond [(= 0 (shg-lives sg)) (empty-scene 0 0)]
[else (beside mini-car
mini-car-spacer
(draw-livesleft (shg (- (shg-lives sg) 1)
(shg-score sg)
(shg-spy sg)
(shg-objects sg)
(shg-shots sg)
(shg-dtop sg))))]))
;; draw-lives shg Image --> Image
;; draws the livesleft image onto the bg
(check-expect (draw-lives shg1 splash-image)
(place-image (draw-livesleft shg1)
(* 1/8 W) 25 splash-image))
(check-expect (draw-lives shg4 splash-image)
(place-image (empty-scene 0 0) (* 1/8 W) 25 splash-image))
(define (draw-lives sg bg)
(place-image (draw-livesleft sg) (* 1/8 W) 25 bg))
;; draw-spy: Spy Image --> Image
;; draws the spy as well as
(define (draw-spy s bg)
(place-image spy-car (spy-x s) (spy-y s) bg))
;; draw-os: Spy Image --> Image
;; draws an oilslick icon if the number of oilslicks left is > 0 and
(define (draw-os s bg)
(if (> (spy-osleft s) 0)
(place-image os-image (* 9/10 W) (* 7/8 H) bg)
bg))
;; draw-ss: Spy Image --> Image
;; draws a smokescreen icon if the number of smokescreens left is > 0
(define (draw-ss s bg)
(if (> (spy-ssleft s) 0)
(place-image mini-ss-image (* 9/10 W) (- (* 7/8 H) 80) bg)
bg))
;; draw-object: Object Image --> Image
;; draws the given object onto the given image
(define (draw-object o bg)
;; TODO instead of first 4 cases here, use vehicle? and a fn for
;; looking up the image
(cond [(FriendlyCar? o) (place-image frnd
(vehicle-x o) ;; XXX x/y access looks like this now
(vehicle-y o)
bg)]
[(small-enemy? o ) (place-image sml-enemy
(vehicle-x o)
(vehicle-y o)
bg)]
[(large-enemy? o ) (place-image lrg-enemy
(vehicle-x o)
(vehicle-y o)
bg)]
[(hlpr-truck? o) (place-image truck
(vehicle-x o)
(vehicle-y o)
bg)]
[(os? o) (place-image os-image
(os-x o)
(os-y o)
bg)]
[(ss? o) (place-image ss-image
(ss-x o)
(ss-y o)
bg)]
[(crash? o) (place-image (crash-img o)
(crash-x o)
(crash-y o)
bg)]))
;; draw-LOO: LOO Image --> Image
;; draws all of the objects contained in the SpyGame's LOO
(check-expect (draw-LOO empty plain-bg) plain-bg)
(check-expect (draw-LOO LOO1 plain-bg)
(place-image os-image 300 400
(place-image ss-image 200 500
(place-image sml-enemy
300 790
(place-image lrg-enemy 300
200
(place-image
truck 200
150
(place-image
frnd 400
150
plain-bg)))
))))
(define (draw-LOO ls bg)
(cond [(empty? ls) bg]
[(cons? ls) (draw-LOO (rest ls) (draw-object (first ls) bg))]))
;; draw-shot: LOS Image --> Image
;; draws the shot contained in the SpyGame's LOS
(check-expect (draw-shot LOS1 plain-bg)
(place-image player-shot 150 380
(place-image player-shot 150 360
(place-image player-shot 150 340
plain-bg))))
(define (draw-shot ls bg)
(cond [(empty? ls) bg]
[(cons? ls) (draw-shot (rest ls)
(place-image player-shot
(shot-x (first ls))
(shot-y (first ls))
bg))]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Key Handler
;; a KeyEvent is one of
;; "w" accelerates spy
;; "s" deccelerates spy
;; "a" moves the spy left
;; "d" moves the spy right
;; "e" deploys an oilslick to the road
;; "q" deploys a smokescreen behind spy
;; " " fires shot from spy
;; or any other key which switches
; splash --> gameover
; gameover --> splash or
; does nothing if SpyGame is an shg
;; handle-key: key SpyGame --> SpyGame
;; performs a key's proper function to produce the next SpyGame
(check-expect (handle-key (splash splash-image) "w")
starting-shg)
(check-expect (handle-key (splash splash-image) "s")
starting-shg)
(check-expect (handle-key (splash splash-image) "d")
starting-shg)
(check-expect (handle-key (splash splash-image) "a")
starting-shg)
(check-expect (handle-key (splash splash-image) "o")
starting-shg)
(check-expect (handle-key (gameover 100) "w")
(splash splash-image))
(check-expect (handle-key (gameover 100) "s")
(splash splash-image))
(check-expect (handle-key (gameover 100) "d")
(splash splash-image))
(check-expect (handle-key (gameover 100) "a")
(splash splash-image))
(check-expect (handle-key (gameover 100) "i")
(splash splash-image))
(check-expect (handle-key shg1 "w")
(shg 3 0 (spy 300 400 10 0 0) empty empty -10))
(check-expect (handle-key shg2 "s")
(shg 2 45 (spy 200 400 10 2 3) LOO1 LOS1 -20))
(check-expect (handle-key shg1 "a")
(shg 3 0 (spy 290 400 0 0 0) empty empty -10))
(check-expect (handle-key shg2 "d")
(shg 2 45 (spy 210 400 11 2 3) LOO1 LOS1 -20))
(check-expect (handle-key shg1 "e")
(shg 3 0 (spy 300 400 0 0 0) empty empty -10))
(check-expect (handle-key shg2 "e")
(shg 2 45 (spy 200 400 11 1 3)
(cons (os 200 453) LOO1) LOS1 -20))
(check-expect (handle-key shg1 "q")
(shg 3 0 (spy 300 400 0 0 0) empty empty -10))
(check-expect (handle-key shg2 "q")
(shg 2 45 (spy 200 400 11 2 2)
(cons (ss 200 (+ 400 (/ (image-height spy-car) 2))
MAX-SSD) LOO1) LOS1 -20))
(check-expect (handle-key shg1 " ")
(shg 3 0 (spy 300 400 0 0 0) empty
(cons (shot 300 (- 400
(/ (image-height spy-car) 2)
)) empty) -10))
(check-expect (handle-key shg1 "p")
shg1)
(check-expect (handle-key shg1 "h")
shg1)
(define (handle-key sg k)
(cond [(splash? sg) starting-shg] ;; on any-key it makes the starting shg
[(gameover? sg) (splash splash-image)] ;;on any-key goes to splash
[(shg? sg)
(cond [(key=? "w" k) (shg (shg-lives sg)
(shg-score sg)
(spy (spy-x (shg-spy sg))
(spy-y (shg-spy sg))
(accelerate
(spy-vel (shg-spy sg)))
(spy-osleft (shg-spy sg))
(spy-ssleft (shg-spy sg)))
(shg-objects sg)
(shg-shots sg)
(shg-dtop sg))]
[(key=? "s" k) (shg (shg-lives sg)
(shg-score sg)
(spy (spy-x (shg-spy sg))
(spy-y (shg-spy sg))
(decelerate
(spy-vel (shg-spy sg)))
(spy-osleft (shg-spy sg))
(spy-ssleft (shg-spy sg)))
(shg-objects sg)
(shg-shots sg)
(shg-dtop sg))]
[(key=? "a" k) (shg (shg-lives sg)
(shg-score sg)
(spy (- (spy-x (shg-spy sg))
10)
(spy-y (shg-spy sg))
(spy-vel (shg-spy sg))
(spy-osleft (shg-spy sg))
(spy-ssleft (shg-spy sg)))
(shg-objects sg)
(shg-shots sg)
(shg-dtop sg))]
[(key=? "d" k) (shg (shg-lives sg)
(shg-score sg)
(spy (+ (spy-x (shg-spy sg))
10)
(spy-y (shg-spy sg))
(spy-vel (shg-spy sg))
(spy-osleft (shg-spy sg))
(spy-ssleft (shg-spy sg)))
(shg-objects sg)
(shg-shots sg)
(shg-dtop sg))]
[(key=? "e" k) (add-os sg (shg-spy sg))]
[(key=? "q" k) (add-ss sg (shg-spy sg))]
[(key=? " " k) (shg (shg-lives sg)
(shg-score sg)
(spy (spy-x (shg-spy sg))
(spy-y (shg-spy sg))
(spy-vel (shg-spy sg))
(spy-osleft (shg-spy sg))
(spy-ssleft (shg-spy sg)))
(shg-objects sg)
(cons (shot (spy-x (shg-spy sg))
(- (spy-y (shg-spy sg)
)
(/ (image-height
spy-car) 2)))
(shg-shots sg))
(shg-dtop sg))]
[(key=? "r" k) (backup sg)]
[else sg])]))
;; backup: shg --> shg
(define (backup sg)
(shg (shg-lives sg)
(shg-score sg)
(shg-spy sg)
(backupLOO (shg-objects sg))
(shg-shots sg)
(shg-dtop sg)))
;; backupLOO: LOO --> LO
(define (backupLOO ls)
(cond [(empty? ls) empty]
[(cons? ls) (if (hlpr-truck? (first ls))
(cons (change-d (first ls))
(backupLOO (rest ls)))
(cons (first ls) (backupLOO (rest ls))))]))
;; change-d: hlpr-truck --> hlpr-truck
(define (change-d t)
(if (<= (vehicle-y t) 300)
(hlpr-truck (vehicle-x t)
(vehicle-y t)
(vehicle-dx t)
(vehicle-dy t)
(hlpr-truck-gadget t)
#t)
(if (eq? #t (hlpr-truck-loading-spy-car? t))
(hlpr-truck (vehicle-x t)
(vehicle-y t)
(vehicle-dx t)
(vehicle-dy t)
(hlpr-truck-gadget t)
#t)
(hlpr-truck (vehicle-x t)
(vehicle-y t)
(vehicle-dx t)
(vehicle-dy t)
(hlpr-truck-gadget t)
#f))))
;; accelerate: Nat --> Nat
;; increases Nat by 10 unless it is >= (START-VEL + 20)
(define (accelerate s)
(if (>= s (+ START-VEL 20))
s
(clamp MIN-SPYVEL (+ s 10) MAX-SPYVEL)))
;; decelerate: Nat --> Nat
;; decreases Nat by 10 unless it is < (START-VEL - 20)
(define (decelerate s)
(if (<= s (- START-VEL 20))
s
(clamp MIN-SPYVEL (- s 10) MAX-SPYVEL)))
;; add-os: shg spy --> shg
;; adds an oilslick to the beginning of the shg's loo and subs 1 from osleft
;; if the car has no oilslicks left add-os does nothing
;; the oilsick as the same coordinates as the spy
(define (add-os sg s)
(if (> (spy-osleft s) 0)
(shg (shg-lives sg)
(shg-score sg)
(spy (spy-x s) (spy-y s) (spy-vel s) (- (spy-osleft s) 1)
(spy-ssleft s))
(cons (os (spy-x s)
(+ (spy-y s) (+ (/ (image-height spy-car) 2)
(/ (image-height os-image) 2))))
(shg-objects sg))
(shg-shots sg) (shg-dtop sg))
sg))
;; addss: shg spy --> shg
;; adds a smokescreen to the beginning of the shg's loo and subs 1 from ssleft
;; if the car has no smokescreens left add-ss does nothing
(define (add-ss sg s)
(if (> (spy-ssleft s) 0)
(shg (shg-lives sg)
(shg-score sg)
(spy (spy-x s) (spy-y s) (spy-vel s) (spy-osleft s)
(- (spy-ssleft s) 1))
(cons (ss (spy-x s) (+ (spy-y s)
(/ (image-height spy-car) 2))
MAX-SSD) (shg-objects sg))
(shg-shots sg) (shg-dtop sg))
sg))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Tick Handler
;
; ;
; ;;;;;;; ; ; ;
; ; ; ;
; ; ;;; ;;; ; ; ;
; ; ; ;; ; ; ; ;
; ; ; ; ; ; ;
; ; ; ; ;;; ;
; ; ; ; ; ;
; ; ; ;; ; ;
; ; ;;;;; ;;;; ; ; ;;
;
;
;
;; handle-tick: SpyGame --> Spygame
(define (handle-tick sg)
(cond [(splash? sg) sg]
[(gameover? sg) sg]
[(shg? sg)
(endgame
(handle-collisions
(handle-score
(handle-spy
(handle-enemies
(move-window
(move-friends
(handle-crash
(handle-shot
(generate-frnd
(generate-truck
(generate-enemies
(remove-enemies
(remove-offscreen
(die
(handle-ss
(move-os
(handle-hlpr sg)))))
)))))))))))))]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; endgame?: shg --> Boolean
;; determines if the game should end (when shg-live < 0
;; checks for shg
(check-expect (endgame? shg1) #f)
(define (endgame? sg)
(< (shg-lives sg) 0))
;; endgame: shg --> shg
;; if the game is over it makes shg a gameover
(define (endgame sg)
(if (endgame? sg)
(gameover (shg-score sg))
sg))
;; offroad?: any car or crash --> Boolean
;; determines if a car is off of the road. Any car is off the road if
;; its x coordinate > 5/6 * W or x < 1/6 * W
;; or if its y coor. is > 3/2 * H or y < -H/2
(define (offroad? o)
(cond [(FriendlyCar? o) (or (< (vehicle-x o) (* 1/6 W))
(> (vehicle-x o) (* 5/6 W))
(< (vehicle-y o) (* -1/2 H))
(> (vehicle-y o) (* 3/2 H)))]
[(small-enemy? o) (or (< (vehicle-x o) (* 1/6 W))
(> (vehicle-x o) (* 5/6 W))
(< (vehicle-y o) (* -1/2 H))
(> (vehicle-y o) (* 3/2 H)))]
[(large-enemy? o) (or (< (vehicle-x o) (* 1/6 W))
(> (vehicle-x o) (* 5/6 W))
(< (vehicle-y o) (* -1/2 H))
(> (vehicle-y o) (* 3/2 H)))]
[(spy? o) (or (< (spy-x o) (* 1/6 W))
(> (spy-x o) (* 5/6 W)))]
[(hlpr-truck? o) (or (< (vehicle-x o) (* 1/6 W))
(> (vehicle-x o) (* 5/6 W))
(< (vehicle-y o) (* -1/2 H))
(> (vehicle-y o) (* 3/2 H)))]
[(crash? o) (or (< (crash-x o) (* 1/6 W))
(> (crash-x o) (* 5/6 W)))]
[else #f]))