Skip to content

Instantly share code, notes, and snippets.

@RobinBoers
Last active June 16, 2021 19:07
Show Gist options
  • Save RobinBoers/9f05cacb771428b6b120289005703e60 to your computer and use it in GitHub Desktop.
Save RobinBoers/9f05cacb771428b6b120289005703e60 to your computer and use it in GitHub Desktop.
Space invaders style game on the micro:bit
# Simple spaceinvaders style python game for the micro:bit
from microbit import *
import random, music
timer = 0
moveTimer = 0
playerMoveTimer = 0
moveSpeedEnemie = 3 #higher is slower
playerMoveSpeed = 2 #higher is slower
levelTimer = 0
enemieAccel = 0
enemieSpawnInterval = 8
enemies = []
playing = True
maxPlayerHealth = 3
def spawn_enemie():
enemies.append([random.randint(0,4),0])
tune = ["F4:4", "D4:4", "B3:4"]
tune2 = ["F4:1", "A4:2"]
# Player object
class Player:
def __init__(self, x, y, hp):
self.x = x
self.y = y
self.hp = hp
# Function to move in a direction
def walk(self, direction):
if(direction == "right"):
# Prevent the player moving off the screen on the right side
if(self.x < 4):
self.x += 1
if(direction == "left" ):
# Prevent the player moving off the screen at the other side (left)
if(self.x > 0):
self.x -= 1
# Create a player at coords (3,3) and with 3 hp health
player1 = Player(3,3,maxPlayerHealth)
# Main gameloop
while True:
if playing == True:
# Check for gameover
if(player1.hp < 1):
playing = False
# Increase timers
timer += 1
moveTimer += 1 + (enemieAccel * moveTimer)
playerMoveTimer += 1
levelTimer += 1
# Levelup tune
if(levelTimer == 100 or levelTimer == 200 or levelTimer == 400 or levelTimer == 600 or levelTimer == 900 or levelTimer == 1200 or levelTimer == 1900 or levelTimer == 2700 or levelTimer == 4000):
music.play(tune2)
enemieAccel += 0.1
# Activate endlessmode at 8000
if(levelTimer == 8000):
music.play(music.POWER_UP)
display.show("ENDLESS MODE")
# Spawn enemies
if(timer > enemieSpawnInterval and moveTimer < moveSpeedEnemie):
spawn_enemie()
timer = 0
# Display enemies and check for collusions
for index, i in enumerate(enemies):
if i[1] > 5:
enemies.pop(index) # Remove enemies from array to prevent lag
# If an enemies and player overlap
# the player loses a life
if i[0] == player1.x and i[1] == player1.y:
music.pitch(500, 10)
# Reduce player hp with 1
player1.hp -= 1
print(player1.hp)
# Check for gameover
if(player1.hp < 1):
playing = False
# Move enemie of screen
i[1] = 5
# Prevent enemies from moving on warp speed
if(moveTimer > moveSpeedEnemie):
# Reset timer
moveTimer = 0
for i in enemies:
# Move enemie
i[1] += 1
# Debug message to see coords
#display.show(str(player1.x))
# Player input (move left and right)
if button_a.is_pressed() and playerMoveTimer > playerMoveSpeed:
player1.walk("left")
playerMoveTimer = 0
elif button_b.is_pressed() and playerMoveTimer > playerMoveSpeed:
player1.walk("right")
playerMoveTimer = 0
# A bit of input delay to prevent movement at warp speed
sleep(100)
# Draw the frame to screen (at a insanely small resolution)
display.clear()
# Show enemies
for i in enemies:
if i[1] < 5:
display.set_pixel(i[0], i[1], 7)
# Show player
display.set_pixel(player1.x, player1.y, 9)
elif playing == False:
display.show(Image.SILLY)
sleep(500)
music.play(tune)
sleep(500)
display.scroll("Score: "+str(levelTimer))
if levelTimer > 600 and levelTimer < 1200:
display.show(Image.SMILE)
elif levelTimer > 1200 and levelTimer < 3000:
display.show(Image.HAPPY)
elif levelTimer > 3000:
display.show(Image.HEART)
else:
display.show(Image.SAD)
sleep(1200)
enemies = []
timer = 0
moveTimer = 0
levelTimer = 0
player1.hp = maxPlayerHealth
playing = True
display.clear()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment