100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Other

CPSC110 problem set 5 solution

Rating
-
Sold
-
Pages
17
Uploaded on
05-01-2024
Written in
2023/2024

completed solution for problem set 5











Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Uploaded on
January 5, 2024
Number of pages
17
Written in
2023/2024
Type
Other
Person
Unknown

Content preview

;; Bounce any number of balls around the screen.

(@problem 1)
(@htdw ListOfBall)

;; Constants:
(define WIDTH 605)
(define HEIGHT 535)

(define PADDLE-WIDTH 60)
(define PADDLE-THICKNESS 10)
(define PADDLE (rectangle PADDLE-WIDTH PADDLE-THICKNESS "solid" "white"))
(define PADDLE-CTR-Y (- HEIGHT 40))
(define PADDLE-MOVE-PER-KEY 10)

(define BALL-RADIUS 10)

(define TOP BALL-RADIUS)
(define BOT (- HEIGHT 1 BALL-RADIUS))
(define LEF BALL-RADIUS)
(define RIG (- WIDTH 1 BALL-RADIUS))

(define BALL (circle BALL-RADIUS "solid" "white"))

(define MTS (rectangle WIDTH HEIGHT "solid" "green"))


;; ===========================================================================
;; ===========================================================================
;; Data defini\ons:

(@htdd Ball)
(define-struct ball (x y dx dy))
;; Ball is (make-ball Number Number Number Number)
;; interp. (make-ball x y dx dy) is ball
;; - posi\on x, y in screen coordinates
;; - velocity dx, dy in pixels/\ck
;; CONSTRAINT: x is in [LEF, RIG]; y is in [TOP, BOT]
(define B1 (make-ball (/ WIDTH 2) (/ HEIGHT 2) 4 -3))

(@dd-template-rules compound)

(define (fn-for-ball b)
(... (ball-x b)

, (ball-y b)
(ball-dx b)
(ball-dy b)))

(@htdd ListOfBall)
;; ListOfBall is one of:
;; - empty
;; - (cons Ball ListOfBall)
;; interp. a list of balls
(define LOB1 empty)
(define LOB2 (cons B1 empty))

(@dd-template-rules one-of
atomic-dis\nct
compound
ref
self-ref)

(define (fn-for-lob lob)
(cond [(empty? lob) (...)]
[else
(... (fn-for-ball (first lob))
(fn-for-lob (rest lob)))]))


(@htdd Game)
(define-struct game (balls paddle))
;; Game is (make-game ListOfBall Number)
;; interp. the current state of a game, with all the balls in play,
;; as well as the x-posi\on of the paddle in screen coordinates
(define G0 (make-game empty (/ WIDTH 2)))
(define G1 (make-game (cons B1 empty) (/ WIDTH 2)))

(@dd-template-rules compound ref)

(define (fn-for-game g)
(... (fn-for-lob (game-balls g))
(game-paddle g)))



;; ===========================================================================
;; ===========================================================================
;; Func\ons:

, (@htdf main)
(@signature Game -> Game)
;; start the game, call with (main G0)
;; <no tests for main func\ons>

(@template-origin htdw-main)

(define (main g)
(big-bang g
(on-draw render-game) ;Game -> Image
(on-\ck next-game) ;Game -> Game
(on-mouse handle-mouse) ;Game Integer Integer MouseEvent
(on-key handle-key) )) ;Game KeyEvent -> Game


(@htdf render-game)
(@signature Game -> Image)
;; render the game onto MTS
(check-expect (render-game (make-game empty 100))
(place-image PADDLE 100 PADDLE-CTR-Y MTS))
(check-expect (render-game (make-game (cons (make-ball 3 4 3 1)
(cons (make-ball 1 3 2 3)
empty))
50))
(place-image PADDLE 50 PADDLE-CTR-Y
(render-balls (cons (make-ball 3 4 3 1)
(cons (make-ball 1 3 2 3)
empty)))))

(@template-origin Game)
(@template
(define (render-game g)
(... (fn-for-lob (game-balls g))
(game-paddle g))))

(define (render-game g)
(place-image PADDLE
(game-paddle g)
PADDLE-CTR-Y
(render-balls (game-balls g))))
CA$12.04
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
skye3

Get to know the seller

Seller avatar
skye3 UBC
View profile
Follow You need to be logged in order to follow users or courses
Sold
5
Member since
1 year
Number of followers
1
Documents
9
Last sold
1 year ago

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions