Arcade Game Development Library
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Comprehensive window lifecycle management, view system for game states, sections for viewport management, and event handling for creating robust game applications.
Main window and application classes that form the foundation of Arcade applications.
class Window(pyglet.window.Window):
"""
Main application window class that handles rendering, events, and game loop management.
Inherits from pyglet.window.Window and adds game-specific functionality.
"""
def __init__(self, width: int = 800, height: int = 600, title: str = "Arcade Window",
fullscreen: bool = False, resizable: bool = False, update_rate: float = 1/60,
antialiasing: bool = True, gl_version: tuple = (3, 3), screen: int = None,
style: str = None, visible: bool = True, vsync: bool = False,
gc_mode: str = "context_gc", center_window: bool = False,
samples: int = 4, enable_polling: bool = True):
"""
Create a new game window.
Args:
width: Window width in pixels
height: Window height in pixels
title: Window title text
fullscreen: Whether to open in fullscreen mode
resizable: Whether window can be resized
update_rate: Target frame rate (frames per second)
antialiasing: Enable multisampling antialiasing
gl_version: OpenGL version tuple (major, minor)
screen: Which screen to open window on (multi-monitor)
style: Window style ("default", "dialog", "tool")
visible: Whether window is initially visible
vsync: Enable vertical synchronization
gc_mode: Garbage collection mode ("context_gc", "auto")
center_window: Whether to center window on screen
samples: Number of samples for antialiasing
enable_polling: Enable input device polling
"""
# Window properties
width: int
height: int
title: str
fullscreen: bool
current_view: arcade.View
# Rendering properties
ctx: arcade.ArcadeContext
background_color: tuple[int, int, int, int]
# Timing properties
delta_time: float
def set_update_rate(self, rate: float) -> None:
"""Set the target update rate (FPS)."""
def set_vsync(self, vsync: bool) -> None:
"""Enable or disable vertical synchronization."""
def set_visible(self, visible: bool) -> None:
"""Show or hide the window."""
def center_window(self) -> None:
"""Center the window on the screen."""
def maximize(self) -> None:
"""Maximize the window."""
def minimize(self) -> None:
"""Minimize the window."""
def set_size(self, width: int, height: int) -> None:
"""Set window size."""
def get_size(self) -> tuple[int, int]:
"""Get current window size."""
def get_location(self) -> tuple[int, int]:
"""Get window position on screen."""
def set_location(self, x: int, y: int) -> None:
"""Set window position on screen."""
# Event handlers (override these in subclasses)
def on_draw(self) -> None:
"""Called when the window needs to be redrawn."""
def on_update(self, delta_time: float) -> None:
"""
Called to update game logic.
Args:
delta_time: Time elapsed since last update (seconds)
"""
def on_resize(self, width: int, height: int) -> None:
"""
Called when window is resized.
Args:
width: New window width
height: New window height
"""
def on_mouse_motion(self, x: int, y: int, dx: int, dy: int) -> None:
"""Called when mouse moves."""
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> None:
"""Called when mouse button is pressed."""
def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> None:
"""Called when mouse button is released."""
def on_mouse_scroll(self, x: int, y: int, scroll_x: float, scroll_y: float) -> None:
"""Called when mouse wheel is scrolled."""
def on_key_press(self, symbol: int, modifiers: int) -> None:
"""Called when keyboard key is pressed."""
def on_key_release(self, symbol: int, modifiers: int) -> None:
"""Called when keyboard key is released."""
def on_text(self, text: str) -> None:
"""Called when text is typed."""
def on_text_motion(self, motion: int) -> None:
"""Called for text cursor movement."""
def on_text_motion_select(self, motion: int) -> None:
"""Called for text selection movement."""
def clear(self, color: tuple[int, int, int, int] = None) -> None:
"""Clear the window with specified color or background color."""
def use(self) -> None:
"""Make this window the active OpenGL context."""
def flip(self) -> None:
"""Swap the OpenGL front and back buffers."""
def show_view(self, new_view: arcade.View) -> None:
"""Switch to a new view."""
def hide_view(self) -> None:
"""Hide the current view."""View classes for managing different game states and scenes.
class View:
"""
Base class for managing different screens/states in a game (menu, gameplay, pause, etc.).
Views handle their own event processing and drawing.
"""
def __init__(self):
"""Create a new view."""
window: arcade.Window
def setup(self) -> None:
"""Override to set up the view when it becomes active."""
def on_show_view(self) -> None:
"""Called when this view becomes the active view."""
def on_hide_view(self) -> None:
"""Called when this view is no longer the active view."""
def on_draw(self) -> None:
"""Override to draw the view."""
def on_update(self, delta_time: float) -> None:
"""Override to update view logic."""
def on_resize(self, width: int, height: int) -> None:
"""Called when window is resized."""
# Input event handlers
def on_mouse_motion(self, x: int, y: int, dx: int, dy: int) -> None:
"""Called when mouse moves."""
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> None:
"""Called when mouse button is pressed."""
def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> None:
"""Called when mouse button is released."""
def on_mouse_scroll(self, x: int, y: int, scroll_x: float, scroll_y: float) -> None:
"""Called when mouse wheel is scrolled."""
def on_key_press(self, symbol: int, modifiers: int) -> None:
"""Called when keyboard key is pressed."""
def on_key_release(self, symbol: int, modifiers: int) -> None:
"""Called when keyboard key is released."""
def on_text(self, text: str) -> None:
"""Called when text is typed."""
def on_text_motion(self, motion: int) -> None:
"""Called for text cursor movement."""
def on_text_motion_select(self, motion: int) -> None:
"""Called for text selection movement."""
def clear(self, color: tuple[int, int, int, int] = None) -> None:
"""Clear the screen."""Classes for managing viewport subdivisions and screen regions.
class Section:
"""
Rectangular portion of the viewport for managing different screen regions.
Useful for split-screen games, mini-maps, or UI panels.
"""
def __init__(self, left: int, bottom: int, width: int, height: int,
name: str = "Section", accept_keyboard_keys: bool = True,
accept_mouse_events: bool = True, enabled: bool = True,
local_mouse_coordinates: bool = False, prevent_dispatch: bool = True,
prevent_dispatch_view: bool = True):
"""
Create a section.
Args:
left: Left coordinate of section
bottom: Bottom coordinate of section
width: Width of section
height: Height of section
name: Name for identifying the section
accept_keyboard_keys: Whether section receives keyboard events
accept_mouse_events: Whether section receives mouse events
enabled: Whether section is active
local_mouse_coordinates: Use section-local mouse coordinates
prevent_dispatch: Prevent event propagation to other sections
prevent_dispatch_view: Prevent event propagation to views
"""
# Properties
left: int
bottom: int
width: int
height: int
name: str
enabled: bool
view: arcade.View
camera: arcade.Camera2D
def on_update(self, delta_time: float) -> None:
"""Called to update section logic."""
def on_draw(self) -> None:
"""Called to draw the section."""
def on_resize(self, width: int, height: int) -> None:
"""Called when window is resized."""
# Input event handlers
def on_mouse_motion(self, x: int, y: int, dx: int, dy: int) -> None:
"""Called when mouse moves within section."""
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> None:
"""Called when mouse is pressed within section."""
def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> None:
"""Called when mouse is released within section."""
def on_mouse_scroll(self, x: int, y: int, scroll_x: float, scroll_y: float) -> None:
"""Called when mouse wheel is scrolled within section."""
def on_key_press(self, symbol: int, modifiers: int) -> None:
"""Called when keyboard key is pressed and section has focus."""
def on_key_release(self, symbol: int, modifiers: int) -> None:
"""Called when keyboard key is released and section has focus."""
def mouse_is_on_top(self) -> bool:
"""Check if mouse is currently over this section."""
def get_xy_screen_relative(self) -> tuple[int, int]:
"""Get section position relative to screen."""
def get_xy_section_relative(self, x: int, y: int) -> tuple[int, int]:
"""Convert screen coordinates to section-relative coordinates."""
class SectionManager:
"""
Manages multiple sections within a view, handling event dispatch and rendering order.
"""
def __init__(self, view: arcade.View):
"""
Create a section manager.
Args:
view: The view that owns this section manager
"""
view: arcade.View
def add_section(self, section: arcade.Section, at_index: int = None) -> None:
"""
Add a section to the manager.
Args:
section: Section to add
at_index: Position to insert section (None = append)
"""
def remove_section(self, section: arcade.Section) -> None:
"""Remove a section from the manager."""
def clear(self) -> None:
"""Remove all sections."""
def get_section_by_name(self, name: str) -> arcade.Section:
"""Find a section by its name."""
def disable_all_keyboard_events(self) -> None:
"""Disable keyboard events for all sections."""
def enable_all_keyboard_events(self) -> None:
"""Enable keyboard events for all sections."""
def on_update(self, delta_time: float) -> None:
"""Update all enabled sections."""
def on_draw(self) -> None:
"""Draw all enabled sections."""
def on_resize(self, width: int, height: int) -> None:
"""Handle window resize for all sections."""
# Event dispatch methods
def on_mouse_motion(self, x: int, y: int, dx: int, dy: int) -> None:
"""Dispatch mouse motion events to appropriate sections."""
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> None:
"""Dispatch mouse press events to appropriate sections."""
def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> None:
"""Dispatch mouse release events to appropriate sections."""Utility functions for window creation and lifecycle management.
def open_window(width: int, height: int, title: str = "Arcade Window",
fullscreen: bool = False, resizable: bool = False,
update_rate: float = 1/60, antialiasing: bool = True) -> arcade.Window:
"""
Create and open a game window with specified parameters.
Args:
width: Window width in pixels
height: Window height in pixels
title: Window title text
fullscreen: Whether to open in fullscreen mode
resizable: Whether window can be resized
update_rate: Target frame rate (frames per second)
antialiasing: Enable multisampling antialiasing
Returns:
The created Window instance
"""
def close_window() -> None:
"""Close the current game window and clean up resources."""
def get_window() -> arcade.Window:
"""
Get reference to the current active window.
Returns:
The current Window instance or None if no window exists
"""
def set_window(window: arcade.Window) -> None:
"""
Set the current active window reference.
Args:
window: Window instance to set as current
"""
def run() -> None:
"""
Start the game loop. This will run until the window is closed.
Must be called after creating a window and setting up the initial view.
"""
def exit() -> None:
"""
Exit the application and close all windows.
"""
def get_display_size(screen_id: int = 0) -> tuple[int, int]:
"""
Get the display size of a screen.
Args:
screen_id: ID of the screen to query
Returns:
Tuple of (width, height) in pixels
"""
def get_screens() -> list:
"""
Get list of available screens/displays.
Returns:
List of screen objects with display information
"""Functions for scheduling timed events and recurring tasks.
def schedule(function_pointer: callable, interval: float) -> None:
"""
Schedule a function to be called at regular intervals.
Args:
function_pointer: Function to call (must accept delta_time parameter)
interval: Time between calls in seconds
"""
def unschedule(function_pointer: callable) -> None:
"""
Remove a scheduled function.
Args:
function_pointer: Previously scheduled function to remove
"""
def schedule_once(function_pointer: callable, delay: float) -> None:
"""
Schedule a function to be called once after a delay.
Args:
function_pointer: Function to call
delay: Delay before calling in seconds
"""Constants for mouse button and key modifier identification.
# Mouse button constants
MOUSE_BUTTON_LEFT: int = 1
MOUSE_BUTTON_MIDDLE: int = 2
MOUSE_BUTTON_RIGHT: int = 4
# Controller functions (non-headless mode only)
def get_game_controllers() -> list:
"""Get list of available game controllers."""
def get_joysticks() -> list:
"""Get list of available joystick devices."""
class ControllerManager:
"""Manages game controller input and events."""
def __init__(self):
"""Create controller manager."""
def get_controllers() -> arcade.ControllerManager:
"""Get the global controller manager instance."""import arcade
class MenuView(arcade.View):
def on_show_view(self):
arcade.set_background_color(arcade.color.DARK_BLUE_GRAY)
def on_draw(self):
self.clear()
arcade.draw_text("Main Menu", 400, 300, arcade.color.WHITE, 50, anchor_x="center")
arcade.draw_text("Press SPACE to start game", 400, 200, arcade.color.WHITE, 20, anchor_x="center")
def on_key_press(self, key, modifiers):
if key == arcade.key.SPACE:
game_view = GameView()
game_view.setup()
self.window.show_view(game_view)
class GameView(arcade.View):
def __init__(self):
super().__init__()
self.player_list = None
def setup(self):
arcade.set_background_color(arcade.color.AMAZON)
self.player_list = arcade.SpriteList()
# Create player
self.player = arcade.Sprite(":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png", 0.5)
self.player.center_x = 400
self.player.center_y = 300
self.player_list.append(self.player)
def on_draw(self):
self.clear()
self.player_list.draw()
# Draw UI
arcade.draw_text(f"Position: ({self.player.center_x:.0f}, {self.player.center_y:.0f})",
10, 550, arcade.color.WHITE, 16)
def on_update(self, delta_time):
self.player_list.update()
def on_key_press(self, key, modifiers):
if key == arcade.key.ESCAPE:
menu_view = MenuView()
self.window.show_view(menu_view)
elif key == arcade.key.UP:
self.player.change_y = 5
elif key == arcade.key.DOWN:
self.player.change_y = -5
elif key == arcade.key.LEFT:
self.player.change_x = -5
elif key == arcade.key.RIGHT:
self.player.change_x = 5
def on_key_release(self, key, modifiers):
if key in (arcade.key.UP, arcade.key.DOWN):
self.player.change_y = 0
elif key in (arcade.key.LEFT, arcade.key.RIGHT):
self.player.change_x = 0
def main():
window = arcade.Window(800, 600, "Game with Views")
# Start with menu view
menu_view = MenuView()
window.show_view(menu_view)
arcade.run()
if __name__ == "__main__":
main()import arcade
class SplitScreenView(arcade.View):
def __init__(self):
super().__init__()
self.section_manager = arcade.SectionManager(self)
def setup(self):
# Create left section for player 1
left_section = PlayerSection(0, 0, 400, 600, "Player 1")
left_section.player_x = 100
left_section.player_y = 300
self.section_manager.add_section(left_section)
# Create right section for player 2
right_section = PlayerSection(400, 0, 400, 600, "Player 2")
right_section.player_x = 300
right_section.player_y = 300
self.section_manager.add_section(right_section)
def on_draw(self):
self.clear()
self.section_manager.on_draw()
# Draw divider line
arcade.draw_line(400, 0, 400, 600, arcade.color.WHITE, 2)
def on_update(self, delta_time):
self.section_manager.on_update(delta_time)
def on_key_press(self, key, modifiers):
self.section_manager.on_key_press(key, modifiers)
def on_key_release(self, key, modifiers):
self.section_manager.on_key_release(key, modifiers)
class PlayerSection(arcade.Section):
def __init__(self, left, bottom, width, height, name):
super().__init__(left, bottom, width, height, name,
accept_keyboard_keys=True, prevent_dispatch=False)
self.player_x = 200
self.player_y = 300
self.player_speed = 5
def on_draw(self):
# Draw player in section coordinates
arcade.draw_circle_filled(self.player_x, self.player_y, 20, arcade.color.RED)
arcade.draw_text(self.name, 10, self.height - 30, arcade.color.WHITE, 16)
def on_key_press(self, key, modifiers):
if self.name == "Player 1":
if key == arcade.key.W:
self.player_y = min(self.height - 20, self.player_y + self.player_speed)
elif key == arcade.key.S:
self.player_y = max(20, self.player_y - self.player_speed)
elif key == arcade.key.A:
self.player_x = max(20, self.player_x - self.player_speed)
elif key == arcade.key.D:
self.player_x = min(self.width - 20, self.player_x + self.player_speed)
elif self.name == "Player 2":
if key == arcade.key.UP:
self.player_y = min(self.height - 20, self.player_y + self.player_speed)
elif key == arcade.key.DOWN:
self.player_y = max(20, self.player_y - self.player_speed)
elif key == arcade.key.LEFT:
self.player_x = max(20, self.player_x - self.player_speed)
elif key == arcade.key.RIGHT:
self.player_x = min(self.width - 20, self.player_x + self.player_speed)
def main():
window = arcade.Window(800, 600, "Split Screen Game")
view = SplitScreenView()
view.setup()
window.show_view(view)
arcade.run()
if __name__ == "__main__":
main()Install with Tessl CLI
npx tessl i tessl/pypi-arcade