Window and View#
arcade.close_window#
arcade.create_orthogonal_projection#
- arcade.create_orthogonal_projection(left: float, right: float, bottom: float, top: float, near: float = 1, far: float = - 1) pyglet.math.Mat4 [source]#
Creates an orthogonal projection matrix. Used internally with the OpenGL shaders. It creates the same matrix as the deprecated/removed
glOrtho
OpenGL function.- Parameters
left (float) – The left of the near plane relative to the plane’s center.
right (float) – The right of the near plane relative to the plane’s center.
top (float) – The top of the near plane relative to the plane’s center.
bottom (float) – The bottom of the near plane relative to the plane’s center.
near (float) – The distance of the near plane from the camera’s origin. It is recommended that the near plane is set to 1.0 or above to avoid rendering issues at close range.
far (float) – The distance of the far plane from the camera’s origin.
- Returns
A projection matrix representing the specified orthogonal perspective.
- Return type
arcade.exit#
arcade.finish_render#
arcade.get_display_size#
arcade.get_projection#
- arcade.get_projection() pyglet.math.Mat4 [source]#
Returns the current projection matrix used by sprites and shapes in arcade.
This is a shortcut for
`window.ctx.projection_2d_matrix
.- Returns
Projection matrix
- Return type
Mat4
arcade.get_scaling_factor#
- arcade.get_scaling_factor(window: Window = None) float [source]#
Gets the scaling factor of the given Window. This is the ratio between the window and framebuffer size. If no window is supplied the currently active window will be used.
- Parameters
window (Window) – Handle to window we want to get scaling factor of.
- Returns
Scaling factor. E.g., 2.0 would indicate the framebuffer width and height being 2.0 times the window width and height. This means one “window pixel” is actual a 2 x 2 square of pixels in the framebuffer.
- Return type
arcade.get_viewport#
arcade.get_window#
arcade.pause#
- arcade.pause(seconds: numbers.Number) None [source]#
Pause for the specified number of seconds. This is a convenience function that just calls time.sleep().
Warning
This is mostly used for unit tests and is not likely to be a good solution for pausing an application or game.
- Parameters
seconds (float) – Time interval to pause in seconds.
arcade.run#
arcade.schedule#
- arcade.schedule(function_pointer: Callable, interval: numbers.Number)[source]#
Schedule a function to be automatically called every
interval
seconds. The function/callable needs to take a delta time argument similar toon_update
. This is a float representing the number of seconds since it was scheduled or called.A function can be scheduled multiple times, but this is not recommended.
Warning
Scheduled functions should always be unscheduled using
arcade.unschedule()
. Having lingering scheduled functions will lead to crashes.Example:
def some_action(delta_time): print(delta_time) # Call the function every second arcade.schedule(some_action, 1) # Unschedule
- Parameters
function_pointer (Callable) – Pointer to the function to be called.
interval (Number) – Interval to call the function (float or integer)
arcade.set_background_color#
- arcade.set_background_color(color: Union[Tuple[int, int, int], List[int], Tuple[int, int, int, int]]) None [source]#
Set the color
arcade.Window.clear()
will use when clearing the window. This only needs to be called when the background color changes.Note
A shorter and faster way to set background color is using
arcade.Window.background_color
.Examples:
# Use Arcade's built in color values arcade.set_background_color(arcade.color.AMAZON) # Specify RGB value directly (red) arcade.set_background_color((255, 0, 0))
- Parameters
color (Color) – List of 3 or 4 values in RGB/RGBA format.
arcade.set_viewport#
- arcade.set_viewport(left: float, right: float, bottom: float, top: float) None [source]#
This sets what coordinates the window will cover.
Tip
Beginners will want to use
Camera
. It provides easy to use support for common tasks such as screen shake and movement to a destination.If you are making a game with complex control over the viewport, this function can help.
By default, the lower left coordinate will be
(0, 0)
, the top y coordinate will be the height of the window in pixels, and the right x coordinate will be the width of the window in pixels.Warning
Be careful of fractional or non-multiple values!
It is recommended to only set the viewport to integer values that line up with the pixels on the screen. Otherwise, tiled pixel art may not line up well during render, creating rectangle artifacts.
Note
Window.on_resize
callsset_viewport
by default. If you want to set your own custom viewport during the game, you may need to over-ride theon_resize
method.Note
For more advanced users
This functions sets the orthogonal projection used by shapes and sprites. It also updates the viewport to match the current screen resolution.
window.ctx.projection_2d
(projection_2d()
) andwindow.ctx.viewport
(viewport()
) can be used to set viewport and projection separately.- Parameters
left (Number) – Left-most (smallest) x value.
right (Number) – Right-most (largest) x value.
bottom (Number) – Bottom (smallest) y value.
top (Number) – Top (largest) y value.
arcade.set_window#
arcade.start_render#
- arcade.start_render() None [source]#
Clears the window.
More practical alternatives to this function is
arcade.Window.clear()
orarcade.View.clear()
.
arcade.unschedule#
- arcade.unschedule(function_pointer: Callable)[source]#
Unschedule a function being automatically called.
Example:
def some_action(delta_time): print(delta_time) arcade.schedule(some_action, 1) arcade.unschedule(some_action)
- Parameters
function_pointer (Callable) – Pointer to the function to be unscheduled.
arcade.NoOpenGLException#
arcade.View#
- class arcade.View(window: Optional[arcade.application.Window] = None)[source]#
Support different views/screens in a window.
- add_section(section, at_index: Optional[int] = None) None [source]#
Adds a section to the view Section Manager. :param section: the section to add to this section manager :param at_index: inserts the section at that index. If None at the end
- clear(color: Optional[Union[Tuple[int, int, int], List[int], Tuple[int, int, int, int]]] = None, normalized: bool = False, viewport: Optional[Tuple[int, int, int, int]] = None)[source]#
Clears the View’s Window with the configured background color set through
arcade.Window.background_color
.
- on_key_press(symbol: int, modifiers: int)[source]#
Override this function to add key press functionality.
- on_key_release(_symbol: int, _modifiers: int)[source]#
Override this function to add key release functionality.
- on_mouse_drag(x: int, y: int, dx: int, dy: int, _buttons: int, _modifiers: int)[source]#
Override this function to add mouse button functionality.
- Parameters
x (int) – x position of mouse
y (int) – y position of mouse
dx (int) – Change in x since the last time this method was called
dy (int) – Change in y since the last time this method was called
_buttons (int) – Which button is pressed
_modifiers (int) – Bitwise ‘and’ of all modifiers (shift, ctrl, num lock) pressed during this event. See Modifiers.
- on_mouse_enter(x: int, y: int)[source]#
Called when the mouse was moved into the window. This event will not be triggered if the mouse is currently being dragged.
- on_mouse_leave(x: int, y: int)[source]#
Called when the mouse was moved outside of the window. This event will not be triggered if the mouse is currently being dragged. Note that the coordinates of the mouse pointer will be outside of the window rectangle.
- on_mouse_motion(x: int, y: int, dx: int, dy: int)[source]#
Override this function to add mouse functionality.
- on_mouse_press(x: int, y: int, button: int, modifiers: int)[source]#
Override this function to add mouse button functionality.
- Parameters
x (int) – x position of the mouse
y (int) – y position of the mouse
button (int) – What button was hit. One of: arcade.MOUSE_BUTTON_LEFT, arcade.MOUSE_BUTTON_RIGHT, arcade.MOUSE_BUTTON_MIDDLE
modifiers (int) – Bitwise ‘and’ of all modifiers (shift, ctrl, num lock) pressed during this event. See Modifiers.
- on_mouse_release(x: int, y: int, button: int, modifiers: int)[source]#
Override this function to add mouse button functionality.
- Parameters
- on_resize(width: int, height: int)[source]#
Called when the window is resized while this view is active.
on_resize()
is also called separately. By default this method does nothing and can be overridden to handle resize logic.
- on_show()[source]#
Deprecated. Use
on_show_view()
instead.
arcade.Window#
- class arcade.Window(width: int = 800, height: int = 600, title: Optional[str] = 'Arcade Window', fullscreen: bool = False, resizable: bool = False, update_rate: Optional[float] = 0.016666666666666666, antialiasing: bool = True, gl_version: Tuple[int, int] = (3, 3), screen: Optional[pyglet.canvas.base.Screen] = None, style: Optional[str] = None, visible: bool = True, vsync: bool = False, gc_mode: str = 'context_gc', center_window: bool = False, samples: int = 4, enable_polling: bool = True)[source]#
The Window class forms the basis of most advanced games that use Arcade. It represents a window on the screen, and manages events.
- Parameters
width (int) – Window width
height (int) – Window height
title (str) – Title (appears in title bar)
fullscreen (bool) – Should this be full screen?
resizable (bool) – Can the user resize the window?
update_rate (float) – How frequently to update the window.
antialiasing (bool) – Should OpenGL’s anti-aliasing be enabled?
gl_version (Tuple[int,int]) – What OpenGL version to request. This is
(3, 3)
by default and can be overridden when using more advanced OpenGL features.visible (bool) – Should the window be visible immediately
vsync (bool) – Wait for vertical screen refresh before swapping buffer This can make animations and movement look smoother.
gc_mode (bool) – Decides how OpenGL objects should be garbage collected (“context_gc” (default) or “auto”)
center_window (bool) – If true, will center the window.
samples (bool) – Number of samples used in antialiasing (default 4). Usually this is 2, 4, 8 or 16.
enable_polling (bool) – Enabled input polling capability. This makes the
keyboard
andmouse
attributes available for use.
- property background_color: Union[Tuple[int, int, int], List[int], Tuple[int, int, int, int]]#
Get or set the background color for this window. This affects what color the window will contain when
clear()
is called.Examples:
# Use Arcade's built in color values window.background_color = arcade.color.AMAZON # Specify RGB value directly (red) window.background_color = 255, 0, 0
If the background color is an
RGB
value instead ofRGBA
we assume alpha value 255.- Type
Color
- clear(color: Optional[Union[Tuple[int, int, int], List[int], Tuple[int, int, int, int]]] = None, normalized: bool = False, viewport: Optional[Tuple[int, int, int, int]] = None)[source]#
Clears the window with the configured background color set through
arcade.Window.background_color
.
- property ctx: arcade.context.ArcadeContext#
The OpenGL context for this window.
- Type
- property current_view: Optional[arcade.application.View]#
This property returns the current view being shown. To set a different view, call the
arcade.Window.show_view()
method.- Return type
- flip()[source]#
Window framebuffers normally have a back and front buffer. This method makes the back buffer visible and hides the front buffer. A frame is rendered into the back buffer, so this method displays the frame we currently worked on.
This method also garbage collect OpenGL resources before swapping the buffers.
- get_location() Tuple[int, int] [source]#
Return the X/Y coordinates of the window
- Returns
x, y of window location
- get_viewport() Tuple[float, float, float, float] [source]#
Get the viewport. (What coordinates we can see.)
- headless#
bool: If this is a headless window
- hide_view()[source]#
Hide the currently active view (if any) returning us back to
on_draw
andon_update
functions in the window.This is not necessary to call if you are switching views. Simply call
show_view
again.
- on_key_press(symbol: int, modifiers: int)[source]#
Override this function to add key press functionality.
- on_key_release(symbol: int, modifiers: int)[source]#
Override this function to add key release functionality.
- on_mouse_drag(x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int)[source]#
Override this function to add mouse button functionality.
- Parameters
x (int) – x position of mouse
y (int) – y position of mouse
dx (int) – Change in x since the last time this method was called
dy (int) – Change in y since the last time this method was called
buttons (int) – Which button is pressed
modifiers (int) – Bitwise ‘and’ of all modifiers (shift, ctrl, num lock) pressed during this event. See Modifiers.
- on_mouse_enter(x: int, y: int)[source]#
Called when the mouse was moved into the window. This event will not be triggered if the mouse is currently being dragged.
- on_mouse_leave(x: int, y: int)[source]#
Called when the mouse was moved outside of the window. This event will not be triggered if the mouse is currently being dragged. Note that the coordinates of the mouse pointer will be outside of the window rectangle.
- on_mouse_motion(x: int, y: int, dx: int, dy: int)[source]#
Override this function to add mouse functionality.
- on_mouse_press(x: int, y: int, button: int, modifiers: int)[source]#
Override this function to add mouse button functionality.
- Parameters
x (int) – x position of the mouse
y (int) – y position of the mouse
button (int) – What button was hit. One of: arcade.MOUSE_BUTTON_LEFT, arcade.MOUSE_BUTTON_RIGHT, arcade.MOUSE_BUTTON_MIDDLE
modifiers (int) – Bitwise ‘and’ of all modifiers (shift, ctrl, num lock) pressed during this event. See Modifiers.
- on_mouse_release(x: int, y: int, button: int, modifiers: int)[source]#
Override this function to add mouse button functionality.
- Parameters
- on_resize(width: float, height: float)[source]#
Override this function to add custom code to be called any time the window is resized. The main responsibility of this method is updating the projection and the viewport.
If you are not changing the default behavior when overriding, make sure you call the parent’s
on_resize
first:def on_resize(self, width: int, height: int): super().on_resize(width, height) # Add extra resize logic here
- on_update(delta_time: float)[source]#
Move everything. Perform collision checks. Do all the game logic here.
- Parameters
delta_time (float) – Time interval since the last time the function was called.
- run()[source]#
Shortcut for
arcade.run()
.For example:
MyWindow().run()
Set the caption for the window.
- set_fullscreen(fullscreen: bool = True, screen: Optional[arcade.application.Window] = None, mode: Optional[pyglet.canvas.base.ScreenMode] = None, width: Optional[float] = None, height: Optional[float] = None)[source]#
Set if we are full screen or not.
- Parameters
fullscreen (bool) –
screen – Which screen should we display on? See
get_screens()
mode (pyglet.canvas.ScreenMode) – The screen will be switched to the given mode. The mode must have been obtained by enumerating Screen.get_modes. If None, an appropriate mode will be selected from the given width and height.
width (int) –
height (int) –
- set_mouse_platform_visible(platform_visible=None)[source]#
This method is only exposed/overridden because it causes PyCharm to display a warning. This function is setting the platform specific mouse cursor visibility and would only be something an advanced user would care about.
See pyglet documentation for details.
- set_mouse_visible(visible: bool = True)[source]#
If true, user can see the mouse cursor while it is over the window. Set false, the mouse is not visible. Default is true.
- Parameters
visible (bool) –
- set_update_rate(rate: float)[source]#
Set how often the screen should be updated. For example, self.set_update_rate(1 / 60) will set the update rate to 60 fps
- Parameters
rate (float) – Update frequency in seconds
- set_viewport(left: float, right: float, bottom: float, top: float)[source]#
Set the viewport. (What coordinates we can see. Used to scale and/or scroll the screen).
See
arcade.set_viewport()
for more detailed information.- Parameters
left (Number) –
right (Number) –
bottom (Number) –
top (Number) –
- set_visible(visible: bool = True)[source]#
Set if the window is visible or not. Normally, a program’s window is visible.
- Parameters
visible (bool) –
- show_view(new_view: arcade.application.View)[source]#
Select the view to show in the next frame. This is not a blocking call showing the view. Your code will continue to run after this call and the view will appear in the next dispatch of
on_update
/on_draw`
.Calling this function is the same as setting the
arcade.Window.current_view
attribute.- Parameters
new_view (View) – View to show
- test(frames: int = 10)[source]#
Used by unit test cases. Runs the event loop a few times and stops.
- Parameters
frames (int) –
arcade.get_screens#
arcade.open_window#
- arcade.open_window(width: int, height: int, window_title: Optional[str] = None, resizable: bool = False, antialiasing: bool = True) arcade.application.Window [source]#
This function opens a window. For ease-of-use we assume there will only be one window, and the programmer does not need to keep a handle to the window. This isn’t the best architecture, because the window handle is stored in a global, but it makes things easier for programmers if they don’t have to track a window pointer.
arcade.Section#
- class arcade.Section(left: int, bottom: int, width: int, height: int, *, name: Optional[str] = None, accept_keyboard_events: Union[bool, Iterable] = True, prevent_dispatch: Optional[Iterable] = None, prevent_dispatch_view: Optional[Iterable] = None, local_mouse_coordinates: bool = False, enabled: bool = True, modal: bool = False)[source]#
A Section represents a rectangular portion of the viewport Events are dispatched to the section based on it’s position on the screen.
- get_xy_screen_relative(section_x: int, section_y: int)[source]#
Returns screen coordinates from section coordinates
- get_xy_section_relative(screen_x: int, screen_y: int)[source]#
returns section coordinates from screen coordinates
- property modal: bool#
Returns the modal state (Prevent the following sections from receiving input events and updating)
- mouse_is_on_top(x: int, y: int) bool [source]#
Check if the current mouse position is on top of this section
- property section_manager: Optional[arcade.sections.SectionManager]#
Returns the section manager
- property view#
The view this section is set on
- property window#
The view window
arcade.SectionManager#
- class arcade.SectionManager(view)[source]#
This manages the different Sections a View has. Actions such as dispatching the events to the correct Section, draw order, etc.
- add_section(section: arcade.sections.Section, at_index: Optional[int] = None) None [source]#
Adds a section to this Section Manager :param section: the section to add to this section manager :param at_index: inserts the section at that index. If None at the end
- dispatch_keyboard_event(event, *args, **kwargs) Optional[bool] [source]#
Generic method to dispatch keyboard events to the correct sections
- dispatch_mouse_event(event: str, x: int, y: int, *args, **kwargs) Optional[bool] [source]#
Generic method to dispatch mouse events to the correct Section
- get_section(x: int, y: int) Optional[arcade.sections.Section] [source]#
Returns the first section based on x,y position
- get_section_by_name(name: str) Optional[arcade.sections.Section] [source]#
Returns the first section with the given name
- on_draw()[source]#
Called on each event loop. First dispatch the view event, then the section ones. It automatically calls camera.use() for each section that has a camera and resets the camera effects by calling the default SectionManager camera afterwards if needed.
- on_mouse_drag(x: int, y: int, *args, **kwargs) Optional[bool] [source]#
- This method dispatches the on_mouse_drag and also calculates
if on_mouse_enter/leave should be fired
- on_mouse_motion(x: int, y: int, *args, **kwargs) Optional[bool] [source]#
- This method dispatches the on_mouse_motion and also calculates
if on_mouse_enter/leave should be fired
- on_resize(width: int, height: int)[source]#
Called when the window is resized. First dispatch the view event, then the section ones.
- on_update(delta_time: float)[source]#
Called on each event loop. First dispatch the view event, then the section ones.
- remove_section(section: arcade.sections.Section) None [source]#
Removes a section from this section manager