Created
August 19, 2018 19:58
-
-
Save timb-machine/7ce2ad70d9f92f50aae01394b2d645bd to your computer and use it in GitHub Desktop.
micro:bit Space Invaders 2
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
# https://makecode.microbit.org/31667-75115-87037-56723 | |
let score = 0 | |
let delay = 0 | |
let highscore = 0 | |
let onscreen = 0 | |
let heart: Image = null | |
let lives = 0 | |
let clearscreen: Image = null | |
let ship: Image = null | |
input.onButtonPressed(Button.A, () => { | |
if (onscreen == 1) { | |
game.addScore(1) | |
basic.showString("SUCCESS!") | |
basic.pause(500) | |
} else { | |
lives += -1 | |
clearscreen.showImage(0) | |
heart.showImage(0) | |
basic.pause(500) | |
clearscreen.showImage(0) | |
} | |
}) | |
ship = images.createImage(` | |
. . # . . | |
. # # # . | |
# # # # # | |
# # # # # | |
. # . # . | |
`) | |
clearscreen = images.createImage(` | |
. . . . . | |
. . . . . | |
. . . . . | |
. . . . . | |
. . . . . | |
`) | |
heart = images.createImage(` | |
. . . . . | |
. # . # . | |
# # # # # | |
. # # # . | |
. . # . . | |
`) | |
basic.showString("Space Invaders!") | |
ship.showImage(0) | |
basic.pause(100) | |
clearscreen.showImage(0) | |
highscore = 0 | |
while (true) { | |
basic.showString("READY?") | |
basic.pause(100) | |
game.setScore(0) | |
lives = 3 | |
for (let i = 0; i <= 100 - 1; i++) { | |
delay = Math.random(501) | |
onscreen = 1 | |
ship.showImage(0) | |
basic.pause(delay) | |
onscreen = 0 | |
clearscreen.showImage(0) | |
basic.pause(1000) | |
if (lives == 0) { | |
basic.showString("DEAD!") | |
i = 100 | |
} | |
} | |
if (game.score() > highscore) { | |
highscore = game.score() | |
basic.showString("HIGH SCORE!") | |
basic.pause(500) | |
if (score > 99) { | |
serial.writeLine("flag{b00m!}") | |
} | |
} else { | |
basic.showString("GAME OVER!") | |
basic.pause(500) | |
score = game.score() | |
basic.showNumber(score) | |
basic.pause(500) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment