#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Bouncy balls

Posts: 167

This game uses physics to get the balls to me and bounce.

``````--# Main
--Bounce

--The awesome setup function!
function setup()
displayMode(FULLSCREEN)

--Boss physics!
gr = physics.body(EDGE, vec2(0,10), vec2(1000, 10))
gr.type = STATIC
gr.y = 10

wall = physics.body(EDGE, vec2(0, 10), vec2(10, 800))
wall.type = STATIC
wall.y = 10

w = physics.body(EDGE, vec2(1000, 10), vec2(1000, 900))
w.type = STATIC
w.y = 10

roof = physics.body(EDGE, vec2(0, 10), vec2(1000, 10))
roof.type = STATIC
roof.y = 750

ball = physics.body(CIRCLE, 10)
ball.restitution = 1
ball.friction = 0.5
ball.y = math.random(600, 730)
ball.x = math.random(100, 900)

b = physics.body(CIRCLE, 10)
b.restitution = 1
b.friction = 0.5
b.x = WIDTH/2
b.y = HEIGHT/2

ba = physics.body(CIRCLE, 10)
ba.restitution = 1
ba.friction = 0.5
ba.x = 500
ba.y = 300

bal = physics.body(CIRCLE, 10)
bal.restitution = 1
bal.friction = 0.5
bal.x = math.random(100, 900)
bal.y = math.random(300, 600)
end

function draw()

background(0, 0, 255, 255)
--Walls
fill(127, 127, 127, 255)
rect(0,0, 1000, gr.y)
rect(0, wall.y, 10, 800)
rect(1000, w.y, 10, 800)
rect(0, roof.y, 1000, 10)

--DA BALLZ!
fill(255, 255, 255, 255)
ellipse(ball.x, ball.y, 40)
fill(255, 0, 0, 255)
ellipse(b.x, b.y, 40)
fill(0, 255, 150, 255)
ellipse(ba.x, ba.y, 40)
fill(255, 255, 0, 255)
ellipse(bal.x, bal.y, 40)

end

function touched(t)
b.x = t.x
b.y = t.y
if t.tapCount == 3 then
ball.y = 600
end
end

function collide(c)
sound(SOUND_JUMP, math.random(1,2000))
end
``````
Tagged:

## Comments

• Posts: 167

Simple thi g I coded in like 10mins to half an hour.

• Mod
Posts: 2,020

Instead of 4 variables `b`, `ba`, `bal`,`ball`, why not hold the bodies in a single `ball` array? Then you could have 400 balls without having to think of 400 variable names. You could also update them all with a `for` loop.

• Posts: 167

How could I make a ball array? And where would I put the for loop?

• Mod
Posts: 2,020

Declare an empty array with `ball={}` in your `setup` function.

I would use two `for` loops, one in `setup`, one in `draw`. I think you should try to work it out yourself, but here is a little hint to get you started...

``````local numberOfBalls = 100
for i=1,numberOfBalls do
ball[i]=physics.body ....
``````

Then, in the `for` loop in draw, you can write

`ellipse(ball[i].x .....` etc

• Posts: 216

If youre doing that and end up having lots of balls, spriting a circle really speeds up the process (rather the adjusting a mesh to a circle, it simply draws a rectangle with a circles texture which is much faster). To do so, simply make an image, call setcontext and draw an ellipse to it then sprite the circle where you draw your balls.

• Posts: 167

Here is the code I wrote, it's broken.

``````-- bounce

-- Use this function to perform your initial setup
function setup()
ball = {}
local balls = 10
for i = 1,balls do
ball[i] = physics.body(CIRCLE, 25)
ball[i] = 900
end
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
fill(255, 255, 255, 255)
ellipse(WIDTH/2, ball[i].y, 40)

end
``````
• Posts: 167

Oh! I forgot to put the ~y~ in my setup line ~ball[i].y = 600~

• Mod
Posts: 8,625
``````set ball[i].x=some value
set ball[i].y=some value
``````
• Mod
Posts: 8,625

It was easier to fix the code then explain what was wrong.

``````-- bounce

function setup()
ball = {}
local balls = 10
for i = 1,balls do
ball[i] = physics.body(CIRCLE, 25)
ball[i].x = math.random(50,WIDTH-50)
ball[i].y = math.random(HEIGHT-200,HEIGHT)
end
end

function draw()
background(40, 40, 50)
fill(255, 255, 255, 255)
for a,b in pairs(ball) do
ellipse(b.x, b.y, 40)
end
end

``````
Sign In or Register to comment.