API Reference¶
- run(main_func, *, fps=30, auto_quit=True)¶
- quit(*args)¶
- class PriorityExecutor¶
Calls registered functions in the order of their priorities.
executor = PriorityExecutor() values = [] executor.register(lambda: values.append('A'), priority=2) executor.register(lambda: values.append('B'), priority=0) executor() assert values == ['B', 'A', ] values.clear() executor.register(lambda: values.append('C'), priority=1) executor() assert values == ['B', 'C', 'A', ]
The
asyncpygame.run()
creates an instance of this class and calls its__call__()
every frame.- register(func, priority=0) ExecutionRequest ¶
- class SDLEvent¶
# Waits for any mouse button to be pressed. e = await sdlevent.wait(MOUSEBUTTONDOWN) # Waits for any mouse button to be pressed or released. e = await sdlevent.wait(MOUSEBUTTONDOWN, MOUSEBUTTONUP) # Waits for the left mouse button to be pressed. e = await sdlevent.wait(MOUSEBUTTONDOWN, filter=lambda e: e.button == 1)
- wait(*event_types, filter=<function _accept_any>, priority=0, consume=False) Awaitable[Event] ¶
Waits for any of the specified types of events to occur.
- wait_freq(*event_types, filter=<function _accept_any>, priority=0, consume=False)¶
MOUSEMOTION
やFINGERMOTION
などの頻りに起こりうるイベントを効率良く捌けるかもしれない機能。 以下のようなコードはwhile True: e = await sdlevent.wait(FINGERMOTION) ...
以下のように書き換える事で効率が良くなるかもしれません。
async with sdlevent.wait_freq(FINGERMOTION) as finger_motion: while True: e = await finger_motion() ...
ただ代償としてコードが読みにくくなるうえ逆に効率が悪くなる可能性すらあるので人間が感知できる程の改善がみられない限りは使用を控えてください。 また多くの状況では
FINGERUP
が起きた時にループを抜けたいでしょうから典型的な用例は以下のようになります。async with ( asyncpygame.move_on_when(sdlevent.wait(FINGERUP)), sdlevent.wait_freq(FINGERMOTION) as finger_motion, ): while True: e = await finger_motion() ...
注意点としてas節で得た関数(上のコードでいう
finger_motion
)は必ずそのwithブロック内で直に用いるようにしてください。 以下の様に別の関数に渡したり、戻り値を別の関数に渡してはいけません。async with sdlevent.wait_freq(FINGERMOTION) as finger_motion: another_func(finger_motion) # NOT ALLOWED another_func(finger_motion()) # NOT ALLOWED
(sub module) scene_switcher¶
- class FadeTransition(*, overlay_color='black', out_duration=300, in_duration=300, interval=100)¶
scene_switcher.switch_to(next_scene, FadeTransition())
- Parameters:
out_duration – The duration of the fadeout animation.
in_duration – The duration of the fadein animation.
interval – The interval between the fadeout animation and the fadein animation.
- class SceneSwitcher¶
- async run(first_scene, *, userdata: Any = None, priority, sdlevent, **kwargs)¶
- Parameters:
userdata – Use this to share data between scenes without relying on global variables.
- switch_to(next_scene, transition: ~collections.abc.Callable[[...], ~collections.abc.AsyncGenerator[None, None]] = <function no_transition>)¶
Instructs the scene switcher to transition to another scene. Calling this method during an ongoing transition will have no effect.
- class SlideTransition(*, direction='right', duration=800)¶
scene_switcher.switch_to(next_scene, SlideTransition())
- Parameters:
direction – ‘right’, ‘left’, ‘up’ or ‘down’.
duration – The duration of the sliding animation.
- Transition¶
Transition
is acollections.abc.Callable
that returns an async generator that yields twice. It defines a transition between two scenes.async def my_transition(**common_params): # 1st part yield # 2nd part yield # 3rd part scene_switcher.switch_to(next_scene, my_transition)
When your app switches from one scene to another, the transition between them will proceed as follows:
The 1st part of the transition and the current scene run concurrently.
The current scene gets cancelled.
The 2nd part of the transition runs.
The next scene starts.
The 3rd part of the transition and the next scene run concurrently.
time ------------------------------------------------------> ----------------------| |------------------------ current scene | | next scene ----------------------| |------------------------ |----------|----------|----------| | 1st part | 2nd part | 3rd part | |----------|----------|----------|
alias of
Callable
[[…],AsyncGenerator
[None
,None
]]
- async no_transition(**common_params)¶
scene_switcher.switch_to(next_scene, no_transition)