Skip to content

Instantly share code, notes, and snippets.

@will-moore
Forked from gingemonster/WarGames.js
Created May 15, 2018 21:21
Show Gist options
  • Save will-moore/4c79a901bb79a695f7e0601565e28793 to your computer and use it in GitHub Desktop.
Save will-moore/4c79a901bb79a695f7e0601565e28793 to your computer and use it in GitHub Desktop.
Micromonsters Episode 7 - WarGames code
let missile: game.LedSprite = null
let opponentsmissile: game.LedSprite = null
let score = 0
let nummissles = 0
let player: game.LedSprite = null
let switchxposition = 0
input.onButtonPressed(Button.A, () => {
player.change(LedSpriteProperty.X, -1)
})
radio.onDataPacketReceived( ({ receivedString: name, receivedNumber: value }) => {
switchxposition = Math.abs(value - 4)
if (name == "missileX") {
opponentsmissile = game.createSprite(switchxposition, 0)
basic.pause(100)
while (opponentsmissile.get(LedSpriteProperty.Y) < 4) {
opponentsmissile.change(LedSpriteProperty.Y, 1)
if (opponentsmissile.isTouching(player)) {
radio.sendValue("winner", 1)
basic.showNumber(score)
basic.pause(2000)
} else {
basic.pause(100)
}
}
opponentsmissile.set(LedSpriteProperty.Brightness, 0)
}
if (name == "winner") {
score += 1
basic.showNumber(score)
basic.pause(2000)
}
})
input.onButtonPressed(Button.AB, () => {
if (nummissles < 1) {
nummissles += 1
serial.writeNumber(nummissles)
missile = game.createSprite(player.get(LedSpriteProperty.X), player.get(LedSpriteProperty.Y))
while (missile.get(LedSpriteProperty.Y) > 0) {
missile.change(LedSpriteProperty.Y, -1)
basic.pause(100)
}
missile.set(LedSpriteProperty.Brightness, 0)
nummissles += -1
radio.sendValue("missileX", missile.get(LedSpriteProperty.X))
}
})
input.onButtonPressed(Button.B, () => {
player.change(LedSpriteProperty.X, 1)
})
player = game.createSprite(2, 4)
score = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment