Question: @problem 1 ) ( @htdw Ball ) ;; Constants: ( define WIDTH 6 0 5 ) ( define HEIGHT 5 3 5 ) ( define
@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
;;
define renderball 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 ;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"
makeball x y random random
else b
@htdf touch
top?
@signature Ball Boolean
;; true if ball is going up and edge will hit or pass top edge of box
checkexpect touchtop? makeball LEF TOP false
checkexpect touchtop? makeball LEF TOP true
checkexpect touchtop? makeball LEF TOP true
checkexpect touchtop? makeball LEF TOP false
#;
define touchtop? b false
@templateorigin Ball
@template
define touchtop? b
ballx b
bally b
balldx b
balldy b
define touchtop? b
bally bballdy b TOP
@htdf touchbottom?
@signature Ball Boolean
;;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
