|
-
Aug 18th, 2002, 06:14 PM
#1
Thread Starter
Fanatic Member
Making a Level File???
Ive got my game pretty much worked out, but i can think of the best way to make the level file. I want it to be able to give me a number (1 - 4), for each spot on the grid (15-30). What would be the best way to do this. I dont need to encode the file.
-
Aug 18th, 2002, 07:42 PM
#2
Frenzied Member
It's very easy, are you already using an array (with 2 dimensions)? If you are it's gonna be easy, loading it from a file... if not, I'll have to tell ya how to use it to store your tiles in memory...
-
Aug 20th, 2002, 11:36 AM
#3
Thread Starter
Fanatic Member
Im using a 2d array for the level map, i dont know how i should write the file for fastest use?
-
Aug 20th, 2002, 02:42 PM
#4
Addicted Member
-
Aug 20th, 2002, 04:39 PM
#5
Frenzied Member
Here's some code:
VB Code:
Dim X As Long, Y As Long, TempByte As Byte
Open App.Path & "\map.txt" For Binary As #1
X=0
Y=0
Do Until EOF(1) 'Loop trough all the characters (bytes) in the file
Get #1,,TempByte
If TempByte=10 Then
'Characters 10 and 13 mean a line break - so one of them makes us go to the next line
Y=Y+1
X=0
ElseIf TempByte=13 Then
'Do nothing...
Else
'Another character - put it in the map array
MapArray(X,Y)=TempByte
End If
X=X+1
Loop
Close #1
-
Aug 20th, 2002, 07:04 PM
#6
Addicted Member
If you store your map data in a UDT you can write the entire UDT to the file with one line:
Code:
Public Type udtMap
'Map properties here
End Type
Dim iFileNum As Integer
Dim objMap As udtMap
'Fill in the map here
iFileNum = FreeFile
Put iFileNum, , objMap
Close iFileNum
There are a two caveats to doing it this way, but if you have a simple map it should work fine.
Notice the call to FreeFile.
NEVER just use a file number as in the above example. It's very sloppy programming and can get you into trouble if you use multiple files.
-
Aug 20th, 2002, 07:54 PM
#7
-
Aug 21st, 2002, 01:03 PM
#8
Thread Starter
Fanatic Member
I tried to clean up the code to better suit me:
Dim X, Y, TempByte, Counter As Byte
Do Until EOF(1)
For Y = 1 To 15
For X = 1 To 30
Get #1, , TempByte
Map(X, Y) = TempByte
For Counter = 0 To 17
SetPixel picPercent.hdc, (X * Y), Counter, vbBlue
DoEvents
Next Counter
Next X
Next Y
Loop
Close
picpercent is a picturebox that i wanted to fill in while it loads, the height is 17, that part works fine.
I think its loading right, but later when im setting the form to display the contents of the map it doesnt work right. I wanted to scan for numbers (0 - 3) 0 is a clear spot, 1 is a wall, 2 is u, 3 is an enemy.:
Private Sub SetWalls()
Dim X, Y, TempX, TempY, Temp As Byte
For Y = 1 To 15
For X = 1 To 30
Check = Map(X, Y)
If Check = 1 Then
For TempY = (Y * 10) To ((Y * 10) + 10)
For TempX = (X * 10) To ((X * 10) + 10)
SetPixel frmScreen.hdc, TempX, TempY, vbBlack
Next TempX
Next TempY
End If
Next X
Next Y
End Sub
Private Sub SetPixelLocation()
Dim X, Y, Temp As Byte
For Y = 1 To 15
For X = 1 To 30
Check = Map(X, Y)
If Check = 2 Then
Pixel.Move (X * 10), (Y * 10)
End If
Next X
Next Y
End Sub
Private Sub DisplayEnemies()
Dim X, Y, Check, Temp As Byte
For Y = 1 To 15
For X = 1 To 30
Check = Map(X, Y)
If Check = 3 Then
Temp = picEnemy.Count
If Temp > 1 Then
Load picEnemy(picEnemy.UBound + 1)
picEnemy(picEnemy.UBound).Visible = True
End If
picEnemy(picEnemy.UBound).Move (X * 10), (Y * 10)
End If
Next X
Next Y
End Sub
Thats all three subs that i call. Though the load is on a different form , could that be the problem?
-
Aug 21st, 2002, 01:06 PM
#9
Thread Starter
Fanatic Member
Pixel is the characters name
-
Aug 21st, 2002, 01:44 PM
#10
PowerPoster
Mushroom: Use [vbcode] and [/vbcode] to make your code readable...
-
Aug 21st, 2002, 02:59 PM
#11
Addicted Member
Originally posted by Jotaf98
Your code is cool but I'm not sure if it's good for dynamic arrays...
Yup, it allocates whatever it needs when it loads it from the file. That's what makes it so cool.
-
Aug 21st, 2002, 03:02 PM
#12
Addicted Member
Ugh, you're looping through the array three times?!?! Just loop through it once and display whatever you need for whatever is at that position Where are you calling the three functions from? It doesn't make a difference where you load the array from as long as it's Public.
-
Aug 21st, 2002, 03:58 PM
#13
PowerPoster
Just some ideas..
VB Code:
Public Type tMap
Tile as Integer
Event as Integer
'whatever...
End Type
Public MapW as Integer
Public MapH as Integer
Public Map() as tMap
'Save data
Open Filename for Binary as Filenumber
Put Filenumber ,, MapW
Put Filenumber ,, MapH
Put Filenumber ,, Map
Close Filenumber
'Load data
Open Filename for Binary as Filenumber
Get Filenumber ,, MapW
Get Filenumber ,, MapH
ReDim Map(MapW, MapH)
Get Filenumber ,, Map
Close Filenumber
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|