Skip to content

FadeTransitionで任意の画像を使えるようにする #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/asyncpygame/scene_switcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,30 @@ class FadeTransition:

switcher.switch_to(next_scene, FadeTransition())
'''
def __init__(self, *, overlay_color='black', out_duration=300, in_duration=300, interval=100):
def __init__(self, *, overlay_color='black', overlay_image=None, out_duration=300, in_duration=300, interval=100):
'''
:param overlay_image: If specified, this image will be used instead of the ``overlay_color``.
:param out_duration: The duration of the fadeout animation.
:param in_duration: The duration of the fadein animation.
:param interval: The interval between the fadeout animation and the fadein animation.

.. versionchanged:: 0.2.0
Added the ``overlay_image`` parameter.
'''
self.overlay_color = overlay_color
self._overlay_color = overlay_color
self._overlay_image = overlay_image
self.out_duration = out_duration
self.in_duration = in_duration
self.interval = interval

async def __call__(self, *, priority, draw_target, clock, executor, **kwargs):
overlay_surface = draw_target.copy()
overlay_surface.fill(self.overlay_color)
set_alpha = overlay_surface.set_alpha
with executor.register(partial(draw_target.blit, overlay_surface), priority=priority):
if (img := self._overlay_image) is None:
img = draw_target.copy()
img.fill(self._overlay_color)
self._overlay_image = img
set_alpha = img.set_alpha
target_center = draw_target.get_rect().center
with executor.register(partial(draw_target.blit, img, img.get_rect(center=target_center)), priority=priority):
async for v in clock.interpolate(0, 255, duration=self.out_duration):
set_alpha(v)
yield
Expand Down
Loading