init
This commit is contained in:
commit
079dc00345
21 changed files with 1729 additions and 0 deletions
0
pygame_animation/cli/__init__.py
Normal file
0
pygame_animation/cli/__init__.py
Normal file
54
pygame_animation/cli/app.py
Normal file
54
pygame_animation/cli/app.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import typer
|
||||
|
||||
from pygame_animation.cli.ball import ball_app
|
||||
from pygame_animation.cli.common import verbose_callback
|
||||
from pygame_animation.cli.config import config_app
|
||||
from pygame_animation.cli.tui import tui_app
|
||||
|
||||
app = typer.Typer(
|
||||
name="pygame_animation",
|
||||
help="A rich terminal report for coveragepy.",
|
||||
)
|
||||
app.add_typer(config_app)
|
||||
app.add_typer(ball_app)
|
||||
app.add_typer(tui_app)
|
||||
|
||||
|
||||
def version_callback(value: bool) -> None:
|
||||
"""Callback function to print the version of the pygame-animation package.
|
||||
|
||||
Args:
|
||||
value (bool): Boolean value to determine if the version should be printed.
|
||||
|
||||
Raises:
|
||||
typer.Exit: If the value is True, the version will be printed and the program will exit.
|
||||
|
||||
Example:
|
||||
version_callback(True)
|
||||
"""
|
||||
if value:
|
||||
from pygame_animation.__about__ import __version__
|
||||
|
||||
typer.echo(f"{__version__}")
|
||||
raise typer.Exit()
|
||||
|
||||
|
||||
@app.callback()
|
||||
def main(
|
||||
version: bool = typer.Option(
|
||||
None,
|
||||
"--version",
|
||||
callback=version_callback,
|
||||
is_eager=True,
|
||||
),
|
||||
verbose: bool = typer.Option(
|
||||
False,
|
||||
callback=verbose_callback,
|
||||
help="show the log messages",
|
||||
),
|
||||
) -> None:
|
||||
return
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
typer.run(main)
|
||||
114
pygame_animation/cli/ball.py
Normal file
114
pygame_animation/cli/ball.py
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
import random
|
||||
import sys
|
||||
|
||||
import pygame
|
||||
import typer
|
||||
|
||||
from pygame_animation.cli.common import verbose_callback
|
||||
|
||||
ball_app = typer.Typer()
|
||||
|
||||
|
||||
@ball_app.callback()
|
||||
def ball(
|
||||
verbose: bool = typer.Option(
|
||||
False,
|
||||
callback=verbose_callback,
|
||||
help="run the ball annimation",
|
||||
),
|
||||
):
|
||||
"ball cli"
|
||||
|
||||
|
||||
@ball_app.command()
|
||||
def run(
|
||||
verbose: bool = typer.Option(
|
||||
False,
|
||||
callback=verbose_callback,
|
||||
help="show the log messages",
|
||||
),
|
||||
ball_radius: int = typer.Option(20),
|
||||
restitution: int = typer.Option(-2),
|
||||
gravity: int = typer.Option(5),
|
||||
friction: int = typer.Option(0.9),
|
||||
):
|
||||
|
||||
pygame.init()
|
||||
|
||||
# Set up the display
|
||||
screen_width = 640
|
||||
screen_height = 480
|
||||
screen = pygame.display.set_mode((screen_width, screen_height))
|
||||
pygame.display.set_caption("Bouncing Ball Animation")
|
||||
|
||||
# Set up the ball
|
||||
ball_color = (255, 255, 255)
|
||||
ball_x = screen_width // 2
|
||||
ball_y = screen_height // 2
|
||||
ball_speed_x = random.randint(-5, 5)
|
||||
ball_speed_y = random.randint(-5, 5)
|
||||
squishyness = 100
|
||||
|
||||
# Set up the clock
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
# Run the game loop
|
||||
while True:
|
||||
squish = 0
|
||||
# Handle events
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
|
||||
# Update the ball position
|
||||
ball_x += ball_speed_x
|
||||
ball_y += ball_speed_y
|
||||
|
||||
# Check for collisions with the walls
|
||||
if ball_x + ball_radius >= screen_width or ball_x - ball_radius <= 0:
|
||||
ball_speed_x = -ball_speed_x
|
||||
|
||||
if ball_y + ball_radius >= screen_height or ball_y - ball_radius <= 0:
|
||||
# restitution
|
||||
ball_speed_y = -ball_speed_y - restitution
|
||||
# friction
|
||||
ball_speed_x *= friction
|
||||
|
||||
if ball_y <= ball_radius:
|
||||
ball_y = ball_radius
|
||||
if ball_y >= screen_height - ball_radius:
|
||||
ball_y = screen_height - ball_radius
|
||||
if ball_x <= ball_radius:
|
||||
ball_x = ball_radius
|
||||
if ball_x >= screen_width - ball_radius:
|
||||
ball_x = screen_width - ball_radius
|
||||
|
||||
if ball_y - ball_radius <= 0:
|
||||
squish = ball_speed_y * squishyness
|
||||
|
||||
else:
|
||||
ball_speed_y += gravity
|
||||
|
||||
# Draw the ball
|
||||
screen.fill((0, 0, 0))
|
||||
if squish != 0:
|
||||
ball_width = ball_radius * (2 + squish)
|
||||
ball_height = ball_radius * (2 - squish)
|
||||
print(ball_width, ball_height)
|
||||
ball_x = ball_x - ball_width // 2
|
||||
pygame.draw.ellipse(
|
||||
screen,
|
||||
ball_color,
|
||||
(ball_x, ball_y, ball_width, ball_height),
|
||||
)
|
||||
# pygame.draw.circle(screen, ball_color, (ball_x, ball_y), ball_radius)
|
||||
|
||||
else:
|
||||
pygame.draw.circle(screen, ball_color, (ball_x, ball_y), ball_radius)
|
||||
|
||||
# Update the display
|
||||
pygame.display.flip()
|
||||
|
||||
# Wait for a little bit to control the frame rate
|
||||
clock.tick(60)
|
||||
6
pygame_animation/cli/common.py
Normal file
6
pygame_animation/cli/common.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from pygame_animation.console import console
|
||||
|
||||
|
||||
def verbose_callback(value: bool) -> None:
|
||||
if value:
|
||||
console.quiet = False
|
||||
29
pygame_animation/cli/config.py
Normal file
29
pygame_animation/cli/config.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
from rich.console import Console
|
||||
import typer
|
||||
|
||||
from pygame_animation.cli.common import verbose_callback
|
||||
from pygame_animation.config import config as configuration
|
||||
|
||||
config_app = typer.Typer()
|
||||
|
||||
|
||||
@config_app.callback()
|
||||
def config(
|
||||
verbose: bool = typer.Option(
|
||||
False,
|
||||
callback=verbose_callback,
|
||||
help="show the log messages",
|
||||
),
|
||||
):
|
||||
"configuration cli"
|
||||
|
||||
|
||||
@config_app.command()
|
||||
def show(
|
||||
verbose: bool = typer.Option(
|
||||
False,
|
||||
callback=verbose_callback,
|
||||
help="show the log messages",
|
||||
),
|
||||
):
|
||||
Console().print(configuration)
|
||||
18
pygame_animation/cli/tui.py
Normal file
18
pygame_animation/cli/tui.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import typer
|
||||
|
||||
from pygame_animation.cli.common import verbose_callback
|
||||
from pygame_animation.tui.app import run_app
|
||||
|
||||
tui_app = typer.Typer()
|
||||
|
||||
|
||||
@tui_app.callback(invoke_without_command=True)
|
||||
def i(
|
||||
verbose: bool = typer.Option(
|
||||
False,
|
||||
callback=verbose_callback,
|
||||
help="show the log messages",
|
||||
),
|
||||
):
|
||||
"interactive tui"
|
||||
run_app()
|
||||
Loading…
Add table
Add a link
Reference in a new issue