Actor > Widgets#
Use Button when a widget should simply send a message.
Use SaveButton and LoadButton when a world should be saved to or
restored from an SQLite database file with one click.
API Reference#
Label#
- class miniworlds.actors.widgets.label.Label(position: Tuple[float, float] | None = (0, 0), *args, **kwargs)[source]#
A text label for use in a
Toolbar.Labelis usually used for headings, status text, scores, or other information that should be shown inside a toolbar.- Parameters:
text – The text to display in the label.
image – Optional path to an image to show instead of (or alongside) text.
Examples
Add a label to a toolbar:
from miniworlds import * world = World(200, 200) toolbar = Toolbar() score_label = Label("Score: 0") toolbar.add(score_label) world.camera.add_right(toolbar) world.run()
Public Data Attributes:
Inherited from
Buttonfixed_widthchildrenactor_countclass_imageInherited from
SingleWidgetpadding_leftpadding_rightpadding_toppadding_bottompositionThe position of the actor as Position(x, y)
topleftTop-left position of the actor in world coordinates.
centerCenter position of the actor in world coordinates.
row_heighttext_alignDefines how text is aligned.
textThe text which is displayed on the widget.
fixed_widthchildrenactor_countclass_imageInherited from
BaseWidgetpadding_leftpadding_rightpadding_toppadding_bottompositionThe position of the actor as Position(x, y)
topleftTop-left position of the actor in world coordinates.
centerCenter position of the actor in world coordinates.
row_heightfixed_widthchildrenactor_countclass_imageInherited from
ParentActorvisibleWhether the actor is currently visible.
layerDefines the layer on which the actor is drawn if several actors overlap.
childrenactor_countclass_imageInherited from
ActorCurrent origin mode used for size and position operations.
collision_type specifies how collisions should be checked:
A actor with the property
is_blockablecannot move through actors with the propertyis_blocking.A actor with the property
is_blockablecannot move through actors with the propertyis_blocking.Defines the layer on which the actor is drawn if several actors overlap.
Actor position in last frame
Direction value from the previous frame.
Returns number of costumes of actor, 0 if actor has no costume
Whether the actor costume is mirrored on the horizontal axis.
Gets the costume of the actor, if available.
Gets the costume manager
Costume orientation offset in degrees.
Directions are handled exactly as in the Scratch programming language, see: Scratch Wiki
Gets the direction as value in unit circle (0° right, 90° top, 180° left...)
Size of the actor
The width of the actor in pixels.
The height of the actor in pixels.
The x-value of a actor
The y-value of a actor
Class name of this actor instance.
x-value of actor topleft-position
y-value of actor topleft-position
Top-left position of the actor in world coordinates.
x-value of actor center-position inside the current camera-screen
x-value of actor center-position
y-value of actor center-position
Center position of the actor in world coordinates.
Defines if the costume of a actor should be rotatable.
Should actor react to events? You can turn this option off for additional performance boost.
Fill color of the actor as RGBA tuple.
Fill color of the actor as RGBA tuple.
Is actor filled with color?
Border color as RGBA tuple.
Border color as RGBA tuple.
Border width of the actor.
Whether the actor is currently visible.
World this actor belongs to.
The image of the actor:
The position of the actor as Position(x, y)
Inherited from
ActorBasedirtyIf actor is dirty, it will be repainted.
rectThe surrounding Rectangle as pygame.Rect.
position_managersensor_managercostume_managerInherited from
DirtySpritevisibleYou can make this sprite disappear without removing it from the group assign 0 for invisible and 1 for visible
layerLayer property can only be set before the sprite is added to a group, after that it is read only and a sprite's layer in a group should be set via the group's change_layer() method.
Inherited from
SpriteimagerectlayerDynamic, read only property for protected _layer attribute.
Public Methods:
__init__(text[, image])Creates a label widget.
Inherited from
Button__init__([text, image])Creates a toolbar button.
on_clicked_left(mouse_pos)Called when the button is clicked.
act()Counts down the short click cooldown.
Inherited from
SingleWidget__init__([position])resize()cut_widget_text()update_positions()get_local_pos(position)set_row_height([value])set_border([color, width])Sets the widget border color and width.
set_position(value)Set actor position in world coordinates.
set_world(new_world)Move the actor to another world and return the actor.
get_widget_text()set_image(_img_source)sets image of widget
set_text(text)Sets text of widget.
get_text()Inherited from
BaseWidget__init__([position])new_costume()Create and attach a new empty costume to this actor.
resize()update_positions()get_local_pos(position)set_row_height(value)set_border([color, width])Sets the widget border color and width.
set_world(new_world)Move the actor to another world and return the actor.
set_position(value)Set actor position in world coordinates.
Inherited from
ParentActor__init__(position[, children])add_child(actor)set_layer(value)set_world(new_world)Move the actor to another world and return the actor.
reset_costumes()Remove all costumes and reset appearance state.
before_remove()Hook called immediately before the actor is removed from the world.
Inherited from
Actor__init__([position])switch_origin(value)Switch actor origin while preserving the visual on-screen position.
create_on_world(world)Creates a actor to a specific world
from_topleft(topleft_position, *args, **kwargs)Create an actor whose origin is interpreted as top-left.
from_center(center_position, *args, **kwargs)Create an actor whose origin is interpreted as center.
flip_x()Flips the actor by 180° degrees.
add_costume([source])Adds a new costume to actor.
add_costumes(sources)Adds multiple costumes
remove_costume([source])Removes a costume from actor
switch_costume(source)Switches the costume of actor
set_costume(costume)Set the current costume from an index, source, or appearance object.
Remove all costumes and reset appearance state.
set_background_color(color)Set a background color behind the actor costume image.
Switches to the next costume of actor
Return True when the actor currently has a costume.
turn_left([degrees])Turns actor by degrees degrees left :rtype:
intturn_right([degrees])Turns actor by degrees degrees right
set_direction(direction)Actor points in given direction.
point_towards_position(destination)Actor points towards a given position
point_towards_actor(other)Actor points towards another actor.
set_size(value)Set actor size as (width, height) in pixels.
scale_width(value)Scale actor width by a factor.
scale_height(value)Scale actor height by a factor.
move([distance, direction])Moves actor distance steps in current direction
move_vector(vector)Moves actor in direction defined by the vector
move_back(distance)Moves the actor backward by distance steps (opposite of current direction).
Undo the last move.
move_towards(target)Move one step toward a target actor or position.
move_in_direction(direction[, distance])Moves actor distance steps into a direction or towards a position
move_to(position)Moves actor distance to a specific world_posiition
remove([kill])Removes this actor from world
Hook called immediately before the actor is removed from the world.
bounce_from_border(borders)The actor "bounces" from a border.
detect_all([actors, direction, distance])Detects if actors are on actor position.
detect(*args, **kwargs)Detects if actors are on actor position.
detect_borders([distance])Detects borders
Does the actor touch the left border?
Does the actor touch the right border?
Does the actor touch the top border?
Does the actor touch the lower border?
detect_color([color])Detects colors in world-background at actor center-position
detect_color_at([direction, distance])Detects colors in world-background at actor-position
detect_actors_at([direction, distance, actors])Detects a actor in given direction and distance.
detect_actor_at([direction, distance, actors])Detect and return the first actor at a given direction and distance.
detect_actors_in_front([actors, distance])Detect all actors directly in front of this actor.
detect_actor_in_front([actors, distance])Detect and return the first actor directly in front.
detect_point(position)Is the actor colliding with a specific (global) point?
detect_pixel(position)Is the actor colliding with a pixel?
detect_rect(rect)Is the actor colliding with a static rect?
Checks whether the actor is completely inside the world boundaries.
bounce_from_actor(other)Reflect movement direction when colliding with another actor.
animate([speed])Animate the current costume with the given speed.
animate_costume(costume[, speed])Animate a specific costume with the given speed.
animate_loop([speed])Animates a costume with a looping animation
Stops current animation.
send_message(message)Sends a message to world.
on_key_down(key)Called once when a key is pressed.
on_key_pressed(key)Called repeatedly every frame while a key is held down.
on_key_up(key)Called once when a previously pressed key is released.
on_mouse_over(position)Called when the mouse cursor enters or moves over the actor area.
on_mouse_leave(position)Called when the mouse cursor leaves the actor area.
on_mouse_left_down(position)Called when the left mouse button is pressed down.
on_mouse_right_down(position)Called when the right mouse button is pressed down.
on_mouse_left(position)Called when the left mouse button is clicked.
on_mouse_right(position)Called when the right mouse button is clicked.
on_mouse_motion(position)Called when the mouse moves.
on_mouse_left_released(position)Method is called when left mouse key is released.
on_mouse_right_released(position)Method is called when right mouse key is released.
on_clicked_left(position)The mouse is on top of a actor and mouse was clicked.
on_clicked_right(position)The mouse is on top of a actor and mouse was clicked.
on_detecting_world is called, when actor is on the world
Called when the actor is not touching the world (i.e. outside world bounds).
on_detecting_actor(actor)on_detecting_actor is called, when actor is detects a actor on same position
on_detecting_borders(borders)on_detecting_border is called, when actor is near a border
fill(value)Set fill color for borders and lines
hide()Hides a actor (the actor will be invisible)
show()Displays a actor ( an invisible actor will be visible)
register_sensor(*args, **kwargs)This method is used for the @register_sensor decorator.
Return actor rect in camera-local coordinates.
set_world(new_world)Move the actor to another world and return the actor.
Create and attach a new empty costume to this actor.
set_position(value)Set actor position in world coordinates.
get_distance_to(obj)Gets the distance to another actor or a position
Hook called when actor shape-related properties change.
Inherited from
ActorBaseregister(method[, force, name])This method is used for the @register decorator.
register_message(*args, **kwargs)Registers a method to an object to handle specific on_message events.
__str__()Return str(self).
get_costume_class()Inherited from
DirtySprite__init__(*groups)__repr__()Return repr(self).
Inherited from
Sprite__init__(*groups)add(*groups)add the sprite to groups
remove(*groups)remove the sprite from groups
add_internal(group)For adding this sprite to a group internally.
remove_internal(group)For removing this sprite from a group internally.
update(*args, **kwargs)method to control sprite behavior
kill()remove the Sprite from all Groups
groups()list of Groups that contain this Sprite
alive()does the sprite belong to any groups
__repr__()Return repr(self).
Private Data Attributes:
_abc_implInherited from
Button_abc_impl_visible_layer__image__rectInherited from
SingleWidget_abc_impl_visible_layer__image__rectInherited from
BaseWidget_abc_impl_visible_layer__image__rectInherited from
ParentActor_abc_impl_visible_layer__image__rectInherited from
Actor_abc_impl__image__rectInherited from
ActorBase_abc_impl__image__rectPrivate Methods:
Inherited from
Actor_get_initialization_facade()_get_appearance_facade()_get_event_facade()_get_sensor_facade()_get_movement_facade()_get_size_facade()_validate_arguments(position, *args, **kwargs)Inherited from
ActorBase_is_actor_repainted()Inherited from
DirtySprite_set_visible(val)set the visible value (0 or 1) and makes the sprite dirty
_get_visible()return the visible value of that sprite