Created
July 21, 2016 14:39
-
-
Save jirikuncar/4db46449fd8ed6a574258f9602943a50 to your computer and use it in GitHub Desktop.
Micro:Bit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add your Python code here. E.g. | |
import math | |
import random | |
from microbit import * | |
enemies = [[1, 0, 0.05]] | |
shots = [] | |
game = True | |
killed = 0 | |
level = 1 | |
x = 2 | |
def resolve(killed, level): | |
for i, enemy in enumerate(enemies): | |
for j, shot in enumerate(shots): | |
if enemy[0] == shot[0] and enemy[1] >= shot[1]: | |
killed = killed + 1 | |
if killed % 10 == 0: | |
level += 1 | |
display.scroll('L: {0}'.format(level)) | |
sleep(100) | |
enemies.pop(i) | |
shots.pop(j) | |
while len(enemies) < level: | |
enemies.append([random.randint(0, 4), 0, level//100.0 + 0.05]) | |
return killed, level | |
while game: | |
display.clear() | |
if button_a.is_pressed() or button_b.is_pressed(): | |
if len(shots) < level*5: | |
shots.append([x, 4]) | |
if x > 0 and accelerometer.get_x() < -200: | |
x -= 1 | |
elif x < 4 and accelerometer.get_x() > 200: | |
x += 1 | |
for i, enemy in enumerate(enemies): | |
if enemy[1] > 4: | |
game = False | |
display.set_pixel(enemy[0], math.floor(enemy[1]), 6) | |
enemy[1] += enemy[2] | |
for i, shot in enumerate(shots): | |
if shot[1] >= 0: | |
display.set_pixel(shot[0], shot[1], 3) | |
shot[1] -= 1 | |
else: | |
shots.pop(i) | |
display.set_pixel(x, 4, 9) | |
sleep(100) | |
killed, level = resolve(killed, level) | |
# Done | |
display.scroll('GAME OVER: {0}'.format(killed), loop=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment