World > Tiled World#
API Reference#
- class miniworlds.worlds.tiled_world.tiled_world.TiledWorld(x=20, y=16, tile_size=40, empty=False)[Quellcode]#
Ein TiledWorld ist eine Welt, in der jeder Actor auf einer Kachel platziert ist.
Mit der Klase Tiled World kannst du RPGs und Brettspiele verwirklichen.
Jeder Actor in einer TiledWorld kann auf einem Tile, an einer Ecke zwischen Tiles oder an einer Kante zwischen Tiles platziert werden.
Beispiele
Erstelle Actor auf Kachel, Ecke und Kante:
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()
Öffentliche Datenattribute:
Displays grid overlay on background.
Gets the number of horizontal pixels (columns) visible in the world.
Gets the number of vertical pixels (rows) visible in the world.
Tile size of each tile, if world has tiles
Inherited from
WorldBackward-compatible docking API for older example code.
How often world logic runs relative to the frame loop.
Frames per second of the render loop.
Gets the horizontal size of the world in pixels.
Gets the vertical size of the world in pixels.
Gets the number of horizontal pixels (columns) visible in the world.
Gets the number of vertical pixels (rows) visible in the world.
Gets the world size as a tuple (width, height), in pixels.
Returns the currently active background.
Returns True if the world has at least one background appearance.
is_runningevent_managerInherited from
WorldBasewindowsizetopleftwidthheightclass_nameregistered_eventsReturns the set of all event names that are currently registered.
is_runningevent_managerÖffentliche Methoden:
__init__([x, y, tile_size, empty])Initializes the TiledWorld
Removes all tiles, corners and edges from World
add_tile_to_world(position)Creates and registers a tile at a world grid position.
add_corner_to_world(position, direction)Creates and registers a corner for a tile position and direction.
add_edge_to_world(position, direction)Creates and registers an edge for a tile position and 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)Gibt die Grenzen der Welt zurück, wenn der Actor in der Nähe einer Grenze ist.
detect_actors_at_position(position)Returns all actors located at the given tile/world position.
detect_actor_at_position(position)Sensing single actor at same position
draw_on_image(image, position)Draws an image onto the tiled world background at a tile position.
get_from_pixel(position)Returns the tile/world position for a pixel coordinate.
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)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)Sets the tile size in pixels and refreshes camera/background caches.
Inherited from
World__init__([x, y])Initializes the world and all internal managers needed for runtime operation.
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.
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.
switch_world(new_world[, reset])Switch the active scene to another world.
load_world_from_db(file)Load a saved world from a sqlite database file and activate it.
load_actors_from_db(file, actor_classes)Load actors from a sqlite database file into the current world.
save_to_db(file)Save the current world and its actors to a sqlite database file.
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_implInherited from
World_abc_impl_default_start_runningInherited from
WorldBase_abc_impl_default_start_runningPrivate Methoden:
_get_tile_factory()_get_camera_manager_class()_after_init_setup()In this method, corners and edges are created.
_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)_get_initialization_facade()_get_background_facade()_get_runtime_facade()_clear()Clears the world's state: event queue, all backgrounds, and all actors.
Inherited from
WorldBase_after_init_setup()_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)[Quellcode]#
Initializes the TiledWorld
- Parameter:
view_x – Die Anzahl der Spalten
view_y – Die Anzahl der Zeilen
empty – Die Welt hat keine tiles, edges und corners. Sie müssen manuell erstellt werden
- add_corner_to_world(position, direction)[Quellcode]#
Creates and registers a corner for a tile position and direction.
Existing corners are merged when multiple tiles share the same corner.
- Parameter:
position – Base tile position as
(column, row).direction – Corner direction key (for example
"nw").
- Rückgabe:
The registered corner object.
- add_edge_to_world(position, direction)[Quellcode]#
Creates and registers an edge for a tile position and direction.
Existing edges are merged when neighboring tiles describe the same edge.
- Parameter:
position – Base tile position as
(column, row).direction – Edge direction key (for example
"n"or"w").
- Rückgabe:
The registered edge object.
- add_tile_to_world(position)[Quellcode]#
Creates and registers a tile at a world grid position.
- Parameter:
position – Tile position as
(column, row).- Rückgabe:
The created tile instance.
- borders(value)[Quellcode]#
Gibt die Grenzen der Welt zurück, wenn der Actor in der Nähe einer Grenze ist.
- Rückgabetyp:
- clear_tiles()[Quellcode]#
Removes all tiles, corners and edges from World
Anstatt die Welt zu leeren, können Sie den Parameter empty zu World hinzufügen, um eine neue Welt von Grund auf zu erstellen.
Beispiele
Welt löschen und neu erstellen:
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()
Erschaffe eine neue Welt von Grund auf
Bemerkung
Diese Variante ist schneller, weil die Kacheln nicht zweimal erstellt werden
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.
- Rückgabe:
The width of the camera view in pixels.
- detect_actor_at_position(position)[Quellcode]#
Sensing single actor at same position
Schneller als sensing_actors, aber nur der zuerst gefundene Actor wird erkannt.
- detect_actors(position)[Quellcode]#
Gets all actors which are found at a specific position (in global world coordinates)
- Rückgabetyp:
- Parameter:
position – Position, wo Actor gesucht werden sollten.
- Rückgabe:
Eine Liste von Actorn
Beispiele
Alle Actors an der Mausposition abrufen:
position = world.mouse.get_position() actors = world.get_actors_from_pixel(position)
- detect_actors_at_position(position)[Quellcode]#
Returns all actors located at the given tile/world position.
- draw_on_image(image, position)[Quellcode]#
Draws an image onto the tiled world background at a tile position.
- Parameter:
image – The image/surface to draw.
position – Tile position as
(column, row).
- get_actors_from_pixel(position)[Quellcode]#
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.
- Rückgabetyp:
- Parameter:
pixel – A tuple (x, y) representing the screen pixel.
- Rückgabe:
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)[Quellcode]#
Gets Corner at Position.
Löst CornerNotFoundError aus, wenn Tile nicht existiert.
Beispiele
Winkel vom Actor erhalten:
corner = world.get_corner(actor.position)
Erhalte Ecke von Weltposition und Richtung
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()
- Parameter:
position – Position in der Welt
direction – wenn die Richtung nicht None ist, wird die Position als Kachel-Welt-Position interpretiert
- Rückgaben
nächste Ecke, falls Position existiert
- get_edge(position, direction=None)[Quellcode]#
Gets Edge at Position.
Löst EdgeNotFoundError aus, wenn Tile nicht existiert.
Beispiele
Kante vom Actor erhalten:
tile = world.get_edge(actor.position)
Kante von Weltposition und Richtung erhalten
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()
- Parameter:
position – Position in der Welt
- Rückgabe:
Kante auf Position, falls Position existiert
- get_from_pixel(position)[Quellcode]#
Returns the tile/world position for a pixel coordinate.
- Parameter:
position – Pixel position as
(x, y).- Rückgabe:
The corresponding world position, or
Noneif the pixel lies outside the camera view.
- get_tile(position)[Quellcode]#
Gets Tile at Position.
Löst TileNotFoundError aus, wenn Tile nicht existiert.
Beispiele
Kachel vom Actor holen:
tile = world.get_tile(actor.position)
Vollständiges Beispiel:
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()
- get_tile_from_pixel(position)[Quellcode]#
Gets nearest Tile from pixel
- property grid#
Displays grid overlay on background.
- is_corner(position)[Quellcode]#
Returns True, if position is a corner.
- is_edge(position)[Quellcode]#
Returns True, if position is a edge.
- is_tile(position)[Quellcode]#
Returns True, if position is a tile.
- property rows: int#
Gets the number of vertical pixels (rows) visible in the world.
- Rückgabe:
The height of the camera view in pixels.
- set_columns(value)[Quellcode]#
Internal method to set columns and sync world width.
- Parameter:
value – New column count (width in pixels).
- set_rows(value)[Quellcode]#
Internal method to set rows and sync world height.
- Parameter:
value – New row count (height in pixels).
- set_tile_size(value)[Quellcode]#
Sets the tile size in pixels and refreshes camera/background caches.
- Parameter:
value – New tile size in pixels.
- property tile_size: int#
Tile size of each tile, if world has tiles
- Rückgabe:
Die Kachelgröße in Pixeln.
- to_pixel(position, size=(0, 0), origin=(0, 0))[Quellcode]#
Converts WorldPosition to pixel coordinates