英文:
Pass `special_flags` argument to group.draw in pygame
问题
有没有办法将 `special_flags` 参数传递给 `Group.draw`,以便它使用这些标志调用 `.blit` 方法?
我尝试过像这样将其作为关键字参数传递:
```python
group.draw(surface, special_flags=pygame.BLEND_SOURCE_ALPHA)
但是会出现以下错误:
Traceback (most recent call last):
File "C:\Users\MarciAdam\PycharmProjects\pygame_stuff_1\main.py", line 394, in <module>
group.draw(surface, special_flags=pygame.BLEND_RGBA_MAX)
TypeError: draw() got an unexpected keyword argument 'special_flags'
我知道我可以像这样做:
for sprite in group.sprites():
surface.blit(sprite.image, sprite.rect, special_flags=pygame.BLEND_SOURCE_ALPHA)
但是对于更复杂的组类型,例如 LayeredUpdates
,我需要复制大量的pygame代码。
<details>
<summary>英文:</summary>
Is there a way to pass the `special_flags` argument to `Group.draw` so that it calls the `.blit` method with those flags?
I've tried just passing it as a keyword argument like this:
```python
group.draw(surface, special_flags=pygame.BLEND_SOURCE_ALPHA)
but it gives this error:
Traceback (most recent call last):
File "C:\Users\MarciAdam\PycharmProjects\pygame_stuff_1\main.py", line 394, in <module>
group.draw(surface, special_flags=pygame.BLEND_RGBA_MAX)
TypeError: draw() got an unexpected keyword argument 'special_flags'
I know I could do something like this:
for sprite in group.sprites():
surface.blit(sprite.image, sprite.rect, special_flags=pygame.BLEND_SOURCE_ALPHA)
but I would need to duplicate a lot of the pygame code for the more complicated group types eg. LayeredUpdates
.
答案1
得分: 0
除非您自己实现,否则您无法将该标志传递给pygame.sprite.Group.draw
,因为该函数只有一个参数(也可以参考pygame.sprite.Group()是什么):
>draw(Surface) -> List[Rect]
建议:
唯一的方法是使用“for”循环绘制精灵。我建议编写一个函数来实现这一点,或者实现您自己的MyGroup
类,继承自pygame.sprite.Group
:
class MyGroup(pygame.sprite.Group):
def __init__(self, *args):
super().__init__(*args)
def draw(self, surface, special_flags=0):
for sprite in self:
surface.blit(sprite.image, sprite.rect, special_flags=special_flags)
group = MyGroup()
group.draw(surface, special_flags=pygame.BLEND_RGBA_MAX)
如果您想要对pygame.sprite.LayeredUpdates
或pygame.sprite.RenderUpdates
执行相同操作,您必须自己实现这些对象的所有绘图特性。
一个完全不同的解决方案是使用 pygame.sprite.DirtySprite
。这个类继承自pygame.sprite.Sprite
,并具有额外的blendmode
属性,这是blit
的special_flags
参数。
class MySprite(pygame.sprite.DirtySprite):
def __init__(self):
super().__init__()
self.dirty = 2
self.blendmode = pygame.BLEND_RGBA_MIN
# [...]
请注意,DirtySprite
只适用于 pygame.sprite.LayeredDirty
,并且组中的所有精灵必须是这种类型。
英文:
Short answer:
What you want to do is not possible unless you implement it yourself.
You cannot pass that flag to pygame.sprite.Group.draw
, because that function has only 1 argument (Also see What does pygame.sprite.Group() do):
>draw(Surface) -> List[Rect]
Suggestions:
The only way is to draw the sprite with a "for" loop. I suggest to write a function for this or implement your own MyGroup
class derived from pygame.sprite.Group
:
class MyGroup(pygame.sprite.Group):
def __init__(self, *args):
super().__init__(*args)
def draw(self, surface, special_flags=0):
for sprite in self:
surface.blit(sprite.image, sprite.rect, special_flags = special_flags)
group = MyGroup()
group.draw(surface, special_flags=pygame.BLEND_RGBA_MAX)
If you want to do the same for pygame.sprite.LayeredUpdates
or pygame.sprite.RenderUpdates
, you have to implement all the drawing features of these objects yourself.
A completely different solution is to use pygame.sprite.DirtySprite
. This class is derived from pygame.sprite.Sprite
and has an additional blendmode
attribute which is its the special_flags
argument of blit
.
class MySprite(pygame.sprite.DirtySprite):
def __init__(self):
super().__init__()
self.dirty = 2
self.blendmode = pygame.BLEND_RGBA_MIN
# [...]
Note, DirtySprite
only works with pygame.sprite.LayeredDirty
and all sprites in the group must be of this type.
答案2
得分: 0
更新
现在在 pygame 版本 2.3.0
中可以使用,详见文档。
对于旧版本的 pygame,请参考Rabbid76的回答。
英文:
Update
It works in pygame version 2.3.0
now, see the documentation.
For older pygame versions see Rabbid76's answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论