Make code shorter??

I have a simple game i am making with just shapes so anybody can see this, what i want to know is if there is a way to make any of this code shorter??

[code]
local physics = require(“physics”)
physics.start()
local camera = display.newGroup()

–Level 1 and End Game stuff

local circle = display.newCircle( 100, 300, 10 )
physics.addBody( circle, “dynamic”, { density = 1.0, friction = 0.3, bounce = 0.2, radius = 10 } )

circle:setLinearVelocity(200, 0)

camera:insert(circle)

local informer = display.newText(“Level 1”, 10, 10, 10, 30)
local ground = display.newLine( 50,700, 1000000000, 700 )
ground:setColor(250,40,200)

ground.width = 3
physics.addBody(ground, “static”, {friction =.1, bounce = .2})
camera:insert(ground)

local uperground = display.newRect(50, 50, 50001, 100)
physics.addBody(uperground, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(uperground)

local r1 = math.random(0,700)
local r2 = math.random(500,1000)
local r3 = math.random(800, 1300)
local r4 = math.random(1100, 1600)
local r5 = math.random(1300, 2100)
local r6 = math.random(1800, 2600)
local r7 = math.random(2300, 3100)
local r8 = math.random(2800, 3600)

local r10 = math.random(0,700)
local r20 = math.random(500,1000)
local r30 = math.random(800, 1300)
local r40 = math.random(1100, 1600)
local r50 = math.random(1300, 2100)
local r60 = math.random(1800, 2600)
local r70 = math.random(2300, 3100)
local r80 = math.random(2800, 3600)

local r = math.random(0,255)
local g = math.random(0,255)
local b = math.random(0,255)

– x obsticles

local ob1 = display.newRect(r1, 600, 100, 100)

physics.addBody(ob1, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob1)

local ob2 = display.newRect(r2, 600, 100, 100)

physics.addBody(ob2, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob2)

local ob3 = display.newRect(r3, 600, 100, 100)

physics.addBody(ob3, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob3)

local ob4 = display.newRect(r4, 600, 100, 100)

physics.addBody(ob4, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob4)

local ob5 = display.newRect(r5, 600, 100, 100)

physics.addBody(ob5, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob5)

local ob6 = display.newRect(r6, 600, 100, 100)

physics.addBody(ob6, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob6)

local ob7 = display.newRect(r7, 600, 100, 100)

physics.addBody(ob7, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob7)

local ob8 = display.newRect(r8, 600, 100, 100)

physics.addBody(ob8, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob8)

–x colors

ob1:setFillColor(r,g,b)
ob2:setFillColor(r,g,b)
ob3:setFillColor(r,g,b)
ob4:setFillColor(r,g,b)
ob5:setFillColor(r,g,b)
ob6:setFillColor(r,g,b)
ob7:setFillColor(r,g,b)
ob8:setFillColor(r,g,b)


– y obsticles

local ob10 = display.newRect(r10, 150, 100, 100)

physics.addBody(ob10, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob10)

local ob20 = display.newRect(r20, 150, 100, 100)

physics.addBody(ob20, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob20)

local ob30 = display.newRect(r30, 150, 100, 100)

physics.addBody(ob30, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob30)

local ob40 = display.newRect(r40, 150, 100, 100)

physics.addBody(ob40, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob40)

local ob50 = display.newRect(r50, 150, 100, 100)

physics.addBody(ob50, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob50)

local ob60 = display.newRect(r60, 150, 100, 100)

physics.addBody(ob60, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob60)

local ob70 = display.newRect(r70, 150, 100, 100)

physics.addBody(ob70, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob70)

local ob80 = display.newRect(r80, 150, 100, 100)

physics.addBody(ob80, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob80)


–y colors

ob10:setFillColor(r,b,g)
ob20:setFillColor(r,b,g)
ob30:setFillColor(r,b,g)
ob40:setFillColor(r,b,g)
ob50:setFillColor(r,b,g)
ob60:setFillColor(r,b,g)
ob70:setFillColor(r,b,g)
ob80:setFillColor(r,b,g)



–camera move

local function moveCamera(event)
if circle.x > 110 then
camera.x = -circle.x + 100
end
end
Runtime:addEventListener(“enterFrame”, moveCamera)


–endgame stuff

local f = circle.x - 80

local resett = display.newText(“Restart”, f, 430, 0, 28)
resett:setTextColor(0,0,255)
resett.isVisible = false
local function ending(self, event)

resett.isVisible = true
informer:removeSelf()
informer = display.newText(“Game Over”,10,10,10, 30)
end

ground.collision = ending
ground:addEventListener(“collision”, ground)

uperground.collision = ending
uperground:addEventListener(“collision”, uperground)

ob1.collision = ending
ob1:addEventListener(“collision”, ob1)

ob2.collision = ending
ob2:addEventListener(“collision”, ob2)

ob3.collision = ending
ob3:addEventListener(“collision”, ob3)

ob4.collision = ending
ob4:addEventListener(“collision”, ob4)

ob5.collision = ending
ob5:addEventListener(“collision”, ob5)

ob6.collision = ending
ob6:addEventListener(“collision”, ob6)

ob7.collision = ending
ob7:addEventListener(“collision”, ob7)

ob8.collision = ending
ob8:addEventListener(“collision”, ob8)

ob10.collision = ending
ob10:addEventListener(“collision”, ob10)

ob20.collision = ending
ob20:addEventListener(“collision”, ob20)

ob30.collision = ending
ob30:addEventListener(“collision”, ob30)

ob40.collision = ending
ob40:addEventListener(“collision”, ob40)

ob50.collision = ending
ob50:addEventListener(“collision”, ob50)

ob60.collision = ending
ob60:addEventListener(“collision”, ob60)

ob70.collision = ending
ob70:addEventListener(“collision”, ob70)

ob80.collision = ending
ob80:addEventListener(“collision”, ob80)


–change gravity

local function g(event)
if (event.phase == “began”) then
physics.setGravity(0, -9.8)
elseif (event.phase == “ended”) then
physics.setGravity(0, 9.8)
end
end
Runtime:addEventListener(“touch”, g)


–restart

local function restart(event)

circle.x = 110
circle.y = 300
camera.x = 100
circle:setLinearVelocity(200, 0)
resett.isVisible = false
informer:removeSelf()
informer = display.newText(“Level 1”, 10,10,10,30)
ob1.x = math.random(0, 700)
ob2.x = math.random(0, 700)

end
resett:addEventListener(“tap”, restart)
local function level2(event)
if circle.x > 3600 then
informer:removeSelf()
informer = display.newText(“Level 2”, 10,10,10,30)
circle:applyLinearImpulse(.002,0)
end
end
Runtime:addEventListener(“enterFrame”, level2)


– Level 2 stuff

local r100 = math.random(3600,4100)
local r200 = math.random(3800,4600)
local r300 = math.random(4300, 5100)
local r400 = math.random(4800, 5600)
local r500 = math.random(5300, 6100)
local r600 = math.random(5800, 6600)
local r700 = math.random(5300, 7100)
local r800 = math.random(5800, 7600)

local r2 = math.random(0,255)
local g2 = math.random(0,255)
local b2 = math.random(0,255)
local ob100 = display.newRect(r100, 150, 100, 100)

physics.addBody(ob100, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob100)

local ob200 = display.newRect(r200, 150, 100, 100)

physics.addBody(ob200, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob200)

local ob300 = display.newRect(r300, 150, 100, 100)

physics.addBody(ob300, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob300)

local ob400 = display.newRect(r400, 150, 100, 100)

physics.addBody(ob400, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob400)

local ob500 = display.newRect(r500, 150, 100, 100)

physics.addBody(ob500, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob500)

local ob600 = display.newRect(r600, 150, 100, 100)

physics.addBody(ob600, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob600)

local ob700 = display.newRect(r700, 150, 100, 100)

physics.addBody(ob700, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob700)

local ob800 = display.newRect(r800, 150, 100, 100)

physics.addBody(ob800, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob800)


–y colors

ob100:setFillColor(r2,b2,g2)
ob200:setFillColor(r2,b2,g2)
ob300:setFillColor(r2,b2,g2)
ob400:setFillColor(r2,b2,g2)
ob500:setFillColor(r2,b2,g2)
ob600:setFillColor(r2,b2,g2)
ob700:setFillColor(r2,b2,g2)
ob800:setFillColor(r2,b2,g2)

– x part

local r1000 = math.random(3600,4100)
local r2000 = math.random(3800,4600)
local r3000 = math.random(4300, 5100)
local r4000 = math.random(4800, 5600)
local r5000 = math.random(5300, 6100)
local r6000 = math.random(5800, 6600)
local r7000 = math.random(5300, 7100)
local r8000 = math.random(5800, 7600)
local v = math.random(-200, 0)
local v1 = math.random(-200, 0)
local v2 = math.random(-200, 0)
local v3 = math.random(-200, 0)
local v4 = math.random(-200, 0)
local v5 = math.random(-200, 0)
local v6 = math.random(-200, 0)
local v7 = math.random(-200, 0)

local ob1000 = display.newRect(r1000, 700, 100, v)

physics.addBody(ob1000, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob1000)

local ob2000 = display.newRect(r2000, 700, 100, v1)

physics.addBody(ob2000, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob2000)

local ob3000 = display.newRect(r3000, 700, 100, v2)

physics.addBody(ob3000, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob3000)

local ob4000 = display.newRect(r4000, 700, 100, v3)

physics.addBody(ob4000, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob4000)

local ob5000 = display.newRect(r5000, 700, 100, v4)

physics.addBody(ob5000, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob5000)

local ob6000 = display.newRect(r6000, 700, 100, v5)

physics.addBody(ob6000, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob6000)

local ob7000 = display.newRect(r7000, 700, 100, v6)

physics.addBody(ob7000, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob7000)

local ob8000 = display.newRect(r8000, 700, 100, v7)

physics.addBody(ob8000, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(ob8000)
local function level3(event)
if circle.x > 7600 then
informer:removeSelf()
informer = display.newText(“Level 3”, 10, 10, 10, 30)
end
end
Runtime:addEventListener(“enterFrame”, level3)
return camera [import]uid: 38977 topic_id: 17510 reply_id: 317510[/import]

Yes, use tables.

I only fixed part of the code as an example but try something like this:

local r = {}  
r[1000] = math.random(3600,4100)  
r[2000] = math.random(3800,4600)  
r[3000] = math.random(4300, 5100)  
r[4000] = math.random(4800, 5600)  
r[5000] = math.random(5300, 6100)  
r[6000] = math.random(5800, 6600)  
r[7000] = math.random(5300, 7100)  
r[8000] = math.random(5800, 7600)  
  
local v = {}  
for i = 1, 8 do  
 v[i] = math.random(-200, 0)  
end   
   
local ob = {}  
  
for i = 1, 8 do  
 ob[i\*1000] = display.newRect(r[i\*1000], 700, 100, v[i])  
 physics.addBody(ob[i\*1000], "static", {density = 1, friction =.1, bounce = .2})  
 camera:insert(ob[i\*1000])  
end  

[import]uid: 19626 topic_id: 17510 reply_id: 66490[/import]

@Gigabook, you need to read up on and understand Tables/Arrays.

read about it here and here
and another article here

You also need to understand what functions are, so that you can break up repetitive code into blocks that can be managed better.

your 460 lines of code should actually come down to about 200 odd for sure.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17510 reply_id: 66497[/import]

Thank you for all your quick replies, i’ll look more into tables and arrays. [import]uid: 38977 topic_id: 17510 reply_id: 66503[/import]

also, I believe if you are using [lua]math.random()[/lua] more than once, it’s better to assign it to a local var like this:

[lua]local mRand = math.random
local r1000 = mRand(3600,4100)
local r2000 = mRand(3800,4600)
local r3000 = mRand(4300, 5100)
local r4000 = mRand(4800, 5600)
local r5000 = mRand(5300, 6100)[/lua]

etc… [import]uid: 49447 topic_id: 17510 reply_id: 66507[/import]

Thanks for your help everyone!! i came up with this so far…

[code]
local physics = require(“physics”)
physics.start()
local camera = display.newGroup()

–Level 1 and End Game stuff

local circle = display.newCircle( 100, 300, 10 )
physics.addBody( circle, “dynamic”, { density = 1.0, friction = 0.3, bounce = 0.2, radius = 10 } )

circle:setLinearVelocity(200, 0)

camera:insert(circle)

local informer = display.newText(“Level 1”, 10, 10, 10, 30)
local ground = display.newLine( 50,700, 1000000000, 700 )
ground:setColor(250,40,200)

ground.width = 3
physics.addBody(ground, “static”, {friction =.1, bounce = .2})
camera:insert(ground)

local uperground = display.newRect(50, 50, 50001, 100)
physics.addBody(uperground, “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(uperground)
local mRand = math.random


– level 1


– x part

local r = {}
r[1000] = math.random(0,700)
r[2000] = math.random(500,1000)
r[3000] = math.random(800, 1300)
r[4000] = math.random(1100, 1600)
r[5000] = math.random(1300, 2100)
r[6000] = math.random(1800, 2600)
r[7000] = math.random(2300, 3100)
r[8000] = math.random(2800, 3600)

local v = {}
for i = 1, 8 do
v[i] = math.random(-200, 0)
end

local obx = {}

for i = 1, 8 do
obx[i\*1000] = display.newRect(r[i\*1000], 700, 100, v[i])
obx[i\*1000]:setFillColor(mRand(0,255), mRand(0,255), mRand(0,255))
physics.addBody(obx[i\*1000], “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(obx[i\*1000])

end

local r1 = {}
r1[1000] = math.random(0,700)
r1[2000] = math.random(500,1000)
r1[3000] = math.random(800, 1300)
r1[4000] = math.random(1100, 1600)
r1[5000] = math.random(1300, 2100)
r1[6000] = math.random(1800, 2600)
r1[7000] = math.random(2300, 3100)
r1[8000] = math.random(2800, 3600)

local v1 = {}
for i = 1, 8 do
v1[i] = math.random(0, 200)
end

local oby = {}
for i = 1, 8 do
oby[i\*1000] = display.newRect(r1[i\*1000], 150, 100, v1[i])
oby[i\*1000]:setFillColor(mRand(0,255), mRand(0,255), mRand(0,255))
physics.addBody(oby[i\*1000], “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(oby[i\*1000])
end

local r = {}
r[1000] = math.random(0,700)
r[2000] = math.random(500,1000)
r[3000] = math.random(800, 1300)
r[4000] = math.random(1100, 1600)
r[5000] = math.random(1300, 2100)
r[6000] = math.random(1800, 2600)
r[7000] = math.random(2300, 3100)
r[8000] = math.random(2800, 3600)

local v = {}
for i = 1, 8 do
v[i] = math.random(-200, 0)
end

local obx = {}

for i = 1, 8 do
obx[i\*1000] = display.newRect(r[i\*1000], 700, 100, v[i])
obx[i\*1000]:setFillColor(mRand(0,255), mRand(0,255), mRand(0,255))
physics.addBody(obx[i\*1000], “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(obx[i\*1000])

end

local rl2 = {}
rl2[1000] = math.random(3600,4100)
rl2[2000] = math.random(3800,4600)
rl2[3000] = math.random(4300,5100)
rl2[4000] = math.random(4800,5600)
rl2[5000] = math.random(5300,6100)
rl2[6000] = math.random(5800,6600)
rl2[7000] = math.random(6300,7100)
rl2[8000] = math.random(6800,7600)

local vl2 = {}
for i = 1, 8 do
vl2[i] = math.random(0, 200)
end

local obl2 = {}
for i = 1, 8 do
obl2[i\*1000] = display.newRect(rl2[i\*1000], 150, 100, vl2[i])
obl2[i\*1000]:setFillColor(mRand(0,255), mRand(0,255), mRand(0,255))
physics.addBody(obl2[i\*1000], “static”, {density = 1, friction =.1, bounce = .2})
camera:insert(obl2[i\*1000])
end



–camera move

local function moveCamera(event)
if circle.x > 110 then
camera.x = -circle.x + 100
end
end
Runtime:addEventListener(“enterFrame”, moveCamera)


–change gravity

local function g(event)
if (event.phase == “began”) then
physics.setGravity(0, -9.8)
elseif (event.phase == “ended”) then
physics.setGravity(0, 9.8)
end
end
Runtime:addEventListener(“touch”, g)



–endgame stuff

local f = circle.x - 80

local resett = display.newText(“Restart”, f, 430, 0, 28)
resett:setTextColor(0,0,255)
resett.isVisible = false
local function ending(self, event)

resett.isVisible = true
informer:removeSelf()
informer = display.newText(“Game Over”,10,10,10, 30)
end

circle.collision = ending
circle:addEventListener(“collision”, circle)


–restart

local function restart(event)

circle.x = 110
circle.y = 300
camera.x = 100
circle:setLinearVelocity(200, 0)
resett.isVisible = false
informer:removeSelf()
informer = display.newText(“Level 1”, 10,10,10,30)
ob1.x = math.random(0, 700)
ob2.x = math.random(0, 700)

end
resett:addEventListener(“touch”, restart)

local function level2(event)
if circle.x > 3600 then
informer:removeSelf()
informer = display.newText(“Level 2”, 10,10,10,30)
circle:applyLinearImpulse(.002,0)
end
end
Runtime:addEventListener(“enterFrame”, level2)

return camera [import]uid: 38977 topic_id: 17510 reply_id: 66525[/import]

You could go through, line by line and see if you can shorten your code that way, or you could break it all up into “chunks” with functions and split it up into different, descriptive modules.

Here’s a tutorial on modular programming with Corona to help get you started:
http://blog.anscamobile.com/2011/09/a-better-approach-to-external-modules/

And the follow-up, which talks about “modular” classes:
http://blog.anscamobile.com/2011/09/tutorial-modular-classes-in-corona/
[import]uid: 52430 topic_id: 17510 reply_id: 66583[/import]