首页 练字文章 Sprite Module Introduction

Sprite Module Introduction

2024-05-26 14:21  浏览数:281  来源:白可    

Sprite Module Introduction
Pygame version 1.3 comes with a new module, pygame.sprite. This module is written
in Python and includes some higher-level classes to manage your game objects. By
using this module to its full potential, you can easily manage and draw your game
objects. The sprite classes are very optimized, so it's likely your game will run faster
with the sprite module than without.
The sprite module is also meant to be very generic. It turns out you can use it with
nearly any type of gameplay. All this flexibility comes with a slight penalty, it needs a
little understanding to properly use it. The reference documentation for the sprite
module can keep you running, but you'll probably need a bit more explanation of how
to use pygame.sprite in your own game.
Several of the pygame examples (like "chimp" and "aliens") have been updated to use
the sprite module. You may want to look into those first to see what this sprite
module is all about. The chimp module even has its own line-by-line tutorial, which
may help get more an understanding of programming with python and pygame.
Note that this introduction will assume you have a bit of experience programming
with python, and are somewhat familiar with the different parts of creating a simple
game. In this tutorial the word "reference" is occasionally used. This represents a
python variable. Variables in python are references, so you can have several variables
all pointing to the same object.
History Lesson
The term "sprite" is a holdover from older computer and game machines. These older
boxes were unable to draw and erase normal graphics fast enough for them to work
as games. These machines had special hardware to handle game like objects that
needed to animate very quickly. These objects were called "sprites" and had special
limitations, but could be drawn and updated very fast. They usually existed in special
overlay buffers in the video. These days computers have become generally fast enough
to handle sprite like objects without dedicated hardware. The term sprite is still used
to represent just about anything in a 2D game that is animated.
The Classes
The sprite module comes with two main classes. The first is Sprite, which should be
used as a base class for all your game objects. This class doesn't really do anything on
its own, it just includes several functions to help manage the game object. The other
type of class is Group. The Group class is a container for different Sprite objects.
There are actually several different types of group classes. Some of the Groups
can draw all the elements they contain, for example.
This is all there really is to it. We'll start with a description of what each type of
class does, and then discuss the proper ways to use these two classes.
The Sprite Class
As mentioned before, the Sprite class is designed to be a base class for all your game
objects. You cannot really use it on its own, as it only has several methods to help it
work with the different Group classes. The sprite keeps track of which groups it
belongs to. The class constructor (__init__ method) takes an argument of a Group (or
list of Groups) the Sprite instance should belong to. You can also change the Group
membership for the Sprite with the add() and remove() methods. There is also a
groups() method, which returns a list of the current groups containing the sprite.
When using the your Sprite classes it's best to think of them as "valid" or "alive" when
they are belonging to one or more Groups. When you remove the instance from all
groups pygame will clean up the object. (Unless you have your own references to the
instance somewhere else.) The kill() method removes the sprite from all groups it
belongs to. This will cleanly delete the sprite object. If you've put some little games
together, you'll know sometimes cleanly deleting a game object can be tricky. The
sprite also comes with an alive() method, which returns true if it is still a member of
any groups.
The Group Class
The Group class is just a simple container. Similar to the sprite, it has an add() and
remove() method which can change which sprites belong to the group. You also can
pass a sprite or list of sprites to the constructor (__init__() method) to create a Group
instance that contains some initial sprites.
The Group has a few other methods like empty() to remove all sprites from the group
and copy() which will return a copy of the group with all the same members. Also the
has() method will quickly check if the Group contains a sprite or list of sprites.
The other function you will use frequently is the sprites() method. This returns an
object that can be looped on to access every sprite the group contains. Currently this
is just a list of the sprites, but in later version of python this will likely use
iterators for better performance.
As a shortcut, the Group also has an update() method, which will call an update()
method on every sprite in the group. Passing the same arguments to each one. Usually
in a game you need some function that updates the state of a game object. It's very
easy to call your own methods using the Group.sprites() method, but this is a shortcut
that's used enough to be included. Also note that the base Sprite class has a "dummy"
update() method that takes any sort of arguments and does nothing.
Lastly, the Group has a couple other methods that allow you to use it with the builtin
len() function, getting the number of sprites it contains, and the "truth" operator,
which allows you to do "if mygroup:" to check if the group has any sprites.
Mixing Them Together
At this point the two classes seem pretty basic. Not doing a lot more than you can do
with a simple list and your own class of game objects. But there are some big
advantages to using the Sprite and Group together. A sprite can belong to as many
groups as you want. Remember as soon as it belongs to no groups, it will usually be
cleared up (unless you have other "non-group" references to that object).
The first big thing is a fast simple way to categorize sprites. For example, say we had a
Pacman-like game. We could make separate groups for the different types of objects
in the game. Ghosts, Pac, and Pellets. When Pac eats a power pellet, we can change
the state for all ghost objects by effecting everything in the Ghost group. This is
quicker and simpler than looping through a list of all the game objects and checking
which ones are ghosts.
Adding and removing groups and sprites from each other is a very fast operation,
quicker than using lists to store everything. Therefore you can very efficiently change
group memberships. Groups can be used to work like simple attributes for each game
object. Instead of tracking some attribute like "close_to_player" for a bunch of enemy
objects, you could add them to a separate group. Then when you need to access all
the enemies that are near the player, you already have a list of them, instead of going
through a list of all the enemies, checking for the "close_to_player" flag. Later on your
game could add multiple players, and instead of adding more "close_to_player2",
"close_to_player3" attributes, you can easily add them to different groups for each
player.
Another important benefit of using the Sprites and Groups is that the groups cleanly
handle the deleting (or killing) of game objects. In a game where many objects are
referencing other objects, sometimes deleting an object can be the hardest part, since
it can't go away until it is not referenced by anyone. Say we have an object that is
"chasing" another object. The chaser can keep a simple Group that references the
object (or objects) it is chasing. If the object being chased happens to be destroyed,
we don't need to worry about notifying the chaser to stop chasing. The chaser can see
for itself that its group is now empty, and perhaps find a new target.
Again, the thing to remember is that adding and removing sprites from groups is a
very cheap/fast operation. You may be best off by adding many groups to contain and
organize your game objects. Some could even be empty for large portions of the
game, there isn't any penalties for managing your game like this.



声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)