Skip to content

Instantly share code, notes, and snippets.

@will-moore
Last active March 2, 2024 18:17
Show Gist options
  • Save will-moore/4e44ae590d7810c2187c9335d7c4afab to your computer and use it in GitHub Desktop.
Save will-moore/4e44ae590d7810c2187c9335d7c4afab to your computer and use it in GitHub Desktop.
Simple tetris game for microbit using Code-kingdoms editor
let gameOver = false
let y = 0
let x = 0
let rowFull = false
let canFall = false
let delay = 0
input.onButtonPressed(Button.A, () => {
led.unplot(x, y)
if (x > 0 && !(led.point(x - 1, y))) {
x = x - 1
}
led.plot(x, y)
})
input.onButtonPressed(Button.B, () => {
led.unplot(x, y)
if (x < 4 && !(led.point(x + 1, y))) {
x = x + 1
}
led.plot(x, y)
})
function tryFall() {
canFall = y < 4 && !(led.point(x, y + 1))
if (canFall) {
led.unplot(x, y)
y = y + 1
led.plot(x, y)
} else {
gameOver = y == 0
}
checkFullRow()
}
function checkFullRow() {
rowFull = true
for (let xx = 0; xx <= 5 - 1; xx++) {
if (!(led.point(xx, 4))) {
rowFull = false
}
}
if (rowFull) {
delay = delay * 4 / 5
for (let xx2 = 0; xx2 <= 5 - 1; xx2++) {
led.unplot(xx2, 4)
}
for (let yy = 3; yy > 0; yy--) {
for (let xx3 = 0; xx3 < 5; xx3++) {
if (led.point(xx3, yy)) {
led.unplot(xx3, yy)
led.plot(xx3, yy + 1)
}
}
}
}
}
delay = 500
canFall = true
while (!(gameOver)) {
y = 0
x = Math.random(4)
canFall = true
led.plot(x, y)
while (canFall) {
basic.pause(delay)
tryFall()
}
}
basic.showLeds(`
. # . # .
. . . . .
. . . . .
. # # # .
# . . . #
`)
@T-haa
Copy link

T-haa commented May 30, 2023

je suis un crosant

@Rayannn240
Copy link

clean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment