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.
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:
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
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
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
_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:
- 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.
- property background: Background#
Returns the current background
- borders(value)[source]#
Gets all borders from a source (Position or Rect).
- Return type:
- Parameters:
value – Position or rect
- Returns:
A list of borders, e.g. [“left”, “top”], if rect is touching the left a top border.
- property color#
- 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:
- 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:
- 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.
- 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_color_from_pixel(position)[source]#
Returns the color at a specific position
Examples: :rtype:
tuple
- Parameters:
position – The position to search for
- Returns:
The color
- get_from_pixel(position)[source]#
Gets Position from pixel
PixelWorld: the pixel position is returned TiledWorld: the tile-position is returned
- get_mouse_position()[source]#
Gets the current mouse_position
- Return type:
- 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:
- property image: Surface#
The current displayed image
- load_actors_from_db(file, actor_classes)[source]#
Loads all actors from db. Usually you load the actors in __init__() or in on_setup()
- 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
- 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()
- 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:
- 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:
- 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.
- 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)
- 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.
- subclasses = None#
- property surface#
- switch_background(background)[source]#
Switches the background
- Return type:
- 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:
- Returns:
The new background
- switch_world(new_world, reset=False)[source]#
Switches to another world
- Parameters:
new_world (World) – _description_
- update()[source]#
The mainloop, called once per frame.
Called in app.update() when update() from all containers is called.
- property window: App#
Gets the parent window
- Returns:
The window