Full API Documentation#
This is the top level init file for all of rubato.
Note
Every single class can be accessed through the top level or through the full module path.
- game
The global game class that can be accessed anywhere. Initialized when
rubato.init()
is called.- Type:
Game
- begin()[source]
Starts the main game loop.
- Raises:
RuntimeError – Rubato has not been initialized before calling.
- init(options={})[source]
Initializes rubato.
- Parameters:
options (
dict
) – A game config. Defaults to the default config for Game.
Game#
The main game class. It controls everything in the game. The Display is where your game lives at a certain aspect ratio, The Screen is the actual size of the window which the user interacts with.
- scenes#
The global scene manager.
- Type:
SceneManager
- name#
The title of the game window.
- Type:
str
- fps#
The target fps of the game.
- Type:
int
- reset_display#
Controls whether or not the display should reset every frame.
- Type:
bool
- state#
The current state of the game.
- Type:
STATE
- class STATE[source]#
Bases:
Enum
An enum to keep track of the state things
RUNNING: will run everything normally STOPPED: will quit the window PAUSED: will pause physics time calls. Please do not use this feature.
- init(options={})[source]#
Initializes a game. Should only be called by
rubato.init()
.- Parameters:
options (
dict
) – A game config. Defaults to the default config for Game.
- update()[source]#
The update loop for the game. Called automatically every frame. Handles the game states. Will always process timed calls.
- set_window_width(window_width)[source]#
Sets width of actual window (screen). :type window_width:
int
:param window_width: width clamped between the max display size initializedrubato.init()
- Return type:
None
Input#
The Input module is the way you collect input from the user.
- key#
The pygame key module.
- Type:
pygame.key
- mouse#
The pygame mouse module.
- Type:
pygame.mouse
- is_pressed(char)[source]#
Checks if a key is pressed.
- Parameters:
char (
str
) – The name of the key to check.- Returns:
Whether or not the key is pressed.
- Return type:
bool
Radio#
The Radio module is a system used to communicate to all parts of the game. This is similar to event systems in other game engines.
To use this, first you need to listen for a specific key using the
Radio.listen()
function. Then from anywhere else in the code, you can
broadcast that event key using the Radio.broadcast()
function.
- class Radio[source]#
Bases:
object
Broadcast system manages all events and inter-class communication. Has a buffer system and a handler system.
- events#
A list with all the event keys in the queue.
- Type:
List[str]
- listeners#
A dictionary with all of the active listeners.
- Type:
dict[str, Callable]
- listen(event, func)[source]#
Creates an event listener and registers it.
- Parameters:
event (
str
) – The event key to listen for.func (
Callable
) – The function to run once the event is broadcast. It may take in a params dictionary argument.
- class Listener[source]#
Bases:
object
The actual listener object itself.
- event#
The event descriptor
- Type:
str
- callback#
The function called when the event occurs
- Type:
Callable
- registered#
Describes whether the listener is registered
- Type:
bool
Sound#
A fully functional, multi-channel sound system.
- loaded_sounds()[source]#
A dictionary of all the loaded sounds. The keys are the filename.
- Returns:
The returned dictionary.
- Return type:
dict[str, pygame.mixer.Sound]
- import_sound(rel_path, sound_name)[source]#
Imports a sound and saves it in the loaded_sounds dictionary with the key being the sound_name.
- Parameters:
rel_path (
str
) – The relative path to the sound file you wish to import.sound_name (
str
) – The name of the sound.
- import_sound_folder(rel_path)[source]#
Imports a folder of sounds, saving each one in the loaded_sounds dictionary by filename.
- Parameters:
rel_path (
str
) – The relative path to the folder you wish to import.
- get_sound(sound_name)[source]#
Gets the sound based on the sound name.
- Parameters:
sound_name (
str
) – The name of the sound.- Raises:
IdError – No sound is associated to the sound name.
- Returns:
The sound.
- Return type:
mixer.Sound
Sprite#
The default Sprite class.
- class Sprite[source]#
Bases:
object
The base sprite class.
- z_index#
The z_index of the sprite.
- Type:
int
- components#
All the components attached to this sprite.
- Type:
List[Component]
- __init__(options={})[source]#
Initializes a sprite.
- Parameters:
options (
dict
) – A sprite config. Defaults to the default config for Sprite.
- add(component)[source]#
Add a component to the sprite.
- Parameters:
component (
Component
) – The component to add.- Raises:
DuplicateComponentError – Raised when there is already a component of the same type on the sprite.
ComponentNotAllowed – Raised when an added component conflicts with another component.
- Returns:
The current sprite
- Return type:
- remove(comp_type)[source]#
Removes a component from the sprite.
- Parameters:
comp_type (
type
) – The type of the component to remove.- Raises:
Warning – The component was not on the sprite and nothing was removed.
Components#
The default Component class.
A component gives functionally to sprites.
- class Component[source]#
Bases:
object
A base component. Does nothing by itself.
- sprite#
The sprite this component is attached to.
- Type:
Sprite
- required#
A list of components that are required by this component. (For example, a RigidBody needs a Hitbox).
- Type:
List[type]
- not_allowed#
A list of components that cannot be on the same Sprite. (For example, an Animation and an Image cannot be on the same Sprite)
- Type:
List[type]
Image#
The image component that renders an image from the filesystem.
- class Image[source]#
Bases:
Component
A component that handles Images.
- image#
The pygame surface containing the image.
- Type:
pygame.Surface
- __init__(options={})[source]#
Initializes an Image
- Parameters:
options (
dict
) – A Image config. Defaults to the default config for Image.
- scale(scale_factor)[source]#
Scales the image.
- Parameters:
scale_factor (
Vector
) – The 2-d scale factor relative to it’s current size.
Animation#
Animations are a series of images that loop in a set loop
- class Animation[source]#
Bases:
Component
Made of a a dictionary holding the different states ie. running, idle, etc. each holding separate frames and their times.
- rotation#
The rotation of the animation.
- Type:
float
- default_animation_length#
The default length of an animation in frames.
- Type:
int
- default_state#
The key of the default state. Defaults to None.
- Type:
Union[str, None]
- current_state#
The key of the current state. Defaults to “”.
- Type:
str
- animation_frames_left#
The number of animation frames left.
- Type:
int
- current_frame#
The current frame of the animation.
- Type:
int
- loop#
Whether the animation should loop. Defaults to False.
- Type:
bool
- __init__(options={})[source]#
Initializes an Animation.
- Parameters:
options (
dict
) – A Animation config. Defaults to the default config for Animation.
- property image: pygame.Surface#
The current image.
- Returns:
The pygame surface holding the current image.
- Return type:
Surface
- property anim_frame: Image#
The current frame.
- Returns:
The image representing the current frame.
- Return type:
- scale(scale_factor)[source]#
Scales the Images to the given scale factor.
- Parameters:
scale_factor (
Vector
) – The 2-d scale factor relative to it’s current size.
- resize(new_size)[source]#
Resize the image to a given size in pixels.
- Parameters:
new_size (
Vector
) – The new size of the image in pixels.
- set_current_state(new_state, loop=False)[source]#
Set the current animation state.
- Parameters:
new_state (
str
) – The key of the new current state.loop (
bool
) – Whether to loop the state. Defaults to False.
- Raises:
KeyError – The new_state key is not in the initialized states.
- add_state(state_name, image_and_times)[source]#
Initializes a state to this animation component.
- Parameters:
state_name (
str
) – The key used to reference this state.image_and_times (
Union
[List
[tuple
],list
]) – The list of images and times. Should be the output of theimport_animation_folder
function or a list containing tuples following the format: (Image, frame_number)
- Raises:
Error – The animation frame tuple is invalid.
- static import_animation_folder(rel_path)[source]#
Imports a folder of images, creating rubato.Image for each one and placing it in a list by order in directory. Directory must be solely comprised of images.
- Parameters:
rel_path (
str
) – The relative path to the folder you wish to import- Returns:
a list of rubato.Image s. Filled with all images in given directory.
- Return type:
list
Rectangle#
Hitbox#
Various hitbox components that enable collisions
- class Hitbox[source]#
Bases:
Component
The basic hitbox
- bounding_box_dimensions()[source]#
Returns the dimensions of the bounding box surrounding the polygon.
- Returns:
A vector with the x variable holding the width and the y variable holding the height.
- Return type:
- collide(other, callback=lambda c: ...)[source]#
A simple collision engine for most use cases.
- Parameters:
other (
Hitbox
) – The other rigidbody to collide with.callback (
Callable
) – The function to run when a collision is detected. Defaults to None.
- Returns:
Returns a collision info object if a collision is detected or nothing if no collision is detected.
- Return type:
Union[CollisionInfo, None]
- class Polygon[source]#
Bases:
Hitbox
A custom polygon class with an arbitrary number of vertices
- verts#
A list of the vertices in the Polygon, in either clockwise or anticlockwise direction.
- Type:
List[Vector]
- scale#
The scale of the polygon.
- Type:
Union[float, int]
- __init__(options={})[source]#
Initializes a Polygon
- Parameters:
verts – A list of the vertices in the Polygon.
pos – The position of the center of the Polygon as a function. Defaults to lambda: Vector(0, 0).
scale – The scale of the polygon. Defaults to 1.
rotation – The rotation angle of the polygon in degrees as a function. Defaults to lambda: 0.
- static generate_rect(w=32, h=32)[source]#
Creates a rectangle from its dimensions.
- Parameters:
w (
int
) – The width of the hitbox.h (
int
) – The height of the hitbox.
- Returns:
The vertices of the rectangle.
- Return type:
List[Vector]
- static generate_polygon(num_sides, radius=1)[source]#
Creates a normal polygon with a specified number of sides and an optional radius.
- transformed_verts()[source]#
Maps each vertex with the Polygon’s scale and rotation
- Return type:
List
[Vector
]
- real_verts()[source]#
Returns the a list of vertices in absolute coordinates
- Return type:
List
[Vector
]
- class Rectangle[source]#
Bases:
Polygon
_summary_
- Parameters:
Polygon (
_type_
) – _description_
- __init__(options)[source]#
Initializes a Polygon
- Parameters:
verts – A list of the vertices in the Polygon.
pos – The position of the center of the Polygon as a function. Defaults to lambda: Vector(0, 0).
scale – The scale of the polygon. Defaults to 1.
rotation – The rotation angle of the polygon in degrees as a function. Defaults to lambda: 0.
- class Circle[source]#
Bases:
Hitbox
A custom circle class defined by a position, radius, and scale
- radius#
The radius of the circle.
- Type:
int
- scale#
The scale of the circle.
- Type:
int
- class CollisionInfo[source]#
Bases:
object
A class that represents information returned in a successful collision
- shape_a#
A reference to the first shape.
- Type:
Union[Circle, Polygon, None]
- shape_b#
A reference to the second shape.
- Type:
Union[Circle, Polygon, None]
- class SAT[source]#
Bases:
object
A general class that does the collision detection math between circles and polygons
- static overlap(shape_a, shape_b)[source]#
Checks for overlap between any two shapes (Polygon or Circle)
- Parameters:
- Returns:
If a collision occurs, a CollisionInfo is returned. Otherwise None is returned.
- Return type:
Union[CollisionInfo, None]
- static polygon_polygon_test(shape_a, shape_b, flip=False)[source]#
Checks for overlap between two polygons
- Return type:
Optional
[CollisionInfo
]
RigidBody#
The Rigidbody component contains an implementation of rigidbody physics. They have hitboxes and can collide and interact with other rigidbodies.
- class RigidBody[source]#
Bases:
Component
A RigidBody implementation with built in physics and collisions. Rigidbodies require hitboxes.
- static#
Whether or not the rigidbody is static (as in, it does not move).
- Type:
bool
- friction#
The friction coefficient of the Rigidbody (usually a a value between 0 and 1).
- Type:
float
- inv_mass#
The inverse of the mass of the Rigidbody (0 if the mass is infinite).
- Type:
float
- bounciness#
How bouncy the rigidbody is (usually a value between 0 and 1).
- Type:
float
- __init__(options={})[source]#
Initializes a Rigidbody.
- Parameters:
options (
dict
) – A rigidbody config. Defaults to the default config for RigidBody
- property mass: float#
The mass of the Rigidbody (readonly)
- Return type:
float
- add_force(force)[source]#
Add a force to the Rigidbody.
- Parameters:
force (
Vector
) – The force to add.
- add_cont_force(impulse, time)[source]#
Add a continuous force to the Rigidbody. A continuous force is a force that is continuously applied over a time period. (the force is added every frame for a specific duration).
- Parameters:
impulse (
Vector
) – The force to add.time (
int
) – The time in seconds that the force should be added.
- static handle_collision(col)[source]#
Handle the collision between two rigidbodies.
- Parameters:
col (
CollisionInfo
) – The collision information.
Group#
Groups contain sprites and allow specific sprites to be seperated.
Scene#
THe Scene class which is a collection of groups. It also houses the current scene camera. Scenes come with a default group that everything is added to if no other groups are specified.
- class Scene[source]#
Bases:
object
A scene is a collection of groups.
- root#
The base group of sprites in the scene.
- Type:
Group
- camera#
The camera of this scene.
- Type:
Camera
- id#
The id of this scene.
- Type:
str
- __init__()[source]#
Initializes a scene with an empty collection of sprites, a new camera, and a blank id.
SceneManager#
The Scene Manager houses a collection of scenes and allows switching between different scenes. Each Game object has a scene manager. It also handles drawing and updating the current scene.
- class SceneManager[source]#
Bases:
object
The Scene Manager contains and handle multiple scenes.
- scenes#
The collection of scenes in the manager. Accessed by scene id.
- Type:
Dict[str, Scene]
- current#
The id of the current scene.
- Type:
str
- property is_empty: bool#
Checks if the scene manager contains no scene.
- Returns:
True if the scene is empty. False otherwise.
- Return type:
bool
Camera#
The Camera is what handles where things are drawn. A camera can zoom, pan around, and also travel along the z-index. Items only render if their z-index is less than that of the camera’s.
- class Camera[source]#
Bases:
object
The camera class.
- zoom#
The current zoom of the camera.
- Type:
int
- z_index#
The current z_index of the camera.
- Type:
int
- __init__(pos=Vector(), zoom=1, z_index=0)[source]#
Initializes a camera.
- Parameters:
pos (
Vector
) – The starting position of the camera. Defaults to Vector().zoom (
int
) – The starting zoom of the camera. Defaults to 1.z_index (
int
) – The starting z_index of the camera. Defaults to 0.
- transform(point)[source]#
Converts world space coordinates into screen space coordinates according to the camera.
- Parameters:
point (
Vector
) – The point in world space coordinates.- Returns:
- The translated coordinates,
where the first item is the x-coordinate and the second item is the y-coordinate. The coordinates are returned with the same type that is given.
- Return type:
tuple
Utilities#
Math#
A more complete math class.
- INFINITY#
The max value of a float.
- Type:
float
- clamp(a, lower, upper)[source]#
Clamps a value.
- Parameters:
a (
Union
[float
,int
]) – The value to clamp.lower (
Union
[float
,int
]) – The lower bound of the clamp.upper (
Union
[float
,int
]) – The upper bound of the clamp.
- Returns:
The clamped result.
- Return type:
float
Display#
Global display class that allows any file to access the displayed screen.
- set_display(new_surface)[source]#
Set the global display.
- Parameters:
new_surface (
Surface
) – The new surface to set.
- update(surface, pos)[source]#
Update the current display.
- Parameters:
surface (
Surface
) – The surface to draw on the display.pos (
tuple
) – The position to draw the surface on.
Vector#
A vector implementation.
- class Vector[source]#
Bases:
object
A Vector object that defines a 2D point in space
- x#
The x coordinate.
- Type:
Union[float, int]
- y#
The y coordinate.
- Type:
Union[float, int]
- __init__(x=0, y=0)[source]#
Initializes a vector.
- Parameters:
x (
Union
[float
,int
]) – The x coordinate. Defaults to 0.y (
Union
[float
,int
]) – The y coordinate. Defaults to 0.
- translate(x, y)[source]#
Translates the vector’s x y and z coordinates by some constants.
- Parameters:
x (
Union
[float
,int
]) – The change in x.y (
Union
[float
,int
]) – The change in y.
- static from_tuple(coords)[source]#
Returns a Vector from a coordinate tuple :type coords:
tuple
:param coords: tuple with an x and y coordinate [float | int].Returns: Vector with specified coordinates.
- Return type:
- dot(other)[source]#
Takes the dot product of vectors.
- Parameters:
other (
Vector
) – The other vector.- Returns:
The resulting dot product.
- Return type:
Union[float, int]
- cross(other)[source]#
Takes the cross product of vectors.
- Parameters:
other (
Vector
) – The other vector.- Returns:
The resulting cross product.
- Return type:
Union[float, int]
- pow(num)[source]#
Takes the power of the elements in the vector to the num.
- Parameters:
num (
Union
[float
,int
]) – The exponent to use.- Returns:
The new vector.
- Return type:
- property mag: float#
Returns the squared magnitude of the vector.
- Return type:
float
- property magnitude: float#
Returns the magnitude of the vector.
- Return type:
float
- static from_radial(angle, magnitude)[source]#
Gives you a Vector from the given direction and distance.
- Parameters:
angle (
float
) – Direction of vector in radians.magnitude (
float
) – Length of vector.
- Returns:
Vector from the given direction and distance
- Return type:
- property angle: float#
Returns the angle of the vector
- Return type:
float
- invert(axis)[source]#
Inverts the vector in the axis given
- Parameters:
axis (
str
) – The axis to invert the vector in (x or y).- Raises:
ValueError – The value for axis is not “x” or “y”
- round(decimal_places)[source]#
Rounds x and y to decimal_places
- Parameters:
decimal_places (
int
) – the amount of decimal places rounded to.
- direction_to(vector)[source]#
Treating vectors as points the direction to the other vector from the current vector.
- Parameters:
vector (
Vector
) – The other vector.- Returns:
The direction to the new vector in radians.
- Return type:
float
Time#
A time class to monitor time and to call functions in the future.
- frames#
The total number of elapsed frames since the start of the game.
- Type:
int
- fixed_delta_time(form='milli')[source]#
Gets the time since the fixed update frame.
- Parameters:
form (
str
) – The format the output should be (sec, milli). Defaults to milli.- Raises:
ValueError – The form is not “sec” or “milli”.
- Returns:
Time since the fixed update frame, in the given form.
- Return type:
float
- delta_time(form='milli')[source]#
Gets the time since the last frame.
- Parameters:
form (
str
) – The format the output should be (sec, milli). Defaults to milli.- Raises:
ValueError – The form is not “sec” or “milli”.
- Returns:
Time since the last frame, in the given form.
- Return type:
float
- now()[source]#
Gets the time since the start of the game, in milliseconds.
- Returns:
Time since the start of the game, in milliseconds.
- Return type:
int
- delayed_call(time_delta, func)[source]#
Calls the function func at a later
- Parameters:
time_delta (
int
) – The time from now (in milliseconds) to run the function at.func (
Callable
) – The function to call.
- delayed_frames(frames_delta, func)[source]#
Calls the function func at a later frame.
- Parameters:
frames_delta (
int
) – The number of frames to wait.func (
Callable
) – The function to call
- milli_to_sec(milli)[source]#
Converts milliseconds to seconds.
- Parameters:
milli (
int
) – A number in milliseconds.- Returns:
The converted number in seconds.
- Return type:
float
Color#
A Color implementation.
- class Color[source]#
Bases:
object
A Color implentation.
- r#
The red value.
- Type:
float
- g#
The green value.
- Type:
float
- b#
The blue value.
- Type:
float
- __init__(r=0.0, g=0.0, b=0.0)[source]#
Initializes an Color class.
- Parameters:
r (
float
) – The red value. Defaults to 0.0.g (
float
) – The green value. Defaults to 0.0.b (
float
) – The blue value. Defaults to 0.0.
- to_tuple()[source]#
Converts the Color to a tuple.
- Returns:
The tuple representing the color.
- Return type:
tuple(int, int, int)
- check_values()[source]#
Makes the Color values legit. In other words, clamps them between 0 and 255.
- to_hex()[source]#
Converts the Color to hexadecimal.
- Returns:
The hexadecimal output in lowercase. (i.e. ffffff)
- Return type:
str
- static from_hex(h)[source]#
Creates an Color from a hex string.
- Parameters:
h (
str
) – The hexadecimal value in lowercase.- Returns:
The Color value.
- Return type:
- static from_hsv(h, s, v)[source]#
Creates an Color from an HSV.
- Parameters:
h (
int
) – The hue amount.s (
int
) – The saturation amount.v (
int
) – The value amount.
- Returns:
The Color value.
- Return type:
- class property random[source]#
A Color class with a random color.
- Returns:
A random color.
- Return type:
- class property white[source]#
A Color class of the color white.
- Returns:
(255, 255, 255)
- Return type:
- class property yellow[source]#
A Color class of the color yellow.
- Returns:
(255, 255, 0)
- Return type:
- class property magenta[source]#
A Color class of the color magenta.
- Returns:
(255, 0, 255)
- Return type:
- class property silver[source]#
A Color class of the color silver.
- Returns:
(192, 192, 192)
- Return type:
- class property purple[source]#
A Color class of the color purple.
- Returns:
(128, 0, 128)
- Return type:
A Color class of the color navy.
- Returns:
(0, 0, 128)
- Return type:
Errors#
Some custom errors
- exception SideError[source]#
Bases:
Exception
An error that is raised when the number of sides is invalid