-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrogger.rkt
1863 lines (1575 loc) · 62 KB
/
frogger.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
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
;; 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 frogger) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ())))
(require 2htdp/image)
(require 2htdp/universe)
#|
----------------
---- INDEX -----
----------------
0. World Definitions
1. The Canvas
a. Canvas size
b. Canas divisions
2. Images
a. Background
b. Text
c. Cursor
d. Frogger
e. Vehicle
f. Turtle
g. Plank
3. Title Screen
a. Menu
b. Location on Canvas
c. Header
d. Compiling the image
4. The Player
a. Definition
b. Location
c. Movement
5. The Vehicles & Swimmers
a. Definition
b. Location
c. Movement
5.1 The Info Box
a. The Display Box
b. Lives
c. High Score
6. Drawing the Worlds
a. Drawing the Title Screen
b. Drawing the Game Screen
c. Drawing the Restart Screen
d. Drawing the Quit Screen
e. Drawing the Score Screen
7. "Ticking" the World
a. Ticks
8. "On-key" functions
a. Menu Keys
b. Restart Keys
c. Game Keys
d. Score Keys
9. Animation
a. Handling the game
b. Big-bang
10. Tests
|#
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 0. World Definitions
;; a god-world is a (make-god-world title-world game-world score-world Number)
(define-struct god-world (title-world
game-world
score-world
placeholder))
;; where title-world is a (make-title-world ...) and
;; where game-world is a (make-game-world ...) and
;; where score-world is a (make-score-world ...) and
;; the Number is a placeholder for each of the worlds
;; a title-world is a (make-title-world posn posn)
(define-struct title-world (cursor-1 cursor-2))
;; where cursor-1 is the posn of the left cursor's position and
;; cursor-2 is the posn of the right cursor's position
;; a game-world is a (make-game-world player traffic)
(define-struct game-world (player traffic swimmers score))
;; where player is a player and
;; traffic is traffic
;; a score-world is a (make-score-world list)
(define-struct score-world (list-of-scores))
;; where list-of-scores is the list of high scores
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 1. The Canvas
;;;; 1a. Canvas Size
;; Defines the screen size
;; size in x must be an interval of 90
;; size in y must be an interval of 130
(define CANVAS (empty-scene 990 780))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 1b. Canvas Divisions
;; Defines standard constants derived from CANVAS
(define CANVAS_WIDTH (image-width CANVAS))
(define CANVAS_HEIGHT (image-height CANVAS))
(define CENTER_CANVAS_X (/ (image-width CANVAS) 2))
(define CENTER_CANVAS_Y (/ (image-height CANVAS) 2))
;; Defines how big each lane is based on how many there are
(define DIVISION_HEIGHT (/ CANVAS_HEIGHT 13))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 2. Images
;;;; 2a. Background
;; Defines the background layers
(define GRASS
(rectangle
CANVAS_WIDTH (+ DIVISION_HEIGHT 1) 'solid (make-color 8 137 21)))
(define GROUND
(rectangle
CANVAS_WIDTH (+ DIVISION_HEIGHT 1) 'solid (make-color 152 117 46)))
(define ROAD
(rectangle
CANVAS_WIDTH (- DIVISION_HEIGHT 1) 'solid (make-color 134 135 134)))
(define WATER
(rectangle
CANVAS_WIDTH (+ DIVISION_HEIGHT 1) 'solid (make-color 22 150 206)))
;; Defines the center of each lane
(define LANE_1_CENTER (- CANVAS_HEIGHT (/ DIVISION_HEIGHT 2)))
(define LANE_2_CENTER (- LANE_1_CENTER DIVISION_HEIGHT))
(define LANE_3_CENTER (- LANE_2_CENTER DIVISION_HEIGHT))
(define LANE_4_CENTER (- LANE_3_CENTER DIVISION_HEIGHT))
(define LANE_5_CENTER (- LANE_4_CENTER DIVISION_HEIGHT))
(define LANE_6_CENTER (- LANE_5_CENTER DIVISION_HEIGHT))
(define LANE_7_CENTER (- LANE_6_CENTER DIVISION_HEIGHT))
(define LANE_8_CENTER (- LANE_7_CENTER DIVISION_HEIGHT))
(define LANE_9_CENTER (- LANE_8_CENTER DIVISION_HEIGHT))
(define LANE_10_CENTER (- LANE_9_CENTER DIVISION_HEIGHT))
(define LANE_11_CENTER (- LANE_10_CENTER DIVISION_HEIGHT))
(define LANE_12_CENTER (- LANE_11_CENTER DIVISION_HEIGHT))
(define LANE_13_CENTER (- LANE_12_CENTER DIVISION_HEIGHT))
;; Defines the edges of each lane
(define CUT_LINE_1 (* 1 DIVISION_HEIGHT))
(define CUT_LINE_2 (* 2 DIVISION_HEIGHT))
(define CUT_LINE_3 (* 3 DIVISION_HEIGHT))
(define CUT_LINE_4 (* 4 DIVISION_HEIGHT))
(define CUT_LINE_5 (* 5 DIVISION_HEIGHT))
(define CUT_LINE_6 (* 6 DIVISION_HEIGHT))
(define CUT_LINE_7 (* 7 DIVISION_HEIGHT))
(define CUT_LINE_8 (* 8 DIVISION_HEIGHT))
(define CUT_LINE_9 (* 9 DIVISION_HEIGHT))
(define CUT_LINE_10 (* 10 DIVISION_HEIGHT))
(define CUT_LINE_11 (* 11 DIVISION_HEIGHT))
(define CUT_LINE_12 (* 12 DIVISION_HEIGHT))
;; Defines the background image
(define BACKGROUND
;; starting layer of grass
(place-image
GRASS
CENTER_CANVAS_X
LANE_1_CENTER
;; bottom layer of road
(place-image
ROAD
CENTER_CANVAS_X
LANE_2_CENTER
;; second layer of road
(place-image
ROAD
CENTER_CANVAS_X
LANE_3_CENTER
;; middle layer of road
(place-image
ROAD
CENTER_CANVAS_X
LANE_4_CENTER
;; fourth layer of road
(place-image
ROAD
CENTER_CANVAS_X
LANE_5_CENTER
;; top part of road
(place-image
ROAD
CENTER_CANVAS_X
LANE_6_CENTER
;; layer of dirt
(place-image
GROUND
CENTER_CANVAS_X
LANE_7_CENTER
;; bottom layer of water
(place-image
WATER
CENTER_CANVAS_X
LANE_8_CENTER
;; second layer of water
(place-image
WATER
CENTER_CANVAS_X
LANE_9_CENTER
;; middle layer of water
(place-image
WATER
CENTER_CANVAS_X
LANE_10_CENTER
;; fourth layer of water
(place-image
WATER
CENTER_CANVAS_X
LANE_11_CENTER
;; top layer of water
(place-image
WATER
CENTER_CANVAS_X
LANE_12_CENTER
;; final layer of grass
(place-image
GRASS
CENTER_CANVAS_X
LANE_13_CENTER
;; the empty scene
CANVAS))))))))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 2b. Text
;; Defines the size of the text on screen
(define TEXT_SIZE (round (/ DIVISION_HEIGHT 4)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 2c. Cursor
;; Defines the image of the cursor
(define CURSOR (rotate -90 (triangle (/ TEXT_SIZE 2) 'solid 'blue)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 2d. Frogger
;; Defines the player's body parts sizes
(define BODY_SIZE (/ DIVISION_HEIGHT 5))
(define EYE_SIZE (/ BODY_SIZE 3))
(define LEG_SIZE (* EYE_SIZE 2))
;; Defines the player's body parts images
(define FROGGER_BODY (circle BODY_SIZE 'solid (make-color 4 255 29)))
(define FROGGER_EYE (circle EYE_SIZE 'solid 'red))
(define FROGGER_LEG
(ellipse LEG_SIZE (* 2 LEG_SIZE) 'solid (make-color 133 13 156)))
;; Defines the constructed body of the player
(define FROGGER
(overlay/xy
(overlay/xy
(overlay/offset FROGGER_EYE
(- (* BODY_SIZE 2) (* 1.6 EYE_SIZE)) 0
FROGGER_EYE)
0 0
FROGGER_BODY)
(* -1 EYE_SIZE) BODY_SIZE
(overlay/offset (rotate 20 FROGGER_LEG)
(- (* 2 BODY_SIZE) (/ EYE_SIZE 2)) 0
(rotate -20 FROGGER_LEG))))
(define FROGGER_WIDTH (image-width FROGGER))
(define FROG_HEIGHT (image-height FROGGER))
;;Defines the broken frog to the right
(define DEAD_FROG_RIGHT_IMAGE
(overlay/xy
(overlay/xy
(overlay/offset FROGGER_BODY
(- (* BODY_SIZE 4) (* .6 EYE_SIZE)) LEG_SIZE
FROGGER_EYE)
(* -1 LEG_SIZE) EYE_SIZE
FROGGER_EYE)
(* -2 EYE_SIZE) EYE_SIZE
(overlay/offset (rotate 20 FROGGER_LEG)
(- (* 4 BODY_SIZE) (/ EYE_SIZE 2)) (* -1 EYE_SIZE)
(rotate -20 FROGGER_LEG))))
;; Defines the broken frog to the left
(define DEAD_FROG_LEFT_IMAGE
(overlay/xy
(overlay/offset (rotate -20 FROGGER_LEG)
(- (* -4 BODY_SIZE) (/ EYE_SIZE -2)) EYE_SIZE
(rotate 20 FROGGER_LEG))
(* 2 EYE_SIZE) EYE_SIZE
(overlay/xy
(overlay/offset FROGGER_BODY
(- (* BODY_SIZE -4) (* -1 .6 EYE_SIZE)) LEG_SIZE
FROGGER_EYE)
LEG_SIZE EYE_SIZE
FROGGER_EYE)))
;; Defines blood splatter to the right
(define BLOOD_SPLATTER_RIGHT
(overlay/offset
(overlay
(overlay/offset
(circle 4 'solid 'red)
-60 10
(circle 8 'solid 'red))
(overlay/offset
(circle 10 'solid 'red)
75 -30
(circle 6 'solid 'red)))
15 8
(overlay
(overlay/offset
(circle 4 'solid 'red)
-90 30
(circle 8 'solid 'red))
(overlay/offset
(circle 9 'solid 'red)
40 30
(circle 2 'solid 'red)))))
;; Defines blood splatter to the left
(define BLOOD_SPLATTER_LEFT
(overlay/offset
(overlay
(overlay/offset
(circle 4 'solid 'red)
60 -10
(circle 8 'solid 'red))
(overlay/offset
(circle 10 'solid 'red)
-75 30
(circle 6 'solid 'red)))
-15 -8
(overlay
(overlay/offset
(circle 4 'solid 'red)
90 -30
(circle 8 'solid 'red))
(overlay/offset
(circle 9 'solid 'red)
-40 -30
(circle 2 'solid 'red)))))
;; Defines the dead frog with blood to the right
(define DEAD_FROG_RIGHT
(overlay BLOOD_SPLATTER_RIGHT
DEAD_FROG_RIGHT_IMAGE))
;; Defines the dead frog with blood to the left
(define DEAD_FROG_LEFT
(overlay BLOOD_SPLATTER_LEFT
DEAD_FROG_LEFT_IMAGE))
;; Defines different orientations of the player
(define FROGGER_LEFT (rotate 90 FROGGER))
(define FROGGER_RIGHT (rotate -90 FROGGER))
(define FROGGER_DOWN (rotate 180 FROGGER))
;; orientation: player -> image
;; determines the direction the player is facing and produces
;; the coressponding image
(define (orientation player)
(cond [(symbol=? 'up (player-dir player)) FROGGER]
[(symbol=? 'down (player-dir player)) FROGGER_DOWN]
[(symbol=? 'left (player-dir player)) FROGGER_LEFT]
[(symbol=? 'right (player-dir player)) FROGGER_RIGHT]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 2e. Vehicle
(define VEHICLE_WIDTH (/ DIVISION_HEIGHT 1.5))
(define WHEEL_SIZE (/ VEHICLE_WIDTH 6))
(define WHEEL (circle WHEEL_SIZE 'solid 'black))
;; Defines the vehicle's image
(define
BODY_IMAGE
(rectangle VEHICLE_WIDTH (/ VEHICLE_WIDTH 2)
'solid (make-color (random 255) (random 255) (random 255))))
(define WHEELS
(overlay/xy WHEEL
(- (image-width BODY_IMAGE) (image-width WHEEL))
0
WHEEL))
(define VEHICLE_IMAGE
(overlay/xy WHEELS
0
(* (image-height WHEEL) -1)
BODY_IMAGE))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 2f. Turtle
;; Defines the images for turtles
(define TURTLE_BODY
(ellipse (* 2.25 FROG_HEIGHT) (* 1.75 FROG_HEIGHT) 'solid 'tan))
(define TURTLE_HEAD_SHAPE
(ellipse (* 1.5 FROG_HEIGHT) FROG_HEIGHT 'solid 'green))
(define TURTLE_LEG
(ellipse (* .8 FROG_HEIGHT) (* 1.25 FROG_HEIGHT) 'solid 'green))
(define TURTLE_EYE
(overlay
(circle (/ EYE_SIZE 2) 'solid 'black) (circle EYE_SIZE 'solid 'white)))
(define TURTLE_EYES
(overlay/offset
TURTLE_EYE 0 (/ (image-height TURTLE_HEAD_SHAPE) 2) TURTLE_EYE))
(define TURTLE_HEAD
(overlay/xy TURTLE_EYES
(- (/ (image-width TURTLE_HEAD_SHAPE) 2))
(- (/ (image-height TURTLE_HEAD_SHAPE) 8))
TURTLE_HEAD_SHAPE))
(define LEG_SET
(overlay/offset TURTLE_LEG (/ (image-width TURTLE_BODY) 2) 0 TURTLE_LEG))
(define ALL_LEGS
(overlay/offset LEG_SET 0 (/ (image-height TURTLE_BODY) 2) LEG_SET))
(define BODY_LEGS (underlay ALL_LEGS TURTLE_BODY))
(define TURTLE_FACE
(add-curve
TURTLE_HEAD
(* 2.6 BODY_SIZE) (/ (image-height TURTLE_HEAD_SHAPE) 4) 0 1/3
(* 2.6 BODY_SIZE) (* (/ (image-height TURTLE_HEAD_SHAPE) 4) 3) 0 -1/3
"black"))
(define TURTLE_RIGHT
(overlay/xy BODY_LEGS
(/ (image-width BODY_LEGS) 1.5)
(/ (image-height BODY_LEGS) 4)
TURTLE_FACE))
(define TURTLE_LEFT (rotate 180 TURTLE_RIGHT))
(define FROG_TURTLE_RIGHT
(overlay/xy FROGGER
(- (/ (image-width TURTLE_HEAD) 2.75))
(- (/ (image-height TURTLE_HEAD) 2))
TURTLE_RIGHT))
(define FROG_TURTLE_LEFT
(overlay/xy FROGGER
(- (/ (image-width TURTLE_HEAD) 1.125))
(- (/ (image-height TURTLE_HEAD) 2))
TURTLE_LEFT))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 2g. Plank
;; Defines the images of the planks
(define PLANK (rectangle (* 3 FROG_HEIGHT) (* 1.5 FROG_HEIGHT) 'solid 'tan))
(define FROG_ON_PLANK (overlay FROGGER PLANK))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 3. Title Screen
;;;; 3a. Menu
;; Defines each menu item name
(define MENU_ITEM_1
(text/font
"New Game" TEXT_SIZE 'white #f 'system 'normal 'normal #f)) ;; New Game
(define MENU_ITEM_2
(text/font
"High Scores" TEXT_SIZE 'white #f 'system 'normal 'normal #f)) ;; High Scores
(define MENU_ITEM_3
(text/font
"Quit Game" TEXT_SIZE 'white #f 'system 'normal 'normal #f)) ;; Quit Game
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 3b. Location on Canvas
;;Creates a buffer on the side of the image
;;buffer-in-x: image -> number
(define (buffer-in-x image)
(+ 20 (image-width image)))
;; Defines location positions for the cursors
(define LOCATION_1
(make-posn (- CENTER_CANVAS_X (/ (buffer-in-x MENU_ITEM_1) 2)) LANE_6_CENTER))
;; Menu item 1 left side ^
(define LOCATION_3
(make-posn (- CENTER_CANVAS_X (/ (buffer-in-x MENU_ITEM_2) 2)) LANE_4_CENTER))
;; Menu item 2 left side ^
(define LOCATION_5
(make-posn (- CENTER_CANVAS_X (/ (buffer-in-x MENU_ITEM_3) 2)) LANE_2_CENTER))
;; Menu item 3 left side ^
(define LOCATION_6
(make-posn (+ CENTER_CANVAS_X (/ (buffer-in-x MENU_ITEM_1) 2)) LANE_6_CENTER))
;; Menu item 1 right side ^
(define LOCATION_8
(make-posn (+ CENTER_CANVAS_X (/ (buffer-in-x MENU_ITEM_2) 2)) LANE_4_CENTER))
;; Menu item 2 right side ^
(define LOCATION_10
(make-posn (+ CENTER_CANVAS_X (/ (buffer-in-x MENU_ITEM_3) 2)) LANE_2_CENTER))
;; Menu item 3 right side ^
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 3c. Header
;; Defines the name of the game
(define GAME_NAME
(text/font
"FROGGER" (round DIVISION_HEIGHT) 'green #f 'modern 'normal 'bold #t))
;; Defines the author of the game
(define AUTHOR
(text/font "Created by: Joshua Caron & Amanda McAllister" TEXT_SIZE
'white #f 'system 'normal 'bold #f))
;; Defines the title block of the game
(define TITLE
(overlay/offset GAME_NAME
0 (/ DIVISION_HEIGHT 1.5)
AUTHOR))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 3d. Compiling the image
;; Defines the opening scene of the game
(define TITLE_SCREEN
(place-image
;; title block of the game
TITLE
CENTER_CANVAS_X
CUT_LINE_2
;; top menu item
(place-image
MENU_ITEM_1
CENTER_CANVAS_X
LANE_6_CENTER
;; middle menu item
(place-image
MENU_ITEM_2
CENTER_CANVAS_X
LANE_4_CENTER
;; bottom menu item
(place-image
MENU_ITEM_3
CENTER_CANVAS_X
LANE_2_CENTER
BACKGROUND)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 4. The Player
;;;; 4a. Definition
;; a player is a (make-player Number Number Symbol)
(define-struct player (x y dir lives))
;; where x is the x position of the player and
;; y is the y position of the player and
;; dir is the direction the player is facing and
;; lives is the number of chances the player has to win before
;; a restart prompt
;; Number of starting lives of the player
(define START_LIVES 5)
;; The initial position of the player
(define
START_PLAYER (make-player (/ CANVAS_WIDTH 2) LANE_1_CENTER 'up START_LIVES))
;; The interval on which the player moves
(define MOVE_INTERVAL DIVISION_HEIGHT)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 4b. Location
;; on-screen?: player -> boolean
;; determines if the player's position is on the screen or not
(define (on-screen? player)
(and (> (player-x player) 0)
(< (player-x player) CANVAS_WIDTH)
(> (player-y player) 0)
(< (player-y player) CANVAS_HEIGHT)))
;; in-water?: player -> boolean
;; determines if the player's position is in a water lane
(define (in-water? player)
(or (= (player-y player) LANE_8_CENTER)
(= (player-y player) LANE_9_CENTER)
(= (player-y player) LANE_10_CENTER)
(= (player-y player) LANE_11_CENTER)
(= (player-y player) LANE_12_CENTER)))
;; in-range?: number number number -> boolean
;; is n1 within range of n2?
(define (in-range? n1 n2 range)
(and (< n1 (+ n2 range))
(> n1 (- n2 range))))
;; hit?: player vehicle -> boolean
;; was the player hit by the vehicle?
(define (hit? player vehicle)
(and (= (player-y player)
(vehicle-y vehicle))
(in-range? (player-x player)
(vehicle-x vehicle)
(+ (/ (image-width FROGGER) 2)
(/ (image-width VEHICLE_IMAGE) 2)))))
;; list-hit?: player list-of-traffic -> boolean
;; determines if the player hit any car in the list of traffic
(define (list-hit? player list-of-traffic)
(local ((define all-traffic
(append (first list-of-traffic)
(second list-of-traffic)
(third list-of-traffic)
(fourth list-of-traffic)
(fifth list-of-traffic)))
(define (was-hit? p t)
(cond [(empty? t) false]
[(hit? p (first t))
true]
[else (was-hit? p (rest t))])))
(was-hit? player all-traffic)))
;; end-game?: game-world -> boolean
;; Determines if the game should end or not based on
;; the location of the player
(define (end-game? god)
(local ((define the-player (game-world-player (god-world-game-world god)))
(define LoS (game-world-swimmers (god-world-game-world god)))
(define on-a-swimmer?
(cond [(on-swimmer? the-player LoS) true]
[else false])))
(or (list-hit? (game-world-player (god-world-game-world god))
(game-world-traffic (god-world-game-world god)))
(and (in-water? (game-world-player (god-world-game-world god)))
(not on-a-swimmer?)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 4c. Movement
;; move-frog-left: player -> player
;; changes the direction of the frog to left and
;; moves it left in x a set amount
(define (move-frog-left player)
(make-player (- (player-x player) MOVE_INTERVAL)
(player-y player)
'left
(player-lives player)))
;; move-frog-right: player -> player
;; changes the direction of the frog to the right and
;; moves it right in x a set amount
(define (move-frog-right player)
(make-player (+ (player-x player) MOVE_INTERVAL)
(player-y player)
'right
(player-lives player)))
;; move-frog-up: player -> player
;; changes the direction of the frog to facing up and
;; moves it up in y a set amount
(define (move-frog-up player)
(make-player (player-x player)
(- (player-y player) MOVE_INTERVAL)
'up
(player-lives player)))
;; move-frog-down: player -> player
;; changes the direction of the frog to facing down and
;; moves it down in y a set amount
(define (move-frog-down player)
(make-player (player-x player)
(+ (player-y player) MOVE_INTERVAL)
'down
(player-lives player)))
;; move-frog-on-swimmer: player LoS -> player
;; when a player is on a swimmer, makes
;; a new player that will move with the swimmer
(define (move-frog-on-swimmer player LoS)
(local ((define y-location (player-y player)))
(cond [(or (= LANE_8_CENTER y-location)
(= LANE_11_CENTER y-location))
(make-player (new-player-x player LoS)
y-location
(player-dir player)
(player-lives player))]
[(or (= LANE_9_CENTER y-location)
(= LANE_10_CENTER y-location))
(make-player (new-player-x player LoS)
y-location
(player-dir player)
(player-lives player))]
[(= LANE_12_CENTER y-location)
(make-player (new-player-x player LoS)
y-location
(player-dir player)
(player-lives player))]
[else player])))
;; new-player-x: player LoS -> Number
;; checks if a player has collided with a swimmer
;; if so, produces the x-posn of the swimmmer
(define (new-player-x player LoS)
(local ((define LoS-single-list
(append (first LoS)
(second LoS)
(third LoS)
(fourth LoS)
(fifth LoS)))
(define (hit-a-swimmer? player list)
(cond [(hit-swimmer? player (first list))
(cond [(symbol=? 'left (swimmer-dir (first list)))
(+ (* .25 FROGGER_WIDTH) (swimmer-x (first list)))]
;; this part used to center FROG on TURTLE ^
[else (swimmer-x (first list))])]
[else (hit-a-swimmer? player (rest list))])))
(hit-a-swimmer? player LoS-single-list)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 5. The Vehicles
;;;; 5a. Definition
;; a vehicle is a (make-vehicle Number Number Symbol)
(define-struct vehicle (x y dir))
;; where x is the location of the vehicle in x and
;; y is the location of the vehcile in y
;; and dir is the direction the vehicle is moving in
;; a swimmer is a (make-swimmer Number Number Symbol)
(define-struct swimmer (x y dir))
;; where x is the location of the swimmer in x and
;; y is the location of the swimmer in y
;; and dir is the direction the swimmer is moving in
;;Definitions of starting points of cars in each lane
(define LANE_1_CAR (make-vehicle 0 LANE_2_CENTER 'right))
(define LANE_2_CAR (make-vehicle CANVAS_WIDTH LANE_3_CENTER 'left))
(define LANE_3_CAR (make-vehicle 0 LANE_4_CENTER 'right))
(define LANE_4_CAR (make-vehicle CANVAS_WIDTH LANE_5_CENTER 'left))
(define LANE_5_CAR (make-vehicle 0 LANE_6_CENTER 'right))
;; a traffic is either:
;; - empty
;; - (cons vehicle traffic)
;;Defines the starting list of vehicles
(define INITIAL_VEHICLE_LIST
(list (list LANE_1_CAR)
(list LANE_2_CAR)
(list LANE_3_CAR)
(list LANE_4_CAR)
(list LANE_5_CAR)))
;;Defines the various velocities of the vehicles
(define FAST_VELOCITY 20)
(define MEDIUM_VELOCITY 10)
(define SLOW_VELOCITY 5)
;; move-velocity: [X] Symbol Number -> Number
;; moves a given object at the given speed and direction
;; where the symbol is either 'left or 'right
(define (move-velocity thing-x dir how-fast)
[cond [(symbol=? dir 'left)
(- thing-x how-fast)]
[(symbol=? dir 'right)
(+ thing-x how-fast)]])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 5b. Location
;; on-road? vehicle -> boolean
;; determines if the given vehicle is within the scene
(define (on-road? vehicle)
(and (>= (vehicle-x vehicle) 0)
(<= (vehicle-x vehicle) CANVAS_WIDTH)))
;; drowning?: swimmer -> boolean
;; determines if the swimmer is on the screen
(define (drowning? swimmer)
(and (>= (swimmer-x swimmer) 0)
(<= (swimmer-x swimmer) CANVAS_WIDTH)))
;; destroy-car: list of vehicles -> list of vehicles
;; removes vehicles from list that are outside the range of the scene
(define (kill-lane-cars LOV)
(cond [(empty? LOV) empty]
[(not (on-road? (first LOV)))
(kill-lane-cars (rest LOV))]
[else (cons (first LOV)
(kill-lane-cars (rest LOV)))]))
; destroy-cars: lov -> lov
;; removes offscreen vehicles from each lane
(define (destroy-car LOV)
(list (kill-lane-cars (first LOV))
(kill-lane-cars (second LOV))
(kill-lane-cars (third LOV))
(kill-lane-cars (fourth LOV))
(kill-lane-cars (fifth LOV))))
;; kill-lane-swimmers: list of swimmers -> list of swimmers
;; removes swimmers from list that are offscreen
(define (kill-lane-swimmers LOS)
(cond [(empty? LOS) empty]
[(not (drowning? (first LOS)))
(kill-lane-swimmers (rest LOS))]
[else (cons (first LOS)
(kill-lane-swimmers (rest LOS)))]))
; kill-swimmers: los -> los
;; removes offscreen swimmers from each lane
(define (kill-swimmers LOS)
(list (kill-lane-swimmers (first LOS))
(kill-lane-swimmers (second LOS))
(kill-lane-swimmers (third LOS))
(kill-lane-swimmers (fourth LOS))
(kill-lane-swimmers (fifth LOS))))
;; make-more-traffic: list of vehicles -> list of vehicles
;; determines whether or not to add more vehicles to the scene
;; and adds them if nessecary
(define (make-more-traffic LOV)
(local ((define LANE_2_CARS (first LOV))
(define LANE_3_CARS (second LOV))
(define LANE_4_CARS (third LOV))
(define LANE_5_CARS (fourth LOV))
(define LANE_6_CARS (fifth LOV)))
(list
(cond [(integer? (/ (vehicle-x (first LANE_2_CARS)) 300))
(append (first INITIAL_VEHICLE_LIST) LANE_2_CARS)]
[else LANE_2_CARS])
(cond [(integer? (/ (vehicle-x (first LANE_3_CARS)) 600))
(append (second INITIAL_VEHICLE_LIST) LANE_3_CARS)]
[else LANE_3_CARS])
(cond [(integer? (/ (vehicle-x (first LANE_4_CARS)) 300))
(append (third INITIAL_VEHICLE_LIST) LANE_4_CARS)]
[else LANE_4_CARS])
(cond [(integer? (/ (vehicle-x (first LANE_5_CARS)) 600))
(append (fourth INITIAL_VEHICLE_LIST) LANE_5_CARS)]
[else LANE_5_CARS])
(cond [(integer? (/ (vehicle-x (first LANE_6_CARS)) 400))
(append (fifth INITIAL_VEHICLE_LIST) LANE_6_CARS)]
[else LANE_6_CARS]))))
;; make-more-swimmers: los -> los
;; determines whether or not to add more swimmers to the scene
;; and adds them if nessecary
(define (make-more-swimmers LOS)
(local ((define LANE_8_SWIMMERS (first LOS))
(define LANE_9_SWIMMERS (second LOS))
(define LANE_10_SWIMMERS (third LOS))
(define LANE_11_SWIMMERS (fourth LOS))
(define LANE_12_SWIMMERS (fifth LOS)))
(list
(cond [(integer? (/ (swimmer-x (first LANE_8_SWIMMERS)) 330))
(append (first INITIAL_SWIMMER_LIST) LANE_8_SWIMMERS)]
[else LANE_8_SWIMMERS])
(cond [(integer? (/ (swimmer-x (first LANE_9_SWIMMERS)) 350))
(append (second INITIAL_SWIMMER_LIST) LANE_9_SWIMMERS)]
[else LANE_9_SWIMMERS])
(cond [(integer? (/ (swimmer-x (first LANE_10_SWIMMERS)) 500))
(append (third INITIAL_SWIMMER_LIST) LANE_10_SWIMMERS)]
[else LANE_10_SWIMMERS])
(cond [(integer? (/ (swimmer-x (first LANE_11_SWIMMERS)) 300))
(append (fourth INITIAL_SWIMMER_LIST) LANE_11_SWIMMERS)]
[else LANE_11_SWIMMERS])
(cond
[(= (swimmer-x (first LANE_12_SWIMMERS)) 30)
(append (fifth INITIAL_SWIMMER_LIST) LANE_12_SWIMMERS)]
[else LANE_12_SWIMMERS]))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; 5c. Movement
;; move-lane-vehicles: list of vehicles -> list of vehicles
;; moves all vehicles in a list based on their direction
(define (move-lane-vehicles list-of-vehicles)
(local ((define y-location
(cond [(empty? list-of-vehicles) empty]
[else (vehicle-y (first list-of-vehicles))]))
(define x-location
(cond [(empty? list-of-vehicles) empty]
[else (vehicle-x (first list-of-vehicles))]))
(define direction
(cond [(empty? list-of-vehicles) empty]
[else (vehicle-dir (first list-of-vehicles))]))
(define vehicle
(cond [(empty? list-of-vehicles) empty]
[else (first list-of-vehicles)])))
(cond [(empty? list-of-vehicles) empty]
[(or (= LANE_2_CENTER y-location)
(= LANE_5_CENTER y-location))
(cons
(make-vehicle (move-velocity x-location direction SLOW_VELOCITY)
y-location
(vehicle-dir vehicle))
(move-lane-vehicles (rest list-of-vehicles)))]
[(or (= LANE_3_CENTER y-location)
(= LANE_4_CENTER y-location))
(cons
(make-vehicle (move-velocity x-location direction MEDIUM_VELOCITY)
y-location
(vehicle-dir vehicle))
(move-lane-vehicles (rest list-of-vehicles)))]
[(= LANE_6_CENTER y-location)
(cons
(make-vehicle (move-velocity x-location direction FAST_VELOCITY)
y-location
(vehicle-dir vehicle))
(move-lane-vehicles (rest list-of-vehicles)))])))
;; vehicle-movement: lov -> lov
;; moves all the vehicles in each lane
(define (vehicle-movement LOV)
(list (move-lane-vehicles (first LOV))
(move-lane-vehicles (second LOV))
(move-lane-vehicles (third LOV))
(move-lane-vehicles (fourth LOV))
(move-lane-vehicles (fifth LOV))))
;;Definitions of starting points of swimmers in each lane
(define LANE_8_TURTLE (make-swimmer CANVAS_WIDTH LANE_8_CENTER 'left))
(define LANE_9_PLANK (make-swimmer 0 LANE_9_CENTER 'right))
(define LANE_10_TURTLE (make-swimmer CANVAS_WIDTH LANE_10_CENTER 'left))
(define LANE_11_PLANK (make-swimmer 0 LANE_11_CENTER 'right))
(define LANE_12_TURTLE (make-swimmer CANVAS_WIDTH LANE_12_CENTER 'left))
;;Defines the starting list of swimmers
(define INITIAL_SWIMMER_LIST
(list (list LANE_8_TURTLE)
(list LANE_9_PLANK)
(list LANE_10_TURTLE)
(list LANE_11_PLANK)
(list LANE_12_TURTLE)))
(define SLOW_SWIMMER_VELOCITY 2)
(define MEDIUM_SWIMMER_VELOCITY 5)
(define FAST_SWIMMER_VELOCITY 6)
;; swimmer-movement: list of swimmers -> list of swimmers
;; moves all swimmers in a list based on their direction
(define (lane-swimmer-movement list-of-swimmers)
(local ((define y-location