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.

TiledWorld

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:

grid

Displays grid overlay on background.

columns

Gets the number of horizontal pixels (columns) visible in the world.

rows

Gets the number of vertical pixels (rows) visible in the world.

tile_size

Tile size of each tile, if world has tiles

Inherited from World

subclasses

tick_rate

Tick rate defines how often the method act() will be called.

fps

Frames per second shown on the screen.

world_size_x

Gets the horizontal size of the world in pixels.

world_size_y

Gets the vertical size of the world in pixels.

columns

Gets the number of horizontal pixels (columns) visible in the world.

rows

Gets the number of vertical pixels (rows) visible in the world.

size

Gets the world size as a tuple (width, height), in pixels.

background

Returns the currently active background.

has_background

Returns True if the world has at least one background appearance.

actors

event_manager

clock

frame

is_running

actors_fixed_size

app

backgrounds

layout

data

mouse

draw

music

sound

Inherited from WorldBase

window

size

topleft

width

height

class_name

registered_events

Returns the set of all event names that are currently registered.

Public Methods:

__init__([x, y, tile_size, empty])

Initializes the TiledWorld

clear_tiles()

Removes all tiles, corners and edges from World

setup_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 (in global world coordinates)

get_actors_from_pixel(position)

Returns a list of all actors located at the given screen pixel position.

get_corner(position[, direction])

Gets Corner at Position.

get_edge(position[, direction])

Gets Edge at Position.

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

get_edge_points()

get_corner_points()

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)

Internal method to set columns and sync world width.

set_rows(value)

Internal method to set rows and sync world height.

set_tile_size(value)

Inherited from World

__init__([x, y])

contains_position(pos)

Checks if position is in the world.

contains_rect(rect)

Returns True if the entire rectangle is fully inside the world.

contains_rect_any(rect)

Returns True if any part of the rectangle is inside the world.

set_columns(value)

Internal method to set columns and sync world width.

set_rows(value)

Internal method to set rows and sync world height.

get_background()

Returns the current active background from the backgrounds manager.

switch_background(background)

Switches the current background to a specified one.

remove_background([background])

Removes a background from the world.

set_background(source)

Sets a new background and replaces the current active background.

add_background(source)

Adds a new background to the world and sets it as the active one.

start()

Starts or resumes the world.

stop([frames])

Stops the world immediately or after a delay in frames.

run([fullscreen, fit_desktop, replit, ...])

Starts the main application loop of the Miniworlds engine.

is_in_world(position)

Checks whether a given world position lies within the world's boundaries.

send_message(message[, data])

Sends a broadcast message to the world and all actors.

quit([exit_code])

Immediately quits the application and closes the game window.

reset()

Resets the world Creates a new world with init-function - recreates all actors and actors on the world.

get_from_pixel(position)

Converts a screen pixel position into a valid world position if inside bounds.

to_pixel(position)

Converts a world position to a screen pixel position.

on_setup()

Hook method to define initial setup logic when the world is created.

detect_actors(position)

Gets all actors which are found at a specific position (in global world coordinates)

get_actors_from_pixel(pixel)

Returns a list of all actors located at the given screen pixel position.

distance_to(pos1, pos2)

Calculates the Euclidean distance between two positions.

direction_to(pos1, pos2)

Calculates the angle from pos1 to pos2 in degrees.

Inherited from WorldBase

__init__()

remove(actor)

Implemented in subclasses

on_change()

implemented in subclasses

on_new_actor(actor)

on_remove_actor(actor)

get_world_connector(actor)

screenshot([filename])

Saves a screenshot of the current window surface to a file.

get_events()

Prints a list of all events that can be registered in this world.

register(method)

Registers a method as a world event handler.

Private Data Attributes:

_abc_impl

Inherited from World

_abc_impl

_fps

_step

_key_pressed

_animated

_timed_objects

_dynamic_actors

_registered_methods

_mainloop

_collision_manager

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

_validate_parameters(x, y)

_clear()

Clears the world's state: event queue, all backgrounds, and all actors.

Inherited from WorldBase

_get_mainloopmanager_class()

_get_camera_manager_class()

_get_world_connector_class()

needed by get_world_connector in parent class

_create_event_manager()

_unregister(method)

Unregisters a previously registered world method.

_start_listening()

Enables input listening for the world.

_stop_listening()

Disables input listening for the world.


__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

add_corner_to_world(position, direction)[source]#
add_edge_to_world(position, direction)[source]#
add_tile_to_world(position)[source]#
borders(value)[source]#

Returns the World’s borders, if actor is near a Border.

Return type:

list

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()
property columns: int#

Gets the number of horizontal pixels (columns) visible in the world.

Returns:

The width of the camera view in pixels.

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 (in global world coordinates)

Return type:

List[Actor]

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)
detect_actors_at_position(position)[source]#

Sensing actors at same position

draw_on_image(image, position)[source]#
get_actors_from_pixel(position)[source]#

Returns a list of all actors located at the given screen pixel position.

This checks whether each actor’s screen-rect overlaps with the given pixel.

Return type:

List[Actor]

Parameters:

pixel – A tuple (x, y) representing the screen pixel.

Returns:

A list of Actor instances under the given pixel.

Example

>>> actors = world.get_actors_from_pixel((120, 80))
>>> for actor in actors:
...     print(actor.name)
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_corner_points()[source]#
Return type:

Dict[Tuple, Tuple[float, float]]

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_edge_points()[source]#
Return type:

Dict[Tuple, Tuple[float, float]]

get_from_pixel(position)[source]#

Gets world position from pixel coordinates

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()
Parameters:

position (Tuple[float, float]) – Position on World

Returns:

Tile on Position, if position exists

get_tile_from_pixel(position)[source]#

Gets nearest Tile from pixel

property grid#

Displays grid overlay on background.

is_corner(position)[source]#

Returns True, if position is a corner.

is_edge(position)[source]#

Returns True, if position is a edge.

is_tile(position)[source]#

Returns True, if position is a tile.

property rows: int#

Gets the number of vertical pixels (rows) visible in the world.

Returns:

The height of the camera view in pixels.

set_columns(value)[source]#

Internal method to set columns and sync world width.

Parameters:

value – New column count (width in pixels).

set_rows(value)[source]#

Internal method to set rows and sync world height.

Parameters:

value – New row count (height in pixels).

set_tile_size(value)[source]#
setup_world()[source]#

In this method, corners and edges are created.

property tile_size: int#

Tile size of each tile, if world has tiles

Returns:

The tile-size in pixels.

to_pixel(position, size=(0, 0), origin=(0, 0))[source]#

Converts WorldPosition to pixel coordinates