100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
CPSC110 problem set 5 solution CA$11.86   Add to cart

Other

CPSC110 problem set 5 solution

 4 views  0 purchase

completed solution for problem set 5

Preview 3 out of 17  pages

  • January 5, 2024
  • 17
  • 2023/2024
  • Other
  • Unknown
All documents for this subject (19)
avatar-seller
skye3
;; 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))))

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller skye3. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for CA$11.86. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

79976 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
CA$11.86
  • (0)
  Add to cart