pygame编组(精灵组)Group中的常用方法介绍
生活随笔
收集整理的這篇文章主要介紹了
pygame编组(精灵组)Group中的常用方法介绍
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
說明:
1.以下所用的Group均為Group類的對象實例
2.Group類是對AbstractGroup類的繼承
sprite.py文檔中描述如下:
class Group(AbstractGroup):
"""container class for many Sprites
pygame.sprite.Group(*sprites): return Group
A simple container for Sprite objects. This class can be subclassed to
create containers with more specific behaviors. The constructor takes any
number of Sprite arguments to add to the Group. The group supports the
following standard Python operations:
in test if a Sprite is contained
len the number of Sprites contained
bool test if any Sprites are contained
iter iterate through all the Sprites
The Sprites in the Group are not ordered, so the Sprites are drawn and
iterated over in no particular order.
"""
def __init__(self, *sprites):
AbstractGroup.__init__(self)
self.add(*sprites)
方法一:Group.draw(surface)
說明:對精靈組中的每一個精靈依次調用surface.blit(),依次將精靈組中的精靈繪制在surface上
AbstractGroup類中對其的定義:
def draw(self, surface):
"""draw all sprites onto the surface
Group.draw(surface): return None
Draws all of the member sprites onto the given surface.
"""
sprites = self.sprites()
surface_blit = surface.blit
for spr in sprites:
self.spritedict[spr] = surface_blit(spr.image, spr.rect)
self.lostsprites = []
方法二:Group.update()
說明:對精靈組中的每一個精靈依次調用update()方法,并且update()方法需要自己在自己定義的精靈類中去實現
AbstractGroup類中對其的定義:
def update(self, *args):
"""call the update method of every member sprite
Group.update(*args): return None
Calls the update method of every member sprite. All arguments that
were passed to this method are passed to the Sprite update function.
"""
for s in self.sprites():
s.update(*args)
蒹葭蒼蒼,白露為霜;
所謂伊人,在水一方。
總結
以上是生活随笔為你收集整理的pygame编组(精灵组)Group中的常用方法介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS8 使用 aliyun 阿里
- 下一篇: gMap使用简单介绍