Skip to content

Commit

Permalink
3rd try
Browse files Browse the repository at this point in the history
  • Loading branch information
gottadiveintopython authored Jan 23, 2024
1 parent 0898892 commit 85c9b18
Show file tree
Hide file tree
Showing 34 changed files with 620 additions and 1,713 deletions.
25 changes: 1 addition & 24 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,6 @@ on:
branches: [ main ]

jobs:
pygame:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.10', '3.11']
env:
SDL_VIDEODRIVER: dummy
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install pytest flake8 pygame-ce==2.3.2 "asyncgui>=0.6,<0.7"
python -m pip install .
- name: Lint with flake8
run: make style
- name: Test with pytest
run: make test

pygame-ce:
runs-on: ubuntu-latest
strategy:
Expand All @@ -49,7 +26,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install pytest flake8 pygame "asyncgui>=0.6,<0.7"
python -m pip install pytest flake8 "pygame-ce==2.4.0" "asyncgui>=0.6,<0.7"
python -m pip install .
- name: Lint with flake8
run: make style
Expand Down
16 changes: 4 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
# AsyncPygame

```python
import asyncpygame as ap
https://youtu.be/O_AF4xWb0ok

# Waits for a 1 second
await ap.sleep(1000)

# Waits for a mouse button to be pressed
event = await ap.sdl_event(filter=lambda event: event.type == MOUSEBUTTONDOWN)
```

(This library is not at a level where it can be used in a production environment.)
(This library is in a **highly experimental state**. Please refrain from using it in a production environment.)

## Tested on

- CPython 3.10 + pygame-ce 2.3.2
- CPython 3.11 + pygame-ce 2.3.2
- CPython 3.10 + pygame-ce 2.4.0
- CPython 3.11 + pygame-ce 2.4.0
54 changes: 38 additions & 16 deletions examples/coundown.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
import pygame
import pygame.font
from pygame.colordict import THECOLORS as COLORS
import asyncpygame as ap


async def countdown(*, count_from=3):
async def countdown(*, clock: ap.Clock, draw_target: pygame.Surface, count_from=3):
pygame_display_flip = pygame.display.flip
font = pygame.font.SysFont(None, 400)
fg_color = pygame.Color("white")
img = pygame.Surface((0, 0))
bgcolor = COLORS["black"]
fgcolor = COLORS["white"]
center = draw_target.get_rect().center

def draw(draw_target: pygame.Surface):
rect = img.get_rect()
rect.center = draw_target.get_rect().center
draw_target.blit(img, rect)
for i in range(count_from, -1, -1):
draw_target.fill(bgcolor)
img = font.render(str(i), True, fgcolor)
img_rect = img.get_rect()
img_rect.center = center
draw_target.blit(img, img_rect)
pygame_display_flip()
await clock.sleep(1000)

with ap.DrawingRequest(draw):
for i in range(count_from, -1, -1):
img = font.render(str(i), True, fg_color).convert_alpha()
await ap.sleep(1000)
await ap.sleep_forever()


if __name__ == "__main__":
def 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, bgcolor=pygame.Color("black"))

clock = ap.Clock()
root_task = ap.start(countdown(clock=clock, draw_target=screen, count_from=5))

# LOAD_FAST
QUIT = pygame.QUIT
pygame_event_get = pygame.event.get
pygame_clock_tick = pygame.Clock().tick

alive = True
while alive:
for event in pygame_event_get():
if event.type == QUIT:
alive = False
dt = pygame_clock_tick(30)
clock.tick(dt)

root_task.cancel()
pygame.quit()


if __name__ == "__main__":
main()
32 changes: 0 additions & 32 deletions examples/fade_transition.py

This file was deleted.

52 changes: 52 additions & 0 deletions examples/loop_animation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pygame
import pygame.font
from pygame.colordict import THECOLORS as COLORS
import asyncpygame as ap


async def loop_animation(clock: ap.Clock, draw_target: pygame.Surface, group: pygame.sprite.Group, **kwargs):
font = pygame.font.SysFont(None, 200)
sprite = pygame.sprite.Sprite(group)
sprite.image = font.render("(-.-)", True, COLORS["white"]).convert_alpha()
sprite.rect = src_rect = sprite.image.get_rect()
dst_rect = draw_target.get_rect()

while True:
await clock.anim_attrs(src_rect, duration=1000, right=dst_rect.right)
await clock.anim_attrs(src_rect, duration=1000, bottom=dst_rect.bottom)
await clock.anim_attrs(src_rect, duration=1000, x=dst_rect.x)
await clock.anim_attrs(src_rect, duration=1000, y=dst_rect.y)


def main():
pygame.init()
screen = pygame.display.set_mode((1280, 720), pygame.FULLSCREEN)

clock = ap.Clock()
group = pygame.sprite.Group()
root_task = ap.start(loop_animation(clock=clock, draw_target=screen, group=group))

# LOAD_FAST
QUIT = pygame.QUIT
pygame_display_flip = pygame.display.flip
pygame_event_get = pygame.event.get
pygame_clock_tick = pygame.Clock().tick
bgcolor = COLORS["black"]

alive = True
while alive:
for event in pygame_event_get():
if event.type == QUIT:
alive = False
dt = pygame_clock_tick(30)
clock.tick(dt)
screen.fill(bgcolor)
group.draw(screen)
pygame_display_flip()

root_task.cancel()
pygame.quit()


if __name__ == "__main__":
main()
43 changes: 0 additions & 43 deletions examples/move_on_after.py

This file was deleted.

64 changes: 0 additions & 64 deletions examples/overlapping_buttons.py

This file was deleted.

66 changes: 66 additions & 0 deletions examples/skippable_animation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from functools import partial
import pygame
import pygame.font
from pygame.colordict import THECOLORS as COLORS
import asyncpygame as ap


async def skippable_animation(
clock: ap.Clock, dispatcher: ap.SDLEventDispatcher, draw_target: pygame.Surface, group: pygame.sprite.Group,
**kwargs):
font = pygame.font.SysFont(None, 200)
sprite = pygame.sprite.Sprite(group)
sprite.image = font.render("(-.-)", True, COLORS["white"]).convert_alpha()
sprite.rect = src_rect = sprite.image.get_rect()
dst_rect = draw_target.get_rect()

mouse_button_down = partial(dispatcher.wait_sdl_event, filter=lambda e: e.type == pygame.MOUSEBUTTONDOWN)
move_sprite = partial(clock.anim_attrs, src_rect, duration=1000)

while True:
await ap.wait_any(mouse_button_down(), move_sprite(right=dst_rect.right))
src_rect.right = dst_rect.right
await ap.wait_any(mouse_button_down(), move_sprite(bottom=dst_rect.bottom))
src_rect.bottom = dst_rect.bottom
await ap.wait_any(mouse_button_down(), move_sprite(x=dst_rect.x))
src_rect.x = dst_rect.x
await ap.wait_any(mouse_button_down(), move_sprite(y=dst_rect.y))
src_rect.y = dst_rect.y


def main():
pygame.init()
pygame.display.set_caption("touch the screen")
screen = pygame.display.set_mode((1280, 720))

clock = ap.Clock()
dispatcher = ap.SDLEventDispatcher()
group = pygame.sprite.Group()
root_task = ap.start(skippable_animation(clock=clock, dispatcher=dispatcher, draw_target=screen, group=group))

# LOAD_FAST
QUIT = pygame.QUIT
pygame_display_flip = pygame.display.flip
pygame_event_get = pygame.event.get
pygame_clock_tick = pygame.Clock().tick
bgcolor = COLORS["black"]

alive = True
while alive:
for event in pygame_event_get():
if event.type == QUIT:
alive = False
else:
dispatcher.dispatch(event)
dt = pygame_clock_tick(60)
clock.tick(dt)
screen.fill(bgcolor)
group.draw(screen)
pygame_display_flip()

root_task.cancel()
pygame.quit()


if __name__ == "__main__":
main()
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ packages = [
[tool.poetry.dependencies]
python = "^3.10"
asyncgui = "~0.6"
pygame-ce = "^2.3.2"
pygame-ce = "^2.4.0"
asyncgui-ext-clock = "~0.1"

[tool.poetry.group.dev.dependencies]
pytest = "^7.4.3"
Expand Down
Loading

0 comments on commit 85c9b18

Please sign in to comment.