Monday, July 17, 2017

Micro:bit Game Programming

Test it live online at makecode

https://makecode.microbit.org/_Vcs0WxcD3Fyd
//Niranjan Meegammana shilpasayura.org
let counter = 0
let x = 0
let y = 0
let pointsA = 0
let pointsB = 0
let playturn = 0
let mainplayer = 0
let Apos: number[] = []
let clrScreen = 0
let Bpos: number[] = []
let draws = 0

input.onButtonPressed(Button.A, () => {
    if (clrScreen == 1) {
        basic.clearScreen()
        clrScreen = 0
    }
    drawApos()
})

input.onButtonPressed(Button.B, () => {
    if (clrScreen == 1) {
        basic.clearScreen()
        clrScreen = 0
    }
    drawBpos()
})

draws = 0
mainplayer = Math.random(2)
playturn = mainplayer
pointsB = 0
pointsA = 0
y = 0
x = 0
counter = 0
Apos = [6]
Bpos = [6]
init()
playerSelect()

function playerSelect() {
    basic.clearScreen()
    clrScreen = 0
    if (playturn == 0) {
        leftArrow()
    } else {
        rightArrow()
    }
    clrScreen = 1
}
function init() {
    counter = 0
    pointsA = 0
    pointsB = 0
    draws = 0
    clrScreen = 1
    for (let i = 0; i < 3; i++) {
        Apos[i] = 0
        Apos[i + 3] = Math.random(4)
        Bpos[i] = 4
        Bpos[i + 3] = Math.random(4)
    }
    //basic.showNumber(Apos[4])
}

function eraseOld(ledPos: number[]) {
    for (let k = 0; k < 3; k++) {
        playTone()
        x = ledPos[k] // old position
        y = ledPos[k + 3]
        led.unplot(x, y)
    }
}
function drawCol(ledPos: number[]) {
    // if ((mainplayer == 0) && (draws == 6)) {
    //    winner()
    //}
    //basic.showNumber(ledPos[4])
    if (counter > 0) {
        eraseOld(ledPos)
    }
    for (let k = 0; k < 3; k++) {
        playTone()
        if (playturn == 0) {
            ledPos[k] = counter
        }
        else {
            ledPos[k] = 4 - counter
        }
        ledPos[k + 3] = Math.random(4)
        if (draws == 6) {
            cut(ledPos[k], ledPos[k + 3])
        }
        else {
            led.plot(ledPos[k], ledPos[k + 3])
        }

    }
    return ledPos
}

function drawBpos() {
    if (playturn == 0) {
        return
    }
    draws++
    Bpos = drawCol(Bpos)
    playturn = 0
    if (mainplayer == 0) {
        counter++
    }
    if (draws == 6) {
        winner()
    }
}


function drawApos() {
    if (playturn == 1) {
        return
    }
    draws++
    Apos = drawCol(Apos)
    playturn = 1
    if (mainplayer == 1) {
        counter++
    }
    if (draws == 6) {
        winner()
    }

}

function cut(x: number, y: number) {
    if (led.point(x, y)) {
        playTone()
        led.unplot(x, y)
        if (playturn == 0) {
            pointsA++
            pointsB--
        }
        else {
            pointsB++
            pointsA--
        }
    }
    else {
        if (playturn == 0) {
            pointsB++
            pointsA--
        }
        else {
            pointsA++
            pointsB--
        }
    }
    basic.pause(300)
    led.plot(x, y)
    playTone()
    basic.pause(200)

}

function winner() {
    counter = 0
    basic.clearScreen()

    if (pointsA > pointsB) {
        basic.showString("A")
        mainplayer = 1
    }
    else {
        if (pointsA == pointsB) {
            basic.showString("DRAW")
            mainplayer = playturn
        }
        else {
            basic.showString("B")
            mainplayer = 0
        }
    }
    playturn = mainplayer
    pointsA = 0
    pointsB = 0
    counter = 0
    draws = 0
    basic.pause(1000)
    playerSelect()
    clrScreen = 1
}
function rightArrow() {
    led.plot(4, 2)
    led.plot(3, 1)
    led.plot(3, 2)
    led.plot(3, 3)
}
function leftArrow() {
    led.plot(0, 2)
    led.plot(1, 1)
    led.plot(1, 2)
    led.plot(1, 3)
}
function playTone() {
    music.playTone(Math.random(1000), 30)
}

No comments:

Post a Comment