reading each individual character in a file, array style

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrnn
    New Member
    • Sep 2007
    • 29

    reading each individual character in a file, array style

    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 :)
  • VBPhilly
    New Member
    • Aug 2007
    • 95

    #2
    Originally posted by mrnn
    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:
    Code:
    'declare the array 
    dim myarray(1)  As String 
    myarray(0) = "hello" 
    
    'expand the array by 1 leaving "hello" in the array
    redim preserve myarray(ubound(myarray) + 1)
    
    'assign a value to the last index:
    myarray(ubound(myarray) - 1) = "mars"
    Last edited by Killer42; Sep 18 '07, 11:31 PM. Reason: Reduced excessive quote block

    Comment

    • kadghar
      Recognized Expert Top Contributor
      • Apr 2007
      • 1302

      #3
      Originally posted by mrnn
      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

      [CODE=vb]dim myArr() as string
      dim myArr2() as string
      dim i as integer
      dim j as integer


      myArr = split(str1,chr( 10))'This will give you an array where each element is each line

      redim myarr2(1 to ubound(myarr), 1 to len(myarr(1)))

      for i = 1 to ubound(myarr)
      for j = 1 to len(myarr(1))
      myarr2(i,j) = mid(myarr(i),j, 1)
      next
      next[/CODE]

      HTH
      Last edited by Killer42; Sep 18 '07, 11:32 PM. Reason: Reduced excessive quote block

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        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?

        Comment

        • mrnn
          New Member
          • Sep 2007
          • 29

          #5
          Originally posted by Killer42
          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?

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            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...

            [CODE=vb]Line Input #1, strLine
            Set X, Y to start of line, set up starting image or whatever.
            MyArray = String2ByteArra y(strLine)
            For I = 1 To Ubound(MyArray)
            Select Case MyArray(I)
            Case Blocking
            Paint appropriate picture at X, Y.
            Case ...
            Paint whatever picture at X, Y
            End Select
            Add 32 to X
            Next[/CODE]
            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.

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Oops! Forgot to include the function(s) that I wrote for this. These are untested, but I think they'll work...

              [CODE=vb]Public Function String2ByteArra y(ByVal pmString As String) As Byte()

              Dim TempByte() As Byte
              Dim L As Long, I As Long
              L = Len(pmString)

              ' If no string, just exit.
              If L = 0 Then Exit Function

              ReDim TempByte(1 To L)
              For I = 1 To L
              TempByte(I) = Asc(Mid$(pmStri ng, I, 1))
              Next

              String2ByteArra y = TempByte

              End Function


              Public Function ByteArray2Strin g(ByVal pmByteArray As Variant) As String
              Dim L As Long, U As Long, I As Long

              L = LBound(pmByteAr ray)
              U = UBound(pmByteAr ray)

              For I = L To U
              ByteArray2Strin g = ByteArray2Strin g & Chr$(pmByteArra y(I))
              Next

              End Function[/CODE]

              Comment

              • mrnn
                New Member
                • Sep 2007
                • 29

                #8
                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 :)

                Comment

                Working...