hi,

I'm creating a game with pygame and i want to create a matrix that will represent the map(where each tile will be).
my pygame code is ok, because if i create the map without loading the tilemap (the matrix file) the game runs ok.
that would be good but I want to be able to create more than one map.
I created a code to generate a map matrix file

#MAP GENERATOR

import pickle

tilemap = []
for y in range(100):
    tilemap.append([])
    for x in range(100):
        tilemap[y].append(1)

file = open('data/tilemap2.txt', 'w+b')
pickle.dump(tilemap, file)

file.close()

but when I try to open i get this

Traceback (most recent call last):
File "C:\Users\Mateus\Desktop\Martial Arts\Martial Arts1.py", line 6, in <module>
tilemap = pickle.load('data/tilemap2.txt')
File "C:\Python31\lib\pickle.py", line 1365, in load
encoding=encoding, errors=errors).load()
AttributeError: 'str' object has no attribute 'readline'

here is the code that i use to open

import pickle
tilemap = pickle.load('data/tilemap2.txt')
print (tilemap)

Recommended Answers

All 3 Replies

Try this

import pickle
tilemap = pickle.load(open('data/tilemap2.txt', 'rb'))
print (tilemap)

thx man
=D

Crash python course:

AttributeError: 'str' object has no attribute 'readline'

.... hmmm readline ? ding dong! ==> python was expecting a file.
It found a string ahah! I forgot to open a file :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.