sh
Th
is ar stu ed d vi y re aC s o ou urc rs e eH w er as o. co m
1 ;; DO NOT PUT ANYTHING PERSONALLY IDENTIFYING BEYOND YOUR CWL IN THIS FILE. 2 ;; YOUR CWLs WILL BE SUFFICIENT TO IDENTIFY YOU AND, IF YOU HAVE ONE, YOUR 3 ;; PARTNER. 4 (require 2htdp/universe) 5 (require 2htdp/image) 6 (require spd/tags) 7 8 (@assignment pset-05);Do not edit or remove this tag 9 (@cwl sumi1008) ;Replace ??? with your cwl, 10 ;; ;second ??? is replaced with partner cwl if you have one 11 12 ;; Bounce any number of balls around the screen. 13 14 15 ;; 16 ;; In this problem set you are given our official solution to problem 17 ;; set 4 (with a few additional things added) as a starting point. 18 ;; We have given you some more constants, a helper function called 19 ;; touch-paddle? which you may use, and a new data defintion called Game. 20 ;; You need to revise the program so that: 21 ;; - the game includes a paddle that moves back and forth across the 22 ;; bottom of the screen 23 ;; - the paddle is controlled by the left and right arrow keys 24 ;; - when a ball hits the paddle it disappears 25 ;; - as before the mouse can be used to add balls to the game 26 ;; 27 ;; As stated above, we have given you a new data definition called Game. 28 ;; You MUST revise the program so that it uses Game as the world state. 29 ;; You MUST NOT change the Game data definition in anyway (though you are 30 ;; allowed to add more Game constants). 31 ;; 32 ;; We suggest you work in three distinct phases, making sure your program 33 ;; works correctly at the end of each phase before going on to the next. 34 ;; - change the program's world state to Game 35 ;; - provide left/right arrow key control over the paddle 36 ;; - make it so that when a ball hits the paddle it disappears 37 ;; 38 ;; In each of these phases you should follow the design recipes! Re-work 39 ;; the domain analysis for changing and constant information, update the 40 ;; data definitions, revise the main function, and so on. Make sure that 41 ;; your tags are correct and that all your tests work correctly before you 42 ;; proceed to the next phase. 43 ;; 44 ;; NOTE: Your on-tick function MUST be designed as a composition of two other 45 ;; functions called game-with-next-balls and game-with-caught-balls. 46 ;; 47 ;; Note that we are giving you significant help in the starter file. 48 ;; You absolutely MUST USE OUR STARTER AS THE BASIS FOR YOUR WORK. 49 ;; 50 ;; We recommend that you begin by printing this file and planning out what 51 ;; needs to change, what needs to be added, and what will be unchanged. 52 ;; 53 (@problem 1) 54 (@htdw ListOfBall) 55 56 ;; Constants: 57 (define WIDTH 605) 58 (define HEIGHT 535) 59 60 (define PADDLE-WIDTH 60) 61 (define PADDLE-THICKNESS 10) 62 (define PADDLE (rectangle PADDLE-WIDTH PADDLE-THICKNESS "solid" "white")) 63 (define PADDLE-CTR-Y (- HEIGHT 40)) 64 (define PADDLE-MOVE-PER-KEY 10) 65 66This(define BALL-RADIUS 10) study source was downloaded by 100000805705997 from CourseHero.com on 10-14-2021 20:26:46 GMT -05:00 67 https://www.coursehero.com/file/81743757/Pset5pdf/
sh
Th
is ar stu ed d vi y re aC s o ou urc rs e eH w er as o. co m
68 (define TOP BALL-RADIUS) 69 (define BOT (- HEIGHT BALL-RADIUS 1)) 70 (define LEF BALL-RADIUS) 71 (define RIG (- WIDTH BALL-RADIUS 1)) 72 73 (define BALL (circle BALL-RADIUS "solid" "white")) 74 75 (define MTS (rectangle WIDTH HEIGHT "solid" "green")) 76 77 78 ;; =========================================================================== 79 ;; =========================================================================== 80 ;; Data definitions: 81 82 (@htdd Ball) 83 (define-struct ball (x y dx dy)) 84 ;; Ball is (make-ball Number Number Number Number) 85 ;; interp. (make-ball x y dx dy) is ball 86 ;; - position x, y in screen coordinates 87 ;; - velocity dx, dy in pixels/tick 88 ;; CONSTRAINT: x is in [LEF, RIG]; y is in [TOP, BOT] 89 (define B1 (make-ball (/ WIDTH 2) (/ HEIGHT 2) 4 -3)) 90 91 (@dd-template-rules compound) 92 93 (define (fn-for-ball b) 94 (... (ball-x b) 95 (ball-y b) 96 (ball-dx b) 97 (ball-dy b))) 98 99 (@htdd ListOfBall) 100 ;; ListOfBall is one of: 101 ;; - empty 102 ;; - (cons Ball ListOfBall) 103 ;; interp. a list of balls 104 (define LOB1 empty) 105 (define LOB2 (cons B1 empty)) 106 107 (@dd-template-rules one-of 108 atomic-distinct 109 compound 110 ref 111 self-ref) 112 113 (define (fn-for-lob lob) 114 (cond [(empty? lob) (...)] 115 [else 116 (... (fn-for-ball (first lob)) 117 (fn-for-lob (rest lob)))])) 118 119 120 (@htdd Game) 121 (define-struct game (balls paddle)) 122 ;; Game is (make-game ListOfBall Number) 123 ;; interp. the current state of a game, with all the balls in play, 124 ;; as well as the x-position of the paddle in screen coordinates 125 (define G0 (make-game empty (/ WIDTH 2))) 126 (define G1 (make-game (cons B1 empty) (/ WIDTH 2))) 127 128 (@dd-template-rules compound ref) 129 130 (define (fn-for-game g) 131 (... (fn-for-lob (game-balls g)) 132 (game-paddle g))) 133This study source was downloaded by 100000805705997 from CourseHero.com on 10-14-2021 20:26:46 GMT -05:00 134 https://www.coursehero.com/file/81743757/Pset5pdf/
sh
Th
is ar stu ed d vi y re aC s o ou urc rs e eH w er as o. co m
135 136 ;; =========================================================================== 137 ;; =========================================================================== 138 ;; Functions: 139 140 (@htdf main) 141 (@signature ListOfBall -> ListOfBall) 142 ;; start the game, call with (main LOB1) 143 ;; <no tests for main functions> 144 145 (@template htdw-main) 146 147 (define (main lob) 148 (big-bang lob 149 (on-draw render-balls) ;ListOfBall -> Image 150 (on-tick next-balls) ;ListOfBall -> ListOfBall 151 (on-key handle-key) ;ListOfBall KeyEvent -> ListOfBall 152 (on-mouse handle-mouse))) ;ListOfBall Integer Integer MouseEvent 153 ; -> ListOfBall 154 155 (@htdf render-balls) 156 (@signature ListOfBall -> Image) 157 ;; render all balls onto MTS 158 (check-expect (render-balls empty) MTS) 159 (check-expect (render-balls (cons (make-ball 10 20 3 4) 160 (cons (make-ball 30 40 1 2) 161 empty))) 162 (place-ball (make-ball 10 20 3 4) 163 (place-ball (make-ball 30 40 1 2) 164 MTS))) 165 166 ;(define (render-balls lob) MTS) ;stub 167 168 (@template ListOfBall) 169 170 (define (render-balls lob) 171 (cond [(empty? lob) MTS] 172 [else 173 (place-ball (first lob) 174 (render-balls (rest lob)))])) 175 176 177 (@htdf place-ball) 178 (@signature Ball Image -> Image) 179 ;; place BALL on image at appropriate x, y coordinate 180 (check-expect (place-ball (make-ball 20 30 3 3) MTS) 181 (place-image BALL 20 30 MTS)) 182 (check-expect (place-ball (make-ball 10 20 -2 -1) empty-image) 183 (place-image BALL 10 20 empty-image)) 184 #; 185 (define (place-ball b img) img) 186 187 (@template Ball) 188 189 (define (place-ball b img) 190 (place-image BALL (ball-x b) (ball-y b) img)) 191 192 193 (@htdf next-balls) 194 (@signature ListOfBall -> ListOfBall) 195 ;; produce list of balls at their next x, y coordinates 196 (check-expect (next-balls empty) empty) 197 (check-expect (next-balls (cons (make-ball (+ LEF 1) TOP 3 -4) 198 (cons (make-ball 200 100 3 4) 199 empty))) 200This study source was downloaded (cons (next-ball (make-ball LEF 1) TOPGMT 3 -05:00 -4)) by 100000805705997 from CourseHero.com on(+ 10-14-2021 20:26:46 201 (cons (next-ball (make-ball 200 100 3 4)) https://www.coursehero.com/file/81743757/Pset5pdf/
sh
Th
is ar stu ed d vi y re aC s o ou urc rs e eH w er as o. co m
202 empty))) 203 (@template ListOfBall) 204 205 #; 206 (define (next-balls lob) empty) 207 208 (define (next-balls lob) 209 (cond [(empty? lob) empty] 210 [else 211 (cons (next-ball (first lob)) 212 (next-balls (rest lob)))])) 213 214 215 (@htdf next-ball) 216 (@signature Ball -> Ball) 217 ;; produce ball at next x,y; checks bounces off top/right/bottom/left wall 218 (check-expect (next-ball (make-ball (+ LEF 1) TOP 3 -4)) 219 (bounce-top (make-ball (+ LEF 1) TOP 3 -4))) 220 (check-expect (next-ball (make-ball (+ LEF 1) BOT 3 4)) 221 (bounce-bottom (make-ball (+ LEF 1) BOT 3 4))) 222 (check-expect (next-ball (make-ball LEF (+ TOP 1) -3 4)) 223 (bounce-left (make-ball LEF (+ TOP 1) -3 4))) 224 (check-expect (next-ball (make-ball RIG (+ TOP 1) 3 4)) 225 (bounce-right (make-ball RIG (+ TOP 1) 3 4))) 226 (check-expect (next-ball (make-ball (/ WIDTH 2) (/ HEIGHT 2) 3 4)) 227 (glide (make-ball (/ WIDTH 2) (/ HEIGHT 2) 3 4))) 228 #; 229 (define (next-ball b) b) 230 231 (@template Number) ;(@template Number) because b is treated as atomic 232 233 (define (next-ball b) 234 (cond [(touch-top? b) (bounce-top b)] 235 [(touch-bottom? b) (bounce-bottom b)] 236 [(touch-right? b) (bounce-right b)] 237 [(touch-left? b) (bounce-left b)] 238 [else 239 (glide b)])) 240 241 242 (@htdf handle-mouse) 243 (@signature ListOfBall Integer Integer MouseEvent -> ListOfBall) 244 ;; adds new ball at x, y to lob 245 ;; NOTE: uses random, so testing has to use check-random 246 (check-random (handle-mouse empty 100 200 "button-down") 247 (cons (make-ball 100 200 (- 5 (random 11)) (- 5 (random 11))) 248 empty)) 249 (check-random (handle-mouse (cons (make-ball 10 20 -3 3) empty) 250 300 100 "button-down") 251 (cons (make-ball 300 100 (- 5 (random 11)) (- 5 (random 11))) 252 (cons (make-ball 10 20 -3 3) empty))) 253 (check-random (handle-mouse empty 100 200 "button-up") empty) 254 (check-random (handle-mouse (cons (make-ball 10 20 -3 3) empty) 255 100 200 "button-up") 256 (cons (make-ball 10 20 -3 3) empty)) 257 #; 258 (define (handle-mouse lob x y me) empty) 259 260 (@template MouseEvent) 261 262 (define (handle-mouse lob x y me) 263 (cond [(mouse=? me "button-down") 264 (cons (make-ball x y (- 5 (random 11)) (- 5 (random 11))) lob)] 265 [else lob])) 266 267This study source was downloaded by 100000805705997 from CourseHero.com on 10-14-2021 20:26:46 GMT -05:00 268 (@htdf handle-key) https://www.coursehero.com/file/81743757/Pset5pdf/
sh
Th
is ar stu ed d vi y re aC s o ou urc rs e eH w er as o. co m
269 (@signature ListOfBall KeyEvent -> ListOfBall) 270 ;; clear all balls if space key pressed; else do nothing 271 (check-expect (handle-key (cons (make-ball (/ WIDTH 2) (/ HEIGHT 2) 2 4) 272 (cons (make-ball (+ TOP 2) (+ LEF 5) 3 2) 273 empty)) 274 " ") 275 empty) 276 (check-expect (handle-key (cons (make-ball (/ WIDTH 3) (/ HEIGHT 4) 1 -3) 277 (cons (make-ball (+ TOP 5) (+ LEF 2) 3 -2) 278 empty)) 279 "c") 280 (cons (make-ball (/ WIDTH 3) (/ HEIGHT 4) 1 -3) 281 (cons (make-ball (+ TOP 5) (+ LEF 2) 3 -2) 282 empty))) 283 #; 284 (define (handle-key lob ke) empty) 285 286 (@template KeyEvent) 287 288 (define (handle-key lob ke) 289 (cond [(key=? ke " ") empty] 290 [else lob])) 291 292 293 (@htdf touch-paddle?) 294 (@signature Ball Number -> Boolean) 295 ;; produce true if ball's center is inside the paddle 296 ;; NOTE: There are many better and more complex ways to design this function. 297 ;; This design is fairly primitive (just checks that the center of the 298 ;; ball is in the paddle), but people playing the game shouldn't see 299 ;; much difference if the balls are moving quickly. 300 (check-expect (touch-paddle? (make-ball (- 100 (/ PADDLE-WIDTH 2) 1) 301 PADDLE-CTR-Y 302 1 2) 303 100) 304 false) 305 (check-expect (touch-paddle? (make-ball (- 100 (/ PADDLE-WIDTH 2)) 306 PADDLE-CTR-Y 307 1 2) 308 100) 309 true) 310 (check-expect (touch-paddle? (make-ball (+ 100 (/ PADDLE-WIDTH 2)) 311 PADDLE-CTR-Y 312 1 2) 313 100) 314 true) 315 (check-expect (touch-paddle? (make-ball (+ 100 (/ PADDLE-WIDTH 2) 1) 316 PADDLE-CTR-Y 317 1 2) 318 100) 319 false) 320 (check-expect (touch-paddle? 321 (make-ball (+ 100 (/ PADDLE-WIDTH 2)) 322 (- PADDLE-CTR-Y (/ PADDLE-THICKNESS 2) 1) 323 1 2) 324 100) 325 false) 326 (check-expect (touch-paddle? 327 (make-ball (+ 100 (/ PADDLE-WIDTH 2)) 328 (- PADDLE-CTR-Y (/ PADDLE-THICKNESS 2)) 329 1 2) 330 100) 331 true) 332 (check-expect (touch-paddle? 333 (make-ball (+ 100 (/ PADDLE-WIDTH 2)) 334This study source was downloaded by 100000805705997 (+ from PADDLE-CTR-Y PADDLE-THICKNESS CourseHero.com on(/ 10-14-2021 20:26:46 GMT -05:00 2)) 335 1 2) https://www.coursehero.com/file/81743757/Pset5pdf/