Don't like ads? PRO users don't see any ads ;-)
Guest

Field Goal Game

By: a guest on May 14th, 2012  |  syntax: None  |  size: 65.89 KB  |  hits: 37  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ;Aalok Kharidia
  2. ;Computer Science FINAL PROJECT
  3. ;Section C
  4. ;Part 1: Due: Monday April 30th, 2012
  5. ;Part 2: Due: Monday May 14th, 2012
  6. ;ULTIMATE FIELD GOAL KICKING GAME!!
  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. ;                                       GAME DESCRPTION                                            ;
  43. ;                                                                                                  ;
  44. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  45.  
  46.  
  47. ;This game is very closely related to kicking a field goal in a normal football game online. The   ;
  48. ;game will contain a field goal post, a ball, a kicker, and a scoreboard. The field goal post will ;
  49. ;be represented by two yellow squares of dimensions 25x25 pixels. These squares will be separated  ;
  50. ;by a vertical distance of 50 pixels. The ball will be represented by a circle of radius 10. Also, ;
  51. ;the starting position of the ball will be at a random x-location between 50 and 100, and the y-   ;
  52. ;location will be constant. The kicker will be a rectangle of height 50 and width 5. The scene     ;
  53. ;will be of width 400 and height 300. The kick will be successful if the ball passes between the   ;
  54. ;two yellow squares. The user will first use the UP and DOWN arrow keys to set the angle at        ;
  55. ;which the kicker will kick the ball. The arrow will be in the direction of an arrow. After setting;
  56. ;the angle, the user will hit the SPACEBAR to verify the angle. Next, there will be a countdown    ;
  57. ;from 3. When the countdown reaches 0, the kicker will move towards the ball at a constant         ;
  58. ;speed. The kicker will kick the ball, and the ball will move towards the field goal post at the   ;
  59. ;specified angle. The kick will be successful if the ball passes between the two yellow squares.   ;
  60. ;Each kick will be worth 15 points, and the user will have 10 lives. The scoreboard will say how   ;
  61. ;many lives remain, and how many points the user has gained. There will be acid rain. This rain    ;
  62. ;will have the ability to make any ball disintegrate. The rain will fall down in columns in the    ;
  63. ;middle of the scene at reasonable intervals. The intervals will not be too small. This is so that ;
  64. ;the ball will have the possibility of safely passing through the rain. The rain will be in a      ;
  65. ;random number of columns. There will be 1, 2, or 3 columns of rain. The rain will begin to fall   ;
  66. ;when the SPACEBAR is hit.                                                                         ;                                                                             ;
  67.  
  68.  
  69.  
  70.  
  71.  
  72. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  73. ;;;;;;;;;;;;```______````_```````````````````````````_``````````````````````````````````;;;;;;;;;;;;
  74. ;;;;;;;;;;;;`.'`____`\``/`|_````````````````````````/`|_````````````````````````````````;;;;;;;;;;;;
  75. ;;;;;;;;;;;;`|`(___`\_|`|`|-'_`.--.``__```_```.---.`|`|-'__```_```_`.--.``.---.``.--.```;;;;;;;;;;;;
  76. ;;;;;;;;;;;;``_.____`.``|`|`[``/'`\][``|`|`|`/`/'`\]|`|`[``|`|`|`[``/'`\]/`/__\\(`(`\]``;;;;;;;;;;;;
  77. ;;;;;;;;;;;;`|`\____)`|`|`|,`|`|`````|`\_/`|,|`\__.`|`|,`|`\_/`|,`|`|````|`\__.,``'.'.``;;;;;;;;;;;;
  78. ;;;;;;;;;;;;``\______.'`\__/[___]````'.__.'_/'.___.'\__/`'.__.'_/[___]````'.__.'[\__)`)`;;;;;;;;;;;;
  79. ;;;;;;;;;;;;````````````````````````````````````````````````````````````````````````````;;;;;;;;;;;;
  80. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  81.  
  82. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  83. ;                                      ;
  84. ;                 BALL                 ;
  85. ;                                      ;
  86. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  87. ;A ball is a structure. This structure
  88. ;will contain x location, y location
  89. ;speed, an angle and a color,
  90. ;besides yellow.
  91. ;(make-ball number number number number string)
  92. (define-struct ball (x y speed angle color))
  93. ; ;Template
  94. ; (define (ball-func ball)
  95. ;   (... (ball-x ball)...
  96. ;        (ball-y ball)...
  97. ;        (ball-speed ball)...
  98. ;        (ball-angle ball)...
  99. ;        (ball-color ball)...))
  100.  
  101. (make-ball 50 570 10 30 "red")
  102. (make-ball 70 570 13 30 "blue")
  103. (make-ball 80 570 15 45 "black")
  104. (make-ball 100 570 24 45 "brown")
  105.  
  106. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  107. ;                                      ;
  108. ;                 POST                 ;
  109. ;                                      ;
  110. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  111. ;The post is a structure. This structure
  112. ;will contain x location, and y location.
  113. ;(make-post number number)
  114. (define-struct post (x y))
  115. ; ;Template
  116. ; (define (post-func post)
  117. ;   (... (post-x post)...
  118. ;        (post-y post)...))
  119.  
  120. (make-post 760 60)
  121. (make-post 760 90)
  122. (make-post 760 120)
  123. (make-post 760 150)
  124.  
  125. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  126. ;                                      ;
  127. ;                 RAIN                 ;
  128. ;                                      ;
  129. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  130. ;A drop of rain is a structure. This
  131. ;structure will contain x location,
  132. ;y location, fall speed, and color.
  133. ;(make-rain number number number string)
  134. (define-struct rain (x y speed color))
  135. ; ;Template
  136. ; (define (rain-func rain)
  137. ;   (... (rain-x rain)...
  138. ;        (rain-y rain)...
  139. ;        (rain-speed rain)...
  140. ;        (rain-color rain)...))
  141.  
  142. (make-rain 400 10 13 "green")
  143. (make-rain 420 20 12 "green")
  144. (make-rain 440 35 11 "green")
  145. (make-rain 460 55 10 "green")
  146.  
  147. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  148. ;                                      ;
  149. ;            LIST OF RAIN              ;
  150. ;                                      ;
  151. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  152. ;A list of rain is a structure. This
  153. ;structure will contain multiple rain.
  154. ;list-of-rain is either:
  155. ;empty or
  156. ;(cons rain list-of-rain)
  157. ; ;Template
  158. ; (define (list-of-rain-func list)
  159. ;   (... (rain-x (first list)...
  160. ;        (rain-y (first list))...
  161. ;        (rain-speed (first list))...
  162. ;        (rain-color (first list))...))
  163. ;   (rest list)...)
  164.  
  165. (cons (make-rain 400 10 13 "green") empty)
  166. (cons (make-rain 400 10 13 "green")
  167.       (cons (make-rain 420 20 12 "green") empty))
  168. (cons (make-rain 400 10 13 "green")
  169.       (cons (make-rain 420 20 12 "green")
  170.             (cons (make-rain 240 35 11 "green") empty)))
  171. (cons (make-rain 400 10 13 "green")
  172.       (cons (make-rain 420 20 12 "green")
  173.             (cons (make-rain 420 35 11 "green")
  174.                   (cons (make-rain 460 55 10 "green") empty))))
  175.  
  176. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  177. ;                                      ;
  178. ;                KICKER                ;
  179. ;                                      ;
  180. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  181. ;A kicker is a structure. This
  182. ;structure will contain x location,
  183. ;y location, run speed, and color.
  184. ;(make-rain number number number string)
  185. (define-struct kicker (x y speed color))
  186. ; ;Template
  187. ; (define (kicker-func kicker)
  188. ;   (... (kicker-x kicker)...
  189. ;        (kicker-y kicker)...
  190. ;        (kicker-speed kicker)...
  191. ;        (kicker-color kicker)...))
  192.  
  193. (make-kicker 13 570 10 "black")
  194. (make-kicker 24 570 12 "black")
  195. (make-kicker 30 570 14 "black")
  196. (make-kicker 35 570 16 "black")
  197.  
  198. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  199. ;                                      ;
  200. ;             SCOREBOARD               ;
  201. ;                                      ;
  202. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  203. ;A scoreboard is a structure. This
  204. ;structure will contain a number representing score,
  205. ;and a number representing the number of lives remaining
  206. ;(make-scoreboard number number)
  207. (define-struct scoreboard (score lives))
  208. ; ;Template
  209. ; (define (kicker-func scoreboard)
  210. ;   (... (scoreboard-score scoreboard)...
  211. ;        (scoreboard-lives scoreboard)...))
  212.  
  213. (make-scoreboard 0 10)
  214. (make-scoreboard 30 10)
  215. (make-scoreboard 30 9)
  216. (make-scoreboard 60 0)
  217.  
  218. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  219. ;                                      ;
  220. ;                WORLD                 ;
  221. ;                                      ;
  222. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  223. ;A world is a structure. This
  224. ;structure will contain a ball, a post,
  225. ;rain and a kicker.
  226. ;(make-world ball post rain kicker)
  227. (define-struct world (ball post list-of-rain kicker scoreboard))
  228. ; ;Template
  229. ; (define (world-func world)
  230. ;   (...(...(ball-x (world-ball world))...
  231. ;           (ball-y (world-ball world))...
  232. ;           (ball-speed (world-ball world))...
  233. ;           (ball-angle (world-ball world))...
  234. ;           (ball-color (world-ball world))...)
  235. ;       (...(post-x (world-post world))...
  236. ;           (post-y (world-post world))...)
  237. ;       (...(...(rain-x (first (world-list-of-rain world))...
  238. ;               (rain-y (first (world-list-of-rain world)))...
  239. ;               (rain-speed (first (world-list-of-rain world)))...
  240. ;               (rain-color (first (world-list-of-rain world)))...))
  241. ;           (rest (world-list-of-rain world))...)
  242. ;       (...(kicker-x (world-kicker world))...
  243. ;           (kicker-y (world-kicker world))...
  244. ;           (kicker-speed (world-kicker world))...
  245. ;           (kicker-color (world-kicker world))...)
  246. ;       (... (scoreboard-score scoreboard)...
  247. ;        (scoreboard-lives scoreboard)...)))
  248.  
  249. (make-world (make-ball 50 570 10 30 "red")
  250.             (make-post 760 60)
  251.             (list (make-rain 400 10 13 "green"))
  252.             (make-kicker 13 570 10 "black")
  253.             (make-scoreboard 0 10))
  254. (make-world (make-ball 70 570 13 30 "blue")
  255.             (make-post 760 90)
  256.             (list  (make-rain 400 10 13 "green")
  257.                    (make-rain 420 20 12 "green"))
  258.             (make-kicker 24 570 12 "black")
  259.             (make-scoreboard 30 10))
  260. (make-world (make-ball 80 570 15 45 "orange")
  261.             (make-post 760 120)
  262.             (list  (make-rain 400 10 13 "green")
  263.                    (make-rain 420 20 12 "green")
  264.                    (make-rain 440 35 11 "green"))
  265.             (make-kicker 30 570 14 "black")
  266.             (make-scoreboard 30 9))
  267. (make-world (make-ball 100 570 24 45 "brown")
  268.             (make-post 760 150)
  269.             (list  (make-rain 400 10 13 "green")
  270.                    (make-rain 420 20 12 "green")
  271.                    (make-rain 440 35 11 "green")
  272.                    (make-rain 460 55 10 "green"))
  273.             (make-kicker 35 570 16 "black")
  274.             (make-scoreboard 60 0))
  275.  
  276.  
  277.  
  278.  
  279. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  280. ;;;;;;;;;;;;;;;;;```______``````````````````````````_```````````````````_``````````;;;;;;;;;;;;;;;;;
  281. ;;;;;;;;;;;;;;;;;`.'`___``|````````````````````````/`|_````````````````/`|_````````;;;;;;;;;;;;;;;;;
  282. ;;;;;;;;;;;;;;;;;/`.'```\_|``.--.```_`.--.```.--.``|`|-',--.```_`.--.``|`|-'.--.```;;;;;;;;;;;;;;;;;
  283. ;;;;;;;;;;;;;;;;;|`|```````/`.'`\`\[``.-.`|`(`(`\]`|`|``'_\`:`[``.-.`|`|`|`(`(`\]``;;;;;;;;;;;;;;;;;
  284. ;;;;;;;;;;;;;;;;;\``.___.'\|`\__.`|`|`|`|`|```'.'.`|`|,//`|`|,`|`|`|`|`|`|,``'.'.``;;;;;;;;;;;;;;;;;
  285. ;;;;;;;;;;;;;;;;;``.____`.'`'.__.'`[___||__][\__)`)\__/\'-;__/[___||__]\__/[\__)`)`;;;;;;;;;;;;;;;;;
  286. ;;;;;;;;;;;;;;;;;``````````````````````````````````````````````````````````````````;;;;;;;;;;;;;;;;;
  287. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  288. (define HEIGHT 600)                                    ;Height of SCENE
  289. (define WIDTH 800)                                     ;Width of SCENE
  290. (define BOX-HEIGHT 125)                                ;POST Box Height
  291. (define BOX-WIDTH 50)                                  ;POST Box Width
  292. (define POST-HEIGHT 25)                                ;POST Square Side Length
  293. (define POST-WIDTH 50)                                 ;POST Square Side Length
  294. (define KICKER-WIDTH 5)                                ;Width of KICKER
  295. (define KICKER-HEIGHT 50)                              ;Height of KICKER
  296. (define BALL-RADIUS 10)                                ;Radius of BALL
  297. (define RAIN-WIDTH 4)                                  ;Width of RAIN
  298. (define RAIN-HEIGHT 8)                                 ;Height of RAIN
  299. (define BALL-Y 570)                                    ;Starting y-location of BALL
  300. (define BALL-SPEED 10)                                 ;Speed of BALL
  301. (define POST-X 775)                                    ;Starting x-location of POST
  302. (define KICKER-X 10)                                   ;Starting x-position of KICKER
  303. (define KICKER-Y 570)                                  ;Starting y-location of KICKER
  304. (define KICKER-SPEED 5)                                ;Speed of all KICKER
  305. (define RAIN-X 500)                                    ;X-Positions of all RAIN
  306. (define RAIN-SPEED 3)                                  ;Speed of all RAIN
  307. (define SCOREBOARD-X 75)                               ;Starting x-position of scoreboard
  308. (define SCOREBOARD-Y 75)                               ;Starting y-position of scoreboard
  309.  
  310. (define SCENE (rectangle WIDTH HEIGHT "solid" "darkgreen"))                     ;SCENE
  311.  
  312. (define KICKER-IMAGE (rectangle KICKER-WIDTH  KICKER-HEIGHT "solid" "black"))   ;KICKER image
  313.  
  314. (define BALL-IMAGE (circle BALL-RADIUS "solid" "red"))                          ;BALL image
  315.  
  316. (define POST-SQUARE (rectangle POST-WIDTH POST-HEIGHT "solid" "yellow"))        ;yellow square
  317.  
  318. (define POST-IMAGE (place-image POST-SQUARE 25 12.5
  319.                                 (place-image POST-SQUARE 25 112.5
  320.                                              (rectangle BOX-WIDTH BOX-HEIGHT "solid" "darkgreen"))))      ;POST image
  321.  
  322. (define ARROW-IMAGE
  323.   (place-image (rectangle 100 7.5 "solid" "darkgreen") 50 3.75
  324.          (place-image (rectangle 100 7.5 "solid" "darkgreen") 50 16.25
  325.                  (place-image (rectangle 50 10 "solid" "darkgreen") 25 10
  326.                         (place-image (triangle/sas 10 90 10 "solid" "darkgreen") 105 3.75
  327.                                (place-image(flip-vertical
  328.                                       (triangle/sas 10 90 10 "solid" "darkgreen")) 105 16.25
  329.                                             (rectangle 110 20 "solid" "blue")))))))                     ;ARROW image
  330.  
  331. (define BALL-ARROW-IMAGE (place-image BALL-IMAGE 55 10 ARROW-IMAGE))            ;BALL on ARROW image
  332.  
  333. (define RAIN (rectangle RAIN-WIDTH RAIN-HEIGHT "solid" "green"))                ;RAIN
  334.  
  335.  
  336.  
  337.  
  338.  
  339. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  340. ;;;;;;;;;;;;;;;;;`________``````````````````````````_````_`````````````````````````;;;;;;;;;;;;;;;;;
  341. ;;;;;;;;;;;;;;;;;|_```__``|````````````````````````/`|_`(_)````````````````````````;;;;;;;;;;;;;;;;;
  342. ;;;;;;;;;;;;;;;;;``|`|_`\_|__```_```_`.--.```.---.`|`|-'__```.--.```_`.--.```.--.``;;;;;;;;;;;;;;;;;
  343. ;;;;;;;;;;;;;;;;;``|``_|``[``|`|`|`[``.-.`|`/`/'`\]|`|`[``|/`.'`\`\[``.-.`|`(`(`\]`;;;;;;;;;;;;;;;;;
  344. ;;;;;;;;;;;;;;;;;`_|`|_````|`\_/`|,`|`|`|`|`|`\__.`|`|,`|`||`\__.`|`|`|`|`|```'.'.`;;;;;;;;;;;;;;;;;
  345. ;;;;;;;;;;;;;;;;;|_____|```'.__.'_/[___||__]'.___.'\__/[___]'.__.'`[___||__][\__)`);;;;;;;;;;;;;;;;;
  346. ;;;;;;;;;;;;;;;;;``````````````````````````````````````````````````````````````````;;;;;;;;;;;;;;;;;
  347. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  348.  
  349.  
  350. ; Convert-to-Radians Function
  351. ; convert-to-radians: ball -> number
  352. ; Takes ball with an angle in degrees, and converts the angle
  353. ; into radians to be used in ball-xspeed and ball-yspeed.
  354. ; Examples:
  355. (check-within (convert-to-radians (make-ball 50 270 10 30 "red")) .5235987756 .1)
  356. (check-within (convert-to-radians (make-ball 70 270 13 30 "blue")) .5235987756 .1)
  357. (check-within (convert-to-radians (make-ball 80 270 15 45 "black"))  0.7853981633974483 .1)
  358. (check-within (convert-to-radians (make-ball 100 270 24 45 "brown")) 0.7853981633974483 .1)
  359. (define (convert-to-radians ball)
  360.   (* (ball-angle ball) (/ pi 180)))
  361.  
  362.  
  363. ; Update-Angle Function
  364. ; update-angle: ball number string -> ball
  365. ; Given a ball and a number and a string which will be either "up" or "down",
  366. ; Outputs a ball with an angle updated based on string and number.
  367. ; Examples:
  368. (check-expect (update-angle (make-ball 50 270 10 30 "red") 5 "up")
  369.               (make-ball 50 270 10 35 "red"))
  370. (check-expect (update-angle (make-ball 50 270 10 30 "red") 5 "down")
  371.               (make-ball 50 270 10 25 "red"))
  372. (check-expect (update-angle (make-ball 50 270 10 30 "red") 1 "up")
  373.               (make-ball 50 270 10 31 "red"))
  374. (check-expect (update-angle (make-ball 50 270 10 30 "red") 1 "down")
  375.               (make-ball 50 270 10 29 "red"))
  376. (define (update-angle ball number string)
  377.   (cond [(string=? string "up")
  378.          (make-ball (ball-x ball)
  379.                     (ball-y ball)
  380.                     (ball-speed ball)
  381.                     (+ (ball-angle ball) number)
  382.                     (ball-color ball))]
  383.         [(string=? string "down")
  384.          (make-ball (ball-x ball)
  385.                     (ball-y ball)
  386.                     (ball-speed ball)
  387.                     (- (ball-angle ball) number)
  388.                     (ball-color ball))]))
  389.  
  390.  
  391. ; Ball-X-Start-Position Function
  392. ; ball-x-start-position: number -> ball
  393. ; Given a number that is never actually used,
  394. ; outputs a ball with an X location that is at
  395. ; random between 50 and 75.
  396. (define (ball-x-start-position number) (+ (random 26) 50))
  397. ; Test Cases:
  398. ; Each should generate a random number between 50 and 75
  399. (ball-x-start-position 1)
  400. (ball-x-start-position 13)
  401. (ball-x-start-position 24)
  402. (ball-x-start-position 25)
  403.  
  404.  
  405. ; Ball-XSpeed Function
  406. ; ball-xspeed: ball -> number
  407. ; Given a ball with a specified speed and angle,
  408. ; calculates the x-speed of the ball (COSINE). Uses the
  409. ; convert-to-radians function.
  410. ; Examples:
  411. (check-within (ball-xspeed (make-ball 50 270 10 30 "red")) 8.660254038 .1)
  412. (check-within (ball-xspeed (make-ball 70 270 13 30 "blue")) 11.25833025 .1)
  413. (check-within (ball-xspeed (make-ball 80 270 15 45 "black")) 10.60660172 .1)
  414. (check-within (ball-xspeed (make-ball 100 270 24 45 "brown")) 16.97056275 .1)
  415. (define (ball-xspeed ball)
  416.   (inexact->exact (* (ball-speed ball) (cos (convert-to-radians ball)))))
  417.  
  418.  
  419. ; Ball-YSpeed Function
  420. ; ball-yspeed: ball -> number
  421. ; Given a ball with a specified speed and angle,
  422. ; calculates the y-speed of the ball (SINE). Uses the
  423. ; convert-to-radians function.
  424. ; Examples:
  425. (check-within (ball-yspeed (make-ball 50 270 10 30 "red")) 5 .1)
  426. (check-within (ball-yspeed (make-ball 70 270 13 30 "blue")) 6.5 .1)
  427. (check-within (ball-yspeed (make-ball 80 270 15 45 "black")) 10.60660172 .1)
  428. (check-within (ball-yspeed (make-ball 100 270 24 45 "brown")) 16.97056275 .1)
  429. (define (ball-yspeed ball)
  430.   (inexact->exact (* (ball-speed ball) (sin (convert-to-radians ball)))))
  431.  
  432.  
  433. ; Update-Ball Function
  434. ; update-ball: ball -> ball
  435. ; Given a ball, outputs a ball with updated
  436. ; x and y positions based on the x-speed claculated in
  437. ; the ball-xspeed function and the y-speed calculated in
  438. ; the ball-yspeed function. The speed, angle, and color
  439. ; of the ball remain constant.
  440. ; Examples:
  441. (check-expect (update-ball (make-ball 50 270 10 30 "red"))
  442.               (make-ball (+ 50 (ball-xspeed (make-ball 50 270 10 30 "red")))
  443.                          (- 270 (ball-yspeed (make-ball 50 270 10 30 "red")))
  444.                          10 30 "red"))
  445. (check-expect (update-ball (make-ball 70 270 13 30 "blue"))
  446.               (make-ball (+ 70 (ball-xspeed (make-ball 70 270 13 30 "blue")))
  447.                          (- 270 (ball-yspeed (make-ball 70 270 13 30 "blue")))
  448.                          13 30 "blue"))
  449. (check-expect (update-ball (make-ball 80 270 15 45 "black"))
  450.               (make-ball (+ 80 (ball-xspeed (make-ball 80 270 15 45 "black")))
  451.                          (- 270 (ball-yspeed (make-ball 80 270 15 45 "black")))
  452.                          15 45 "black"))
  453. (check-expect (update-ball (make-ball 100 270 24 45 "brown"))
  454.               (make-ball (+ 100 (ball-xspeed (make-ball 100 270 24 45 "brown")))
  455.                          (- 270 (ball-yspeed (make-ball 100 270 24 45 "brown")))
  456.                          24 45 "brown"))
  457. (define (update-ball ball)
  458.   (make-ball (+ (ball-x ball) (ball-xspeed ball))
  459.              (- (ball-y ball) (ball-yspeed ball))
  460.              (ball-speed ball)
  461.              (ball-angle ball)
  462.              (ball-color ball)))
  463.  
  464.  
  465. ; Render-Ball-and-Arrow Function
  466. ; render-ball-and-arrow: ball image-> scene
  467. ; Given a ball, outputs a scene with the ball and arrow placed
  468. ; at the correct x and y location, with angle facing correct angle direction.
  469. ; Examples:
  470. (check-expect (render-ball-and-arrow (make-ball 50 270 10 30 "red") SCENE)
  471.               (place-image (rotate 30 BALL-ARROW-IMAGE) 50 270 SCENE))
  472. (check-expect (render-ball-and-arrow (make-ball 70 270 13 30 "red") SCENE)
  473.               (place-image (rotate 30 BALL-ARROW-IMAGE) 70 270 SCENE))
  474. (check-expect (render-ball-and-arrow (make-ball 80 270 15 45 "red") SCENE)
  475.               (place-image (rotate 45 BALL-ARROW-IMAGE) 80 270 SCENE))
  476. (check-expect (render-ball-and-arrow (make-ball 100 270 24 45 "red") SCENE)
  477.               (place-image (rotate 45 BALL-ARROW-IMAGE) 100 270 SCENE))
  478. (define (render-ball-and-arrow ball scene-so-far)
  479.   (place-image (rotate (ball-angle ball) BALL-ARROW-IMAGE)
  480.                (ball-x ball) (ball-y ball) scene-so-far))
  481.  
  482.  
  483. ; Render-Ball Function
  484. ; render-ball: ball image -> scene
  485. ; Given a ball that is NOT at its starrting/launching position, but rather
  486. ; in flight. Places the ball in the SCENE at the specific x and y locations.
  487. ; Examples:
  488. (check-expect (render-ball (make-ball 100 150 10 30 "red") SCENE)
  489.               (place-image BALL-IMAGE 100 150 SCENE))
  490. (check-expect (render-ball (make-ball 170 170 13 30 "red") SCENE)
  491.               (place-image BALL-IMAGE 170 170 SCENE))
  492. (check-expect (render-ball (make-ball 240 130 15 45 "red") SCENE)
  493.               (place-image BALL-IMAGE 240 130 SCENE))
  494. (check-expect (render-ball (make-ball 150 200 24 45 "red") SCENE)
  495.               (place-image BALL-IMAGE 150 200 SCENE))
  496. (define (render-ball ball scene-so-far)
  497.   (place-image BALL-IMAGE (ball-x ball) (ball-y ball) scene-so-far))
  498.  
  499.  
  500. ; Update-Kicker Function
  501. ; update-function: kicker -> kicker
  502. ; Given a kicker, outputs a kicker with an updated x-location
  503. ; based on speed.
  504. ; Examples:
  505. (check-expect (update-kicker (make-kicker 13 270 10 "black"))
  506.               (make-kicker 23 270 10 "black"))
  507. (check-expect (update-kicker (make-kicker 24 270 12 "black"))
  508.               (make-kicker 36 270 12 "black"))
  509. (check-expect (update-kicker (make-kicker 30 270 14 "black"))
  510.               (make-kicker 44 270 14 "black"))
  511. (check-expect (update-kicker (make-kicker 33 270 16 "black"))
  512.               (make-kicker 49 270 16 "black"))
  513. (define (update-kicker kicker)
  514.   (make-kicker (+ (kicker-x kicker) (kicker-speed kicker))
  515.                (kicker-y kicker) (kicker-speed kicker) (kicker-color kicker)))
  516.  
  517.  
  518. ; Render-Kicker Function
  519. ; render-kicker: kicker image -> scene
  520. ; Given a kicker, outputs a scene with the kicker placed at the
  521. ; correct x and y locations.
  522. ; Examples
  523. (check-expect (render-kicker (make-kicker 13 270 10 "black") SCENE)
  524.               (place-image KICKER-IMAGE 13 270 SCENE))
  525. (check-expect (render-kicker (make-kicker 24 270 12 "black") SCENE)
  526.               (place-image KICKER-IMAGE 24 270 SCENE))
  527. (check-expect (render-kicker (make-kicker 30 270 14 "black") SCENE)
  528.               (place-image KICKER-IMAGE 30 270 SCENE))
  529. (check-expect (render-kicker (make-kicker 35 270 16 "black") SCENE)
  530.               (place-image KICKER-IMAGE 35 270 SCENE))
  531. (define (render-kicker kicker scene-so-far)
  532.   (place-image KICKER-IMAGE (kicker-x kicker) (kicker-y kicker) scene-so-far))
  533.  
  534.  
  535. ; Rain-Fall Function
  536. ; rain-fall: rain -> rain
  537. ; Given a rain, outputs an updated rain, with y-position updated by speed.
  538. ; Examples:
  539. (check-expect (rain-fall (make-rain 200 10 13 "green")) (make-rain 200 23 13 "green"))
  540. (check-expect (rain-fall (make-rain 220 20 12 "green")) (make-rain 220 32 12 "green"))
  541. (check-expect (rain-fall (make-rain 240 35 11 "green")) (make-rain 240 46 11 "green"))
  542. (check-expect (rain-fall (make-rain 260 40 10 "green")) (make-rain 260 50 10 "green"))
  543. (define (rain-fall rain)
  544.   (make-rain (rain-x rain) (+ (rain-y rain) (rain-speed rain))
  545.              (rain-speed rain) (rain-color rain)))
  546.  
  547.  
  548. ; Rainstorm Function
  549. ; rainstorm: list-of-rain -> list-of-rain
  550. ; Given a list of rain, outputs a list of rain with updated y-locations based on speeds.
  551. (check-expect (rainstorm (list  (make-rain 200 10 13 "green")
  552.                                 (make-rain 200 40 13 "green")
  553.                                 (make-rain 200 70 13 "green")))
  554.               (list  (make-rain 200 23 13 "green")
  555.                      (make-rain 200 53 13 "green")
  556.                      (make-rain 200 83 13 "green")))
  557. (check-expect (rainstorm (list  (make-rain 180 10 13 "green")
  558.                                 (make-rain 180 40 13 "green")
  559.                                 (make-rain 180 70 13 "green")
  560.                                 (make-rain 200 40 13 "green")
  561.                                 (make-rain 200 70 13 "green")
  562.                                 (make-rain 200 100 13 "green")))
  563.               (list  (make-rain 180 23 13 "green")
  564.                      (make-rain 180 53 13 "green")
  565.                      (make-rain 180 83 13 "green")
  566.                      (make-rain 200 53 13 "green")
  567.                      (make-rain 200 83 13 "green")
  568.                      (make-rain 200 113 13 "green")))
  569. (check-expect (rainstorm (list  (make-rain 180 0 13 "green")
  570.                                 (make-rain 180 30 13 "green")
  571.                                 (make-rain 180 60 13 "green")
  572.                                 (make-rain 200 30 13 "green")
  573.                                 (make-rain 200 60 13 "green")
  574.                                 (make-rain 200 90 13 "green")
  575.                                 (make-rain 220 60 13 "green")
  576.                                 (make-rain 220 90 13 "green")
  577.                                 (make-rain 220 120 13 "green")))
  578.               (list  (make-rain 180 13 13 "green")
  579.                      (make-rain 180 43 13 "green")
  580.                      (make-rain 180 73 13 "green")
  581.                      (make-rain 200 43 13 "green")
  582.                      (make-rain 200 73 13 "green")
  583.                      (make-rain 200 103 13 "green")
  584.                      (make-rain 220 73 13 "green")
  585.                      (make-rain 220 103 13 "green")
  586.                      (make-rain 220 133 13 "green")))
  587. (define (rainstorm list-of-rain)
  588.   (cond [(empty? list-of-rain) empty]
  589.         [(>= (rain-y (first list-of-rain)) HEIGHT) (rainstorm (rest list-of-rain))]
  590.         [else (cons (rain-fall (first list-of-rain))
  591.                     (rainstorm (rest list-of-rain)))]))
  592.  
  593.  
  594. ; Last-of-List Function
  595. ; last-of-list: list -> element
  596. ; Takes a list, outputs the last element in that list.
  597. (check-expect (last-of-list (list 1 2 3)) 3)
  598. (check-expect (last-of-list (list (make-rain 180 45 13 "green")
  599.                                   (make-rain 135 54 12 "red")
  600.                                   (make-rain 234 24 23 "blue")))
  601.               (make-rain 234 24 23 "blue"))
  602. (define (last-of-list alist)
  603.   (cond [(empty? (rest alist)) (first alist)]
  604.         [else (last-of-list (rest alist))]))
  605.  
  606.  
  607. ;Render-Rain Function
  608. ;render-rain: list-of-rain image -> scene
  609. ;Given a list-of-rain, outputs a scene with each rain drop drawn at it correct locations.
  610. (check-expect (render-rain  (list  (make-rain 180 213 13 "green")
  611.                                    (make-rain 180 163 13 "green")
  612.                                    (make-rain 180 113 13 "green")
  613.                                    (make-rain 180 63 13 "green")
  614.                                    (make-rain 180 13 13 "green")) SCENE)
  615.               (place-image RAIN 180 213
  616.                      (place-image RAIN 180 163
  617.                             (place-image RAIN 180 113
  618.                                    (place-image RAIN 180 63
  619.                                           (place-image RAIN 180 13 SCENE))))))
  620. (define (render-rain list scene-so-far)
  621.   (cond [(empty? list) scene-so-far]
  622.         [else (place-image RAIN (rain-x (first list)) (rain-y (first list))
  623.                            (render-rain (rest list) scene-so-far))]))
  624.  
  625.  
  626. ; Post-Random-Y Function
  627. ; post-random-y: number -> post
  628. ; Given a random dumby number, which is never used,
  629. ; Outputs a post with a random y position between 60 and 150.
  630. (define (post-random-y number) (+ (random 251) 100))
  631. ; Test Cases
  632. ; Each should output a number between 60 and 150
  633. (post-random-y 1)
  634. (post-random-y 2)
  635. (post-random-y 3)
  636. (post-random-y 4)
  637.  
  638.  
  639. ; Render-Post Function
  640. ; render-post: post -> scene
  641. ; Given a post, outputs a scene with the POST placed at the correct x and y locations.
  642. ; Examples:
  643. (check-expect (render-post (make-post 380 60) SCENE)
  644.               (place-image POST-IMAGE 380 60 SCENE))
  645. (check-expect (render-post (make-post 380 90) SCENE)
  646.               (place-image POST-IMAGE 380 90 SCENE))
  647. (check-expect (render-post (make-post 380 120) SCENE)
  648.               (place-image POST-IMAGE 380 120 SCENE))
  649. (check-expect (render-post (make-post 380 150) SCENE)
  650.               (place-image POST-IMAGE 380 150 SCENE))
  651. (define (render-post post scene-so-far)
  652.   (place-image POST-IMAGE (post-x post) (post-y post) scene-so-far))
  653.  
  654.  
  655. ; Ball-Rain-Collision Function
  656. ; ball-rain collision: ball and world -> boolean
  657. ; Given a ball and a world, returns true if the ball hits rain.
  658. ; Returns false otherwise.
  659. (check-expect (ball-rain-collision (make-ball 100 570 24 45 "brown")
  660.                                    (list
  661.                                     (make-rain 400 10 13 "green")
  662.                                     (make-rain 420 20 12 "green")
  663.                                     (make-rain 440 35 11 "green")
  664.                                     (make-rain 460 55 10 "green"))) false)
  665. (check-expect (ball-rain-collision (make-ball 420 30 24 45 "brown")
  666.                                    (list
  667.                                     (make-rain 400 10 13 "green")
  668.                                     (make-rain 420 20 12 "green")
  669.                                     (make-rain 440 35 11 "green")
  670.                                     (make-rain 460 55 10 "green"))) true)
  671. (define (ball-rain-collision ball rain-list)
  672.   (cond [(empty? rain-list) false]
  673.         [(and (= (ball-x ball) (rain-x (first rain-list)))
  674.               (<= (abs (- (ball-y ball) (rain-y (first rain-list))))
  675.                  (+ BALL-RADIUS (/ RAIN-HEIGHT 2)))) true]
  676.         [else (ball-rain-collision ball (rest rain-list))]))
  677.  
  678.  
  679. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  680. ;                                                                                                   ;
  681. ;                                         WORLD FUNCTIONS                                           ;
  682. ;                                                                                                   ;                                                                                                  
  683. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  684.  
  685. ; Launch-Ball? Function
  686. ; launch-ball?: world -> boolean
  687. ; Given a world, returns true if the kicker of the world will launch the ball
  688. ; immediately (same x-position). Returns false if the kicker will not.
  689. ; Examples:
  690. (check-expect (launch-ball? (make-world (make-ball 50 70 10 30 "red")
  691.                                         (make-post 380 60)
  692.                                         (list (make-rain 200 10 13 "green"))
  693.                                         (make-kicker 13 270 10 "black")
  694.                                         (make-scoreboard 0 10))) true)
  695. (check-expect (launch-ball? (make-world (make-ball 60 270 10 30 "red")
  696.                                         (make-post 380 60)
  697.                                         (list
  698.                                           (make-rain 200 10 13 "green")
  699.                                           (make-rain 220 20 12 "green"))
  700.                                         (make-kicker 23 270 10 "black")
  701.                                         (make-scoreboard 0 10))) false)
  702. (check-expect (launch-ball? (make-world (make-ball 73 170 10 30 "red")
  703.                                         (make-post 380 60)
  704.                                         (list
  705.                                           (make-rain 200 10 13 "green")
  706.                                           (make-rain 220 20 12 "green")
  707.                                           (make-rain 240 35 11 "green"))
  708.                                         (make-kicker 73 270 10 "black")
  709.                                         (make-scoreboard 15 10))) true)
  710. (check-expect (launch-ball? (make-world (make-ball 93 270 10 30 "red")
  711.                                         (make-post 380 60)
  712.                                         (list
  713.                                           (make-rain 200 10 13 "green")
  714.                                           (make-rain 220 20 12 "green")
  715.                                           (make-rain 240 35 11 "green")
  716.                                           (make-rain 260 55 10 "green"))
  717.                                         (make-kicker 83 270 10 "black")
  718.                                         (make-scoreboard 15 10))) false)
  719. (define (launch-ball? world)
  720.   (cond [(= (ball-y (world-ball world))
  721.             (kicker-y (world-kicker world))) false]
  722.         [else true]))
  723.  
  724.  
  725. ;Kicker-Disappear Function
  726. ;kicker-disappear: world -> wold
  727. (check-expect (kicker-disappear (make-world (make-ball 60 270 10 30 "red")
  728.                                             (make-post 380 60)
  729.                                             (list
  730.                                              (make-rain 200 10 13 "green")
  731.                                              (make-rain 220 20 12 "green"))
  732.                                             (make-kicker 23 270 10 "black")
  733.                                             (make-scoreboard 0 10)))
  734.               (make-world (make-ball 60 270 10 30 "red")
  735.                           (make-post 380 60)
  736.                           (list
  737.                            (make-rain 200 10 13 "green")
  738.                            (make-rain 220 20 12 "green"))
  739.                           (make-kicker 33 270 10 "black")
  740.                           (make-scoreboard 0 10)))
  741. (check-expect (kicker-disappear (make-world (make-ball 253 70 10 30 "red")
  742.                                             (make-post 380 60)
  743.                                             (list
  744.                                              (make-rain 200 10 13 "green")
  745.                                              (make-rain 220 20 12 "green")
  746.                                              (make-rain 240 35 11 "green"))
  747.                                             (make-kicker 43 270 10 "black")
  748.                                             (make-scoreboard 15 10)))
  749.               (make-world (make-ball (+ (ball-xspeed (make-ball 253 70 10 30 "red")) 253)
  750.                                      (- 70 (ball-yspeed (make-ball 253 70 10 30 "red")))10 30 "red")
  751.                           (make-post 380 60)
  752.                           (list
  753.                            (make-rain 200 10 13 "green")
  754.                            (make-rain 220 20 12 "green")
  755.                            (make-rain 240 35 11 "green"))
  756.                           (make-kicker 900 900 10 "black")
  757.                           (make-scoreboard 15 10)))
  758. (define (kicker-disappear world)
  759.   (cond [(boolean=? (launch-ball? world) true)
  760.          (make-world (update-ball (world-ball world))
  761.                      (world-post world)
  762.                      (world-list-of-rain world)
  763.                      (make-kicker 900 900 10 "black")
  764.                      (world-scoreboard world))]
  765.         [else (make-world (world-ball world)
  766.                           (world-post world)
  767.                           (world-list-of-rain world)
  768.                           (update-kicker (world-kicker world))
  769.                           (world-scoreboard world))]))
  770.  
  771.  
  772. ; Ball-Score? Function
  773. ; ball-score?: world -> boolean
  774. ; Given a world with the ball at its final position, returns true if the ball's
  775. ; x and y positions are inside the post. Returns false if otherwise.
  776. ; Examples:
  777. (check-expect (ball-score? (make-world (make-ball 800 60 10 30 "red")
  778.                                        (make-post 800 60)
  779.                                        (list (make-rain 200 10 13 "green"))
  780.                                        (make-kicker 13 270 10 "black")
  781.                                        (make-scoreboard 0 10))) true)
  782. (check-expect (ball-score? (make-world (make-ball 800 88 13 30 "blue")
  783.                                        (make-post 800 90)
  784.                                        (list(make-rain 200 10 13 "green")
  785.                                              (make-rain 220 20 12 "green"))
  786.                                        (make-kicker 24 270 12 "black")
  787.                                        (make-scoreboard 30 10))) true)
  788. (check-expect (ball-score? (make-world (make-ball 270 150 15 45 "red")
  789.                                        (make-post 380 120)
  790.                                        (list(make-rain 200 10 13 "green")
  791.                                              (make-rain 220 20 12 "green")
  792.                                              (make-rain 240 35 11 "green"))
  793.                                        (make-kicker 24 270 12 "black")
  794.                                        (make-scoreboard 30 10))) false)
  795. (check-expect (ball-score? (make-world (make-ball 400 198 24 45 "brown")
  796.                                        (make-post 800 150)
  797.                                        (list (make-rain 200 10 13 "green")
  798.                                               (make-rain 220 20 12 "green")
  799.                                               (make-rain 240 35 11 "green")
  800.                                               (make-rain 260 55 10 "green"))
  801.                                        (make-kicker 35 270 16 "black")
  802.                                        (make-scoreboard 60 0))) false)
  803. (define (ball-score? world)
  804.   (cond [(and (>= (ball-x (world-ball world)) (- POST-X (/ BOX-WIDTH 2)))      
  805.               (and (>= (ball-y (world-ball world))
  806.                        (- (post-y (world-post world)) (/ BOX-HEIGHT 2)))
  807.                    (<= (ball-y (world-ball world))
  808.                        (+ (post-y (world-post world)) (/ BOX-HEIGHT 2))))) true]
  809.         [else false]))
  810.  
  811.  
  812. ; Ball-Off-Screen? Function
  813. ; ball-off-screen: world -> boolean
  814. ; Given a world, returns true if the ball is off the screen.
  815. (check-expect (ball-off-screen? (make-world (make-ball 100 570 24 45 "brown")
  816.                                             (make-post 760 150)
  817.                                             (list (make-rain 400 10 13 "green")
  818.                                                   (make-rain 420 20 12 "green")
  819.                                                   (make-rain 440 35 11 "green")
  820.                                                   (make-rain 460 55 10 "green"))
  821.                                             (make-kicker 35 570 16 "black")
  822.                                             (make-scoreboard 60 0))) false)
  823. (check-expect (ball-off-screen? (make-world (make-ball -10 570 24 45 "brown")
  824.                                             (make-post 760 150)
  825.                                             (list (make-rain 400 10 13 "green")
  826.                                                   (make-rain 420 20 12 "green")
  827.                                                   (make-rain 440 35 11 "green")
  828.                                                   (make-rain 460 55 10 "green"))
  829.                                             (make-kicker 35 570 16 "black")
  830.                                             (make-scoreboard 60 0))) true)
  831. (check-expect (ball-off-screen? (make-world (make-ball 100 -570 24 45 "brown")
  832.                                             (make-post 760 150)
  833.                                             (list (make-rain 400 10 13 "green")
  834.                                                   (make-rain 420 20 12 "green")
  835.                                                   (make-rain 440 35 11 "green")
  836.                                                   (make-rain 460 55 10 "green"))
  837.                                             (make-kicker 35 570 16 "black")
  838.                                             (make-scoreboard 60 0))) true)
  839. (check-expect (ball-off-screen? (make-world (make-ball 100 670 24 45 "brown")
  840.                                             (make-post 760 150)
  841.                                             (list (make-rain 400 10 13 "green")
  842.                                                   (make-rain 420 20 12 "green")
  843.                                                   (make-rain 440 35 11 "green")
  844.                                                   (make-rain 460 55 10 "green"))
  845.                                             (make-kicker 35 570 16 "black")
  846.                                             (make-scoreboard 60 0))) true)
  847. (define (ball-off-screen? world)
  848.   (cond [(or (< (ball-x (world-ball world)) 0)
  849.              (< (ball-y (world-ball world)) 0)
  850.              (> (ball-y (world-ball world)) HEIGHT)) true]
  851.         [else false]))
  852.  
  853.  
  854. ;Rain-Frequency-Adder Function
  855. ;rain-frequency-adder: world -> list
  856. ;Given a world, adds a rain drop to the existing list-of-rain based on score.
  857. ;Adds a rain drop EVERY 100 pixels FOR SCORE OF 0-100
  858. ;Adds a rain drop EVERY 80 pixels FOR SCORE OF 100-200
  859. ;Adds a rain drop EVERY 60 pixels FOR SCORE OF 200-300
  860. ;Adds a rain drop EVERY 40 pixels  FOR SCORE OF 300-400
  861. ; INSERT CHECK-EXPECTS
  862.  
  863. (define (rain-frequency-adder world)
  864.   (cond [(and (>= (scoreboard-score (world-scoreboard world)) 0)
  865.               (<= (scoreboard-score (world-scoreboard world)) 100))
  866.          (cond [(>= (rain-y (last-of-list (world-list-of-rain world))) 120)
  867.                 (append (rainstorm (world-list-of-rain world))
  868.                         (list (make-rain (rain-x (last-of-list (world-list-of-rain world)))
  869.                                          (+ (- (rain-y (last-of-list (world-list-of-rain world))) 120)
  870.                                             (rain-speed (last-of-list (world-list-of-rain world))))
  871.                                          (rain-speed (last-of-list (world-list-of-rain world)))
  872.                                          (rain-color (last-of-list (world-list-of-rain world))))))]
  873.                [else (world-list-of-rain world)])]
  874.         [(and (>= (scoreboard-score (world-scoreboard world)) 101)
  875.                    (<= (scoreboard-score (world-scoreboard world)) 200))
  876.          (cond [(>= (rain-y (last-of-list (world-list-of-rain world))) 100)
  877.                 (append (rainstorm (world-list-of-rain world))
  878.                         (list (make-rain (rain-x (last-of-list (world-list-of-rain world)))
  879.                                          (+ (- (rain-y (last-of-list (world-list-of-rain world))) 100)
  880.                                             (rain-speed (last-of-list (world-list-of-rain world))))
  881.                                          (rain-speed (last-of-list (world-list-of-rain world)))
  882.                                          (rain-color (last-of-list (world-list-of-rain world))))))]
  883.                [else (world-list-of-rain world)])]
  884.         [(and (>= (scoreboard-score (world-scoreboard world)) 201)
  885.               (<= (scoreboard-score (world-scoreboard world)) 300))
  886.          (cond [(>= (rain-y (last-of-list (world-list-of-rain world))) 80)
  887.                 (append (rainstorm (world-list-of-rain world))
  888.                         (list (make-rain (rain-x (last-of-list (world-list-of-rain world)))
  889.                                          (+ (- (rain-y (last-of-list (world-list-of-rain world))) 80)
  890.                                             (rain-speed (last-of-list (world-list-of-rain world))))
  891.                                          (rain-speed (last-of-list (world-list-of-rain world)))
  892.                                          (rain-color (last-of-list (world-list-of-rain world))))))]
  893.                [else (world-list-of-rain world)])]
  894.         [(and (>= (scoreboard-score (world-scoreboard world)) 301)
  895.               (<= (scoreboard-score (world-scoreboard world)) 405))
  896.          (cond [(>= (rain-y (last-of-list (world-list-of-rain world))) 60)
  897.                 (append (rainstorm (world-list-of-rain world))
  898.                         (list (make-rain (rain-x (last-of-list (world-list-of-rain world)))
  899.                                          (+ (- (rain-y (last-of-list (world-list-of-rain world))) 60)
  900.                                             (rain-speed (last-of-list (world-list-of-rain world))))
  901.                                          (rain-speed (last-of-list (world-list-of-rain world)))
  902.                                          (rain-color (last-of-list (world-list-of-rain world))))))]
  903.                [else (world-list-of-rain world)])]))
  904.  
  905.  
  906. ; Update-World-Main Function
  907. ; update-world-main: world -> world
  908. ; Given a world, outputs the updated world.
  909. ; The ball's x and y positions are updated by its speed, if launch-ball? is true.
  910. ; The ball's x stays constant if launch-ball? is false.
  911. ; The post stays the same.
  912. ; The y positions of the list-of-rains are updated by each drops speed.
  913. ; The kicker's x position is updated by its speed if launch-ball? is false.
  914. ; Otherwise, the kicker disappears.
  915. ; The scoreboard's score is updated by +15 points whether or not the ball has
  916. ; passed between the posts.
  917. ; The scoreboard's lives are updated by -1 if the ball has been hit by acid rain, or if it misses.
  918. ; INSERT CHECK-EXPECTS
  919.  
  920. (define (update-world-main world)
  921.   (make-world (cond [(and (< (ball-x (world-ball world)) WIDTH)
  922.                           (boolean=? (launch-ball? world) true)
  923.                           (boolean=? (ball-off-screen? world) false)
  924.                           (boolean=? (ball-rain-collision (world-ball world)
  925.                                                           (world-list-of-rain world)) false))
  926.                      (update-ball (world-ball world))]
  927.                     [else (world-ball world)])
  928.               (world-post world)
  929.               (cond [(empty? (world-list-of-rain world)) (make-rain RAIN-X 0 RAIN-SPEED "black")]
  930.                     [else (rainstorm (rain-frequency-adder world))])
  931.               (world-kicker world)
  932.               (cond [(and (= (ball-x (world-ball world)) POST-X)                    
  933.                           (boolean=? (ball-score? world) true))
  934.                      (make-scoreboard (+ (scoreboard-score (world-scoreboard world)) 15)
  935.                                       (scoreboard-lives (world-scoreboard world)))]
  936.                     [(and (= (ball-x (world-ball world)) POST-X)
  937.                           (boolean=? (ball-score? world) false))
  938.                      (make-scoreboard (scoreboard-score (world-scoreboard world))
  939.                                       (- (scoreboard-lives (world-scoreboard world)) 1))]
  940.                     [(boolean=? (ball-rain-collision (world-ball world) (world-list-of-rain world))
  941.                                 true)
  942.                      (make-scoreboard (scoreboard-score (world-scoreboard world))
  943.                                       (- (scoreboard-lives (world-scoreboard world)) 1))]
  944.                     [(boolean=? (ball-off-screen? world) true)
  945.                      (make-scoreboard (scoreboard-score (world-scoreboard world))
  946.                                       (- (scoreboard-lives (world-scoreboard world)) 1))]
  947.                     [else (world-scoreboard world)])))
  948.                                  
  949.  
  950. ; Which-Ball-Image Function
  951. ; which-ball-image? : world -> image
  952. ; Given a world, outputs BALL image if the ball is in motion.
  953. ; Outputs BALL-ARROW image if the ball is at the starting position.
  954. ; Examples:
  955. (check-expect (which-ball-image? (make-world (make-ball 375 198 10 30 "red")
  956.                                              (make-post 380 150)
  957.                                              (list (make-rain 200 10 13 "green")
  958.                                                    (make-rain 200 40 13 "green")
  959.                                                    (make-rain 200 70 13 "green")
  960.                                                    (make-rain 220 40 13 "green"))
  961.                                              (make-kicker 200 270 10 "black")
  962.                                              (make-scoreboard 60 10))) BALL-IMAGE)
  963. (check-expect (which-ball-image? (make-world (make-ball 50 570 10 30 "red")
  964.                                              (make-post 380 60)
  965.                                              (list (make-rain 200 10 13 "green"))
  966.                                              (make-kicker 10 570 10 "black")
  967.                                              (make-scoreboard 0 10))) BALL-ARROW-IMAGE)
  968. (define (which-ball-image? world)
  969.   (cond [(and (= (kicker-y (world-kicker world)) KICKER-Y)
  970.               (= (ball-y (world-ball world)) BALL-Y)
  971.               (and (>= (ball-x (world-ball world)) 0)
  972.                    (<= (ball-x (world-ball world)) 75))) BALL-ARROW-IMAGE]
  973.         [else BALL-IMAGE]))
  974.  
  975.  
  976. ; Game-Over? Function
  977. ; game-over? : world -> boolean
  978. ; Given a world, outputs true if there are zero lives remaining.
  979. ; Outputs false otherwise.
  980. (check-expect (game-over? (make-world (make-ball 375 198 10 30 "red")
  981.                                         (make-post 380 150)
  982.                                         (list (make-rain 200 10 13 "green")
  983.                                               (make-rain 200 40 13 "green")
  984.                                               (make-rain 200 70 13 "green")
  985.                                               (make-rain 220 40 13 "green"))
  986.                                         (make-kicker 200 270 10 "black")
  987.                                         (make-scoreboard 60 10))) false)
  988. (check-expect (game-over? (make-world (make-ball 375 198 10 30 "red")
  989.                                         (make-post 380 150)
  990.                                         (list (make-rain 200 10 13 "green")
  991.                                               (make-rain 200 40 13 "green")
  992.                                               (make-rain 200 70 13 "green")
  993.                                               (make-rain 220 40 13 "green"))
  994.                                         (make-kicker 200 270 10 "black")
  995.                                         (make-scoreboard 60 0))) true)
  996. (define (game-over? world)
  997.   (cond [(= (scoreboard-lives (world-scoreboard world)) 0) true]
  998.         [else false]))
  999.  
  1000.  
  1001. ; Render-Scoreboard Function
  1002. ; render-scoreboard: world -> image
  1003. ; Given a world, outputs an image which will represent the scoreboard.
  1004. ; Example:
  1005. (check-expect (render-scoreboard (make-world (make-ball 50 270 24 45 "brown")
  1006.                                              (make-post 380 150)
  1007.                                              (list
  1008.                                               (make-rain 200 10 13 "green")
  1009.                                               (make-rain 200 40 13 "green")
  1010.                                               (make-rain 200 70 13 "green")
  1011.                                               (make-rain 220 40 13 "green"))
  1012.                                              (make-kicker 35 270 16 "black")
  1013.                                              (make-scoreboard 60 0)) SCENE)
  1014.               (place-image (place-image (text (string-append "SCORE:  " "60")
  1015.                                               18 "black") 50 30
  1016.                                         (place-image (text (string-append "LIVES:  " "0")
  1017.                                                            18 "black") 50 80
  1018.                                                      (rectangle 110 110 "solid" "red")))
  1019.                            60 60 SCENE))
  1020. (define (render-scoreboard world scene-so-far)
  1021.   (place-image (place-image (text (string-append "SCORE:  "
  1022.                                   (number->string (scoreboard-score (world-scoreboard world))))
  1023.                                                   18 "black") 50 30
  1024.                                   (place-image (text (string-append "LIVES:  "
  1025.                                                      (number->string
  1026.                                                      (scoreboard-lives (world-scoreboard world))))
  1027.                                                      18 "black") 50 80
  1028.                                                         (rectangle 110 110 "solid" "red"))) 60 60
  1029.                scene-so-far))
  1030.  
  1031.  
  1032. ;Which-Message Function
  1033. ;Which-message: world -> message
  1034. ;Given a world, outputs a message.
  1035. ; INSERT CHECK-EXPECTS
  1036.  
  1037. (define (which-message world)
  1038.   (cond [(boolean=? (ball-off-screen? world) true) "The ball went off the screen."]
  1039.         [(boolean=? (ball-rain-collision (world-ball world) (world-list-of-rain world)) true)
  1040.          "Your ball got hit by acid rain."]
  1041.         [(>= (ball-x (world-ball world)) POST-X)
  1042.               (cond [(boolean=? (ball-score? world) true) "You scored 15 points with that kick."]
  1043.                     [(boolean=? (ball-score? world) false) "Your kick missed the field goal post."])]
  1044.         [else " "]))
  1045.  
  1046.  
  1047. ; Render-Message Function
  1048. ; render-message: world image -> scene
  1049. ; INSERT CHECK-EXPECTS
  1050.  
  1051. (define (render-message world scene-so-far)
  1052.   (place-image (text (which-message world) 18 "black") (/ WIDTH 2) (/ HEIGHT 2) scene-so-far))
  1053.  
  1054. ; Render-World Function
  1055. ; render-world: world -> scene
  1056. ; Given a world, outputs a scene with the objects at their correct x and y positions.
  1057. ; Examples:
  1058. ; INSERT CHECK-EXPECTS
  1059.  
  1060. (check-expect (render-world (make-world (make-ball 90 500 24 45 "brown")
  1061.                                         (make-post 760 150)
  1062.                                         (list
  1063.                                          (make-rain 500 10 13 "green")
  1064.                                          (make-rain 500 40 13 "green")
  1065.                                          (make-rain 500 70 13 "green")
  1066.                                          (make-rain 520 40 13 "green"))
  1067.                                         (make-kicker 35 570 16 "black")
  1068.                                         (make-scoreboard 60 0)))
  1069.               (place-image KICKER-IMAGE 35 570
  1070.                  (place-image BALL-IMAGE 90 500
  1071.                     (place-image POST-IMAGE 760 150
  1072.                        (place-image RAIN 500 10
  1073.                           (place-image RAIN 500 40
  1074.                              (place-image RAIN 500 70
  1075.                                 (place-image RAIN 520 40
  1076.                                    (place-image
  1077.                                       (place-image
  1078.                                          (text (string-append "SCORE:  " "60") 18 "black") 50 30
  1079.                                             (place-image
  1080.                                                (text (string-append "LIVES:  " "0") 18 "black") 50 80
  1081.                                                   (rectangle 110 110 "solid" "red"))) 60 60
  1082.                                                      (place-image (text " " 18 "black") 400 300 SCENE))))))))))
  1083. (check-expect (render-world (make-world (make-ball 50 570 24 45 "brown")
  1084.                                         (make-post 760 150)
  1085.                                         (list
  1086.                                          (make-rain 500 10 13 "green")
  1087.                                          (make-rain 500 40 13 "green")
  1088.                                          (make-rain 500 70 13 "green")
  1089.                                          (make-rain 520 40 13 "green"))
  1090.                                         (make-kicker 10 570 16 "black")
  1091.                                         (make-scoreboard 60 10)))
  1092.               (place-image KICKER-IMAGE 10 570
  1093.                  (place-image (rotate 45 BALL-ARROW-IMAGE) 50 570
  1094.                     (place-image POST-IMAGE 760 150
  1095.                        (place-image RAIN 500 10
  1096.                           (place-image RAIN 500 40
  1097.                              (place-image RAIN 500 70
  1098.                                 (place-image RAIN 520 40
  1099.                                    (place-image
  1100.                                       (place-image
  1101.                                          (text (string-append "SCORE:  " "60") 18 "black") 50 30
  1102.                                             (place-image
  1103.                                                (text (string-append "LIVES:  " "10") 18 "black") 50 80
  1104.                                                   (rectangle 110 110 "solid" "red"))) 60 60
  1105.                                                       (place-image (text " " 18 "black") 400 300 SCENE))))))))))
  1106. (define (render-world world)
  1107.   (cond [(image=? (which-ball-image? world) BALL-ARROW-IMAGE)
  1108.          (render-kicker (world-kicker world)
  1109.                  (render-ball-and-arrow (world-ball world)
  1110.                          (render-post (world-post world)
  1111.                                  (render-rain (world-list-of-rain world)
  1112.                                          (render-scoreboard world
  1113.                                                  (render-message world SCENE))))))]
  1114.         [(image=? (which-ball-image? world) BALL-IMAGE)
  1115.          (render-kicker (world-kicker world)
  1116.                  (render-ball (world-ball world)
  1117.                          (render-post (world-post world)
  1118.                                  (render-rain (world-list-of-rain world)
  1119.                                          (render-scoreboard world
  1120.                                                  (render-message world SCENE))))))]))
  1121.  
  1122.  
  1123. ;Reset Function
  1124. ;reset: world action -> world
  1125. ;This will be called in key-event-handler. Occurs when "enter" pressed.
  1126. ;This function generates a world at the starting positions.
  1127. (define (reset world)
  1128.   (make-world (make-ball (ball-x-start-position 1) BALL-Y BALL-SPEED (random 46) "red")
  1129.               (make-post POST-X (post-random-y 1))
  1130.               (list (make-rain RAIN-X 0 RAIN-SPEED "black"))
  1131.               (make-kicker KICKER-X KICKER-Y KICKER-SPEED "black")
  1132.               (world-scoreboard world)))
  1133. ;Test Cases:
  1134. (reset (make-world (make-ball 800 100 13 31 "red")
  1135.                    (make-post 760 110)
  1136.                    (list (make-rain RAIN-X 0 RAIN-SPEED "black"))
  1137.                    (make-kicker 900 900 13 "black")
  1138.                    (make-scoreboard 60 6)))
  1139. (reset (make-world (make-ball 800 100 13 31 "red")
  1140.                    (make-post 760 110)
  1141.                    (list (make-rain RAIN-X 0 RAIN-SPEED "black"))
  1142.                    (make-kicker 900 900 13 "black")
  1143.                    (make-scoreboard 60 6)))
  1144.  
  1145.  
  1146. ; Key-Event-Handler Function
  1147. ; key-event-handler: world keyevent -> world
  1148. ; Given a world and a key that is pressed, updates the world accordingly.
  1149. (define (key-event-handler world key)
  1150.   (cond [(and (string=? key "up") (= (ball-y (world-ball world)) BALL-Y))
  1151.          (make-world (update-angle (world-ball world) 1 key)
  1152.                      (world-post world)
  1153.                      (world-list-of-rain world)
  1154.                      (world-kicker world)
  1155.                      (world-scoreboard world))]
  1156.         [(and (string=? key "down") (= (ball-y (world-ball world)) BALL-Y))
  1157.          (make-world (update-angle (world-ball world) 1 key)
  1158.                      (world-post world)
  1159.                      (world-list-of-rain world)
  1160.                      (world-kicker world)
  1161.                      (world-scoreboard world))]
  1162.         [(and (string=? key " ")
  1163.               (= (ball-y (world-ball world)) BALL-Y)
  1164.               (= (kicker-y (world-kicker world)) KICKER-Y))
  1165.          (cond [(< (kicker-x (world-kicker world)) (ball-x (world-ball world)))
  1166.                 (make-world (world-ball world)
  1167.                             (world-post world)
  1168.                             (world-list-of-rain world)
  1169.                             (update-kicker (world-kicker world))
  1170.                             (world-scoreboard world))]
  1171.                [else (make-world (update-ball (world-ball world))
  1172.                                  (world-post world)
  1173.                                  (world-list-of-rain world)
  1174.                                  (world-kicker world)
  1175.                                  (world-scoreboard world))])]
  1176.          [(and (string=? key "\r") (>= (ball-x (world-ball world)) 760))
  1177.           (reset world)]
  1178.          [else world]))
  1179. ; Examples
  1180. (check-expect (key-event-handler (make-world (make-ball 50 570 13 30 "red")
  1181.                                         (make-post 760 60)
  1182.                                         (list (make-rain 200 10 13 "green"))
  1183.                                         (make-kicker 10 570 13 "black")
  1184.                                         (make-scoreboard 0 10)) "up")
  1185.               (make-world (make-ball 50 570 13 31 "red")
  1186.                           (make-post 760 60)
  1187.                           (list (make-rain 200 10 13 "green"))
  1188.                           (make-kicker 10 570 13 "black")
  1189.                           (make-scoreboard 0 10)))
  1190. (check-expect (key-event-handler (make-world (make-ball 50 570 13 30 "red")
  1191.                                         (make-post 760 60)
  1192.                                         (list (make-rain 200 10 13 "green"))
  1193.                                         (make-kicker 10 570 13 "black")
  1194.                                         (make-scoreboard 0 10)) "down")
  1195.               (make-world (make-ball 50 570 13 29 "red")
  1196.                           (make-post 760 60)
  1197.                           (list (make-rain 200 10 13 "green"))
  1198.                           (make-kicker 10 570 13 "black")
  1199.                           (make-scoreboard 0 10)))
  1200. (check-expect (key-event-handler (make-world (make-ball 50 570 13 30 "red")
  1201.                                         (make-post 760 60)
  1202.                                         (list (make-rain 200 10 13 "green"))
  1203.                                         (make-kicker 10 570 13 "black")
  1204.                                         (make-scoreboard 0 10)) " ")
  1205.               (make-world (make-ball 50 570 13 30 "red")
  1206.                           (make-post 760 60)
  1207.                           (list (make-rain 200 10 13 "green"))
  1208.                           (make-kicker 23 570 13 "black")
  1209.                           (make-scoreboard 0 10)))
  1210. ;Test Case because angle and a few numbers are random:
  1211. (key-event-handler (make-world (make-ball 800 70 13 30 "red")
  1212.                                (make-post 760 60)
  1213.                                (list (make-rain 500 10 13 "green"))
  1214.                                (make-kicker 10 570 13 "black")
  1215.                                (make-scoreboard 75 10)) "\r")
  1216.  
  1217.  
  1218. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1219. (big-bang (make-world (make-ball (ball-x-start-position 1) BALL-Y BALL-SPEED (random 46) "red")
  1220.                       (make-post POST-X (post-random-y 1))
  1221.                       (list (make-rain RAIN-X 0 RAIN-SPEED "green"))
  1222.                       (make-kicker KICKER-X KICKER-Y KICKER-SPEED "black")
  1223.                       (make-scoreboard 110 10))
  1224.           (on-tick update-world-main)
  1225.           (on-key key-event-handler)
  1226.           (on-draw render-world)
  1227.           (stop-when game-over?))