Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Bouncing Balls Animation
- -- by @beezing
- abs = math.abs
- sin = math.sin
- cos = math.cos
- sqrt = math.sqrt
- random = math.random
- function rectangle(x, y, width, height)
- move(x, y)
- draw(x+width, y)
- draw(x+width, y+height)
- draw(x, y+height)
- draw(x, y)
- end
- function circle(x, y, radius)
- move(x+radius, y)
- for i = 0, 3.14*2 + 0.1, 0.1 do
- draw(x + radius*cos(i), y + radius*sin(i))
- end
- end
- function moveBall(ball, wall)
- -- calculate movement borders
- minX = wall.x + ball.sz + 2
- maxX = wall.width - ball.sz + 2
- minY = wall.y + ball.sz + 2
- maxY = wall.height - ball.sz + 2
- -- check ball for borders
- if ball.x <= minX then newX = minX
- elseif ball.x >= maxX then newX = maxX
- else newX = ball.x end
- if ball.y <= minY then newY = minY
- elseif ball.y >= maxY then newY = maxY
- else newY = ball.y end
- -- check ball for collision
- hitX = (ball.x <= minX) or (ball.x >= maxX)
- hitY = (ball.y <= minY) or (ball.y >= maxY)
- return hitX, hitY, newX, newY
- end
- -- define wall
- wall = {}
- wall.x = 4
- wall.y = 24
- wall.width = 312 --312 --760 --312
- wall.height = 740 --388 --760 --740
- -- define animation constants
- ballCount = 5
- minSize = 10
- maxSize = 50
- maxSpeed = 50
- slowRate = 0.1
- infinite = false
- -- create balls
- balls = {}
- for i = 1, ballCount do
- -- randomly create ball
- ball = {}
- ball.sz = random(minSize, maxSize)
- ball.x = random(wall.width -2*ball.sz) + ball.sz
- ball.y = random(wall.height-2*ball.sz) + ball.sz
- ball.dx = random(-maxSpeed, maxSpeed)
- ball.dy = random(-maxSpeed, maxSpeed)
- ball.r = random(192)
- ball.g = random(192)
- ball.b = random(192)
- balls[i] = ball
- end
- -- animate bouncing balls
- stopAll = 1
- while stopAll > 0 do
- disableScreenRefresh()
- -- draw wall
- clear()
- setcolor(0,0,0)
- rectangle(wall.x, wall.y, wall.width, wall.height)
- -- animate balls
- for i = 1, #balls do
- -- draw ball at its current position
- setcolor(balls[i].r, balls[i].g, balls[i].b)
- circle(balls[i].x, balls[i].y, balls[i].sz)
- -- move ball to its next position based on its speed
- balls[i].x = balls[i].x + balls[i].dx
- balls[i].y = balls[i].y + balls[i].dy
- -- check ball's movement for collision with the wall
- hX, hY, balls[i].x, balls[i].y = moveBall(balls[i], wall)
- -- bounce ball if it hits the wall
- if hX then balls[i].dx = -balls[i].dx end
- if hY then balls[i].dy = -balls[i].dy end
- end
- if not infinite then
- -- slow down animation
- stopAll = 0
- for i = 1, #balls do
- -- calculate deacceleration
- a = balls[i].dy * balls[i].dy
- b = balls[i].dx * balls[i].dx
- c = sqrt(a+b)
- c_ = c-slowRate
- b_ = abs(balls[i].dx) - abs((balls[i].dx * c_)/c)
- a_ = abs(balls[i].dy) - abs((balls[i].dy * c_)/c)
- -- deaccelerate ball speed
- if balls[i].dx < 0 then
- balls[i].dx = balls[i].dx + b_
- elseif balls[i].dx > 0 then
- balls[i].dx = balls[i].dx - b_ end
- if balls[i].dy < 0 then
- balls[i].dy = balls[i].dy + a_
- elseif balls[i].dy > 0 then
- balls[i].dy = balls[i].dy - a_ end
- -- check for remaining movement based on velocity
- if abs(c_) >= slowRate then
- stopAll = stopAll + abs(c_)
- end
- end
- -- stop if sum of velocity is less then slowing rate
- if stopAll < slowRate then stopAll = 0 end
- end
- enableScreenRefresh()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement