From 3ccbf6ffba2d1d785e1dbde9dfc68a21f37cc0d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natt=C5=8Dsai=20Mit=C5=8D?= Date: Thu, 26 Dec 2024 11:03:03 +0900 Subject: [PATCH 1/4] optimize bouncing_balls.py --- examples/bouncing_balls.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/examples/bouncing_balls.py b/examples/bouncing_balls.py index 72b3d32..5a0517e 100644 --- a/examples/bouncing_balls.py +++ b/examples/bouncing_balls.py @@ -3,13 +3,29 @@ import pygame from pygame.colordict import THECOLORS -from pygame import Rect, Vector2, Color +from pygame import Rect, Vector2, Color, Surface import asyncpygame as apg -async def bouncing_ball(*, dest: Rect, space: Rect, color, velocity: Vector2, **kwargs: Unpack[apg.CommonParams]): - draw_func = partial(pygame.draw.ellipse, kwargs["draw_target"], color, dest) +BLACK = Color("black") +WHITE = Color("white") + + +def generate_ball_image(*, color, size) -> Surface: + img = Surface((size, size)) + bgcolor = WHITE if color == BLACK else BLACK + img.fill(bgcolor) + pygame.draw.ellipse(img, color, img.get_rect()) + img = img.convert() + img.set_colorkey(bgcolor) + return img + + +async def bouncing_ball(*, initial_pos, size: tuple, space: Rect, color, velocity: Vector2, **kwargs: Unpack[apg.CommonParams]): + ball_img = generate_ball_image(color=color, size=size) + dest = ball_img.get_rect(center=initial_pos) + draw_func = partial(kwargs["draw_target"].blit, ball_img, dest) with kwargs["executor"].register(draw_func, kwargs["priority"]): async for dt in kwargs["clock"].anim_with_dt(): dest.move_ip(velocity * (dt / 1000.0)) @@ -38,9 +54,9 @@ async def main(**kwargs: Unpack[apg.CommonParams]): screen_rect = screen.get_rect() while True: await clock.sleep(randint(1000, 2000)) - ball_size = randint(20, 150) nursery.start(bouncing_ball( - dest=Rect(0, 0, ball_size, ball_size).move_to(center=screen_rect.center), + initial_pos=screen_rect.center, + size=randint(20, 150), space=screen_rect, color=Color(randint(0, 255), randint(0, 255), randint(0, 255)), velocity=Vector2(randint(-150, 150), randint(-150, 150)), From 0a223d405ce6e89ea17e013372562f05b064d32f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natt=C5=8Dsai=20Mit=C5=8D?= Date: Thu, 26 Dec 2024 11:09:12 +0900 Subject: [PATCH 2/4] update asyncgui-ext-clock --- examples/_uix/modal_dialog.py | 4 ++-- examples/whack_a_human.py | 6 +++--- poetry.lock | 6 +++--- src/asyncpygame/scene_switcher.py | 6 +++--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/_uix/modal_dialog.py b/examples/_uix/modal_dialog.py index d322847..93c690a 100644 --- a/examples/_uix/modal_dialog.py +++ b/examples/_uix/modal_dialog.py @@ -18,7 +18,7 @@ @asynccontextmanager async def darken(*, priority, **kwargs: Unpack[CommonParams]): - interpolate = kwargs["clock"].interpolate_scalar + interpolate = kwargs["clock"].interpolate draw_target = kwargs["draw_target"] overlay_surface = Surface(draw_target.size) set_alpha = overlay_surface.set_alpha @@ -32,7 +32,7 @@ async def darken(*, priority, **kwargs: Unpack[CommonParams]): async def move_rects_vertically(clock: Clock, rects, movement, duration): org_ys = tuple(rect.y for rect in rects) - async for v in clock.interpolate_scalar(0, movement, duration=duration): + async for v in clock.interpolate(0, movement, duration=duration): for rect, org_y in zip(rects, org_ys): rect.y = org_y + v diff --git a/examples/whack_a_human.py b/examples/whack_a_human.py index 2321b89..a960b2a 100644 --- a/examples/whack_a_human.py +++ b/examples/whack_a_human.py @@ -324,7 +324,7 @@ async def pop_out_enemy( sounds["hit"].play() plus_one = with_isolated_alpha(images["plus_one"]) with register(partial(draw_target.blit, plus_one, plus_one.get_rect(midtop=enemy_dest.midtop).move(0, 30)), priority + 0xFFFF0000): - async for v in clock.interpolate_scalar(255, 0, duration=500): + async for v in clock.interpolate(255, 0, duration=500): enemy_img.set_alpha(v) plus_one.set_alpha(v) else: @@ -403,7 +403,7 @@ async def pop_out_ally( await sleep(1000 * speed) if hit_tracker.finished: sounds["hit"].play() - async for v in clock.interpolate_scalar(255, 0, duration=500): + async for v in clock.interpolate(255, 0, duration=500): ally_img.set_alpha(v) else: score.value += 3 @@ -414,7 +414,7 @@ async def pop_out_ally( draw_ally_req.callback = partial(draw_target.blit, ally_img, ally_dest, ally_clip) plus_three = with_isolated_alpha(images["plus_three"]) with register(partial(draw_target.blit, plus_three, plus_three.get_rect(midbottom=ally_dest.midtop)), priority + 0xFFFF0000): - async for v in clock.interpolate_scalar(255, 0, duration=500): + async for v in clock.interpolate(255, 0, duration=500): plus_three.set_alpha(v) await apg.wait_all( anim_attrs(ally_dest, top=ally_dest.bottom, duration=500 * speed), diff --git a/poetry.lock b/poetry.lock index 7b02b4a..b38dba4 100644 --- a/poetry.lock +++ b/poetry.lock @@ -27,13 +27,13 @@ exceptiongroup = {version = ">=1.0.4,<2.0.0", markers = "python_version < \"3.11 [[package]] name = "asyncgui-ext-clock" -version = "0.5.1" +version = "0.5.2" description = "An event scheduler for asyncgui programs" optional = false python-versions = "<4.0,>=3.10" files = [ - {file = "asyncgui_ext_clock-0.5.1-py3-none-any.whl", hash = "sha256:511ebdfaf644f2f3e30b6c9656345c0781c994b0d63ce7b51dbd7a098a164a4e"}, - {file = "asyncgui_ext_clock-0.5.1.tar.gz", hash = "sha256:275950dbcc0d7145c34f026f49aaf70d76e877223b673be38d2076c6d286fcfb"}, + {file = "asyncgui_ext_clock-0.5.2-py3-none-any.whl", hash = "sha256:c857ec6f52b1d8471b9654989e27d57e6bfec97214e36bfc7a470c0feed2047e"}, + {file = "asyncgui_ext_clock-0.5.2.tar.gz", hash = "sha256:22c6efc8d8da6194e500e5f190ad978352a57e1e4289beab4059a2421a25b6b3"}, ] [package.dependencies] diff --git a/src/asyncpygame/scene_switcher.py b/src/asyncpygame/scene_switcher.py index 4d4f2c1..6e85542 100644 --- a/src/asyncpygame/scene_switcher.py +++ b/src/asyncpygame/scene_switcher.py @@ -124,12 +124,12 @@ async def __call__(self, *, priority, draw_target, clock, executor, **kwargs): overlay_surface.fill(self.overlay_color) set_alpha = overlay_surface.set_alpha with executor.register(partial(draw_target.blit, overlay_surface), priority=priority): - async for v in clock.interpolate_scalar(0, 255, duration=self.out_duration): + async for v in clock.interpolate(0, 255, duration=self.out_duration): set_alpha(v) yield await clock.sleep(self.interval) yield - async for v in clock.interpolate_scalar(255, 0, duration=self.in_duration): + async for v in clock.interpolate(255, 0, duration=self.in_duration): set_alpha(v) @@ -191,7 +191,7 @@ async def __call__(self, *, priority, draw_target, clock, executor, **kwargs): executor.register(scroll_inst, priority), executor.register(partial(draw_target.blit, frame1, dest1), priority + 1), ): - async for v in clock.interpolate_scalar(0, end_pos1, duration=self.duration): + async for v in clock.interpolate(0, end_pos1, duration=self.duration): v = int_(v) if is_vertical: dest1.y = v From b13e6ed7916529932af0808234a781ce923b0f39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natt=C5=8Dsai=20Mit=C5=8D?= Date: Thu, 26 Dec 2024 11:10:29 +0900 Subject: [PATCH 3/4] fix typo --- tests/test_the_python_language.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_the_python_language.py b/tests/test_the_python_language.py index 28f5a2c..b0695c4 100644 --- a/tests/test_the_python_language.py +++ b/tests/test_the_python_language.py @@ -1,7 +1,7 @@ import pytest -def test_close_a_full_consumed_async_generator_1(): +def test_close_a_fully_consumed_async_generator_1(): async def create_agen(): yield @@ -16,7 +16,7 @@ async def async_fn(agen): async_fn(create_agen()).send(None) -def test_close_a_full_consumed_async_generator_2(): +def test_close_a_fully_consumed_async_generator_2(): async def create_agen(): return yield From 2334573c23dedec39e7611e4a3b05dada923d83d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natt=C5=8Dsai=20Mit=C5=8D?= Date: Thu, 26 Dec 2024 11:18:45 +0900 Subject: [PATCH 4/4] =?UTF-8?q?SDLEvent=E3=81=AEpriority=E5=BC=95=E6=95=B0?= =?UTF-8?q?=E3=82=92=E5=B0=91=E3=81=97=E8=A7=A3=E8=AA=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/asyncpygame/_sdlevent.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/asyncpygame/_sdlevent.py b/src/asyncpygame/_sdlevent.py index e5e1bce..2ae9f19 100644 --- a/src/asyncpygame/_sdlevent.py +++ b/src/asyncpygame/_sdlevent.py @@ -96,6 +96,10 @@ class SDLEvent: # Waits for the left mouse button to be pressed. e = await sdlevent.wait(MOUSEBUTTONDOWN, filter=lambda e: e.button == 1) + + .. note:: + + ``priority`` 引数は ``PriorityExecutor`` の物とは逆に働きます。すなわち大きい値で ``wait()`` しているタスクほと早く再開します。 ''' def __init__(self):