Code Sketch
Bouncing Ball Without Variables (val or var)
Category: Programming
cleari()
drawStage(ColorMaker.white)
def pic = Picture.circle(25)
.thatsFilledWith(cm.radialGradient(10, 10, white, 25, red, false))
.thatsStrokeColored(noColor)
var velocity = Vector2D(8, 8)
case class Bouncing(velocity:Vector2D, shape:Picture){
def handle:Bouncing = {
def movedShape =
shape
.thatsTranslated(velocity.x, velocity.y)
return handleBounce(movedShape)
}
def handleBounce(shape:Picture):Bouncing = {
shape.draw()
def shapeCollideWithBorder = shape.collidesWith(stageBorder)
return if (shapeCollideWithBorder){
Bouncing(
bouncePicVectorOffStage(shape, velocity),
shape.copy
)
} else {
Bouncing(
velocity,
shape.copy
)
}
}
}
animateWithState(Bouncing(velocity, pic)) { s =>
erasePictures()
s.handle
}