Skip to content

Add a screen recording feature #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions examples/bouncing_balls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from typing import Unpack
from functools import partial

import pygame
from pygame.colordict import THECOLORS
from pygame import Rect, Vector2, Color

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)
with kwargs["executor"].register(draw_func, kwargs["priority"]):
async for dt in kwargs["clock"].anim_with_dt():
dest.move_ip(velocity * (dt / 1000.0))
if not dest.colliderect(space):
return
if dest.left < space.left or dest.right > space.right:
velocity.x = -velocity.x
if dest.top < space.top or dest.bottom > space.bottom:
velocity.y = -velocity.y


async def main(**kwargs: Unpack[apg.CommonParams]):
from random import randint
pygame.init()
pygame.display.set_caption("Bouncing Balls")

kwargs["draw_target"] = screen = pygame.display.set_mode((1280, 720))

r = kwargs["executor"].register
r(partial(screen.fill, THECOLORS["black"]), priority=0)
r(pygame.display.flip, priority=0xFFFFFF00)

async with apg.open_nursery() as nursery:
clock = kwargs["clock"]
priority = 0x100
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),
space=screen_rect,
color=Color(randint(0, 255), randint(0, 255), randint(0, 255)),
velocity=Vector2(randint(-150, 150), randint(-150, 150)),
priority=priority,
**kwargs
))
priority += 1


if __name__ == "__main__":
apg.run(main)
104 changes: 56 additions & 48 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ furo = "^2023.9.10"
[tool.poetry.group.invest.dependencies]
matplotlib = "^3.9.2"

[tool.poetry.group.recording.dependencies]
numpy = "^2.1.2"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Expand Down
4 changes: 2 additions & 2 deletions src/asyncpygame/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
__all__ = (
'run', 'quit', 'Clock', 'SDLEvent', 'PriorityExecutor',
'run', 'quit', 'run_and_record', 'Clock', 'SDLEvent', 'PriorityExecutor',
'CommonParams', 'capture_current_frame', 'block_input_events',
)

from asyncgui import *
from asyncgui_ext.clock import Clock
from ._runner import run, quit
from ._runner import run, quit, run_and_record
from ._sdlevent import SDLEvent
from ._priority_executor import PriorityExecutor
from ._utils import CommonParams, capture_current_frame, block_input_events
Loading
Loading