Skip to main content
GameDev.net gamedev.net
🔒 Locked

Python and 2d arrays

Started by Spinningcubes Sep 5, 2006 at 5:39 AM 9 replies 4k views
Original Post
Spinningcubes
Spinningcubes
Helloas :). I'm coding on a tile bases 2d game in python and im thinking about a way to store the tiles in the world for rendering, collision and so on and im wondering if anyone have any good ideas on how to do it. Is it possible to make 2d arrays like C++ tiles[j] or are there any other good way in the language of the snake? Btw, i have no experience in python or pygames before so its ok to talk to me like i'm 1 day old ;)
Kylotan
Kylotan
There are no 2D arrays, but then arguably there aren't truly 2D arrays in C++ either...

There are many ways you could choose to do it in Python:

- a dictionary, with the keys as (x,y) pairs
- a list, using multiplication to find the position (eg. tiles[X + Y *TILES_PER_ROW]
- a list of lists, where each row is a list of tiles, and each map is a list of rows
- via a 2D array type supplied by an external numeric library (sounds tempting, but almost certainly not worth your while)

Do you need more information on these methods?
cypherx
cypherx
My first reaction was "OH GOD! LISTS!"...the linear access times will surely kill him! But then I realized that with a 2d tile game, performance isn't really an issue anyways. So go right ahead, use that lists of lists, it's probably the simplest. Be sure to abstract away your implementation, so if you ever need to switch to something with better performance, it will be simple.

Spinningcubes
Spinningcubes
Thanks for the answers :).

I now use the tiles[X + Y *TILES_PER_ROW] version right now. Mostly as i have done it in C++ before so i don't have to worry to much about it and can focus on the other parts in the game. If there is a problem i might change i later but as it only 25*18 tiles in total i think i will be fine.

I might look up the dictionaries thing later when the game is complete if so only to learn as it seems something that can be good to know :). For now i will code some collision detection and simple AI also and then i might put the game up here when it's done. Small packman clone really and not much more :).

Overall i think i can live with python. The thing with no {} and only spaces/tab to make blocks takes some time getting use to and some better tools would also be nice but i might make some more games with it in the future.
kordova
kordova
And you should get into the habit of using Python's interactive environment. This is probably one of the greatest programming tools I have ever come across (available also in Ruby, Lisp, Scheme...).

kordova@kordova ~ $ pythonPython 2.4.3 (#1, Aug 21 2006, 18:08:46) [GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> a = [[5,8],[9,10]]>>> a[0][1]8>>> # I can use nested lists/2d arrays!...>>> 
Kylotan
Kylotan
Quote:
Original post by cypherx
My first reaction was "OH GOD! LISTS!"...the linear access times will surely kill him!


No, because Python lists do not have linear access time, they have constant access time. They are much like std::vector in C++.

Fruny
Fruny
Quote:
Original post by cypherx
My first reaction was "OH GOD! LISTS!"...the linear access times will surely kill him! But then I realized that with a 2d tile game, performance isn't really an issue anyways.


Python's lists are not linked lists, but dynamic arrays equivalent to C++'s vector. You should really avoid talking about topics which you know nothing about so as not to spread misinformation among beginners.

As for performance, who are you to say that performance isn't an issue for a 2d, tile-based game?

Quote:
Be sure to abstract away your implementation, so if you ever need to switch to something with better performance, it will be simple.


Thankfully, Python uses duck typing, so that's not a problem either.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
cypherx
cypherx
Woops, my bad.

I'll refrain from posting about anything Python-related until I know the language.

However, Fruny, it's very unlikely for a 2D tile-based game to have the CPU as its bottleneck.

Also, I told DUDVim to use a layer of abstraction since he was considering using "tiles[X + Y *TILES_PER_ROW]".

-Alex
Fruny
Fruny
Quote:
Original post by cypherx
I'll refrain from posting about anything Python-related until I know the language.


Wise decision; you rarely see me giving any Java or DirectX advice. [smile]

Quote:
However, Fruny, it's very unlikely for a 2D tile-based game to have the CPU as its bottleneck.


Well, it is Python we are talking about. He's probably going to let SDL do his rendering, which is likely to be significantly faster than his own code. I know that from personal experience (I am using a multidimensional array library)

Quote:
Also, I told DUDVim to use a layer of abstraction since he was considering using "tiles[X + Y *TILES_PER_ROW]".


Fair enough, although to be honest, grabbing an existing library would be faster than creating such a layer, especially for a beginner. I think he's got more on his plate right now than to worry about how to deal with Python slices. Barring that, the list of lists is the simplest solution.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.