Question: ( define BALLS ' ( ) ) ; inital empty list of balls ;To make multiple balls move, use map the next - ball function
define BALLS ; inital empty list of balls
;To make multiple balls move, use map the nextball function over all balls
;then to get their next positions accordingly.
define ListOfBall balls ; how this balls relate to b below???
cond
null balls
else cons ListOfBall balls
;create a firstball to start the problem
define firstball ListOfBall makeball
;Check if ball near edges
define touchtop? b
bally bballdy b TOP
define touchbot? b
bally bballdy b BOT
define touchrig? b
ballx bballdx b RIG
define touchlef? b
ballx bballdx b LEF
;Bounce the ball of the edges
define bouncetop bmakeball ballx b TOP balldx bballdy b
define bouncebot bmakeball ballx b BOT balldx bballdy b
define bouncelef bmakeball LEF bally bballdx bballdy b
define bouncerig bmakeball RIG bally bballdx bballdy b
;Move the ball
define glide bmakeball ballx bballdx bbally bballdy bballdx bballdy b
;Handle mouse click to creat a new ball
define handlemouse b x y me ;how does this b relate to balls below?
cond equal me "buttondown"
makeball x y random random
else b
; handlemouse adds a new ball on mouse click
define handlemouse balls x y event
cond mouse event "buttondown"
cons makeball x y random random
balls
else balls
;To handle key event, when the space bar is pressed, it will make the ListOfBall empty
define handlekey lob key ; how does this lob relate to balls below?
cond key key
else lob
; handlekey clears all balls on space press
define handlekey balls key
cond key key
else balls
;Render the ball on scene with green background
define renderball b ; how does this b relate to lob below?
placeimage BALL ballx bbally bemptyscene WIDTH HEIGHT "green"
;renderballs to render image of multiple balls, use map the renderball function over all balls
;then place them on the background image.
define renderballs balls
foldr lambda ball acc ;what is difference ball vs balls??
placeimage BALL ballx ballbally ball acc ;How does acc do in this foldrprocedure??
MTS
lob
; nextballs produces the next list of balls, moving each one
define nextballs balls ; how does this balls relate to b below?
map nextball balls ; what does procedure of map doing??
;Determine the next state of the ball using current position
define nextball b
cond
touchtop? bbouncetop b
touchbot? bbouncebot b
touchlef? bbouncelef b
touchrig? bbouncerig b
else glide b
;Main program start here use B to initialize
bigbang B ; how to convert B to first ball below??
ontick nextball
ondraw renderball
onkey handlekey
onmouse handlemouse
; main remains the same but now uses BALLS as the initial world state
define main firstball ;use firstball to start the program.
bigbang firstball
ondraw renderballs ;ListOfBall Image
ontick nextballs ;ListOfBall ListOfBall
onmouse handlemouse ;ListOfBall Integer Integer MouseEvent ListOfBall
onkey handlekey ;ListOfBall KeyEvent ListOfBall ;questions:
; how does lob relate to balls?
; what are difference of lob vs ball vs balls?
; How does acc do in this foldrprocedure??
; what does map in procedure nextballs doing??
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
