Why do my leds start flicking on and off, even though it doesn't do it at the start? Micro:bit JavaScript

90 Views Asked by At

yesterday I got into coding micro:bit, the problem I am having, is that when I lose the "snake" game, the "jim face" starts blinking, even though it doesn't do that when I first start the program, here is my program ( I do not want tips on how to shorten the code and make it better - that is up to me to find out - I only want you to answer the question I have) I have coded this on makecode.microbit.org or something like that; sorry if code format is wrong, I am new here:

let happy_score = 5
let food_counter = 5
let debounce_jim = 0
let debounce = 0 
function jim(expression: string = "meh"){
    basic.clearScreen()
    debounce = 1
    console.log("jim")
    input.onButtonPressed(Button.A,function(){
        if (food_counter > 0){
            food_counter -= 1
            happy_score += 1
        }

    })

    while (debounce_jim == 0){
        input.onButtonPressed(Button.AB, function () {
            debounce = 0
            debounce_jim = 1
            snake()
        })
        pause(0)
        happy_score -= 1
        if (food_counter < 1){
            basic.showNumber(0)
        }
        if (food_counter < 0){
            food_counter = 0
        }
        if (happy_score > 2) {
            expression = "happy"
        }else if(happy_score <= 2 && happy_score >0){
            expression = "meh"
        } else {
            expression = "sad"
        }
    

        if (expression == "happy"){
            basic.showLeds(`
            . # . # .
            . # . # .
            . . # . .
            # . . . #
            . # # # .
            `)
        }
        if (expression == "meh"){
            basic.showLeds(`
            . # . # .
            . # . # .
            . . . . .
            # # # # #
            . . . . .
            `)
        }
        if (expression == "sad"){
            basic.showLeds(`
            . # . # .
            . . . . .
            . # # # .
            # . . . #
            . . . . .
            `)
        }
    }

}

function snake() {
    let player = game.createSprite(2,2)
    let food = game.createSprite(1,1)
    let score = 0
    food.setBlink(100)
    console.log("snake")
    while (debounce == 0){
        if (player.isTouchingEdge()){
            debounce = 1 
            debounce_jim = 0
            console.log("debounce")
            player.off()
            food.off()
            game_end(score)
        }
        
        if (player.isTouching(food)){
            score += 1
            food_counter += 1
            food.setX(randint(1,3))
            food.setY(randint(1,3))
        }
            
        pause(1000)
        player.move(1)
    input.onButtonPressed(Button.A, function(){
        player.changeDirectionBy(90)
    })
    input.onButtonPressed(Button.B, function () {
        player.changeDirectionBy(-90)
    })

    }}

function game_end(score: number){
    basic.showNumber(score)
    console.log("game end")
    pause(100)
    basic.showString("||")
    jim(null)

}

jim(null)
1

There are 1 best solutions below

0
On

i had the same problem with my microbit and my solution was just to blow and wipe the connection pins at the bottom.

If that doesn’t work try to isolate and rewrite the problematic part (this can be hard if you just started)

If you are up for the try you can maybe try swift to program your microbit. I found it much easier to do than in the microbit website and app (do this only when you understand/want to understand swift and if you are deeper in to programming)

Good luck to your programming journey