Miniwelten#
miniworlds is a game engine written in Python and Pygame, developed for students to create 2D mini-worlds and games.
Die Erstellung der ersten Spiele mit minigames ist einfach und macht Spaß.
miniworlds und miniworldmaker
miniworlds is the successor project of miniworldmakers. Many things are similar, some names and functions differ from miniworldmaker. (e.g., Tokens became Actors, Boards became Worlds)
Beispiele#
Zwei Akteure, die per Tastatur gesteuert werden#
Zwei Akteure, die per Tastatur gesteuert werden
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()
Tontaubenschießen#
A randomly placed target can be “shot” with 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()
Flugzeuge#
Classic game where you control a plane and fend off and dodge enemies.
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 ist stark von Greenfoot inspiriert.Kenney Assets <https://www.kenney.nl/assets>
_ Die meisten Bilder der Beispiele basieren auf Kenney Assets