# Multiple physics objects

That didn’t work out it increased the value infinitely
But I found out a solution I made a table called score and ADDED THIS
And btw “b =nil” didn’t remove the bullet from the table (found out by parameter.watch(“#bullets”) so I also changed that to “table.remove(bullets, 1, b”)

``````function draw()

....other code

pushStyle()
fill(0, 0, 0, 255) -- drawing the bullets
for i,b in pairs(bullets) do
ellipse(b.x,b.y,10)
if b.x<1 or b.x>WIDTH or b.y<1 or b.y>HEIGHT then
table.remove(bullets,1,b)
end
end
popStyle()

.....other code....
end

``````

It increased a value in the table since I added only one element ‘1’ by one only and I could find the value using parameter.watch(“#score”)

Now I have one more question.

1. If I add a value to the table what position is it stored at the last position or the first position or a random position if I haven’t mentioned the position?

2. if I want to remove a value from the last instead of the first position want should i put in table.remove
Example table.remove(bullets, ??, b) what should be there for last position

Actually, the code below works perfectly for me, in scoring

``````for i,b in pairs(bullets) do
if b.x<1 or b.x>WIDTH or b.y<1 or b.y>HEIGHT then
b:destroy()
table.remove(bullets,i)
score=score+1
else
fill(0)
ellipse(b.x,b.y,10)
end
end
``````

Lua stores all table values as key-value pairs, in no particular order. It’s too hard to explain this properly in a forum post.

Tables are very important in Codea, so I recommend you look at the Lua documentation, and some tutorials or explanations on the Internet, I have a tutorial as well, which refers to another good one by Reefwing.

I dont know why it didnt work for me! This is the whole game till now if you want to check it out not that difficult but okay to pass your time. I’m still building it up but here’s where I’ve reached till

``````
function setup()
physics.pause()
physics.gravity(0,0)
score = {}
bullets = {}
number = {}
ball = {}
for i = 1,5 do
ball[i] = Ballons()
end
sound(DATA, "ZgBAKgBLQFRAQEBAAAAAAK5T+j46fh4/UwBAf0BAQEBAZEBA")
parameter.watch("#number")
parameter.watch("#score")
t = 300
end

function draw()
t = t - 1
if t>=0 then
background(255, 255, 255, 255)
pushStyle()
font("MarkerFelt-Thin")
fontSize(25)
fill(0, 199, 255, 255)
text("no more than 5 lives", WIDTH - 380, HEIGHT - 50)
text("waste an arrow or miss a balloon and lose a life", WIDTH - 250, HEIGHT - 75)
popStyle()
pushStyle()
fontSize(100)
font("SnellRoundhand-Black")
fill(192, 26, 18, 117)
text("BALLONS", WIDTH/2, HEIGHT/2)
popStyle()
pushStyle()
font("AmericanTypewriter-Bold")
fontSize(20)
text("(inspired by the old windows game)", WIDTH/2, HEIGHT/2 - 75)
text("game begins in 5 seconds", WIDTH/2,HEIGHT/2 - 95)
popStyle()
pushStyle()
font("MarkerFelt-Thin")
fontSize(25)
fill(0, 199, 255, 255)
text("Touch behined this line to shoot arrows", 350, HEIGHT - 250)
strokeWidth(3)
stroke(0, 200, 255, 255)
lineCapMode(ROUND)
line(160,HEIGHT-250,75,HEIGHT-250)
line(75,HEIGHT-250,85,HEIGHT-245)
line(75,HEIGHT-250,85,HEIGHT-255)
popStyle()
pushStyle()
strokeWidth(2)
stroke(81, 31, 31, 255)
line(50,0,50,HEIGHT)
popStyle()
end
if t<= 0 then
background(255, 255, 255, 255)
strokeWidth(2)
stroke(81, 31, 31, 255)
line(50,0,50,HEIGHT)
physics.resume()
end

if collision~=nil then        ------------------- see note 3. above
ball[collision[1]].ballon:destroy()   --- see note 2 above
table.remove(ball,collision[1])
bullets[collision[2]]:destroy()
table.remove(bullets,collision[2])
collision=nil
table.insert(score,1)
end

pushStyle()
fill(0, 0, 0, 255) -- drawing the bullets
for i,b in pairs(bullets) do
sprite("Documents:arrow",b.x - 37,b.y,74)
if b.x<1 or b.x>WIDTH or b.y<1 or b.y>HEIGHT then
table.remove(bullets,1,b)
table.insert(number,1)
end
end
for i,b in pairs(ball) do
b:count()
end
end
popStyle()

for i,b in pairs(ball) do -- drawing the ballons
pushStyle()
fill(b.colr)
end
popStyle()

if #number >= 5 then
pushStyle()
fill(255, 0, 0, 255)
font("HoeflerText-BlackItalic")
fontSize(50)
text("Game Over", WIDTH/2, HEIGHT/2)
physics.pause()
popStyle()
end

if #ball == 0 then
for i = 1,5 do
ball[i] = Ballons()
end
end
end

function createBullet()
local bullet = physics.body(CIRCLE,5)
bullet.x = x
bullet.y = y
bullet.linearVelocity = vec2(750,0)
bullet.restitution = 0
bullet.type = DYNAMIC
bullet.info = "bullet"
table.insert(bullets,bullet)
end

function touched(touch)
x = touch.x
y = touch.y
if touch.x<=50 then
if touch.state == BEGAN then
a = true
createBullet()
end
end
end

function collide(contact)
if contact.bodyA.info~=nil and contact.bodyB.info~=nil then
for i,v in pairs(ball) do
if v.ballon==contact.bodyA or v.ballon==contact.bodyB then
for j,bb in pairs(bullets) do
if bb==contact.bodyA or bb==contact.bodyB then
sound(DATA, "ZgNAXS4wQEBAQEBA/9iePiN3mD64nKs+cgBAf0BAQEBAb0BA  ")
collision={i,j}   -- see note 3 above
end
end
end
end
end
end

Ballons = class()

function Ballons:init(x,y,r,c,lv)
self.ballon.x = x or math.random(150,WIDTH - self.radius)
self.ballon.linearVelocity = lv or vec2(0,math.random(100,200))
self.ballon.info="ball"
self.colr = c or color(math.random(255),math.random(255),math.random(255),math.random(100,150))
end

function Ballons:count()
for i = 1,#ball do
if self.ballon.y > HEIGHT + self.radius then
table.insert(number,1)
table.remove(ball,i,ball[i])
end
end
end

``````

Can’t make a start button so I’ve added a five second delay.
How can I make a start button?
And how should I write the score as text not print in the end?

There was a recent post on how to create a start button, have a look for that

I would play around with writing text on the screen until you like it.

Thanks

How can I write number of values in a table on the screen as text?

You’d need to figure out the x,y values yourself and use the text function to write each item where you want it. There’s no shortcut.

``````
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)

function setup()
scores={21342,12657,32121,54324,43278,33287,55223,45121,21387,22418}
end

function draw()
background(40, 40, 50)
showScores()
end

function showScores()
fill(255)
fontSize(40)
for z=1,#scores do
text(scores[z],WIDTH/2,HEIGHT-80*z)
end
end

``````

Thanks for the code. Though I didn’t use it as it is but it helped me find a solution for my problem