Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Below is our solution to Problem Set 3 . ;; For problem set 4 you must improve this program in important ways. Be sure ;;
Below is our solution to Problem Set
;; For problem set you must improve this program in important ways. Be sure
;; not to change the program's behaviour in any other ways than we are asking.
;; Making extra improvements could cause you problems with the autograder.
;;
;; Modify the program so that instead of there being a single ball there
;; can be any number of balls. The mouse button should add a new ball.
;; Pressing space should clear all the balls.
;;
;; Again your change should be systematic and complete. All relevant
;; design elements and tags must be updated. You must name the new
;; world state type ListOfBall.
;;
;; NOTE, if you work systematically, starting from the HtDW recipe, you
;; will decide that you need a new ListOfBall type; but that the Ball
;; type does not change. Therefore you should NOT change your nextball
;; function. Your renderball function will also be useful when you
;; work on your placeball function, although renderball will have to
;; change some.
;;
@problem
@htdw Ball
;; Constants:
define WIDTH
define HEIGHT
define BALLRADIUS
define TOP BALLRADIUS;these constants define the "inner box"
define BOT HEIGHT BALLRADIUS;that constrains the center of the ball
define LEF BALLRADIUS;
define RIG WIDTH BALLRADIUS;
define BALL circle BALLRADIUS "solid" "white"
define MTS rectangle WIDTH HEIGHT "solid" "green"
;;
;;
;; Data definitions:
@htdd Ball
definestruct ball x y dx dy
;; Ball is makeball Number Number Number Number
;; interp. makeball x y dx dy is ball
;; position x y in screen coordinates
;; velocity dx dy in pixelstick
define Bmakeball WIDTH HEIGHT
@ddtemplaterules compound
define fnforball b
ballx b
bally b
balldx b
balldy b
;;
;;
;; Functions:
@htdf main
@signature Ball Ball
;; start the game, call with main B
;;
@templateorigin htdwmain
define main b
bigbang b
ondraw renderball ;Ball Image
ontick nextball ;Ball Ball
onmouse handlemouse;Ball Integer Integer MouseEvent Ball
@htdf renderball
@signature Ball Image
;; place BALL on image at appropriate x y coordinate
checkexpect renderball makeball
placeimage BALL MTS
checkexpect renderball makeball WIDTH HEIGHT
placeimage BALL WIDTH HEIGHT MTS
#;
define renderball b MTS
@templateorigin Ball
@template
define renderball b
ballx b
bally b
balldx b
balldy b
define renderball b
placeimage BALL ballx bbally b MTS
@htdf nextball
@signature Ball Ball
;; produce ball at next xy; checks bounces off toprightbottomleft wall
checkexpect nextball makeball LEF TOP
bouncetop makeball LEF TOP
checkexpect nextball makeball LEF BOT
bouncebottom makeball LEF BOT
checkexpect nextball makeball LEF TOP
bounceleft makeball LEF TOP
checkexpect nextball makeball RIG TOP
bounceright makeball RIG TOP
checkexpect nextball makeball WIDTH HEIGHT
glide makeball WIDTH HEIGHT
#;
define nextball b b
@templateorigin Number ; because b is treated as atomic
@template
define nextball b
b
define nextball b
cond touchtop? bbouncetop b
touchbottom? bbouncebottom b
touchright? bbounceright b
touchleft? bbounceleft b
else
glide b
@htdf handlemouse
@signature Ball Integer Integer MouseEvent Ball
;; replace ball with new ball on mouse click
;; NOTE: uses random, so testing has to use checkrandom
checkrandom handlemouse makeball "buttondown"
makeball random random
checkrandom handlemouse makeball "buttonup
makeball
#;
define handlemouse b x y me b
@templateorigin MouseEvent
@template
define handlemouse b x y me
cond mouse me "buttondown" b x y
else
b x y
define handlemouse b x y me
cond mouse me "buttondown"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started