0

I have gotten to the point in making a game where I need to figure out how I do load the levels.

So if I had a resource file called "level0" and its contents would be something like this:

0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000200000003
1111111111111111
16
160

How I would load them to a 16x12 array?

Also, I need to load the player's start coordinates as well. In this case, they would be 16 and 160.

EDIT:

The resource file is a text file.

4
  • What type is the resource file? Text / Byte[] ? Commented Nov 21, 2015 at 16:21
  • Is it a project resource or an external file which the project does not have reference to? I will post the code accordingly. Commented Nov 21, 2015 at 16:26
  • The file is in the project itself. Commented Nov 21, 2015 at 16:27
  • Ok, please wait for just a few minutes. Commented Nov 21, 2015 at 16:28

1 Answer 1

1

Here's the code:

Dim levelDataRaw As String() = (My.Resources.level0).Split({Environment.NewLine}, StringSplitOptions.None)
Dim levelData16x12(12) As String
For i As Integer = 0 To 11
    levelData16x12(i) = levelDataRaw(i)
Next
Dim startCoordinates(2) As String
startCoordinates(0) = levelDataRaw(12)
startCoordinates(1) = levelDataRaw(13)

So, you can access the data by row and then by column, i.e, to get the third digit of the second line, use:

levelData16x12(1)(2) 'For 2nd line, (2-1) or 1 and for 3rd digit, (3-1) or 2.

Just in case you don't know (please don't think I consider you a newbie) : Because indices are zero-based, be sure to subtract 1 from both positions as I have done in the code.

To access the coordinates:

startCoordinates(0) '16
startCoordinates(1) '160

I hope this helps :)

Update - To use external files, just replace My.Resources.level0 with `File.ReadAllText("level0.txt") like this:

Dim levelDataRaw As String() = File.ReadAllText("level0.txt").Split({Environment.NewLine}, StringSplitOptions.None) 'Or some other filename instead of [level0.txt]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.