[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] stupidity, and a lesson learned



Im glad I could help. I use it that way and it works fast and reliable.

Am 19.12.2006 um 20:44 schrieb spotter .:

Very nice implementation and code, Farai, this will be a great help. Thanks!

-spot

On 12/18/06, Farai Aschwanden <fash@xxxxxxxxxx> wrote:
Maybe this helps: you can read the whole map directly into a
dictionary/list. Lets say the map is divided into two parts:
1. line: meta data
2. - n. line: map data

The whole map file could look like this:

['Dark Forrest', 'Donald Duck', timestamp, 20, 50,
'weather_sun', ...] # Line 1(meta data)
{'map_pos1': ['stone.png', passable_yes, trap_no, ...], # must be in
structure of a dictionary while map_pos1 is the key
'map_pos2: ['grass.png', passable_yes, trap_yes, ...], # ...
'map_posn: [...]
}


That way the position inside the lists defines what it represents,
like 1. item of meta data is the map name. The file will be smaller
than having it in the file like:
map_name = 'Dark Forrest'
author = 'Donald Duck'
etc.

Having the first line as list and the further lines as dict you can
now read it w/o care of items inside:


map_meta_dict = [] # List to store metadata of map
map_area_dict = {} # Dict to store whole map
map_merge = '' # Temporarily needed to read/add all map lines


# Read head of map
map_head = eval(map_read.readline()) # Read first line of file
and set file read pointer to line 2 (eval is important)


# Read map structure
for map_line in map_read: # Iterate now from line 2 to to end
of the file
map_merge += (line.rstrip ()) # Read line by line of map and
add it to to the previously read lines


map_area_dict = eval(map_merge) # Evaluate file structure after
completly reading it and assign it to the dict


file.close (map_read) # Tnx and goodbye file


So you dont have to care every single item when reading in the file and you can easy access it when the map is into dictionary/list format. Its not even optimized yet but quite fast. Another advantage is that the map lines can have different lengths and must not be square like. There is also an option to read in the whole file what would make it even faster but then you cant initially split meta and map not that easy. File handling is easier if you have meta data and map in one file. No 'if's reading the file and string functionality needed. ;)

Just my 2 cents
Farai





Am 18.12.2006 um 06:26 schrieb spotter .:

> Hi everybody,
>
> I just got hit by the silliest, stupidest error on my part and I
> decided to share it, since it was kinda funny after the fact and so
> that no one else would make my mistake.
>
> This was the data I was trying to parse (a map format I was making):
>
> mapname=DeathMap
> mapauthor=pumaninja
> mapdate=12172006
> mapversion=0.0.1
> mapwidth=10
> mapheight=5
>
> And this was the code I was using to get the info out of the file:
>
> elif info.find("mapauthor=") == 0: # 0 means that the string has
> been found
> temp = ""
> temp = info.strip("mapauthor=")
> map_author.append(temp)
>
> I was thinking that the .strip() simply took out the specified
> absolute phrase. I was wrong. The .strip() looks for all the
> characters specified and takes them out. For the other tags, this did
> not get me since none of the other names matched any where close to
> data type or the combination of letters. Only for the author part
> because I had put in pumaninja which if you see has p, u, m, a
> contained in mapauthor.
>
> Ah, well, at least this didnt come back and get me way after the fact.
> Although, this code was based on a part of a game I had already
> written, that one missed the bullet because the info was numbers and
> not characters.
>
> Moral of the story : Clearly read the docs, and test the function with
> various data types.
>
> Happy coding,
> -spot