Welt > Tiled World#

class miniworlds.worlds.tiled_world.tiled_world.TiledWorld(x=20, y=16, tile_size=40, empty=False)[Quellcode]#

aus der Typisierung Ein TiledWorld ist eine Welt, in der jeder Akteur in einem Kachel platziert ist.

Mit Tiled World können Sie RPGs und Brettspiele verwirklichen.

TiledWorld

Jeder Actor in einer TiledWorld kann auf einem Tile, an einer Ecke zwischen Tiles oder an einer Kante zwischen Tiles platziert werden.

Beispiele

Erstelle Akteur 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:

grid

Zeigt Rasterüberlagerung im Hintergrund an.

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

Kachelgröße jeder Kachel, falls die Welt Kacheln hat

Inherited from World

subclasses

tick_rate

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

fps

Bilder pro Sekunde auf dem Bildschirm angezeigt.

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.

Öffentliche Methoden:

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

Initialisiert die TiledWorld

clear_tiles()

Entfernt alle Fliesen, Ecken und Kanten aus der Welt

setup_world()

In dieser Methode werden Ecken und Kanten erstellt.

add_tile_to_world(position)

add_corner_to_world(position, direction)

add_edge_to_world(position, direction)

get_tile(position)

Erhält Kachel an 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])

Erhält Ecke an Position.

get_edge(position[, direction])

Erhält Kante an Position.

borders(value)

Gibt die Grenzen der Welt zurück, wenn der Akteur in der Nähe einer Grenze ist.

detect_actors_at_position(position)

Akteure an derselben Position erfassen

detect_actor_at_position(position)

Einzelnen Akteur an derselben Position erfassen

draw_on_image(image, position)

get_from_pixel(position)

Ermittelt die Weltposition aus Pixelkoordinaten

get_tile_from_pixel(position)

Erhält die nächstgelegene Kachel vom Pixel

get_edge_points()

get_corner_points()

is_edge(position)

Gibt True zurück, wenn die Position ein Rand ist.

is_corner(position)

Gibt True zurück, wenn die Position eine Ecke ist.

is_tile(position)

Gibt True zurück, wenn die Position eine Kachel ist.

to_pixel(position[, size, origin])

Konvertiert WorldPosition in Pixelkoordinaten

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)

Prüft, ob die Position in der Welt ist.

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

Setzt die Welt zurück Erstellt eine neue Welt mit der Init-Funktion - erstellt alle Akteure und Akteure in der Welt neu.

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)

In Unterklassen implementiert

on_change()

in Unterklassen implementiert

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 Datenattribute:

_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 Methoden:

_get_tile_factory()

_get_camera_manager_class()

_templates()

Gibt Klassen für Kachel, Kante und Ecke zurück

_setup_tiles()

Fügt für jede Weltposition eine Kachel zur Welt hinzu

_setup_corners()

Füge alle Ecken zur Welt für jede Kachel hinzu.

_setup_edges()

Füge alle Kanten für jede Kachel zur Welt hinzu

_get_world_connector_class()

benötigt von get_world_connector in der übergeordneten Klasse

_update_actor_positions()

Aktualisiert das 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()

benötigt von get_world_connector in der übergeordneten Klasse

_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]#

Initialisiert die 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]#
add_edge_to_world(position, direction)[Quellcode]#
add_tile_to_world(position)[Quellcode]#
borders(value)[Quellcode]#

Gibt die Grenzen der Welt zurück, wenn der Akteur in der Nähe einer Grenze ist.

Rückgabetyp:

list

clear_tiles()[Quellcode]#

Entfernt alle Fliesen, Ecken und Kanten aus der Welt

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]#

Einzelnen Akteur an derselben Position erfassen

Schneller als sensing_actors, aber nur der zuerst gefundene Akteur wird erkannt.

detect_actors(position)[Quellcode]#

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

Rückgabetyp:

List[Actor]

Parameter:

position – Position, wo Actor gesucht werden sollten.

Rückgabe:

Eine Liste von Actorn

Beispiele

Alle Akteure an der Mausposition abrufen:

position = world.get_mouse_position()
actors = world.get_actors_by_pixel(position)
detect_actors_at_position(position)[Quellcode]#

Akteure an derselben Position erfassen

draw_on_image(image, position)[Quellcode]#
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:

List[Actor]

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]#

Erhält Ecke an Position.

Löst CornerNotFoundError aus, wenn Tile nicht existiert.

Beispiele

Winkel vom Akteur 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_corner_points()[Quellcode]#
Rückgabetyp:

Dict[Tuple, Tuple[float, float]]

get_edge(position, direction=None)[Quellcode]#

Erhält Kante an Position.

Löst EdgeNotFoundError aus, wenn Tile nicht existiert.

Beispiele

Kante vom Akteur 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_edge_points()[Quellcode]#
Rückgabetyp:

Dict[Tuple, Tuple[float, float]]

get_from_pixel(position)[Quellcode]#

Ermittelt die Weltposition aus Pixelkoordinaten

get_tile(position)[Quellcode]#

Erhält Kachel an Position.

Löst TileNotFoundError aus, wenn Tile nicht existiert.

Beispiele

Kachel vom Akteur 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()
Parameter:

position (Tuple[float, float]) – Position in der Welt

Rückgabe:

Kachel auf Position, falls Position existiert

get_tile_from_pixel(position)[Quellcode]#

Erhält die nächstgelegene Kachel vom Pixel

property grid#

Zeigt Rasterüberlagerung im Hintergrund an.

is_corner(position)[Quellcode]#

Gibt True zurück, wenn die Position eine Ecke ist.

is_edge(position)[Quellcode]#

Gibt True zurück, wenn die Position ein Rand ist.

is_tile(position)[Quellcode]#

Gibt True zurück, wenn die Position eine Kachel ist.

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]#
setup_world()[Quellcode]#

In dieser Methode werden Ecken und Kanten erstellt.

property tile_size: int#

Kachelgröße jeder Kachel, falls die Welt Kacheln hat

Rückgabe:

Die Kachelgröße in Pixeln.

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

Konvertiert WorldPosition in Pixelkoordinaten