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 :)
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: -
'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"
-
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 - 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
HTH
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?
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?
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... - Line Input #1, strLine
-
Set X, Y to start of line, set up starting image or whatever.
-
MyArray = String2ByteArray(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
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.
Oops! Forgot to include the function(s) that I wrote for this. These are untested, but I think they'll work... - Public Function String2ByteArray(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$(pmString, I, 1))
-
Next
-
-
String2ByteArray = TempByte
-
-
End Function
-
-
-
Public Function ByteArray2String(ByVal pmByteArray As Variant) As String
-
Dim L As Long, U As Long, I As Long
-
-
L = LBound(pmByteArray)
-
U = UBound(pmByteArray)
-
-
For I = L To U
-
ByteArray2String = ByteArray2String & Chr$(pmByteArray(I))
-
Next
-
-
End Function
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 :)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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?
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| | |