2D Array Help

Blitz3D Forums/Blitz3D Programming/2D Array Help

Knight #51(Posted 2008) [#1]
I'm making a game like BREAKOUT but I don't know the best way to set up a 2D array of blocks. I thought of something like this but I don't know if it is that efficient and worth going through the trouble to make.

;Here some of my code

;a type to handle the blocks

Type BlockType

Field x,y ;coordinates

End Type

;creates a block row 'DUH'

Function CreateBlockRow(x,y)

maxBlocks = 8
x_spacing = 100

For i = 0 To maxBlocks

block.BlockType = New BlockType
block\x = x
block\y = y

[draw the block image here]

x = x + x_spacing;the blocks won't be very wide

Next

End Function

Function SetupBlocks()

block_rows = 3

y_gap = 0 ;once again,blocks are not very big

For ii = 0 to block_rows

CreateBlockRow(10,y_gap)

y_gap = y_gap + 150

Next

End Function


Knight #51(Posted 2008) [#2]
sorry I forgot to comment my code :(


Beaker(Posted 2008) [#3]
Try and use a code/codebox block, as well as indent your code. See here:
Forum Codes

I really don't think you need to use an array for your bricks. Just use Types like you have done above.


Kryzon(Posted 2008) [#4]
It would be best to set up something like values for each color\type of block. So you wouldn't need to use types, you'd just go through the array and:
TotalBlocksX = 16 ;16 blocks width-wise, where each image should have 40 pixels in width.
TotalBlocksY = 7 ;7 rows of blocks, each one having 40 pixels too in height. 

 Dim Blocks(TotalBlocksX,TotalBlocksY) 



Restore Level_1

For W = 0 To TotalBlocksX-1
      For H = 0 To TotalBlocksY-1
            Read Blocks(W,H)
      Next
Next

.Level_1 
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Data 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1
Data 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1
Data 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1
Data 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Data 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1

Then you could have differente block images for each color\type of block (the main reason for keeping different coloured blocks is, besides being able to draw "pictures" every level and make nice mozaics\patterns with the blocks, enabling items to fall down; each item depending on the block type, of course. There also can be different hardness between the block colors, so as to have blocks that need more than one hit to break).

In order to draw the blocks, judging you'd have 3 types of blocks:
For W = 0 To TotalBlocksX-1
      For H = 0 To TotalBlocksY-1
    
            Select Blocks(W,H)
                       Case 0
                                DrawImage Block1,W*40,H*40
                       Case 1
                                DrawImage Block2,W*40,H*40
                       Case 2
                                DrawImage Block3,W*40,H*40 
           End Select
      
      Next
Next



Knight #51(Posted 2008) [#5]
That code is awsome!!! How did you come up with that?????

How would you use that style of setting up blocks with Types instead of arrays?

p.s Sorry I haven't been really used to advance blitz programming (I'm a Visual Studio Developer).


Stevie G(Posted 2008) [#6]
TotalBlocksX = 16 ;16 blocks width-wise, where each image should have 40 pixels in width.
TotalBlocksY = 7 ;7 rows of blocks, each one having 40 pixels too in height. 
Type BlockT
	Field x
	Field y
	Field ID
End Type
Dim Blocks.BlockT(TotalBlocksX,TotalBlocksY) 


Restore Level_1

For W = 0 To TotalBlocksX-1
      For H = 0 To TotalBlocksY-1
			Blocks(W,H) = New BlockT
            Read Blocks(W,H)\ID
			Blocks(W,H)\x = W
			Blocks(W,H)\y = H
      Next
Next

.Level_1 
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Data 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1
Data 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1
Data 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1
Data 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Data 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1




Knight #51(Posted 2008) [#7]
Awsome. Thanks everybody.