-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0898892
commit 85c9b18
Showing
34 changed files
with
620 additions
and
1,713 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.