Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gottadiveintopython committed Dec 21, 2023
1 parent 01acd7d commit 4e35846
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 226 deletions.
30 changes: 30 additions & 0 deletions examples/coundown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pygame
import pygame.font
import asyncpygame as ap


async def countdown(*, count_from=3):
font = pygame.font.SysFont(None, 400)
fg_color = pygame.Color("white")
img = pygame.Surface((0, 0))

def draw(draw_target: pygame.Surface):
rect = img.get_rect()
rect.center = draw_target.get_rect().center
draw_target.blit(img, rect)

req = ap.DrawingRequest(draw)
try:
for i in range(count_from, -1, -1):
img = font.render(str(i), True, fg_color)
await ap.sleep(1000)
await ap.sleep_forever()
finally:
req.visible = False


if __name__ == "__main__":
pygame.init()
pygame.display.set_caption("Countdown")
screen = pygame.display.set_mode((400, 400))
ap.run(countdown(count_from=5), fps=20, draw_target=screen, bg_color=pygame.Color("black"))
54 changes: 0 additions & 54 deletions examples/coundown_ver_async.py

This file was deleted.

55 changes: 0 additions & 55 deletions examples/coundown_ver_callback.py

This file was deleted.

5 changes: 0 additions & 5 deletions examples/import_test.py

This file was deleted.

9 changes: 4 additions & 5 deletions src/asyncpygame/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
__all__ = (
'progress', 'dispatch_event', 'draw',
'init', 'run',
'DEFAULT_PRIORITY', 'DEFAULT_ZORDER', 'STOP_DISPATCHING',
'sleep', 'move_on_after',
'anim_with_dt', 'anim_with_dt_et', 'anim_with_et', 'anim_with_ratio', 'anim_with_dt_et_ratio',
'run_in_thread', 'run_in_executor',
'sdl_event', 'sdl_frequent_event',
'VisibleEntity',
'DrawingRequest',
)

from asyncgui import *
from asyncpygame_internal.constants import DEFAULT_PRIORITY, DEFAULT_ZORDER, STOP_DISPATCHING
from ._api_facade import (
progress, dispatch_event, draw,
sleep, move_on_after,
anim_with_dt, anim_with_dt_et, anim_with_et, anim_with_ratio, anim_with_dt_et_ratio,
sdl_event, sdl_frequent_event,
run_in_thread, run_in_executor,
VisibleEntity,
DrawingRequest,
)
from . import _replace_facades_with_real_ones
from ._runner import init, run
63 changes: 15 additions & 48 deletions src/asyncpygame/_api_facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,6 @@
from asyncpygame_internal.priority_drawing import DrawFunc


def progress(delta_time: TimeUnit):
'''
You need to notify ``asyncpygame`` of the time elapsed since the previous frame in your main loop.
.. code-block::
# in your main loop
dt = clock.tick(fps)
asyncpygame.progress(dt)
'''


def dispatch_event(event: Event):
'''
You need to notify ``asyncpygame`` of events if you want to use event-related ``asyncpygame`` APIs, such as
:func:`asyncpygame.sdl_event` and :func:`asyncpygame.sdl_frequent_event`.
.. code-block::
for event in pygame.event.get():
asyncpygame.dispatch_event(event)
'''


def draw(draw_target: Surface):
'''
You need to call this function if you want to draw something through drawing-related ``asyncpygame`` APIs, such as
:class:`asyncpygame.VisibleEntity`.
.. code-block::
screen.fill(...)
asyncpygame.draw(screen)
pygame.display.flip()
'''


def sleep(duration) -> Awaitable[None]:
'''
Waits for a specified period of time (in milli seconds).
Expand Down Expand Up @@ -255,31 +218,29 @@ def filter(event, allowed_list=(MOUSEMOTION, MOUSEBUTTONUP)):
'''


class VisibleEntity:
class DrawingRequest:
'''
画面に何か表示したい時に用いるクラス。
このクラスの意義は ``zorder`` による順番を考慮した描画にあります。
そのような機能が要らないのであれば特に使う必要はありません。
.. code-block::
class Sprite(VisibleEntity):
def draw(self, draw_target: Surface):
...
def draw(draw_target: Surface):
...
sprite = Sprite(zorder=10)
req = DrawingRequest(draw, zorder=200)
また以下のように継承せずに使うこともできます
``visible`` は勿論 ``zorder`` と ``draw`` も後からいつでも変更可です
.. code-block::
def draw(draw_target: Surface):
...
entity = VisibleEntity(draw=draw)
req.zorder = 100
req.draw = another_func
req.visiable = False
'''

def __init__(self, *, zorder=DEFAULT_ZORDER, visible=True, draw: DrawFunc=None):
def __init__(self, draw, /, *, zorder=DEFAULT_ZORDER, visible=True):
...

@property
Expand All @@ -288,6 +249,12 @@ def zorder(self) -> int:
描画順。この値が小さい者ほど先に描画を行う。値が同じ者同士の順は未定義。
'''

@property
def draw(self) -> DrawFunc:
'''
実際に描画を行う関数。
'''

@property
def visible(self) -> bool:
'''
Expand Down
38 changes: 0 additions & 38 deletions src/asyncpygame/_replace_facades_with_real_ones.py

This file was deleted.

Loading

0 comments on commit 4e35846

Please sign in to comment.