473,396 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

reading each individual character in a file, array style

mrnn
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
7 3457
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
1,295 Expert 1GB
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
Killer42
8,435 Expert 8TB
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
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
Killer42
8,435 Expert 8TB
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
Killer42
8,435 Expert 8TB
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
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

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

Similar topics

2
by: Nick Hoare | last post by:
Hi, i am new to VB.Net and really enjoying the trip so far =) However I have this problem: I was wondering if anyone is familiar with how to go about reading in an Excel ".csv" file? I had a...
2
by: Rakesh Sinha | last post by:
Hi, I am writing this application in C++. It reads data from binary files. My current requirement is that: Given a positive number N, I have to read in N bytes from the input stream (which is...
8
by: dbuser | last post by:
Hi, I need help on a problem, as described below. I am reading a file "input.txt"which has data like this: abc def gh izk lmnopq rst uvwxyz I am using fstream object to read the file and...
6
by: The_Kingpin | last post by:
Hi again guys, I've decided to cut my project in section and I found it way easier like this. I'm having a little problem reading struct in a file though. I think after this I'll be able to...
6
by: Neil Patel | last post by:
I have a log file that puts the most recent record at the bottom of the file. Each line is delimited by a \r\n Does anyone know how to seek to the end of the file and start reading backwards?
9
by: paczkow | last post by:
Dear Python Community, I am an engineering and I am experiencing some trouble. Having output data from other software I want to use it. To achieve this I decided to use Python since this...
1
by: j7.henry | last post by:
I am trying to pull specific data that is in a comma delimited file into a web page. So if my comma delimited file looks like: Name,Address,Zip Fred,123 Elm,66666 Mike,23 Jump,11111 I would...
2
by: GeoUK | last post by:
Hi All, New member here with a bit of problem. I have read the FAQ's and searched text books but still cannot solve the problem that I have. As part of a course I am doing at University I had...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.