Skip to content
Advertisement

Pygame advanced map editor

I want to make a level editor in pygame, and I have a simple code that does this.

JavaScript

If I have dozens of objects to draw on the screen, should I check them one by one example: [0 = None, 1=dirt_img, 2=grass,3=tree,4=rock], actually I can’t think of any other way but this is there an easy way to do it simpler? sorry for bad english:)

Advertisement

Answer

There are many ways to approach this. Before I begin, if you are just getting started with Python, do not let this answer intimidate you. It is totally okay for you to have an if statement for each case.

My first recommendation is to store the game_map as a list of integers so that you do not need to add all the single quotes.

JavaScript

One approach to the problem is as you have,

JavaScript

We could also be a bit clever and do a dictionary lookup:

JavaScript

And since your tile IDs are numerical starting at 0, we could get really crazy and put the functions in a list:

JavaScript

While this is a fun Python exercise, the more common way this is handled is with object oriented programming.

Say we have a Tile object:

JavaScript

Then we can store game_map as a list of tiles, enumerate over them, and then say tile.draw() like the following:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement