Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9a033e8

Browse files
authored
Merge pull request#3722 from MarcellPerger1/group-draw-special-flags-arg
Add `special_flags` argument to `Group.draw`
2 parents036ead8 +d242a1c commit9a033e8

File tree

4 files changed

+71
-40
lines changed

4 files changed

+71
-40
lines changed

‎buildconfig/stubs/pygame/sprite.pyi‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ class AbstractGroup(Generic[_TSprite]):
152152
self,*sprites:Union[_TSprite,AbstractGroup[_TSprite],Iterable[_TSprite]]
153153
)->bool: ...
154154
defupdate(self,*args:Any,**kwargs:Any)->None: ...
155-
defdraw(self,surface:Surface)->List[Rect]: ...
155+
defdraw(
156+
self,surface:Surface,bgsurf:Optional[Surface]=None,special_flags:int=0
157+
)->List[Rect]: ...
156158
defclear(
157159
self,surface:Surface,bgd:Union[Surface,Callable[[Surface,Rect],Any]]
158160
)->None: ...
@@ -205,7 +207,9 @@ class LayeredUpdates(AbstractGroup[_TSprite]):
205207

206208
classLayeredDirty(LayeredUpdates[_TDirtySprite]):
207209
def__init__(self,*sprites:_TDirtySprite,**kwargs:Any)->None: ...
208-
defdraw(self,surface:Surface,bgd:Optional[Surface]=None)->List[Rect]: ...
210+
defdraw(
211+
self,surface:Surface,bgsurf:Optional[Surface]=None,special_flags:int=None
212+
)->List[Rect]: ...
209213
# clear breaks Liskov substitution principle in code
210214
defclear(self,surface:Surface,bgd:Surface)->None: ...# type: ignore[override]
211215
defrepaint_rect(self,screen_rect:RectValue)->None: ...

‎docs/reST/ref/sprite.rst‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,13 @@ Sprites are not thread safe. So lock them yourself if using threads.
294294
..method::draw
295295

296296
|:sl:`blit the Sprite images`
297-
|:sg:`draw(Surface) -> List[Rect]`
297+
|:sg:`draw(Surface, bgsurf=None, special_flags=0) -> List[Rect]`
298298
299299
Draws the contained Sprites to the Surface argument. This uses the
300300
``Sprite.image`` attribute for the source surface, and ``Sprite.rect``
301-
for the position.
301+
for the position. ``special_flags`` is passed to ``Surface.blit()``.
302+
``bgsurf`` is unused in this method but ``LayeredDirty.draw()`` uses
303+
it.
302304

303305
The Group does not keep sprites in any order, so the draw order is
304306
arbitrary.
@@ -367,12 +369,13 @@ Sprites are not thread safe. So lock them yourself if using threads.
367369
..method::draw
368370

369371
|:sl:`blit the Sprite images and track changed areas`
370-
|:sg:`draw(surface) -> Rect_list`
372+
|:sg:`draw(surface, bgsurf=None, special_flags=0) -> Rect_list`
371373
372374
Draws all the Sprites to the surface, the same as ``Group.draw()``. This
373375
method also returns a list of Rectangular areas on the screen that have
374376
been changed. The returned changes include areas of the screen that have
375-
been affected by previous ``Group.clear()`` calls.
377+
been affected by previous ``Group.clear()`` calls. ``special_flags`` is
378+
passed to ``Surface.blit()``.
376379

377380
The returned Rect list should be passed to ``pygame.display.update()``.
378381
This will help performance on software driven display modes. This type of
@@ -435,7 +438,7 @@ Sprites are not thread safe. So lock them yourself if using threads.
435438
..method::draw
436439

437440
|:sl:`draw all sprites in the right order onto the passed surface.`
438-
|:sg:`draw(surface) -> Rect_list`
441+
|:sg:`draw(surface, bgsurf=None, special_flags=0) -> Rect_list`
439442
440443
.. ## LayeredUpdates.draw ##
441444
@@ -583,10 +586,13 @@ Sprites are not thread safe. So lock them yourself if using threads.
583586
..method::draw
584587

585588
|:sl:`draw all sprites in the right order onto the passed surface.`
586-
|:sg:`draw(surface,bgd=None) -> Rect_list`
589+
|:sg:`draw(surface,bgsurf=None, special_flags=None) -> Rect_list`
587590
588591
You can pass the background too. If a background is already set, then the
589-
bgd argument has no effect.
592+
bgsurf argument has no effect. If present, the ``special_flags`` argument is
593+
always passed to ``Surface.blit()``, overriding ``DirtySprite.blendmode``.
594+
If ``special_flags`` is not present, ``DirtySprite.blendmode`` is passed
595+
to the ``Surface.blit()`` instead.
590596

591597
.. ## LayeredDirty.draw ##
592598

‎src_c/doc/sprite_doc.h‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515
#defineDOC_GROUPREMOVE "remove(*sprites) -> None\nremove Sprites from the Group"
1616
#defineDOC_GROUPHAS "has(*sprites) -> bool\ntest if a Group contains Sprites"
1717
#defineDOC_GROUPUPDATE "update(*args, **kwargs) -> None\ncall the update method on contained Sprites"
18-
#defineDOC_GROUPDRAW "draw(Surface) -> List[Rect]\nblit the Sprite images"
18+
#defineDOC_GROUPDRAW "draw(Surface, bgsurf=None, special_flags=0) -> List[Rect]\nblit the Sprite images"
1919
#defineDOC_GROUPCLEAR "clear(Surface_dest, background) -> None\ndraw a background over the Sprites"
2020
#defineDOC_GROUPEMPTY "empty() -> None\nremove all Sprites"
2121
#defineDOC_PYGAMESPRITERENDERPLAIN "Same as pygame.sprite.Group"
2222
#defineDOC_PYGAMESPRITERENDERCLEAR "Same as pygame.sprite.Group"
2323
#defineDOC_PYGAMESPRITERENDERUPDATES "RenderUpdates(*sprites) -> RenderUpdates\nGroup sub-class that tracks dirty updates."
24-
#defineDOC_RENDERUPDATESDRAW "draw(surface) -> Rect_list\nblit the Sprite images and track changed areas"
24+
#defineDOC_RENDERUPDATESDRAW "draw(surface, bgsurf=None, special_flags=0) -> Rect_list\nblit the Sprite images and track changed areas"
2525
#defineDOC_PYGAMESPRITEORDEREDUPDATES "OrderedUpdates(*sprites) -> OrderedUpdates\nRenderUpdates sub-class that draws Sprites in order of addition."
2626
#defineDOC_PYGAMESPRITELAYEREDUPDATES "LayeredUpdates(*sprites, **kwargs) -> LayeredUpdates\nLayeredUpdates is a sprite group that handles layers and draws like OrderedUpdates."
2727
#defineDOC_LAYEREDUPDATESADD "add(*sprites, **kwargs) -> None\nadd a sprite or sequence of sprites to a group"
2828
#defineDOC_LAYEREDUPDATESSPRITES "sprites() -> sprites\nreturns a ordered list of sprites (first back, last top)."
29-
#defineDOC_LAYEREDUPDATESDRAW "draw(surface) -> Rect_list\ndraw all sprites in the right order onto the passed surface."
29+
#defineDOC_LAYEREDUPDATESDRAW "draw(surface, bgsurf=None, special_flags=0) -> Rect_list\ndraw all sprites in the right order onto the passed surface."
3030
#defineDOC_LAYEREDUPDATESGETSPRITESAT "get_sprites_at(pos) -> colliding_sprites\nreturns a list with all sprites at that position."
3131
#defineDOC_LAYEREDUPDATESGETSPRITE "get_sprite(idx) -> sprite\nreturns the sprite at the index idx from the groups sprites"
3232
#defineDOC_LAYEREDUPDATESREMOVESPRITESOFLAYER "remove_sprites_of_layer(layer_nr) -> sprites\nremoves all sprites from a layer and returns them as a list."
@@ -41,7 +41,7 @@
4141
#defineDOC_LAYEREDUPDATESGETSPRITESFROMLAYER "get_sprites_from_layer(layer) -> sprites\nreturns all sprites from a layer, ordered by how they where added"
4242
#defineDOC_LAYEREDUPDATESSWITCHLAYER "switch_layer(layer1_nr, layer2_nr) -> None\nswitches the sprites from layer1 to layer2"
4343
#defineDOC_PYGAMESPRITELAYEREDDIRTY "LayeredDirty(*sprites, **kwargs) -> LayeredDirty\nLayeredDirty group is for DirtySprite objects. Subclasses LayeredUpdates."
44-
#defineDOC_LAYEREDDIRTYDRAW "draw(surface,bgd=None) -> Rect_list\ndraw all sprites in the right order onto the passed surface."
44+
#defineDOC_LAYEREDDIRTYDRAW "draw(surface,bgsurf=None, special_flags=None) -> Rect_list\ndraw all sprites in the right order onto the passed surface."
4545
#defineDOC_LAYEREDDIRTYCLEAR "clear(surface, bgd) -> None\nused to set background"
4646
#defineDOC_LAYEREDDIRTYREPAINTRECT "repaint_rect(screen_rect) -> None\nrepaints the given area"
4747
#defineDOC_LAYEREDDIRTYSETCLIP "set_clip(screen_rect=None) -> None\nclip the area where to draw. Just pass None (default) to reset the clip"
@@ -128,7 +128,7 @@ pygame.sprite.Group.update
128128
call the update method on contained Sprites
129129
130130
pygame.sprite.Group.draw
131-
draw(Surface) -> List[Rect]
131+
draw(Surface, bgsurf=None, special_flags=0) -> List[Rect]
132132
blit the Sprite images
133133
134134
pygame.sprite.Group.clear
@@ -150,7 +150,7 @@ pygame.sprite.RenderUpdates
150150
Group sub-class that tracks dirty updates.
151151
152152
pygame.sprite.RenderUpdates.draw
153-
draw(surface) -> Rect_list
153+
draw(surface, bgsurf=None, special_flags=0) -> Rect_list
154154
blit the Sprite images and track changed areas
155155
156156
pygame.sprite.OrderedUpdates
@@ -170,7 +170,7 @@ pygame.sprite.LayeredUpdates.sprites
170170
returns a ordered list of sprites (first back, last top).
171171
172172
pygame.sprite.LayeredUpdates.draw
173-
draw(surface) -> Rect_list
173+
draw(surface, bgsurf=None, special_flags=0) -> Rect_list
174174
draw all sprites in the right order onto the passed surface.
175175
176176
pygame.sprite.LayeredUpdates.get_sprites_at
@@ -230,7 +230,7 @@ pygame.sprite.LayeredDirty
230230
LayeredDirty group is for DirtySprite objects. Subclasses LayeredUpdates.
231231
232232
pygame.sprite.LayeredDirty.draw
233-
draw(surface,bgd=None) -> Rect_list
233+
draw(surface,bgsurf=None, special_flags=None) -> Rect_list
234234
draw all sprites in the right order onto the passed surface.
235235
236236
pygame.sprite.LayeredDirty.clear

‎src_py/sprite.py‎

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -536,22 +536,31 @@ def update(self, *args, **kwargs):
536536
forspriteinself.sprites():
537537
sprite.update(*args,**kwargs)
538538

539-
defdraw(self,surface):
539+
defdraw(
540+
self,surface,bgsurf=None,special_flags=0
541+
):# noqa pylint: disable=unused-argument; bgsurf arg used in LayeredDirty
540542
"""draw all sprites onto the surface
541543
542-
Group.draw(surface): return Rect_list
544+
Group.draw(surface, special_flags=0): return Rect_list
543545
544546
Draws all of the member sprites onto the given surface.
545547
546548
"""
547549
sprites=self.sprites()
548550
ifhasattr(surface,"blits"):
549551
self.spritedict.update(
550-
zip(sprites,surface.blits((spr.image,spr.rect)forsprinsprites))
552+
zip(
553+
sprites,
554+
surface.blits(
555+
(spr.image,spr.rect,None,special_flags)forsprinsprites
556+
),
557+
)
551558
)
552559
else:
553560
forsprinsprites:
554-
self.spritedict[spr]=surface.blit(spr.image,spr.rect)
561+
self.spritedict[spr]=surface.blit(
562+
spr.image,spr.rect,None,special_flags
563+
)
555564
self.lostsprites= []
556565
dirty=self.lostsprites
557566

@@ -650,14 +659,14 @@ class RenderUpdates(Group):
650659
651660
"""
652661

653-
defdraw(self,surface):
662+
defdraw(self,surface,bgsurf=None,special_flags=0):
654663
surface_blit=surface.blit
655664
dirty=self.lostsprites
656665
self.lostsprites= []
657666
dirty_append=dirty.append
658667
forspriteinself.sprites():
659668
old_rect=self.spritedict[sprite]
660-
new_rect=surface_blit(sprite.image,sprite.rect)
669+
new_rect=surface_blit(sprite.image,sprite.rect,None,special_flags)
661670
ifold_rect:
662671
ifnew_rect.colliderect(old_rect):
663672
dirty_append(new_rect.union(old_rect))
@@ -835,10 +844,10 @@ def sprites(self):
835844
"""
836845
returnself._spritelist.copy()
837846

838-
defdraw(self,surface):
847+
defdraw(self,surface,bgsurf=None,special_flags=0):
839848
"""draw all sprites in the right order onto the passed surface
840849
841-
LayeredUpdates.draw(surface): return Rect_list
850+
LayeredUpdates.draw(surface, special_flags=0): return Rect_list
842851
843852
"""
844853
spritedict=self.spritedict
@@ -849,7 +858,7 @@ def draw(self, surface):
849858
init_rect=self._init_rect
850859
forsprinself.sprites():
851860
rec=spritedict[spr]
852-
newrect=surface_blit(spr.image,spr.rect)
861+
newrect=surface_blit(spr.image,spr.rect,None,special_flags)
853862
ifrecisinit_rect:
854863
dirty_append(newrect)
855864
else:
@@ -1108,15 +1117,16 @@ def add_internal(self, sprite, layer=None):
11081117

11091118
LayeredUpdates.add_internal(self,sprite,layer)
11101119

1111-
defdraw(
1112-
self,surface,bgd=None
1113-
):# noqa pylint: disable=arguments-differ; unable to change public interface
1120+
defdraw(self,surface,bgsurf=None,special_flags=None):
11141121
"""draw all sprites in the right order onto the given surface
11151122
1116-
LayeredDirty.draw(surface,bgd=None): return Rect_list
1123+
LayeredDirty.draw(surface,bgsurf=None, special_flags=None): return Rect_list
11171124
11181125
You can pass the background too. If a self.bgd is already set to some
1119-
value that is not None, then the bgd argument has no effect.
1126+
value that is not None, then the bgsurf argument has no effect.
1127+
Passing a value to special_flags will pass that value as the
1128+
special_flags argument to pass to all Surface.blit calls, overriding
1129+
the sprite.blendmode attribute
11201130
11211131
"""
11221132
# functions and classes assigned locally to speed up loops
@@ -1131,8 +1141,8 @@ def draw(
11311141
rect_type=Rect
11321142

11331143
surf_blit_func=surface.blit
1134-
ifbgdisnotNone:
1135-
self._bgd=bgd
1144+
ifbgsurfisnotNone:
1145+
self._bgd=bgsurf
11361146
local_bgd=self._bgd
11371147

11381148
surface.set_clip(latest_clip)
@@ -1156,21 +1166,29 @@ def draw(
11561166

11571167
# clear using background
11581168
iflocal_bgdisnotNone:
1169+
flags=0ifspecial_flagsisNoneelsespecial_flags
11591170
forrecinlocal_update:
1160-
surf_blit_func(local_bgd,rec,rec)
1171+
surf_blit_func(local_bgd,rec,rec,flags)
11611172

11621173
# 2. draw
11631174
self._draw_dirty_internal(
1164-
local_old_rect,rect_type,local_sprites,surf_blit_func,local_update
1175+
local_old_rect,
1176+
rect_type,
1177+
local_sprites,
1178+
surf_blit_func,
1179+
local_update,
1180+
special_flags,
11651181
)
11661182
local_ret=list(local_update)
11671183
else:# flip, full screen mode
11681184
iflocal_bgdisnotNone:
1169-
surf_blit_func(local_bgd, (0,0))
1185+
flags=0ifspecial_flagsisNoneelsespecial_flags
1186+
surf_blit_func(local_bgd, (0,0),None,flags)
11701187
forsprinlocal_sprites:
11711188
ifspr.visible:
1189+
flags=spr.blendmodeifspecial_flagsisNoneelsespecial_flags
11721190
local_old_rect[spr]=surf_blit_func(
1173-
spr.image,spr.rect,spr.source_rect,spr.blendmode
1191+
spr.image,spr.rect,spr.source_rect,flags
11741192
)
11751193
# return only the part of the screen changed
11761194
local_ret= [rect_type(latest_clip)]
@@ -1192,8 +1210,11 @@ def draw(
11921210
returnlocal_ret
11931211

11941212
@staticmethod
1195-
def_draw_dirty_internal(_old_rect,_rect,_sprites,_surf_blit,_update):
1213+
def_draw_dirty_internal(
1214+
_old_rect,_rect,_sprites,_surf_blit,_update,_special_flags
1215+
):
11961216
forsprin_sprites:
1217+
flags=spr.blendmodeif_special_flagsisNoneelse_special_flags
11971218
ifspr.dirty<1andspr.visible:
11981219
# sprite not dirty; blit only the intersecting part
11991220
ifspr.source_rectisnotNone:
@@ -1221,12 +1242,12 @@ def _draw_dirty_internal(_old_rect, _rect, _sprites, _surf_blit, _update):
12211242
clip[2],
12221243
clip[3],
12231244
),
1224-
spr.blendmode,
1245+
flags,
12251246
)
12261247
else:# dirty sprite
12271248
ifspr.visible:
12281249
_old_rect[spr]=_surf_blit(
1229-
spr.image,spr.rect,spr.source_rect,spr.blendmode
1250+
spr.image,spr.rect,spr.source_rect,flags
12301251
)
12311252
ifspr.dirty==1:
12321253
spr.dirty=0

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp