miniworlds#
miniworlds ist eine in Python und Pygame geschriebene Spiel-Engine, die für SchülerInnen entwickelt wurde, um 2D-Miniwelten und Spiele zu erstellen.
Die Erstellung der ersten Spiele mit minigames ist einfach und macht Spaß.
miniworlds und miniworldmaker
miniworlds ist das Nachfolgeprojekt des miniworldmakers. Sehr vieles ist ähnlich, manche Bezeichnungen und Funktionen unterscheiden sich vom miniworldmaker. (z.B. aus Tokens wurden Actors, aus Boards wurden Worlds)
Examples#
Two actors, controlled by keyboard#
Two actors that can be controlled with the keyboard.
from miniworlds import World, Actor
world = World()
world.add_background("images/grass.jpg")
player = Actor((90,90))
player.add_costume("images/player.png")
player.costume.orientation = -90
@player.register
def on_key_down_w(self):
player.y = player.y - 1
player2 = Actor((180,180))
player2.add_costume("images/player.png")
player2.costume.orientation = -90
@player2.register
def on_key_pressed_s(self):
player2.y = player2.y - 1
world.run()
Skeetshooting#
A randomly placed target that can be “shot down” by the mouse.
from miniworlds import World, Actor, Number
import random
world = World(400, 400)
world.add_background("images/skeetshooting.png")
target = Actor((100, 100))
target.add_costume("images/target-red.png")
target.orientation = -90
target.size = (80,80)
cooldown = 5
hits = Number((20,20), 0)
@target.register
def act(self):
global cooldown
if self.world.frame % 50 == 0: # every 50th frame:
target.position = (random.randint(0, 400), random.randint(0, 400))
cooldown -= 1
@target.register
def on_clicked_left(self, position):
global cooldown, hits
if cooldown < 0:
hits += 1
cooldown = 10
world.run()
Aircrafts#
classic game in which you dodge and fend off opponents with an airplane.
from miniworlds import World, Actor, Circle, Text
import random
world = World(300, 600)
world.add_background("images/clouds")
world.background.is_scaled = False
aircraft = Actor((150, 500))
aircraft.add_costume("images/ship.png")
@aircraft.register
def on_setup(self):
self.downtime = 0
@aircraft.register
def act(self):
"""Increment the downtime every frame per 1"""
self.downtime += 1
@aircraft.register
def on_key_pressed(self, keys):
"""Move aircraft left/right with a, d keys.
"""
if "a" in keys:
aircraft.x -= 1
elif "d" in keys:
aircraft.x += 1
@aircraft.register
def on_key_down(self, keys):
"""Shoot, if downtime > 100
"""
if " " in keys and self.downtime > 100:
position = aircraft.center
position = (aircraft.center[0], aircraft.center[1] - 20)
bullet = Circle(position)
self.downtime = 0
@bullet.register
def act(self):
self.y = self.y - 1
@bullet.register
def on_detecting_actor(self, other):
if other in self.world.enemies:
other.remove()
self.remove()
@world.register
def on_setup(self):
self.enemies = []
@world.register
def act(self):
if self.frame % 120 == 0:
enemy = Actor((random.randint(30, 270), 50))
enemy.add_costume("images/enemy.png")
enemy.orientation = 180
self.enemies.append(enemy)
@enemy.register
def act(self):
self.y = self.y + 1
@enemy.register
def on_detecting_actor(self, other):
""" If enemy detects the aircraft, the game ends.
"""
if other == aircraft:
self.world.stop()
aircraft.remove()
t = Text((150, 300), "GAME OVER")
t.color = (0, 0, 0)
for enemy in self.world.enemies:
enemy.remove()
self.remove()
world.run()
Credits#
Greenfoot <https://www.greenfoot.org/>
_
miniworlds is strongly inspired by the high-level abstraction approach to object orientation in Greenfoot. miniworlds extends the approach with its own ideas.
Deepl <https://www.deepl.com/>
_Assistant for translating pages.
Kenney Assets <https://www.kenney.nl/assets>
_Most of the images in the example-code are based on kenny assets.
Links#
Codeberg Repository <https://codeberg.org/a_siebel/miniworlds>
_miniworlds cookbook - Examples and Snippets <https://codeberg.org/a_siebel/miniworlds_cookbook>
_