Escrito por estudiantes que aprobaron Inmediatamente disponible después del pago Leer en línea o como PDF ¿Documento equivocado? Cámbialo gratis 4,6 TrustPilot
logo-home
Otro

CPSC110 problem set 5 solution

Puntuación
-
Vendido
1
Páginas
17
Subido en
05-01-2024
Escrito en
2023/2024

completed solution for problem set 5

Institución
Grado

Vista previa del contenido

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

Escuela, estudio y materia

Institución
Estudio
Grado

Información del documento

Subido en
5 de enero de 2024
Número de páginas
17
Escrito en
2023/2024
Tipo
OTRO
Personaje
Desconocido

Temas

$8.98
Accede al documento completo:

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Conoce al vendedor
Seller avatar
skye3

Conoce al vendedor

Seller avatar
skye3 UBC
Seguir Necesitas iniciar sesión para seguir a otros usuarios o asignaturas
Vendido
8
Miembro desde
2 año
Número de seguidores
1
Documentos
9
Última venta
2 semanas hace

0.0

0 reseñas

5
0
4
0
3
0
2
0
1
0

Documentos populares

Recientemente visto por ti

Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes