;; 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))))
(@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))))