miniworlds#
miniworlds is a game engine written in Python and Pygame, designed for students to create 2D mini-worlds and games.
Creating your first games with miniworlds is simple and fun.
Examples#
Two actors controlled by keyboard#
Two actors that are controlled using 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()
Example: Two actors#
Skeetshooting#
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:
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()
Example: Skeetshooting#
Aircrafts#
Classic game where you control an aircraft, avoid and shoot enemies.
from miniworlds import World, Actor, Circle, Text
import random
world = World(300, 600)
world.add_background("images/clouds.png")
world.background.is_scaled = False
aircraft = Actor((150, 500))
aircraft.add_costume("images/ship.png")
@aircraft.register
def on_setup(self):
"""The downtime specifies the number of frames until the next shot can be fired.
If downtime > 100 a shot can be fired (@see on_key down of aircraft)
"""
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()
Example: Skeetshooting#
Credits#
Greenfoot <https://www.greenfoot.org/>_ Miniworlds is strongly inspired by Greenfoot.Kenney Assets <https://www.kenney.nl/assets>_ Most images in the examples are based on Kenney Assets