0
\$\begingroup\$

I wish to logically represent a map for a 2D top-down RPG using a a bidimensional list (in python) structure. The map is like a variable size chess board. The first dimension represents the north-south direction of the map and the second represents the west-east direction.

For a square map of i.j cells ,in other words, a list of i lists, of j elements each, each element containing another list of variable representing what is contained in that square cell of the map, like type of terrain, any object, character occupying it, etc, which can be dinamically modified during gameplay.

Is this a good practice that can be easily interpreted by a graphical engine to draw a map?

I have the logical part of the game in python clear in my head, but I know nothing of graphical engines and don't wish to create something that cannot be tranformed easily by a graphical engine in future. Does any one knows a graphical engine that could use this data structure and draw based on it? It does not need to be in python itself, as long as it can be worked with without MUCH programing in other languages.

\$\endgroup\$
  • \$\begingroup\$ I think there may be a misunderstanding here of how graphical engines usually work. You don't just feed your gameplay level structure into them for them to interpret. Instead, you'll generally create a distinct set of graphical primitives representing what you want drawn, using methods specific to the graphics library or framework you've chosen. The glue that links those graphics primitives to your gameplay data structures is up to you — the graphics code just draws stuff, and has no knowledge of any gameplay or level structure outside that specific responsibility. \$\endgroup\$ – DMGregory May 21 '17 at 12:40
  • \$\begingroup\$ So in other words, I can do wathever I like before I glue both togheter? Sorry for my obvious ignorance in gaming development =) \$\endgroup\$ – Edu May 21 '17 at 13:49
  • \$\begingroup\$ Well, your graphics engine/framework/library/etc. will never see the data structure used to store your level layouts or process gameplay. But there will still be options in how you structure your data that make the mapping between gameplay & graphical data easier or harder to manage, or better or worse performing. To advise you on that though, you'd first need to select your graphical framework, and do some research into what kind of data you need to feed it, and plan out your rendering strategy. \$\endgroup\$ – DMGregory May 21 '17 at 13:55
  • \$\begingroup\$ I'm looking forward to pyglet or cocod2d python, after some research, even if it might take a good time before I can accomplish something. \$\endgroup\$ – Edu May 21 '17 at 14:13
1
\$\begingroup\$

Using (your programming language's equivalent of) a two-dimensional array is pretty much the default solution for representing a 2d tile-based world.

Other options are:

  • Some form of 2d tree or spatial hashtable, if you want to allow for extremely large (potentially even infinite) levels
  • Having all objects store their x- and y-position themselves, if you have a very sparse map (like a space exploration game where you rarely have more than two planets and three ships on the screen at once).

You can also have a hybrid approach: Divide your maps into chunks of n*n tiles, which you represent as 2d arrays. Then manage these chunks with one of the methods listed above.

You can also use different solutions for different kinds of objects. For example, it's not uncommon to put all the map tiles in a 2d array with indices corresponding to their positions, but put any moving objects into a regular array sorted by drawing order and have them store their own coordinates.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.