-
Notifications
You must be signed in to change notification settings - Fork 0
/
SquashGame.rkt
3253 lines (2628 loc) · 100 KB
/
SquashGame.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-intermediate-lambda-reader.ss" "lang")((modname q2) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
;; q2.rkt : solution for q2 in the problem set 06
;; (squashpractice1)
;; squash simulation
;; game starts with the ready-to-serve state with ball and the racket at
;; the same position.
;; the user can change the state by pressing a space bar.
;; moving from ready-to-serve state to rally state will start the game and
;; the ball moves in the court and collides with walls and racket to change
;; the position and velocity.
;; changing the state from rally to paused will pause the game for three seconds
;; and will go to ready-to-serve state again
;; (squashpractice2)
;; squash simulation
;; like squashpractice1, but user can drag the racket with the mouse in the
;; rally state. button-down to select, drag to move, button up to release.
;; when selected a blue circle as a mouse pointer will be there.
;; racket can be selected by positioning mouse no more than 25 pixels away
;; from center of the racket
;; (SquashPractice3)
;; squash simulation
;; improved version of squashpractice2
;; like squashpractice2, but the user can press the "b" key and create a
;; a new ball in the rally state, the new balls will behave like the ball in
;; squashpractice2. Ball when collides with the back wall, disappears and
;; when all the balls are disappeared the rally state ends to paused state.
;; the racket and ball collision is improved for better user experience.
;; START:
;; can be started with any PosReal number
;; NOTE:
;; greater number e.g. 1, will result in very slow motion.
;; for realistic behavior use 1/24
;; start with (simulation 1/24)
;; including required libraries
(require rackunit) ;; for check-equal?
(require "extras.rkt") ;; for begin-for-test, check-location
(require 2htdp/universe) ;; for creating interactive, graphical programs
(require 2htdp/image) ;; for different image functions used
;; to see if the file is correctly named and in the right location
(check-location "06" "q2.rkt")
;; to require the file and check all functions are provided
(provide
simulation
initial-world
world-ready-to-serve?
world-after-tick
world-after-key-event
world-racket
ball-x
ball-y
racket-x
racket-y
ball-vx
ball-vy
racket-vx
racket-vy
world-after-mouse-event
racket-after-mouse-event
racket-selected?
world-balls
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; MAIN FUNCTION
;; simulation : PosReal -> World
;; GIVEN: the speed of the simulation, in seconds per tick
;; (so larger numbers run slower)
;; EFFECT: runs the simulation, starting with the initial world
;; RETURNS: the final state of the world
;; EXAMPLES:
;; (simulation 1) runs in super slow motion
;; (simulation 1/24) runs at a more realistic speed
;; DESIGN STRATEGY: combine simpler functions
(define (simulation speed)
(big-bang (initial-world speed)
(on-tick world-after-tick speed)
(on-draw world-to-scene)
(on-key world-after-key-event)
(on-mouse world-after-mouse-event)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; CONSTANTS
;; dimensions of the court
;; court width in pixels
(define COURT-WIDTH 425)
;; court height in pixels
(define COURT-HEIGHT 649)
;; radius of the ball
(define BALL-RADIUS 3)
;; image of the ball
(define BALL-IMAGE (circle BALL-RADIUS "solid" "black"))
;; positioning of mouse to select racket
(define MOUSE-SELECT-POS 25)
;; width of racket
(define RACKET-WIDTH 47)
;; height of racket
(define RACKET-HEIGHT 7)
;; image of the racket
(define RACKET-IMAGE (rectangle RACKET-WIDTH RACKET-HEIGHT "solid" "green"))
;; racket half width
(define RACKET-HALF-WIDTH (/ RACKET-WIDTH 2))
;; radius of pointer circle
(define POINTER-CIRCLE-RADIUS 4)
;; image of pointer circle
(define POINTER-CIRCLE-IMAGE (circle POINTER-CIRCLE-RADIUS "solid" "blue"))
;; the simulation start coordinates for location
(define START-X 330)
(define START-Y 384)
;; total time for the paused world-state
(define PAUSED-WAIT-TIME 3)
;; initial value of counter for ticks
(define INITIAL-COUNTER-VALUE 1)
;; examples KeyEvent for testing
(define STATE-KEY-EVENT " ")
(define LEFT-ARROW-KEY-EVENT "left")
(define RIGHT-ARROW-KEY-EVENT "right")
(define UP-ARROW-KEY-EVENT "up")
(define DOWN-ARROW-KEY-EVENT "down")
(define B-KEY-EVENT "b")
(define OTHER-EVENT "q")
;; example MouseEvents for testing:
(define BUTTON-DOWN-EVENT "button-down")
(define DRAG-EVENT "drag")
(define BUTTON-UP-EVENT "button-up")
(define OTHER-MOUSE-EVENT "enter")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; DATA DEFINITIONS:
;; Ball
;; REPRESENTATION:
;; a Ball is represented as a struct
;; (make-ball x y vx vy)
;; with the following fields:
;; x, y : Integer is the position of the center of the ball in
;; the scene
;; vx : Integer is the horizontal component of the velocity of the ball
;; which gives the pixels, the ball move in x direction
;; vy : Integer is the vertical component of the velocity of the ball
;; which gives the pixels, the ball move in y direction
;; IMPLEMENTATION:
(define-struct ball (x y vx vy))
;; CONSTRUCTOR TEMPLATE:
;; (make-ball Integer Integer Integer Integer)
;; OBSERVER TEMPLATE:
;; ball-fn : Ball -> ?
(define (ball-fn b)
(... (ball-x b)
(ball-y b)
(ball-vx b)
(ball-vy b)))
;; Balls
;; Balls is represented as a list of Ball which keeps on adding when "b" key
;; is pressed, with one entry per ball in the court
;; CONSTRUCTOR TEMPLATES:
;; empty
;; (cons b bs)
;; -- where
;; b is a Ball
;; bs is Balls
;; OBSERVER TEMPLATE:
;; balls-fn : BallS -> ?
#;
(define (balls-fn bs)
(cond
[(empty? bs) ...]
[else (...
(ball-fn (first bs))
(balls-fn (rest bs)))]))
;; Racket
;; REPRESENTATION:
;; a Racket is represented as a struct
;; (make-racket x y vx vy mx my selected?)
;; with the following fields:
;; x, y : Integer is the position of the center of the racket in
;; the scene
;; vx : Integer is the horizontal component of the velocity of the racket
;; which gives the pixels, the racket move in x direction
;; vy : Integer is the vertical component of the velocity of the racket
;; which gives the pixels, the racket move in y direction
;; mx, my : Integer is the x and y coordinates of the mouse when the
;; racket is selected
;; selected? : Boolean describes whether or not the racket is selected
;; IMPLEMENTATION:
(define-struct racket (x y vx vy mx my selected?))
;; CONSTRUCTOR TEMPLATE:
;; (make-racket Integer Integer Integer Integer
;; Integer Integer Boolean)
;; OBSERVER TEMPLATE:
;; racket-fn : Racket -> ?
(define (racket-fn r)
(... (racket-x r)
(racket-y r)
(racket-vx r)
(racket-vy r)
(racket-mx r)
(racket-my r)
(racket-selected? r)))
;; State
;; REPRESENTATION:
;; a State is represented by one of the Strings
;; -- "ready-to-serve"
;; -- "rally"
;; -- "paused"
;; INTERPRETATION: the different states of the simulation
;; EXAMPLES:
(define READY-TO-SERVE "ready-to-serve")
(define RALLY "rally")
(define PAUSED "paused")
;; OBSERVER TEMPLATE:
;; state-fn : State -> ?
#;
(define (state-fn s)
(cond [(string=? s READY-TO-SERVE) ...]
[(string=? s RALLY) ...]
[(string=? s PAUSED) ...]))
;; BgColor
;; REPRESENTATION:
;; a BgColor is represented by one of the Strings
;; -- "white"
;; -- "yellow"
;; INTERPRETATION: self-evident
;; EXAMPLES:
(define BG-COLOR-WHITE "white")
(define BG-COLOR-YELLOW "yellow")
;; OBSERVER TEMPLATE:
;; bg-color-fn : BgColor -> ?
#;
(define (bg-color-fn c)
(cond [(string=? c BG-COLOR-WHITE) ...]
[(string=? c BG-COLOR-YELLOW) ...]))
;; World
;; REPRESENTATION:
;; a World is represented as a struct
;; (make-world balls racket state bg-color tick-counter simulation-speed)
;; with the following fields:
;; balls : Balls the list of balls in the world
;; racket : Racket the racket in the world
;; state : State the state of the simulation
;; bg-color : BgColor the background color of the world
;; tick-counter : PosInt is the counter for the clock tick
;; simulation-speed : PosReal the speed of the simulation, in seconds per
;; clock tick
;; (a larger number run slower)
;; IMPLEMENTATION:
(define-struct world (balls
racket
state
bg-color
tick-counter
simulation-speed))
;; CONSTRUCTOR TEMPLATE:
;;(make-world Balls Racket State BgColor PosInt PosReal)
;; OBSERVER TEMPLATE:
;; world-fn : World -> ?
(define (world-fn w)
(... (world-balls w)
(world-racket w)
(world-state w)
(world-bg-color w)
(world-tick-counter w)
(world-simulation-speed w)))
;; examples of balls, for testing
;; balls in the initial world
(define INITIAL-WORLD-BALLS
(list (make-ball START-X START-Y 0 0)))
;; ball in the start of the rally world state
(define INITIAL-RALLY-WORLD-BALLS
(list (make-ball START-X START-Y 3 -9)))
;; random balls in rally state
(define RANDOM-SINGLE-BALL-330-384
(make-ball 330 384 3 -9))
(define RANDOM-SINGLE-BALL-330-380
(make-ball 330 380 3 -9))
(define RANDOM-BALL-330-384
(list (make-ball 330 384 3 -9)))
(define EMPTY-BALL-LIST
(list))
(define MULTIPLE-LIST-RANDOM-BALL
(list (make-ball 424 96 -3 -9) (make-ball 417 123 3 -9)))
(define B-KEY-RANDOM-BALL-330-384
(list (make-ball 330 384 3 -9) (make-ball START-X START-Y 3 -9)))
(define NEXT-RANDOM-BALL-333-375
(list (make-ball 333 375 3 -9)))
(define NEXT-RANDOM-SINGLE-BALL-333-375
(make-ball 333 375 3 -9))
;; ball which will collide with the front wall
(define BALL-FRONT-COLLIDE-Y-6
(make-ball 380 6 -3 -9))
(define BALLS-FRONT-COLLIDE-Y-6
(list (make-ball 380 6 -3 -9)))
(define NEXT-BALL-FRONT-COLLIDE-Y-6
(make-ball 377 3 -3 9))
(define NEXT-BALLS-FRONT-COLLIDE-Y-6
(list (make-ball 377 3 -3 9)))
;; ball which will collide the right wall
(define BALL-RIGHT-COLLIDE-X-423
(make-ball 423 132 3 -9))
(define NEXT-BALL-RIGHT-COLLIDE-X-423
(make-ball 424 123 -3 -9))
;; ball which will collide with the left wall
(define BALL-LEFT-COLLIDE-x-2
(make-ball 2 350 -3 9))
(define NEXT-BALL-LEFT-COLLIDE-x-2
(make-ball 1 359 3 9))
;; ball which will collide with the back wall
(define BALL-BACK-COLLIDE-Y-643
(list (make-ball 200 643 -3 9)))
(define SINGLE-BALL-BACK-COLLIDE-Y-643
(make-ball 200 643 -3 9))
;; ball in the start of the rally world state
(define INITIAL-RALLY-WORLD-BALL
(make-ball START-X START-Y 3 -9))
;; random ball in paused state
(define BALL-PAUSED-STATE-200-643
(list (make-ball 200 643 -3 9)))
;; examples of rackets, for testing
;; racket in the initial world
(define INITIAL-WORLD-RACKET
(make-racket START-X START-Y 0 0 0 0 false))
;; random racket in paused state
(define RACKET-PAUSED-STATE-149-384
(make-racket 149 384 -1 0 0 0 false))
;; random racket in rally state and not selected
(define PREVIOUS-RANDOM-RACKET-149-384
(make-racket 149 384 -1 0 0 0 false))
(define RANDOM-RACKET-149-384
(make-racket 149 384 -1 0 0 0 false))
(define NEXT-RANDOM-RACKET-148-384
(make-racket 148 384 -1 0 0 0 false))
(define NEXT-RANDOM-RACKET-151-384
(make-racket 151 384 -1 0 0 0 false))
;; random racket selected in rally state
(define SELECTED-RANDOM-RACKET-152-384
(make-racket 152 384 -2 0 0 0 true))
(define NEXT-SELECTED-RANDOM-RACKET-152-384
(make-racket 352 684 -2 0 200 300 true))
(define BUTTON-UP-RANDOM-RACKET-152-384
(make-racket 152 384 -2 0 0 0 false))
;; racket colliding with the ball
(define RACKET-BALL-COLLIDE
(make-racket 248 380 0 0 0 0 false))
(define NEXT-RACKET-BALL-COLLIDE
(make-racket 248 380 0 0 0 0 false))
(define RACKET-BALL-COLLIDE-NEG-VY
(make-racket 248 380 0 -3 0 0 false))
(define NEXT-RACKET-BALL-COLLIDE-NEG-VY
(make-racket 248 380 0 0 0 0 false))
;; with mx and my
(define SELECTED-RANDOM-RACKET-152-384-140-200
(make-racket 152 384 -2 0 140 200 true))
(define SELECTED-RANDOM-RACKET-152-384-152-384
(make-racket 152 384 -2 0 152 384 true))
;; random racket with key events in rally state
(define LEFT-RANDOM-RACKET-149-384
(make-racket 149 384 -2 0 0 0 false))
(define RIGHT-RANDOM-RACKET-149-384
(make-racket 149 384 0 0 0 0 false))
(define UP-RANDOM-RACKET-149-384
(make-racket 149 384 -1 -1 0 0 false))
(define DOWN-RANDOM-RACKET-149-384
(make-racket 149 384 -1 1 0 0 false))
;; racket which will collide the right wall
(define RACKET-RIGHT-COLLIDE-X-424
(make-racket 424 384 1 0 0 0 false))
(define NEXT-RACKET-RIGHT-COLLIDE-X-424
(make-racket 402 384 1 0 0 0 false))
;; racket which will collide the left wall
(define RACKET-LEFT-COLLIDE-X-3
(make-racket 3 390 -4 0 0 0 false))
(define NEXT-RACKET-LEFT-COLLIDE-X-3
(make-racket 24 390 -4 0 0 0 false))
;; racket which will collide with the front wall
(define RACKET-FRONT-COLLIDE-Y-4
(make-racket 330 4 0 -5 0 0 false))
;; racket which will collide with the back wall
(define RACKET-BACK-COLLIDE-Y-642
(make-racket 335 642 0 8 0 0 false))
;; ball colliding with the racket
(define BALL-RACKET-COLLIDE
(list (make-ball 249 378 -3 4)))
(define SINGLE-BALL-RACKET-COLLIDE
(make-ball 249 378 -3 4))
(define NEXT-BALL-RACKET-COLLIDE
(make-ball 249 378 -3 -4))
(define NEXT-BALLS-RACKET-COLLIDE
(list (make-ball 249 378 -3 -4)))
;; ball colliding with the racket second time
(define BALL-RACKET-SECOND-COLLIDE
(list (make-ball 249 378 -3 4)))
(define NEXT-BALL-RACKET-SECOND-COLLIDE
(make-ball 246 382 -3 4))
;; initial world
(define INITIAL-STATE-WORLD
(make-world
INITIAL-WORLD-BALLS
INITIAL-WORLD-RACKET
READY-TO-SERVE
BG-COLOR-WHITE
INITIAL-COUNTER-VALUE 1))
;; world in rally state
(define WORLD-RALLY-STATE
(make-world
RANDOM-BALL-330-384
RANDOM-RACKET-149-384
RALLY
BG-COLOR-WHITE
1
1/24))
(define NEXT-WORLD-RALLY-STATE
(make-world
NEXT-RANDOM-BALL-333-375
NEXT-RANDOM-RACKET-148-384
RALLY
BG-COLOR-WHITE
1
1/24))
;; world when mouse "button-down" is pressed
(define AFTER-BUTTON-DOWN-EVENT
(make-world
RANDOM-BALL-330-384
RANDOM-RACKET-149-384
RALLY
BG-COLOR-WHITE
1
1/24))
;; after the left key event
(define NEXT-ON-RIGHT-KEY
(make-world
RANDOM-BALL-330-384
RIGHT-RANDOM-RACKET-149-384
RALLY
BG-COLOR-WHITE
1
1/24))
;; after the up key event
(define NEXT-ON-UP-KEY
(make-world
RANDOM-BALL-330-384
UP-RANDOM-RACKET-149-384
RALLY
BG-COLOR-WHITE
1
1/24))
;; after the down key event
(define NEXT-ON-DOWN-KEY
(make-world
RANDOM-BALL-330-384
DOWN-RANDOM-RACKET-149-384
RALLY
BG-COLOR-WHITE
1
1/24))
;; worlds in ready-to-serve state
(define WORLD-READY-TO-SERVE
(make-world
RANDOM-BALL-330-384
RANDOM-RACKET-149-384
READY-TO-SERVE
BG-COLOR-YELLOW
3
1))
(define NEXT-WORLD-READY-TO-SERVE
(make-world
RANDOM-BALL-330-384
RANDOM-RACKET-149-384
RALLY
BG-COLOR-YELLOW
3
1))
;; after the space key event
(define NEXT-RALLY-WORLD-STATE
(make-world
RANDOM-BALL-330-384
RANDOM-RACKET-149-384
PAUSED
BG-COLOR-WHITE
1
1/24))
;; after the left key event
(define NEXT-ON-LEFT-KEY
(make-world
RANDOM-BALL-330-384
LEFT-RANDOM-RACKET-149-384
RALLY
BG-COLOR-WHITE
1
1/24))
;; worlds with counter 4 and speed 1 in paused state
(define WORLD-PAUSED-COUNTER-C-4-S-1
(make-world
RANDOM-BALL-330-384
RANDOM-RACKET-149-384
PAUSED
BG-COLOR-YELLOW
4
1))
(define NEXT-WORLD-PAUSED-COUNTER-C-4-S-1
(make-world
RANDOM-BALL-330-384
RANDOM-RACKET-149-384
READY-TO-SERVE
BG-COLOR-YELLOW
4
1))
;; worlds with a paused state
;; worlds with consecutive counter in paused state
(define WORLD-PAUSED-COUNTER-5
(make-world
RANDOM-BALL-330-384
RANDOM-RACKET-149-384
PAUSED
BG-COLOR-YELLOW
5
1/24))
(define WORLD-PAUSED-COUNTER-4
(make-world
RANDOM-BALL-330-384
RANDOM-RACKET-149-384
PAUSED
BG-COLOR-YELLOW
4
1/24))
;; worlds with counter 1 and speed 1 in paused state
(define WORLD-PAUSED-COUNTER-C-1-S-1
(make-world
INITIAL-WORLD-BALLS
INITIAL-WORLD-RACKET
READY-TO-SERVE
BG-COLOR-WHITE
1
1))
;; world to end rally state
;; world when ball collides the back wall
(define END-RALLY-STATE-BALL-BACK
(make-world
EMPTY-BALL-LIST
RANDOM-RACKET-149-384
RALLY
BG-COLOR-WHITE
1
1))
;; world when racket collides the front wall
(define END-RALLY-STATE-RACKET-FRONT
(make-world
RANDOM-BALL-330-384
RACKET-FRONT-COLLIDE-Y-4
RALLY
BG-COLOR-WHITE
1
1))
;; world when racket collides the back wall
(define END-RALLY-STATE-RACKET-BACK
(make-world
RANDOM-BALL-330-384
RACKET-BACK-COLLIDE-Y-642
RALLY
BG-COLOR-WHITE
1
1))
;; paused world after rally
(define PAUSED-STATE-AFTER-RALLY
(make-world
EMPTY-BALL-LIST
RACKET-PAUSED-STATE-149-384
PAUSED
BG-COLOR-YELLOW
2
1))
;; image for racket 149 384 in scene
(define RACKET-SCENE-IMAGE (place-image RACKET-IMAGE
149 384
(empty-scene
COURT-WIDTH
COURT-HEIGHT
BG-COLOR-WHITE)))
;; image of ball racket 152 384 in scene
(define RACKET-BALL-SCENE-IMAGE (place-image POINTER-CIRCLE-IMAGE
140 200
(place-image RACKET-IMAGE
152 384
(empty-scene
COURT-WIDTH
COURT-HEIGHT
BG-COLOR-WHITE))))
;; image of multiple balls 424 96 and 417 123 in racket scene 149 384
(define MULTIPLE-BALL-SCENE-IMAGE
(place-image BALL-IMAGE
424 96
(place-image BALL-IMAGE
417 123
(place-image
RACKET-IMAGE
149 384
(empty-scene
COURT-WIDTH
COURT-HEIGHT
BG-COLOR-WHITE)))))
;; image for ball 330 384 and racket 152 384 in paused state
(define IMAGE-RANDOM-PAUSED
(place-image BALL-IMAGE
330 384
(place-image RACKET-IMAGE
152 384
(empty-scene
COURT-WIDTH
COURT-HEIGHT
BG-COLOR-YELLOW))))
;; image for ball 330 384 and racket 149 384 in paused state
(define IMAGE-RANDOM-PAUSED-149-384
(place-image BALL-IMAGE
330 384
(place-image RACKET-IMAGE
149 384
(empty-scene
COURT-WIDTH
COURT-HEIGHT
BG-COLOR-YELLOW))))
;; scene for ball-scene
(define BALL-IMAGE-SCENE
(place-image RACKET-IMAGE
152 384
(empty-scene
COURT-WIDTH
COURT-HEIGHT
BG-COLOR-YELLOW)))
;; scene for racket-scene
(define RACKET-IMAGE-SCENE (empty-scene
COURT-WIDTH
COURT-HEIGHT
BG-COLOR-WHITE))
;; image for ball 330 384 and racket 149 384 in rally state
(define IMAGE-RANDOM-RALLY
(place-image BALL-IMAGE
330 384
(place-image RACKET-IMAGE
149 384
(empty-scene
COURT-WIDTH
COURT-HEIGHT
BG-COLOR-WHITE))))
;; END DATA DEFINITIONS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; initial-world : PosReal -> World
;; GIVEN: the speed of the simulation, in seconds per tick
;; (so larger numbers run slower)
;; RETURNS: the ready-to-serve state of the world
;; EXAMPLE:
;; (initial-world 1) = (make-world INITIAL-WORLD-BALL INITIAL-WORLD-RACKET
;; READY-TO-SERVE BG-COLOR-WHITE
;; INITIAL-COUNTER-VALUE 1)
;; DESIGN STRATEGY: use constructor template for world
(define (initial-world speed)
(make-world
INITIAL-WORLD-BALLS
INITIAL-WORLD-RACKET
READY-TO-SERVE
BG-COLOR-WHITE
INITIAL-COUNTER-VALUE
speed))
;; TESTS:
(begin-for-test
(check-equal?
(initial-world 1)
(make-world
INITIAL-WORLD-BALLS
INITIAL-WORLD-RACKET
READY-TO-SERVE BG-COLOR-WHITE
INITIAL-COUNTER-VALUE 1)
" the initial state is not returning the world that should follow"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; world-after-tick : World -> World
;; GIVEN: any world that's possible for the simulation
;; RETURNS: the world that should follow the given world
;; after a tick
;; EXAMPLES:
;; (world-after-tick WORLD-PAUSED-COUNTER-4) = WORLD-PAUSED-COUNTER-5
;; (world-after-tick WORLD-READY-TO-SERVE) = INITIAL-STATE-WORLD
;; (world-after-tick WORLD-RALLY-STATE) = NEXT-WORLD-RALLY-STATE
;; (world-after-tick END-RALLY-STATE-BALL-BACK) = PAUSED-STATE-AFTER-RALLY
;; DESIGN STRATEGY: cases on state of the world
(define (world-after-tick w)
(cond [(world-paused? w) (world-after-paused-state w)]
[(world-ready-to-serve? w) (initial-world (world-simulation-speed w))]
[(world-rally? w) (world-in-rally-state w)]))
;; TESTS:
(begin-for-test
(check-equal? (world-after-tick WORLD-PAUSED-COUNTER-4)
WORLD-PAUSED-COUNTER-5
"the world is not in the paused state")
(check-equal? (world-after-tick WORLD-READY-TO-SERVE)
INITIAL-STATE-WORLD
"the word is not made as the initial world")
(check-equal? (world-after-tick WORLD-RALLY-STATE)
NEXT-WORLD-RALLY-STATE
"the world state is not the one in the rally state")
(check-equal? (world-after-tick END-RALLY-STATE-BALL-BACK)
PAUSED-STATE-AFTER-RALLY
"the word is not made as the paused world after end rally"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; world-balls : World -> Balls
;; GIVEN: a world
;; RETURNS: a list of the balls that are present in the world
;; (but does not include any balls that have disappeared
;; by colliding with the back wall)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; world-racket : World -> Racket
;; GIVEN: a world
;; RETURNS: the racket that's present in the world
;; NOTE: world-racket is a selector defined by "define-struct world"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ball-x : Ball -> Integer
;; ball-y : Ball -> Integer
;; racket-x : Racket -> Integer
;; racket-y : Racket -> Integer
;; GIVEN: a racket or ball
;; RETURNS: the x or y coordinate of that item's position,
;; in graphics coordinates
;; NOTE: ball-x and ball-y are selector defined by "define-struct ball"
;; racket-x and racket-y are selector define by "define-struct racket"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ball-vx : Ball -> Integer
;; ball-vy : Ball -> Integer
;; racket-vx : Racket -> Integer
;; racket-vy : Racket -> Integer
;; GIVEN: a racket or ball
;; RETURNS: the vx or vy component of that item's velocity,
;; in pixels per tick
;; NOTE: ball-vx and ball-vy are selector defined by "define-struct ball"
;; racket-vx and racket-vy are selector define by "define-struct racket"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; racket-selected? : Racket-> Boolean
;; GIVEN: a racket
;; RETURNS: true iff the racket is selected
;; NOTE: racket-selected? is a selector defined by "define-struct racket"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; world-in-rally-state : World -> World
;; GIVEN: a world
;; RETURNS: the world which follow the given world when the state is rally
;; EXAMPLES:
;; (world-after-tick WORLD-RALLY-STATE) = NEXT-WORLD-RALLY-STATE
;; DESIGN STRATEGY: case on the end of rally state
(define (world-in-rally-state w)
(if (end-rally-state? w)
(world-after-paused-state w)
(world-change-in-rally-state w)))
;; TESTS:
(begin-for-test
(check-equal? (world-after-tick WORLD-RALLY-STATE)
NEXT-WORLD-RALLY-STATE
"the world state is not the one in the rally state"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; world-change-in-rally-state : World -> World
;; GIVEN: a world
;; RETURNS: the world which follow the given world when the state is rally
;; EXAMPLES:
;; (world-change-in-rally-state WORLD-RALLY-STATE) = NEXT-WORLD-RALLY-STATE
;; DESIGN STRATEGY: use constructor template for world
(define (world-change-in-rally-state w)
(make-world
(balls-after-tick (world-balls w) (world-racket w))
(racket-after-tick (world-racket w) (world-balls w))
(world-state w)
(world-bg-color w)
(world-tick-counter w)
(world-simulation-speed w)))
;; TESTS:
(begin-for-test
(check-equal? (world-change-in-rally-state WORLD-RALLY-STATE)
NEXT-WORLD-RALLY-STATE
"the world state is not the one in the rally state"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; world-after-paused-state : World -> World
;; GIVEN: world with the paused world-state
;; RETURNS: the world which follow the given world when the state is paused
;; after a tick and after three seconds of real time reset the
;; simulation to ready-to-serve state
;; EXAMPLES:
;; (world-after-paused-state WORLD-PAUSED-COUNTER-C-4-S-1)
;; = WORLD-PAUSED-COUNTER-C-1-S-1
;; (world-after-paused-state WORLD-PAUSED-COUNTER-4) = WORLD-PAUSED-COUNTER-5
;; DESIGN STRATEGY: combine simpler functions
(define (world-after-paused-state w)
(if (paused-time-counter? (world-tick-counter w) (world-simulation-speed w))
(world-with-paused-state w)
(initial-world (world-simulation-speed w))))
;; TESTS:
(begin-for-test
(check-equal? (world-after-paused-state WORLD-PAUSED-COUNTER-C-4-S-1)
WORLD-PAUSED-COUNTER-C-1-S-1
"the world is not changed to ready to serve state after
three seconds")
(check-equal? (world-after-paused-state WORLD-PAUSED-COUNTER-4)
WORLD-PAUSED-COUNTER-5
"the tick-counter of the world is not increased by 1"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; paused-time-counter? : PosInt PosReal -> Boolean
;; GIVEN: the tick counter and simulation speed of world
;; RETURNS: true iff, the number if tick counter is less than or equal to ticks
;; required in real world three seconds
;; EXAMPLES:
;; (paused-time-counter? 4 1/24)
;; = true
;; DESIGN STRATEGY: use simple function for comparison
(define (paused-time-counter? tc s)
(<=
tc
(/ PAUSED-WAIT-TIME s)))
;; TESTS:
(begin-for-test
(check-equal? (paused-time-counter? 4 1/24)
true
"the tick-counter of the world should be true as less than
the ticks of three seconds"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; world-with-paused-state : World -> World
;; GIVEN: world with the paused world-state
;; RETURNS: the world which follow the given world for three seconds of
;; real time when the state is paused
;; EXAMPLES:
;; (world-after-paused-state WORLD-PAUSED-COUNTER-4) = WORLD-PAUSED-COUNTER-5
;; DESIGN STRATEGY: use constructor template for World on w
(define (world-with-paused-state w)
(make-world
(world-balls w)
(world-racket w)
PAUSED
BG-COLOR-YELLOW
(+ (world-tick-counter w) 1)
(world-simulation-speed w)))
;; TESTS:
(begin-for-test
(check-equal? (world-with-paused-state WORLD-PAUSED-COUNTER-4)
WORLD-PAUSED-COUNTER-5
"the world counter is not increased by 1"))