Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27389

VS 2010 How to efficiently set values in a 2D array

$
0
0
Hello, I've been juggling with this problem for quite a while now...

I'm working with 2D arrays in Console, trying to make games where you move around a player character by pressing arrow keys to change the Player Position. Changing simple elements is easy enough: to place the Player on the grid, I initalise it using:

Code:

Dim PlayerPos() As Integer = {3, 4}
Then the code later reads from this 1D array and places a Player Character at those coordinates.

But I'm running into difficulty because I want to be able to dynamically place multiple items - namely, wall blocks ("█"). In my other program I used an initialised 2D array to hold wall-coordinates which it could then read from when placing elements, like this:

Code:

Dim BlockCoords(,) As Integer = {{7, 3}, {6, 4}}
'Then I used a For Loop to iterate through and place all of the blocks/walls at the initialised positions.
For i = 0 To BlockCoords.GetLength(0) - 1
            PlayGrid(BlockCoords(i, 0), BlockCoords(i, 1)) = BlockCharacter
Next

And that's fine if I'm manually placing all the blocks in Code, but I want to design a Level-editor where you can do that all in Console. To do that I'd need to be able to efficiently add array coordinates to a list which was read and put into the array every time it reloads... I don't want to have to use ReDim as I'm fairly sure that will lag up the Console to high heaven, so I'd rather avoid arrays. Is there a way to use List(Of T) to function like an array, or to hold multiple values at each index? Or would I have to use two parallel List(Of T)s and read from each? I'd like to avoid having to use two of anything to place elements if possible.

Does anyone have any suggestions?

Viewing all articles
Browse latest Browse all 27389

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>