By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,401 Members | 820 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,401 IT Pros & Developers. It's quick & easy.

reading each individual character in a file, array style

mrnn
P: 29
Hello. I'm workin on a 2d game where the levels are Mario style and will either take up only the screen or be a sidescroller like Mario is (as in the level goes off the main screen) and the levels are going to be made determined by the array given in a certain file.
example:

0000000
0000000
0200020
0x00000
1111111

where 1's would be ground (blocking object), 2 a different blockable object, x the player start and 0's blank spaces... I was just wondering how do you make it so when VB reads the file it reads it first determines the size of the array, then using an If/Then statement checks each individual character and creates an image with a set picture, properties (ifblocking = true, etc), and displays it on the screen.

Thanks for the help :)
Sep 18 '07 #1
Share this Question
Share on Google+
7 Replies


VBPhilly
P: 95
Hello. I'm workin on a 2d game ...
in regards to arrays:
in vb, you don't have to know size in advance.
Check this out
example of dynamic adding to an array:
Expand|Select|Wrap|Line Numbers
  1. 'declare the array 
  2. dim myarray(1)  As String 
  3. myarray(0) = "hello" 
  4.  
  5. 'expand the array by 1 leaving "hello" in the array
  6. redim preserve myarray(ubound(myarray) + 1)
  7.  
  8. 'assign a value to the last index:
  9. myarray(ubound(myarray) - 1) = "mars" 
  10.  
Sep 18 '07 #2

kadghar
Expert 100+
P: 1,295
Hello. I'm workin on a 2d game ...
as Philly said you can use Redim Preserve, but please note that you can only redim many times the last dimention. the others, once are defined they cannot be changed. so you'll have to know them before the first redim

to read character by character you can use the MID function

if you have an string called Str1 =

0000000
0000000
0200020
0x00000
1111111

Expand|Select|Wrap|Line Numbers
  1. dim myArr() as string
  2. dim myArr2() as string
  3. dim i as integer
  4. dim j as integer
  5.  
  6.  
  7. myArr = split(str1,chr(10))'This will give you an array where each element is each line
  8.  
  9. redim myarr2(1 to ubound(myarr), 1 to len(myarr(1)))
  10.  
  11. for i = 1 to ubound(myarr)
  12.     for j = 1 to len(myarr(1))
  13.         myarr2(i,j) = mid(myarr(i),j,1)
  14.     next
  15. next
HTH
Sep 18 '07 #3

Expert 5K+
P: 8,434
Two questions.
  • What version of VB are you using?
  • Are you referring to an array in which each element holds one of the characters, the entire array making up the line that was read from the file? Or an array in which each element holds one of the lines? Or perhaps a 2D array holding all the individual characters for all the lines?
Sep 18 '07 #4

mrnn
P: 29
Two questions.
  • What version of VB are you using?
  • Are you referring to an array in which each element holds one of the characters, the entire array making up the line that was read from the file? Or an array in which each element holds one of the lines? Or perhaps a 2D array holding all the individual characters for all the lines?
sweet thanks for the help guys...
also killer42, i got vb6 enterprise and each number has a separate element...

oh and as for the if/then statement to place the image...it'd be something like this, but i know its not truly it...

if arraynum = 0 [say its the first one on the array]
[create a new image]
[load pic file to place in image]
ifblocking = false [or true if arraynum = 1 or something else]
[place pic at newpicx, newpicy (starts as 0, 0)]
newpicx=newpicx+32 (new image will be at 32, 0 instead of overlapping one at 0,0 that's going on now)
[code here for checking to see if its the end of the line, and if it is newpicx now = 0 for the start of a new line and newpicy = newpicy+32]

[then read the next number in the line and repeat, and obviously before this there will be something to check if arraynum = 1,2,the letter "x" or whatever]

also i see that a 32X32pixel image is 60x60 in vb as in height and width so instead of +32's it'd be +60, i was just using that cause the example pic size is 32x32 so u can see the next pic will be 32 over

that clear it up a little more?
Sep 19 '07 #5

Expert 5K+
P: 8,434
One fairly simple way to read the line into an array would be to place it in a string, then call a function (which I've just written) to convert.

Your logic could work something like this...

Expand|Select|Wrap|Line Numbers
  1. Line Input #1, strLine
  2. Set X, Y to start of line, set up starting image or whatever.
  3. MyArray = String2ByteArray(strLine)
  4. For I = 1 To Ubound(MyArray)
  5.   Select Case MyArray(I)
  6.     Case Blocking
  7.       Paint appropriate picture at X, Y.
  8.     Case ...
  9.       Paint whatever picture at X, Y
  10.   End Select
  11.   Add 32 to X
  12. Next
Note that you should set the ScaleMode of your form or picturebox to "3 - Pixel" so that a 32-pixel image actually has a size of 32. And to draw the images for each location, you will probably want to use the PaintPicture method.
Sep 19 '07 #6

Expert 5K+
P: 8,434
Oops! Forgot to include the function(s) that I wrote for this. These are untested, but I think they'll work...

Expand|Select|Wrap|Line Numbers
  1. Public Function String2ByteArray(ByVal pmString As String) As Byte()
  2.  
  3.   Dim TempByte() As Byte
  4.   Dim L As Long, I As Long
  5.   L = Len(pmString)
  6.  
  7.   ' If no string, just exit.
  8.   If L = 0 Then Exit Function
  9.  
  10.   ReDim TempByte(1 To L)
  11.   For I = 1 To L
  12.     TempByte(I) = Asc(Mid$(pmString, I, 1))
  13.   Next
  14.  
  15.   String2ByteArray = TempByte
  16.  
  17. End Function
  18.  
  19.  
  20. Public Function ByteArray2String(ByVal pmByteArray As Variant) As String
  21.   Dim L As Long, U As Long, I As Long
  22.  
  23.   L = LBound(pmByteArray)
  24.   U = UBound(pmByteArray)
  25.  
  26.   For I = L To U
  27.     ByteArray2String = ByteArray2String & Chr$(pmByteArray(I))
  28.   Next
  29.  
  30. End Function
Sep 19 '07 #7

mrnn
P: 29
sweet thanks i'll make sure to try that out when i can, thanks for the help :)
...and i dont completely understand the code but as long as it works then i can look it over and understand it, once again thanks :)
Sep 19 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.