Jump to content

VBS, How to create a loop for a 2 dimensional array.

Hello world,

I'm creating a simple maze game i VBScript, I have the walls in a 37x40 grit with each box in the grit being the same size as the character your moving.

I've already created all of the walls and the character is moving but I was wondering how I can create a loop to check for all X and Y values for the walls.

Atm the character that you are stearing can faze through the walls like they don't exicts.

This is a simpler version of what I have:

 

 

```VBScript

'assume that the block you're stearing already exicts

 

Dim wall(37,40) as Picturebox

Dim wallX(37, 40) wallY(37,40) as Integer

 

'so this is how I created all walls

 

 

For wallX = 0 to 37

For wallY = 0 to 40

  wall(wallX, wallY) = new picturebox

  wall(wallX, wallY).backcolor = new color.black

  wall(wallX, wallY).location = new point(xVAR, yVAR)

  wall(wallX, wallY).size = new size(75, 75)

  wall(wallX, wallY).visible = false

  Form1.controls.add(wall(wallX, wallY))

Next

Next

 

'and then to place all walls, for example

 

wall(4, 31).visible = true

wall(3, 5).visible = true

 

'and so on

 

'I tried adding this

'block is the block you're moving (as integer)

 

If block.bounds.IntersectsWith(wall(wallX, wallY).bounds) And wall(wallX, wallY).visible = true then

//no movement

else

//movement

End If

 

 

Any suggestions, I would greatly apprichiate them.

Thank you!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Try adding the variable after Next

 

ex Next WallY,  Next WallX  ... this way the script knows which loop the next belongs to.

 

Also, use Long instead of Integer - there's no benefit to using Integer as data type. Integer is a 16bit (2 byte) variable, storing -32768 .. 32767 while Long uses 32 bits (4 bytes) to store data. Processors are 32 bit or 64 bit and work with 32 bit (4 byte) registers so there's really no performance benefit to using Integer data types.

 

Also consider maybe having a single big picture box and your tiles (walls, grass, road etc) stored in a hidden picture box as tiles ... as needed, do a copy of your desired tile from your picture box at the coordinates where you want that to be.

Arrange your tiles like this:

[ tile 1 ]

[ tile 2 ]

.....

[ tile n ]

 

and if each tile is 32x32px, and you want to draw tile 3 on the big picture, you know the coordinates:  0, 3 x 32px , 0, 3x32px + 31 px (31px because picture starts from 0)

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×