1

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)
CapLack
  • 11
  • 1
  • When you start the game `debounce` is `0` but when you end the game `debounce` is `1`. Could that be the issue? – kiranvj May 01 '21 at 13:37
  • @kiranvj nope, that is meant to be, or else the game would run either way, so it would mess up jim. Maybe it is that? Because if I press AB again, the game does not run, which is strange because it is changing debounce to 0 again, so the game can run – CapLack May 01 '21 at 13:56

1 Answers1

0

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