Hey guys, I don't understand how to read values form files. This is what I'd like to do: I'd like to create a map, for example for a platform game, by writing some values into a .txt file. For example, this would be my map:
0000000000
0000000000
0000000000
0000000000
0000000000
2000000000
1111111111
0, 1 and 2 are squares of different colors (32*32). How can I read this file and draw the correxponding map in the game?
My game Worm has a similar way of loading and displaying 2D maps, download and look at the source code and you can see how it's done. Some commands might need adjusting since GL Basic has changed a lot since then though.
http://gamecorner.110mb.com (http://gamecorner.110mb.com)
Quote from: Moru on 2009-Jun-22
My game Worm has a similar way of loading and displaying 2D maps, download and look at the source code and you can see how it's done. Some commands might need adjusting since GL Basic has changed a lot since then though.
http://gamecorner.110mb.com (http://gamecorner.110mb.com)
Thank you, but I can't find any source code in the file.
So sorry, must have forgotten to include it on the last update. Now the file contains a zip of the source too.
Quote from: Moru on 2009-Jun-22
So sorry, must have forgotten to include it on the last update. Now the file contains a zip of the source too.
... Really sorry but I can't find it again. :|
Read a text file to an array:
LOCAL map%[]
DIM map%[11][11]
IF OPENFILE 0, "the.map"
FOR y%=0 to 10 // 11 lines
READLINE 0, line$
FOR x%=0 TO 10 // 11 coloums - change to fit your file
map%[x][y] = mid$(line$, x, 1) // convert one digit to a number - adjust to your needs. Maybe 2 digits per whatever...
NEXT
NEXT
ENDIF
Improvements: Read/write binary data, maybe?
Read 2 digits for a number instead
read/write the width/height of the map at the top of the file
read and compare a file header
Quote from: Kitty Hello on 2009-Jun-22
Read a text file to an array:
LOCAL map%[]
DIM map%[11][11]
IF OPENFILE 0, "the.map"
FOR y%=0 to 10 // 11 lines
READLINE 0, line$
FOR x%=0 TO 10 // 11 coloums - change to fit your file
map%[x][y] = mid$(line$, x, 1) // convert one digit to a number - adjust to your needs. Maybe 2 digits per whatever...
NEXT
NEXT
ENDIF
Improvements: Read/write binary data, maybe?
Read 2 digits for a number instead
read/write the width/height of the map at the top of the file
read and compare a file header
I don't understand why you write
IF OPENFILE...
...
ENDIF
Why do you use the "IF" statement?
And it gave me an error to the "IF OPENFILE..." line, probably because the last argument was missing.
Anyways, I can't open my map again, could you please explain me a bit more precisely how to do that? Sorry, I am really a noob... :whistle:
Have a look at this post : http://www.glbasic.com/forum/index.php?topic=3007.0 (http://www.glbasic.com/forum/index.php?topic=3007.0)
Thank you, but I think that if anyone could make me a little example it would be better, bcause Theese are many different ways of doing this thing and I think I' going mad... :whistle:
And sorry for my english... :whistle:
OPENFILE returns a 1 if successful so if you write IF OPENFILE it will only do what is inside the IF block if the open was successful.
worm.zip contains a source.zip file, unpack this and load it up in GLBasic and you should see the load_level function. It reads the size and some options and then the level data. draw_screen draws the map on the screen.
(In case you still can't find it, here are the relevant pieces:
GLOBAL world[] // the layout of the level
GLOBAL world_params AS WORLD_PARAMS
load_level(world[], 1)
state = 1
WHILE state > 0
draw_screen(world[])
SHOWSCREEN
WEND
// ------------------------------------------------------------- //
// -=# LOAD_LEVEL #=-
// ------------------------------------------------------------- //
// Returns true if successful, false otherwise
//
FUNCTION load_level: world[], level
LOCAL file$, channel, ok, data$, item$
//LOCAL world_params AS WORLD_PARAMS
LOCAL x, y
file$ = "level" + level + ".txt"
ok = OPENFILE(channel, file$, TRUE) // open the level-file for reading
IF ok=TRUE
IF ENDOFFILE(channel) = FALSE THEN READLINE channel, data$ // get the width of the map
world_params.width = data$
IF ENDOFFILE(channel) = FALSE THEN READLINE channel, data$ // height
world_params.height = data$
IF ENDOFFILE(channel) = FALSE THEN READLINE channel, data$ // how much food needs to be eaten
world_params.total_food = data$
REDIM world[0][0]
REDIM world[world_params.width][world_params.height] // make space for our map
WHILE ENDOFFILE(channel) = FALSE
READLINE channel, data$
IF LEN(data$) > 0 AND LEN(data$) <= width
FOR x=0 TO LEN(data$)-1
IF x < BOUNDS(world[], 0) AND y < BOUNDS(world[],1) // make sure we are inside the array
item$ = MID$(data$, x, 1) // get this squares type
IF item$="#" THEN world[x][y] = spr.wall // and enter the sprite number into the array for displaying later
IF item$="o" THEN world[x][y] = spr.apple
IF item$="T" THEN world[x][y] = spr.shroom
IF item$="+" AND (x = 0 OR x = world_params.width - 1) THEN world[x][y] = spr.pipe_h
IF item$="+" AND (y = 0 OR y = world_params.height - 1) THEN world[x][y] = spr.pipe_v
IF item$="e" OR item$="w" OR item$="s" OR item$="n" // start position and direction
world_params.start_x = x
world_params.start_y = y
IF item$="n" THEN world_params.start_dir = 0 // North
IF item$="e" THEN world_params.start_dir = 90 // east
IF item$="s" THEN world_params.start_dir = 180 // south
IF item$="w" THEN world_params.start_dir = 270 // west
ENDIF
ENDIF
NEXT
INC y, 1 // next line
ENDIF
WEND
place_randomly(spr.apple, world[]) // Place a new apple in the world
place_randomly(spr.shroom, world[]) // Place a new mushroom
// RETURN world_params // returns the type with all the parameters for this world
ELSE
state = 10
// RETURN -1 // something got wrong, return -1
ENDIF
ENDFUNCTION // LOAD_LEVEL
// ------------------------------------------------------------- //
// -=# DRAW_SCREEN #=-
// ------------------------------------------------------------- //
FUNCTION draw_screen: world[]
LOCAL x, y
ALPHAMODE -.999 // to make the alpha channel in PNG work
FOR y = 0 TO height-1
FOR x = 0 TO width-1
IF x < BOUNDS(world[], 0) AND y < BOUNDS(world[],1)
IF world[x][y] > 0 THEN STRETCHSPRITE world[x][y], x*block_size, y*block_size, block_size, block_size
ENDIF
NEXT
NEXT
ENDFUNCTION // DRAW_SCREEN
Quote from: Moru on 2009-Jun-23
worm.zip contains a source.zip file, unpack this and load it up in GLBasic and you should see the load_level function. It reads the size and some options and then the level data. draw_screen draws the map on the screen.
Now I found it, I downloaded the game yesterday, but there were no zip files. Anyways, now I found it. Thank you very much, it is better with a practical example! Thank you :good:
EDIT: I finally did it. Works fine, thank you again! =D