Skip to content

Instantly share code, notes, and snippets.

@ngabyn
Created March 5, 2020 16:57
Show Gist options
  • Select an option

  • Save ngabyn/1ace7d25652bd6c1debf881d0c2054cc to your computer and use it in GitHub Desktop.

Select an option

Save ngabyn/1ace7d25652bd6c1debf881d0c2054cc to your computer and use it in GitHub Desktop.
function adaptDificulty () {
waves += 1
if (waves > 0 && waves <= 10) {
currentWaveSpeed = waveSpeed - waves
gameDifficulty = 3
} else if (waves > 10 && waves <= 20) {
currentWaveSpeed = waveSpeed - waves * 2
gameDifficulty = 2
} else {
currentWaveSpeed = 200
gameDifficulty = 1
}
}
function MoveWall (difficultyLevel: number) {
for (let index3 = 0; index3 <= 5; index3++) {
hideWall(index3 - 1, difficultyLevel)
showWall(index3, difficultyLevel)
basic.pause(currentWaveSpeed)
}
}
function hideWall (posY: number, difficultyLevel: number) {
for (let index = 0; index <= 4; index++) {
if (wall[index] == "on") {
led.unplot(index, posY)
}
}
}
Kitronik_Game_Controller.onButtonPress(Kitronik_Game_Controller.ControllerButtonPins.Fire1, Kitronik_Game_Controller.ControllerButtonEvents.Down, function () {
waveSpeed += -25
})
Kitronik_Game_Controller.onButtonPress(Kitronik_Game_Controller.ControllerButtonPins.Fire2, Kitronik_Game_Controller.ControllerButtonEvents.Down, function () {
waveSpeed += 25
})
function showWall (posY: number, difficultyLevel: number) {
for (let index = 0; index <= 4; index++) {
if (wall[index] == "on") {
led.plotBrightness(index, posY, 15)
}
}
}
function generateWall (difficultyLevel: number, pixelOn: string, pixelOff: string) {
wall = ["on", "on", "on", "on", "on"]
wallRandom = Math.randomRange(0, 4)
if (wallRandom == 0) {
for (let index2 = 0; index2 <= difficultyLevel - 1; index2++) {
wall[index2] = pixelOff
}
} else if (wallRandom == 4) {
for (let index22 = 0; index22 <= difficultyLevel - 1; index22++) {
wall[4 - index22] = pixelOff
}
} else {
if (difficultyLevel == 3) {
wall[wallRandom - 1] = pixelOff
wall[wallRandom] = pixelOff
wall[wallRandom + 1] = pixelOff
} else if (difficultyLevel == 2) {
wall[wallRandom] = pixelOff
wall[wallRandom + 1] = pixelOff
} else {
wall[wallRandom] = pixelOff
}
}
}
function movePlayer (newX: number, newY: number) {
if (playerX + newX >= 0 && playerX + newX <= 4 && (playerY + newY >= 0 && playerY + newY <= 4)) {
led.unplot(playerX, playerY)
playerX += newX
playerY += newY
led.plot(playerX, playerY)
}
}
let wallRandom = 0
let wall: string[] = []
let gameDifficulty = 0
let currentWaveSpeed = 0
let waveSpeed = 0
let waves = 0
let playerY = 0
let playerX = 0
Kitronik_Game_Controller.onButtonPress(Kitronik_Game_Controller.ControllerButtonPins.Up, Kitronik_Game_Controller.ControllerButtonEvents.Down, function () {
movePlayer(0, -1)
})
Kitronik_Game_Controller.onButtonPress(Kitronik_Game_Controller.ControllerButtonPins.Down, Kitronik_Game_Controller.ControllerButtonEvents.Down, function () {
movePlayer(0, 1)
})
Kitronik_Game_Controller.onButtonPress(Kitronik_Game_Controller.ControllerButtonPins.Right, Kitronik_Game_Controller.ControllerButtonEvents.Down, function () {
movePlayer(1, 0)
})
Kitronik_Game_Controller.onButtonPress(Kitronik_Game_Controller.ControllerButtonPins.Left, Kitronik_Game_Controller.ControllerButtonEvents.Down, function () {
movePlayer(-1, 0)
})
playerX = 2
playerY = 2
waves = 1
waveSpeed = 300
currentWaveSpeed = 300
gameDifficulty = 3
adaptDificulty()
basic.forever(function () {
adaptDificulty()
generateWall(gameDifficulty, "on", "off")
MoveWall(gameDifficulty)
basic.pause(currentWaveSpeed / 2)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment