What is the best Python code for a platform game level?


In my game, I'd like a platform segment, and a proper dungeon Python data structure.
Is it simply a 1200x400 grid of tiles / terrain blocks,
or do coders also like to do placing of terrain features x,y with x_length and y_length?

How would I do an optimal  grid in Python for this, best?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I'm not developing games so perhaps better to listen to somebody else's recommendation.

In general it depends on whether you want to keep all the tiles in memory or whether you want to load only the tiles and all the neighborhood information at a time.

Next question is, how much information you want to store per tile.

If you just want to keep the tile type and a few flags for each tile in memory for all types, then you might look at python arrays.
They are quite memory efficient: https://docs.python.org/2/library/array.html

For more complex values and especially if your tiles contain a lot of information (same for each tile)  (that require a lot of memory)  you might consider ctypes:


import ctypes
from ctypes import c_int, c_char, Structure, sizeof

class Tile(Structure):
    _fields_ = [
        ("name", c_char * 8),
        ("val", c_int),

TileRow = Tile * 1200
TileArray = TileRow * 400

array = TileArray()

print("an array element requires %d bytes of memory" % sizeof(array[0][0]))
print("an array row requires %d bytes of memory" % sizeof(array[0]))
print("array requires %d bytes of memory" % sizeof(array))

array[0][0].name = "topleft"
array[0][0].val = -1
array[399][1199].name = "btmright"
array[0][0].val = 1

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
beavoidAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Computer Games

From novice to tech pro — start learning today.