Snap on grid

Blitz3D Forums/Blitz3D Beginners Area/Snap on grid

Nike(Posted 2009) [#1]
I am making a tile editor for a 2d game and I was woundering how you would make a 800 by 600 grid and how you would make 100 by 100 tiles to it.


Nike(Posted 2009) [#2]
Ok i figured this out but how would you save and load the maps?


_Skully(Posted 2009) [#3]
Well, you might start here to get you started...


Nike(Posted 2009) [#4]
thats pretty neat but how would I turn it into data that would be on a notepad text file? Like if the map was all grass and grass was 1, it would look like

1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111

How could I save and load it like this?


Matty(Posted 2009) [#5]
SeedRnd MilliSecs()

Dim MapArray(10,10) ;array of size 11x11
For x = 0 To 10
	For Y = 0 To 10

		MapArray(x,y) = Rand(0,9)	

	Next
Next

savemap("testmap.txt")
End


;load function
Function loadmap(filename$)
infile=ReadFile(filename$)
If infile<>0 Then
	For y=0 To 10
		instring$=ReadLine(infile)
		For x=0 To 10
			maparray(x,y) = Int(Mid(instring,x+1,1))
		Next
	Next
	CloseFile infile
	Return 0
Else
	Return 1 ;error code, did not load file, interpret as you will
EndIf 
End Function 

;save function
Function savemap(filename$)

Outfile=WriteFile(Filename$)
If outfile<>0 Then 
	For y=0 To 10
		outstring$=""
		For x=0 To 10
			outstring=outstring + Str(maparray(x,y))
		Next
		WriteLine outfile,outstring
	Next

	CloseFile outfile
Return 0
Else
Return 1 ;error code, did not save file, interpret as you will
EndIf 

End Function 


Something like that should work.


Nike(Posted 2009) [#6]
Thank you so much!!!
I do not understand this code tho.
My game is like this
graphics are 800x600
there are 6 differnt tiles that are 50x50 and 50x100
the 50x100 are 2 trees
How would you set the above to my game?


Matty(Posted 2009) [#7]
If in your array the grass tiles are indicated by '1' and the tree tiles are indicated by '2' then simply store '1' where you want grass and '2' where you want trees. The save and load function should handle that fine.

Or in your case use the numbers 1-6 with each value representing a different tile.


Nike(Posted 2009) [#8]
Can you edit your code and post it because I still dont understand.


Matty(Posted 2009) [#9]
Using those two functions defined earlier run the following to create a map which is just grass ('1')

Dim MapArray(10,10) ;array of size 11x11
For x = 0 To 10
	For Y = 0 To 10

		MapArray(x,y) = 1; - all grass

	Next
Next

savemap("testmap.txt")
End



Then open up the testmap.txt with your Text editor And Replace
a few 1s with 2s

eg:

11111111111
11111112111
11121111111
11111211111
11112111111
11121111111
11111111111
11111211111
11111111111
11111111111
11111111111


Then run this:
Graphics 800,600,0,2
Dim maparray(10,10)
loadmap("testmap.txt")
For y=0 To 10
	For x=0 To 10
		Text x*16,y*16,maparray(x,y)
	Next
Next
Flip
WaitKey
End



Nike(Posted 2009) [#10]
Thank you sooooooooooooooooooooooooo much!!!!!


Nike(Posted 2009) [#11]
OK i just got back to that part after saving your code and it works great but i dont understand how it would read off the editor and save different ones. If you need to see my editor for some reason you can download it here .


Nike(Posted 2009) [#12]
How would I also change the loader from only printing number 1-6 to making them the images?

1 = grass
2 = sand
3 = stone
4 = water
5 = evergreen tree
6 = apple tree

Here is the loader code edited for my game
Graphics 800,600,0,2
SeedRnd MilliSecs()

Dim maparray(11,15)
loadmap("map1.txt")
For y=0 To 15
	For x=0 To 11
		Text x*16,y*16,maparray(x,y)
	Next
Next
Flip
WaitKey
End

;load function
Function loadmap(filename$)
infile=ReadFile(filename$)
If infile<>0 Then
	For y=0 To 15
		instring$=ReadLine(infile)
		For x=0 To 11
			maparray(x,y) = Int(Mid(instring,x+1,1))
		Next
	Next
	CloseFile infile
	Return 0
Else
	Return 1 ;error code, did not load file, interpret as you will
EndIf 
End Function 



_Skully(Posted 2009) [#13]
Nike,

I just noticed that you are using tiles you say that are 50x50.. I highly recommend you use tiles that are a power of 2... as in 32x32, 64x64, 128x128

What are you trying to achieve with your tile-map system?... I might be able to offer some ideas to make your life easier.. as it is you might be coding yourself into a corner... for example, you will limit yourself to using only 1 tileset...


Nike(Posted 2009) [#14]
Well I am trying to make a game sorta like NemesisChat (http://www.blitzbasic.com/codearcs/codearcs.php?code=1658)
There are 6 tiles and I tried to make then one image that uses loadanimeimage but I couldent fighure it out so I just made 6 different images. This is just the editor where players can make there own maps and I am also making the main thing where players play on maps.


Nike(Posted 2009) [#15]
O yeah and unlike Nemesis chat, my game has no screen scrolling


_Skully(Posted 2009) [#16]
oic... well that will keep things simple then...

What i'm doing will be networked as well, but a whole lot more complex! Closer to farm town but not anything like farm town ;)

But one piece of advice for you is to think what you might want to do with the game in the future... would you ever need more than one tileset... will players be able to add tilesets. Keeping that in mind will allow you to make decisions that can support expansion later.


Nike(Posted 2009) [#17]
How would I get the saver to save all of the work and how would I get the loader to load images instead of numbers?

The loader code edited how I need it is here:
Graphics 800,600,0,2
SeedRnd MilliSecs()

Dim maparray(11,15)
loadmap("map1.txt")
For y=0 To 15
	For x=0 To 11
		Text x*16,y*16,maparray(x,y)
	Next
Next
Flip
WaitKey
End

;load function
Function loadmap(filename$)
infile=ReadFile(filename$)
If infile<>0 Then
	For y=0 To 15
		instring$=ReadLine(infile)
		For x=0 To 11
			maparray(x,y) = Int(Mid(instring,x+1,1))
		Next
	Next
	CloseFile infile
	Return 0
Else
	Return 1 ;error code, did not load file, interpret as you will
EndIf 
End Function 



Nike(Posted 2009) [#18]
Will someone help me?


_Skully(Posted 2009) [#19]
What you will likely need to do (since you are using different images) is save the path to the image as well...

so.. when you save, you would save a list of paths and reference numbers first and then the list of numbers to indicate which images are used where

' for each image
WriteInt Strm,indx
Writeline Strm,path
Writeline Strm,-1


repeat
   readint Strm,indx
   if Indx
      readline Strm,path
   endif
until indx=-1



Nike(Posted 2009) [#20]
I dont get it. :(


Nike(Posted 2009) [#21]
Can anyone help me some more before 11:00 A.M. Pacific tommarow? If you can I will be able to respond asap but after that for the next week it will be harder for me to be on here. Can anyone else help or explain _Skully's code?


Nike(Posted 2009) [#22]
Please help


Nike(Posted 2009) [#23]
ok I got the loader perfect. But now i need to know how in the world would I save? Also,I dont understand _Skullys code so can someone explain it?


Matty(Posted 2009) [#24]
Sorry I haven't responded, I had no access to a PC for the last 2 days, and I'm at work now, won't be free for a few hours, so not sure if I'll get a chance to help out.


Nike(Posted 2009) [#25]
O I thought my fourm got deleted for some reason and my computer let me see it but nobody else. lol