World > TiledWorld#
- class miniworlds.worlds.tiled_world.tiled_world.TiledWorld(x=20, y=16, tile_size=40, empty=False)[source]#
from typing A TiledWorld is a World where each Actor is placed in one Tile.
With Tiled World, you can realize RPGs and Boardgames.
Each Actor on a TiledWorld can be placed on a Tile, on a Corner between Tiles or on an Edge between Tiles.
Examples
Create Actor on Tile, Corner and Edge:
from miniworlds import * world = TiledWorld(6, 3) world.grid = True last_corner = None tile = Tile((1,1)) t1 = Actor() t1.center = tile.position t1.fill_color = (255,255,255) corner = Corner((3,1), "nw") t2 = Actor() t2.center = corner.position t2.fill_color = (255,0,0) edge = Edge((5,1), "w") t3 = Actor() t3.center = edge.position t3.fill_color = (0,0,255) t3.size = (0.2,1) t3.direction = edge.angle world.run()
Public Data Attributes:
Displays grid overlay on background.
Tile size of each tile, if world has tiles
Inherited from
World
Step defines how often the method
act()
will be called.Frames per second shown on the screen.
The x-world_size (defaults to view_size)
The y-world_size (defaults to view_size)
Set the size of world
Set default fill color for borders and lines
Set default stroke color for borders and lines.
Set default border color for borders and lines.
Sets default border color for actors
Returns all backgrounds of the world as list.
Returns the current background
The current displayed image
Gets the parent window
Inherited from
WorldBase
surface
window
size
rect
topleft
window_docking_position
width
height
Public Methods:
__init__
([x, y, tile_size, empty])Initializes the TiledWorld
Removes all tiles, corners and edges from World
In this method, corners and edges are created.
add_tile_to_world
(position)add_corner_to_world
(position, direction)add_edge_to_world
(position, direction)get_tile
(position)Gets Tile at Position.
detect_actors
(position)Gets all actors which are found at a specific position.
get_actors_from_pixel
(position)get_corner
(position[, direction])Gets Corner at Position.
get_edge
(position[, direction])Gets Edge at Position.
detect_position
(position)Returns True if a position is on the world.
borders
(value)Returns the World's borders, if actor is near a Border.
detect_actors_at_position
(position)Sensing actors at same position
detect_actor_at_position
(position)Sensing single actor at same position
draw_on_image
(image, position)get_from_pixel
(position)Gets world position from pixel coordinates
get_tile_from_pixel
(position)Gets nearest Tile from pixel
is_edge
(position)Returns True, if position is a edge.
is_corner
(position)Returns True, if position is a corner.
is_tile
(position)Returns True, if position is a tile.
to_pixel
(position[, size, origin])Converts WorldPosition to pixel coordinates
set_columns
(value)set_rows
(value)set_tile_size
(value)Inherited from
World
validate_parameters
(x, y)__init__
([x, y])add_right
(world[, size])add_bottom
(world[, size])remove_world
(container)get_world_connector
(actor)detect_position
(pos)Checks if position is in the world.
contains_rect
(rect)Detects if rect is completely on the world.
set_columns
(value)set_rows
(value)borders
(value)Gets all borders from a source (Position or Rect).
default_fill
(value)Set default fill color for borders and lines
Returns the current background
switch_background
(background)Switches the background
remove_background
([background])Removes a background from world
set_background
(source)Adds a new background to the world
add_background
(source)Adds a new background to the world
start
()Starts the world, if world is not running.
stop
([frames])Stops the world.
run
([fullscreen, fit_desktop, replit, ...])The method show() should always be called at the end of your program.
play_sound
(path)plays sound from path
play_music
(path)plays a music from path
stops a music
Gets the current mouse_position
Gets x-coordinate of mouse-position
Gets y-coordinate of mouse-position
gets mouse-position of last frame
Returns True, if mouse is pressed
Returns True, if mouse left button is pressed
Returns True, if mouse right button is pressed
is_in_world
(position)send_message
(message[, data])Sends broadcast message
quit
([exit_code])quits app and closes the window
reset
()Resets the world Creates a new world with init-function - recreates all actors and actors on the world.
clear
()switch_world
(new_world[, reset])Switches to another world
get_color_from_pixel
(position)Returns the color at a specific position
get_from_pixel
(position)Gets Position from pixel
to_pixel
(position)on_setup
()Overwrite or register this method to call on_setup-Actions
add_to_world
(actor, position)Adds a Actor to the world.
detect_actors
(position)Gets all actors which are found at a specific position.
get_actors_from_pixel
(pixel)repaint
()Implemented in subclasses
update
()The mainloop, called once per frame.
handle_event
(event[, data])Event handling
register
(method)Used as decorator e.g. @register def method.
unregister
(method)direction
(point1, point2)distance_to
(pos1, pos2)direction_to
(pos1, pos2)load_world_from_db
(file)Loads a sqlite db file.
load_actors_from_db
(file, actor_classes)Loads all actors from db.
save_to_db
(file)Saves the current world an all actors to database.
screenshot
([filename])Creates a screenshot in given file.
get_columns_by_width
(width)get_rows_by_height
(height)Gets a set of all events you can register
Inherited from
WorldBase
__init__
()on_change
()implemented in subclasses
add_to_window
(app, dock[, size])update_width_and_height
()repaint
()Implemented in subclasses
blit_surface_to_window_surface
()remove
(actor)Implemented in subclasses
handle_event
(event, data)get_event
(event, data)Implemented in subclasses
update
()Implemented in subclasses
get_local_position
(position)on_new_actor
(actor)on_remove_actor
(actor)Private Data Attributes:
_abc_impl
Inherited from
World
_abc_impl
_fps
_key_pressed
_animated
_is_filled
_orientation
_static
_step
_registered_methods
Inherited from
WorldBase
_abc_impl
Inherited from
ABC
_abc_impl
Private Methods:
_get_tile_factory
()_get_camera_manager_class
()_templates
()Returns Classes for Tile, Edge and Corner
_setup_tiles
()Adds Tile to World for each WorldPosition
_setup_corners
()Add all Corner to World for each Tile.
_setup_edges
()Add all Edges to World for each Tile
_get_world_connector_class
()needed by get_world_connector in parent class
_update_actor_positions
()Updates the dynamic_actors_dict.
Inherited from
World
_get_camera_manager_class
()_get_world_connector_class
()needed by get_world_connector in parent class
_create_event_manager
()_update_all_costumes
()updates costumes for all actors on the world
_act_all
()Overwritten in subclasses, e.g. physics_world.
_tick_timed_objects
()
- __init__(x=20, y=16, tile_size=40, empty=False)[source]#
Initializes the TiledWorld
- Parameters:
view_x – The number of columns
view_y – The number of rows
empty – The world has no tiles, edges, and corners. They must be created manually
- clear_tiles()[source]#
Removes all tiles, corners and edges from World
Instead of clearing the world, you can add the parameter empty to World to create a new World from scratch.
Examples
Clear and re-create world:
from miniworlds import * world = HexWorld(8, 8) @world.register def on_setup(self): self.clear_tiles() center = HexTile((4, 4)) for x in range(self.columns): for y in range(self.rows): if center.position.distance((x, y)) < 2: tile = self.add_tile_to_world((x, y)) tt = Actor() t.center = tile.position world.run()
Create a new world from scratch
Note
This variant is faster, because Tiles are not created twice
from miniworlds import * world = HexWorld(8, 8, empty=True) @world.register def on_setup(self): center = HexTile((4, 4)) for x in range(self.columns): for y in range(self.rows): if center.position.distance((x, y)) < 2: tile = self.add_tile_to_world((x, y)) tile.create_actor() world.run()
- detect_actor_at_position(position)[source]#
Sensing single actor at same position
Faster than sensing_actors, but only the first found actor is recognized.
- detect_actors(position)[source]#
Gets all actors which are found at a specific position.
- Return type:
- Parameters:
position – Position, where actors should be searched.
- Returns:
A list of actors
Examples
Get all actors at mouse position:
position = world.get_mouse_position() actors = world.get_actors_by_pixel(position)
- get_corner(position, direction=None)[source]#
Gets Corner at Position.
Raises CornerNotFoundError, if Tile does not exists.
Examples
Get corner from actor:
corner = world.get_corner(actor.position)
Get corner from world-position and direction
from miniworlds import * from miniworlds import * world = TiledWorld(6, 3) world.grid = True last_corner = None corner = Corner((3,1), "nw") t2 = Actor() t1.center = corner.position t2.fill_color = (255,0,0) corner=world.get_corner((3,1),"nw") assert(corner.get_actors()[0] == t2) world.run()
- Parameters:
position – Position on World
direction – if direction is not None, position is interpreted as tile-world-position
- Returns
next corner, if position exists
- get_edge(position, direction=None)[source]#
Gets Edge at Position.
Raises EdgeNotFoundError, if Tile does not exists.
Examples
Get edge from actor:
tile = world.get_edge(actor.position)
Get edge from world-position and direction
from miniworlds import * world = TiledWorld(6, 3) world.grid = True last_corner = None edge=world.get_edge((5,1),"w") assert(edge.get_actors()[0] == t3) world.run()
- Parameters:
position – Position on World
- Returns:
Edge on Position, if position exists
- get_tile(position)[source]#
Gets Tile at Position.
Raises TileNotFoundError, if Tile does not exists.
Examples
Get tile from actor:
tile = world.get_tile(actor.position)
Full example:
from miniworlds import * world = TiledWorld(6, 3) world.grid = True last_corner = None tile = Tile((1,1)) t1 = Actor() t1.center = tile.position t1.fill_color = (255,255,255) tile=world.get_tile((1,1)) assert(tile.get_actors()[0] == t1) world.run()
- property grid#
Displays grid overlay on background.