for something like that you’ll want to use physics. Please note a lot of the practices you may or may not observe in my code are not advised. For example i should have used a table to keep track of the circles. And the way I’ve littered the code with comments is not very… pretty
supportedOrientations(ANY)
function setup()
displayMode(FULLSCREEN)
-- create the edge lines
line1 = physics.body(EDGE,vec2(5,5),vec2(5,HEIGHT-5))
-- creates the first line which starts from the bottom vec2(5,5)
-- and ends in the top left corner vec2(5,HEIGHT-5)
line2 = physics.body(EDGE,vec2(WIDTH-5,5),vec2(WIDTH-5,HEIGHT-5))
-- the reason we begin the lines 5 pixels above ground is so that
-- can see it better. Remmber vec2 is just a shorter way two secify
-- two points.
line3 = physics.body(EDGE,vec2(5,5),vec2(WIDTH-5,5))
-- bear in mind that these dont actually "DRAW" the lines
-- but rather puts invisible barriers.
line4 = physics.body(EDGE,vec2(5,HEIGHT-5),vec2(WIDTH-5,HEIGHT-5))
-- we will draw the lines later
circle1 = physics.body(CIRCLE,50) -- this creates the actual circle but DOESNT draw it
circle1.x = math.random(30,WIDTH-30) -- this gives a random position between
-- 30 and the screen width minus 30
circle1.y = math.random(30,HEIGHT-30) -- same as before but for the y position
circle1.gravityScale = 0 -- gravity doesnt affect this cicle
circle1.restitution = 1 -- this circle is bouncy
circle1.friction = 0 -- the amount of friction to be applied to the circle
circle1.linearVelocity = vec2(math.random(400),math.random(400)) -- the velocity of the circle
-- creating the second circle
circle2 = physics.body(CIRCLE,50)
circle2.x = math.random(30,WIDTH-30)
circle2.y = math.random(30,HEIGHT-30)
circle2.gravityScale = 0
circle2.restitution = 1
circle2.friction = 0
circle2.linearVelocity = vec2(math.random(400),math.random(400))
end
-- the function to be called wehn two physics objects collide
function collide(contact)
if contact.state == BEGAN then
sound(SOUND_HIT,50) -- createca sound for collision
end
end
function draw()
background(50, 50, 50)
stroke(0,0,255)
strokeWidth(4)
noFill()
-- actually draw the circles
ellipse(circle1.x,circle1.y,100,100)
ellipse(circle2.x,circle2.y,100,100)
stroke(255)
strokeWidth(5)
-- actually draw the edge lines
line(5,5,5,HEIGHT-5)
line(WIDTH-5,5,WIDTH-5,HEIGHT-5)
line(5,5,WIDTH-5,5)
line(5,HEIGHT-5,WIDTH-5,HEIGHT-5)
end