[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Managing sprites



David Mikesell wrote:
So, here is a high-level description of how I am managing sprites in my
Frantic game. Does this sound like SOP or am I way off in the weeds?


Each game object (starship, ufo, enemy, powerup, etc.) has a factory or
manager class that is instantiated once in the game_scene object.   It
loads and stores the animation images and sounds for the game object it
creates.   It also has methods to draw, clear, update all sprites it
creates and keeps in a RenderUpdates object.

The sprite objects are created by the manager and contain the logic for
that specific object (mainly movement patterns). The array of images
is passed by reference to the sprite on creation, and it points to the
correct frame depending on a time-based incremented counter.


So, the game_scene iterates through the managers, which create objects
(again, time-based), draw, update, and clear them.

I hope this was clear.

I'm doing something a bit different. Because I'm focusing on more of an RPG-style world, the logic behind the characters might be way, way more complicated than the movement pattern of an action game enemy. So, I've split the game into a "worldsim" that tracks the location of characters in terms of tiles and decides when/where they move, and a "display" that only cares about sprites and tiles as images. So for instance, when running the display by itself, I could send commands directly to the sprite to say "go 1 tile right" and it'd move... through a wall. When I hooked the display up to the worldsim I called a more complicated command, "Hey, character, send a command to the game world saying 'go e'." Then the game world parsed the command, decided whether the move was valid, and only then told the display to move the sprite.


But for the sprites themselves, I did have a SpriteManager class (since folded into the display class itself) that basically says each frame, "for each sprite in the game, DrawSprite." The Sprite class I'm using has its own code for loading frames from images.

Kris