Repeating Code.. Is there a simpler way?

I’ve been working on my first ever game and made a Pong type game that breaks bricks also. I’ve been reading up and learning a lot and want to progress this game. Now the one I have now I could just edit it and make it work how I want but I need to repeat again and again for me to understand and I am changing quite a few things. Now my current code for my blocks I have 3 rows of 6 blocks. I am wondering if there is a better way to make these blocks and destroy them when collided with than I have now. Here is my set up of creating and destroying my top level of blocks.

[lua]---------- Level 1
block1 = display.newRect( 10, 35, 40, 12)
block1:setFillColor(255,25,20)
physics.addBody (block1, “static”, {})

block2 = display.newRect( 60, 35, 40, 12)
block2:setFillColor(255,25,20)
physics.addBody (block2, “static”, {})

block3 = display.newRect( 110, 35, 40, 12)
block3:setFillColor(255,25,20)
physics.addBody (block3, “static”, {})

block4 = display.newRect( 160, 35, 40, 12)
block4:setFillColor(255,25,20)
physics.addBody (block4, “static”, {})

block5 = display.newRect( 210, 35, 40, 12)
block5:setFillColor(255,25,20)
physics.addBody (block5, “static”, {})

block6 = display.newRect( 260, 35, 40, 12)
block6:setFillColor(255,25,20)
physics.addBody (block6, “static”, {})

And now destroy…

------------- Level 1
ball.class = “ball”
block1.class = “b1”
function block1:collision(e)
if (e.phase == “began”) then
ydirection = ydirection * -1
cScore.text = cScore.text +3
block1:removeSelf()
end
–return true
end
block1:addEventListener(“collision”)

ball.class = “ball”
block2.class = “b2”
function block2:collision(e)
if (e.phase == “began”) then
ydirection = ydirection * -1
cScore.text = cScore.text +3
block2:removeSelf()
end
–return true
end
block2:addEventListener(“collision”)

ball.class = “ball”
block3.class = “b3”
function block3:collision(e)
if (e.phase == “began”) then
ydirection = ydirection * -1
cScore.text = cScore.text +3
block3:removeSelf()
end
–return true
end
block3:addEventListener(“collision”)

ball.class = “ball”
block4.class = “b4”
function block4:collision(e)
if (e.phase == “began”) then
ydirection = ydirection * -1
cScore.text = cScore.text +3
block4:removeSelf()
end
–return true
end
block4:addEventListener(“collision”)

ball.class = “ball”
block5.class = “b5”
function block5:collision(e)
if (e.phase == “began”) then
ydirection = ydirection * -1
cScore.text = cScore.text +3
block5:removeSelf()
end
–return true
end
block5:addEventListener(“collision”)

ball.class = “ball”
block6.class = “b6”
function block6:collision(e)
if (e.phase == “began”) then
ydirection = ydirection * -1
cScore.text = cScore.text +3
block6:removeSelf()
end
–return true
end
block6:addEventListener(“collision”)
[import]uid: 20272 topic_id: 34795 reply_id: 334795[/import]

To create the equivalent 6 blocks using a “loop” and a table:

blocks = {} -- create the table to hold everything...  
  
for i = 0, 5 do  
 blocks[i]= {} -- Create this blocks specific table  
 blocks[i] = display.newRect( 10 + ( i \* 50 ), 35, 40, 12)  
 blocks[i]:setFillColor(255,25,20)  
 physics.addBody (blocks[i], "static", {})  
end  

Note that this code uses table index 0, which is different than the usual use of lua tables (which typically start with index number 1).

Your code covers a bunch of lua topics… I don’t have time today to go through much, but hopefully the code sample above sheds some light on setting up “loops” to simplify your code. [import]uid: 79933 topic_id: 34795 reply_id: 138304[/import]

There are a lot of ways to streamline your code. I think the big thing you will want to do is get familiar with loops as well as tables and display groups.

I do my collisions a bit different but you should be able to get the idea.

[lua]local tblBlocks = {}
local function myCollisionEvent(event)

if event.object1.class == “block” then
ydirection = ydirection * -1
cScore.text = cScore.text +3
display.remove(event.object1)
else if event.object2.class == “block” then
ydirection = ydirection * -1
cScore.text = cScore.text +3
display.remove(event.object2)
end

end
–table.getn just gets the size of the table, we want to loop while it’s size < 6
while table.getn(tblBlocks) < 6 do

local block = display.newRect( 260, 35, 40, 12)
block:setFillColor(255,25,20)
physics.addBody (block, “static”, {})
block.class == “block”
block:addEventListener(“collision”, myCollisionEvent)
table.insert(tblBlocks, block)

end[/lua] [import]uid: 147305 topic_id: 34795 reply_id: 138306[/import]

Alright I’ll defiantly practice up on my tables and loops. Appreciate the help [import]uid: 20272 topic_id: 34795 reply_id: 138308[/import]

To create the equivalent 6 blocks using a “loop” and a table:

blocks = {} -- create the table to hold everything...  
  
for i = 0, 5 do  
 blocks[i]= {} -- Create this blocks specific table  
 blocks[i] = display.newRect( 10 + ( i \* 50 ), 35, 40, 12)  
 blocks[i]:setFillColor(255,25,20)  
 physics.addBody (blocks[i], "static", {})  
end  

Note that this code uses table index 0, which is different than the usual use of lua tables (which typically start with index number 1).

Your code covers a bunch of lua topics… I don’t have time today to go through much, but hopefully the code sample above sheds some light on setting up “loops” to simplify your code. [import]uid: 79933 topic_id: 34795 reply_id: 138304[/import]

There are a lot of ways to streamline your code. I think the big thing you will want to do is get familiar with loops as well as tables and display groups.

I do my collisions a bit different but you should be able to get the idea.

[lua]local tblBlocks = {}
local function myCollisionEvent(event)

if event.object1.class == “block” then
ydirection = ydirection * -1
cScore.text = cScore.text +3
display.remove(event.object1)
else if event.object2.class == “block” then
ydirection = ydirection * -1
cScore.text = cScore.text +3
display.remove(event.object2)
end

end
–table.getn just gets the size of the table, we want to loop while it’s size < 6
while table.getn(tblBlocks) < 6 do

local block = display.newRect( 260, 35, 40, 12)
block:setFillColor(255,25,20)
physics.addBody (block, “static”, {})
block.class == “block”
block:addEventListener(“collision”, myCollisionEvent)
table.insert(tblBlocks, block)

end[/lua] [import]uid: 147305 topic_id: 34795 reply_id: 138306[/import]

Alright I’ll defiantly practice up on my tables and loops. Appreciate the help [import]uid: 20272 topic_id: 34795 reply_id: 138308[/import]