English 中文(简体)
Level Design in Pygame
原标题:

Hey--I m trying to design my first game using the Pygame library for Python, and I was wondering what the best practices are for level design in general. I would love to hear what you guys think are good object oriented design patterns for managing levels. Also, I m fairly new to Python--thanks!

最佳回答

With this type of game your maps are in terms of tiles (I m assuming that by level you mean an individual level, not managing all of your levels). Each tile has

  • an associated picture (what it looks like on the display)
  • a type (ie, a wall, the ground, a trap, etc.)

When I create tile-based games in Pygame, I usually have a Map class which contains the current map:

  • the pygame.Surface of the map (what you ll be blitting to the display)
  • a list of lists (ie, a matrix) where each item is a Tile object (I ve also done games where you just have a string that tells you what type of tile it is, and then you don t need a separate Tile class)

The map should be relatively static - you could have that traps become normal tiles after you step on them (this is pretty easy - when you do collision detection and it s a hit, just change that tile to a different Tile object (presumably the one for an empty tile)), but you don t want characters or movable blocks in the map if you can help it. Since the movable blocks have their own rules for how they can be moved, it s not as simple as just changing a tile - you d have a whole set of logic, and at least two tiles would have to be changed (and what if you could move the blocks onto traps - you d then have to remember, separately, what was below it - bleh). In my opinion it s easier to just have a class for each moving object and item.

In short, you have:

  • Tile
  • Map
  • Block
  • other movable objects/sprites

And that s basically your whole level. For multiple levels, if individual levels are always the same, you can just have a list of Map objects, one for each level.

问题回答

If this is your first Pygame application, don t spend time worrying about "object oriented design patterns for managing levels". What you need to do now is to figure out how to make Pygame do what you want it to do.

  • Can you display everything you want to?
  • Is your display flicker-free?
  • Can you read the user input controls properly?
  • etc.

Object oriented patterns for managing levels comes later, much later.

Generally speaking, a simple way to do it is using matrices (or multidimensional arrays - they work the same way here).

Basically, each Map is an Array, with each item in the array being a square on the grid. For example a 3 by 3 grid would be as follows:

(Psuedocode)

var Map = [[1,2,3][1,2,3][1,2,3]];

In place of numbers, you could put strings for a function to parse and draw or take action based on what the value of the cell is.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签