Skip to content

Playzone

src.components.playzone

View source code

PlayZone: a self-contained arena that owns a ball, two paddles and an AI player.

This module keeps simulation in y-down space (origin top-left). Rendering converts to y-up in draw calls of each entity.

PlayZone

The Play-zone container.

Contains unique instances of ball, paddles and player.

Updates by letting the ball, cpu paddle and player paddle make their moves and handling collisions. Updates the scores.

HEIGHT = height instance-attribute

WIDTH = width instance-attribute

ai_paddle = create_paddle(width, height, PLAYER_ACCENT, width=paddle_width) instance-attribute

ai_player = ai_player instance-attribute

ball = create_ball() instance-attribute

ball_speed_magnitude = 5.0 class-attribute instance-attribute

best_ai_fitness = best_ai_fitness instance-attribute

cpu_paddle = create_paddle(width, height, CPU_ACCENT, is_cpu=True, width=paddle_width) instance-attribute

paddle_width = paddle_width instance-attribute

sound_event_callback = sound_event_callback instance-attribute

speed = speed instance-attribute

time_running = 0 instance-attribute

__init__(width, height, speed, ai_player, best_ai_fitness=0, paddle_width=80, sound_event_callback: Callable[[str], None] | None = None)

Parameters:

Name Type Description Default
width int

Width of the play zone.

required
height int

Height of the play zone.

required
speed int

Speed scalar for movements.

required
ai_player IndividualPlayer

AI player object.

required
best_ai_fitness int

Best fitness of the AI player (for display/tuning).

0

check_collisions() -> None

Handle wall and paddle collisions, score updates, and ball respawns.

cpu_move() -> None

Move the CPU paddle to track the ball when ball is moving upwards.

handle_collision(ball, paddle, player_is_cpu=False) -> None

Respond to ball hitting a paddle: bounce and skew, update hit counter.

render_to(window_height: float) -> None

Render zone entities (delegates to their Arcade draw routines).

Parameters:

Name Type Description Default
window_height float

Current window height, used to flip y-axis for Arcade.

required

respawn_ball(ball) -> None

Respawn the ball near the vertical center with a random X.

update() -> None

Advance simulation one step: CPU, collisions, and state sync.

update_variables() -> None

Update the variables of game elements after each update.