World

Contents

World#

Die Basisklasse für all deine Welten.

World#

class miniworlds.worlds.world.World(x=400, y=400)[source]#

A world is a playing field on which actors can move.

A world has a background and provides basic functions for the positioning of actors and for the collision detection of actors, which can be queried via the sensors of the actors.

You can create your own world by creating a class that inherits from World or you can directly create a world object of type World or one of its child classes (TiledWorld, PhysicsWorld, …).

World

A world for pixel accurate games.

  • The position of a actor on a World is the pixel at topleft of actor.

  • New actors are created with top-left corner of actor rect at position.

  • Two actors collide when their sprites overlap.

Asteroids

Other worlds:

  • TiledWorld: For worlds using Tiles, like rogue-like rpgs, see TiledWorld)

  • PhysicsWorld: For worlds using the PhysicsEngine, see PhysicsWorld)

Examples

Creating a TiledWorld Object:

from miniworlds import *

my_world = TiledWorld()
my_world.columns = 30
my_world.rows = 20
my_world.tile_size = 20

Creating a TiledWorld-Subclass.

import miniworlds

class MyWorld(miniworlds.TiledWorld):

    def on_setup(self):
        self.columns = 30
        self.rows = 20
        self.tile_size = 20

Creating a World Object:

from miniworlds import *

my_world = World()
my_world.columns = 300
my_world.rows = 200

Creating a World Subclass

import miniworlds

class MyWorld(miniworlds.World):

    def on_setup(self):
        self.columns = 300
        self.rows = 200

See also

  • See: World

  • See: TiledWorld

Parameters:
  • view_x – columns of new world (default: 40)

  • view_y – rows of new world (default:40)

  • tile_size – Size of tiles (1 for normal worlds, can differ for Tiledworlds)

Public Data Attributes:

subclasses

surface

class_name

step

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

fps

Frames per second shown on the screen.

world_size_x

The x-world_size (defaults to view_size)

world_size_y

The y-world_size (defaults to view_size)

columns

rows

size

Set the size of world

default_fill_color

Set default fill color for borders and lines

default_is_filled

default_stroke_color

Set default stroke color for borders and lines.

default_border_color

Set default border color for borders and lines.

default_border

Sets default border color for actors

backgrounds

Returns all backgrounds of the world as list.

background

Returns the current background

has_background

registered_events

image

The current displayed image

fill_color

color

window

Gets the parent window

actors

event_manager

backgrounds_manager

mouse_manager

ask

is_display_initialized

is_running

is_listening

frame

clock

app

music

sound

collision_manager

timed_objects

dynamic_actors

Inherited from WorldBase

surface

window

size

rect

topleft

window_docking_position

width

height

Public Methods:

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

get_background()

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.

start_listening()

stop_listening()

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

The method show() should always be called at the end of your program.

init_display()

play_sound(path)

plays sound from path

stop_sounds()

play_music(path)

plays a music from path

stop_music()

stops a music

get_mouse_position()

Gets the current mouse_position

get_mouse_x()

Gets x-coordinate of mouse-position

get_mouse_y()

Gets y-coordinate of mouse-position

get_prev_mouse_position()

gets mouse-position of last frame

is_mouse_pressed()

Returns True, if mouse is pressed

is_mouse_left_pressed()

Returns True, if mouse left button is pressed

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

get_events()

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

_fps

_key_pressed

_animated

_is_filled

_orientation

_static

_step

_registered_methods

Inherited from WorldBase

_abc_impl

Inherited from ABC

_abc_impl

Private Methods:

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


add_background(source)[source]#

Adds a new background to the world

If multiple backgrounds are added, the last adds background will be set as active background.

Return type:

Background

Parameters:

source – The path to the first image of the background or a color (e.g. (255,0,0) for red or “images/my_background.png” as path to a background.

Examples

Add multiple Backgrounds:

from miniworlds import *

world = World()
world.add_background((255, 0 ,0)) # red
world.add_background((0, 0 ,255)) # blue
world.run() # Shows a blue world.
Returns:

The new created background.

add_bottom(world, size=100)[source]#
add_right(world, size=100)[source]#
property background: Background#

Returns the current background

property backgrounds: list#

Returns all backgrounds of the world as list.

borders(value)[source]#

Gets all borders from a source (Position or Rect).

Return type:

list

Parameters:

value – Position or rect

Returns:

A list of borders, e.g. [“left”, “top”], if rect is touching the left a top border.

property class_name: str#
clear()[source]#
property color#
property columns: int#
contains_rect(rect)[source]#

Detects if rect is completely on the world.

Parameters:

rect – A rectangle as tuple (top, left, width, height)

property default_border#

Sets default border color for actors

Note

You must also set a border for actor.

Examples

Set default border for actors:

from miniworlds import *

world = World(210,80)
world.default_border_color = (0,0, 255)
world.default_border = 1

t = Actor((10,10))

world.run()
property default_border_color#

Set default border color for borders and lines.

Note

world.default_border_color does not have an effect, if no border is set.

You must also set world.border > 0.

Examples

Create actors with and without with border

from miniworlds import *

world = World(210,80)
world.default_border_color = (0,0, 255)
world.default_border = 1

t = Actor((10,10))

t2 = Actor ((60, 10))
t2.border_color = (0,255, 0)
t2.border = 5 # overwrites default border

t3 = Actor ((110, 10))
t3.border = None # removes border

t4 = Actor ((160, 10))
t4.add_costume("images/player.png") # border for sprite

world.run()

Output:

borders
default_fill(value)[source]#

Set default fill color for borders and lines

property default_fill_color#

Set default fill color for borders and lines

property default_is_filled#
property default_stroke_color#

Set default stroke color for borders and lines. (equivalent to border-color)

detect_actors(position)[source]#

Gets all actors which are found at a specific position.

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_position(pos)[source]#

Checks if position is in the world.

Returns:

True, if Position is in the world.

direction(point1, point2)[source]#
direction_to(pos1, pos2)[source]#
Return type:

float

static distance_to(pos1, pos2)[source]#
property fill_color#
property fps: int#

Frames per second shown on the screen.

This controls how often the screen is redrawn. However, the game logic can be called more often or less often independently of this with world.speed.

Examples

world.speed = 10
world.fps = 24
def act(self):
    nonlocal i
    i = i + 1
    if world.frame == 120:
        test_instance.assertEqual(i, 13)
        test_instance.assertEqual(world.frame, 120)
get_actors_from_pixel(pixel)[source]#
get_background()[source]#

Returns the current background

Return type:

Background

get_color_from_pixel(position)[source]#

Returns the color at a specific position

Examples: :rtype: tuple

from miniworlds import *

world = World((100,60))

@world.register
def on_setup(self):
    self.add_background((255,0,0))
    print(self.get_color_from_pixel((5,5)))

world.run()

Output: (255, 0, 0, 255)

get color of red screen
Parameters:

position – The position to search for

Returns:

The color

get_columns_by_width(width)[source]#
get_events()[source]#

Gets a set of all events you can register

get_from_pixel(position)[source]#

Gets Position from pixel

PixelWorld: the pixel position is returned TiledWorld: the tile-position is returned

Parameters:

position (Tuple) – Position as pixel coordinates

Return type:

Optional[tuple]

Returns:

The pixel position, if position is on the world, None if position is not on World.

get_mouse_position()[source]#

Gets the current mouse_position

Return type:

Optional[tuple]

Returns:

Returns the mouse position if mouse is on the world. Returns None otherwise

Examples

Create circles at current mouse position:

from miniworlds import *

world = PixelWorld()

@world.register
def act(self):
    c = Circle(world.get_mouse_position(), 40)
    c.color = (255,255,255, 100)
    c.border = None

world.run()

Output:

Circles at mouse-position
get_mouse_x()[source]#

Gets x-coordinate of mouse-position

Return type:

int

get_mouse_y()[source]#

Gets y-coordinate of mouse-position

Return type:

int

get_prev_mouse_position()[source]#

gets mouse-position of last frame

get_rows_by_height(height)[source]#
get_world_connector(actor)[source]#
Return type:

WorldConnector

property has_background: bool#
property image: Surface#

The current displayed image

init_display()[source]#
is_in_world(position)[source]#
Return type:

bool

is_mouse_left_pressed()[source]#

Returns True, if mouse left button is pressed

Return type:

bool

is_mouse_pressed()[source]#

Returns True, if mouse is pressed

Return type:

bool

is_mouse_right_pressed()[source]#

Returns True, if mouse right button is pressed

Return type:

bool

load_actors_from_db(file, actor_classes)[source]#

Loads all actors from db. Usually you load the actors in __init__() or in on_setup()

Return type:

List[Actor]

Parameters:
  • file (str) – reference to db file

  • actor_classes (list) – a list of all Actor Classes which should be imported.

Returns:

All Actors

Return type:

[type]

load_world_from_db(file)[source]#

Loads a sqlite db file.

on_setup()[source]#

Overwrite or register this method to call on_setup-Actions

play_music(path)[source]#

plays a music from path

Parameters:

path – The path to the music

Returns:

play_sound(path)[source]#

plays sound from path

quit(exit_code=0)[source]#

quits app and closes the window

register(method)[source]#

Used as decorator e.g. @register def method…

Return type:

Callable

property registered_events: set#
remove_background(background=None)[source]#

Removes a background from world

Parameters:

background – The index of the new background. Defaults to -1 (last background) or an Appearance

remove_world(container)[source]#
repaint()[source]#

Implemented in subclasses

reset()[source]#

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

Examples

Restarts flappy the bird game after collision with pipe:

def on_sensing_collision_with_pipe(self, other, info):
    self.world.is_running = False
    self.world.reset()
property rows: int#
run(fullscreen=False, fit_desktop=False, replit=False, event=None, data=None)[source]#

The method show() should always be called at the end of your program. It starts the mainloop.

Examples

A minimal miniworlds-program:

from miniworlds import *
world = TiledWorld()
actor = Actor()
world.run()

Output:

Minimal program
save_to_db(file)[source]#

Saves the current world an all actors to database. The file is stored as db file and can be opened with sqlite.

Parameters:

file – The file as relative location

Returns:

screenshot(filename='screenshot.jpg')[source]#

Creates a screenshot in given file.

Parameters:

filename – The location of the file. The folder must exist. Defaults to “screenshot.jpg”.

send_message(message, data=None)[source]#

Sends broadcast message

A message can be received by the world or any actor on world

set_background(source)[source]#

Adds a new background to the world

If multiple backgrounds are added, the last adds background will be set as active background.

Return type:

Background

Parameters:

source – The path to the first image of the background or a color (e.g. (255,0,0) for red or “images/my_background.png” as path to a background.

Examples

Add multiple Backgrounds:

from miniworlds import *

world = World()
world.add_background((255, 0 ,0)) # red
world.add_background((0, 0 ,255)) # blue
world.run() # Shows a blue world.
Returns:

The new created background.

set_columns(value)[source]#
set_rows(value)[source]#
property size: tuple#

Set the size of world

Examples

Create a world with 800 columns and 600 rows:

world = miniworlds.PixelWorld()
world.size = (800, 600)
start()[source]#

Starts the world, if world is not running.

start_listening()[source]#
property step: int#

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

If e.g. step = 30, the game logic will be called every 30th-frame.

Note

You can adjust the frame-rate with world.fps

Examples

Set speed and fps.

from miniworlds import *

world = World()
world.size = (120,210)

@world.register
def on_setup(self):
    world.fps = 1
    world.speed = 3

@world.register
def act(self):

world.run()

Output:

` 3 6 9 12 15 `

stop(frames=0)[source]#

Stops the world.

Parameters:

frames (int, optional) – If frames is set, world will be stopped in n frames. . Defaults to 0.

stop_listening()[source]#
stop_music()[source]#

stops a music

Returns:

stop_sounds()[source]#
subclasses = None#
property surface#
switch_background(background)[source]#

Switches the background

Return type:

Background

Parameters:

background – The index of the new background or an Appearance. If index = -1, the next background will be selected

Examples

Switch between different backgrounds:

from miniworlds import *

world = World()
actor = Actor()

world.add_background("images/1.png")
world.add_background((255, 0, 0, 255))
world.add_background("images/2.png")

@timer(frames = 40)
def switch():
    world.switch_background(0)

@timer(frames = 80)
def switch():
    world.switch_background(1)

@timer(frames = 160)
def switch():
    world.switch_background(2)

world.run()

Output:

Switch background
Returns:

The new background

switch_world(new_world, reset=False)[source]#

Switches to another world

Parameters:

new_world (World) – _description_

to_pixel(position)[source]#
unregister(method)[source]#
update()[source]#

The mainloop, called once per frame.

Called in app.update() when update() from all containers is called.

validate_parameters(x, y)[source]#
property window: App#

Gets the parent window

Returns:

The window

property world_size_x: int#

The x-world_size (defaults to view_size)

property world_size_y: int#

The y-world_size (defaults to view_size)