Microbit Game: PacMan


The Pac-Man game: The basic concept of Pac-Man is simple: guide a small round character through a maze while eating pac-gums while avoiding ghosts. In this video we show how we can use the led matrix of the Microbit to play this game.


 


JavaScript Code

let hero = game.createSprite(22)
let food = game.createSprite(44)
let ghost = game.createSprite(00)
ghost.change(LedSpriteProperty.Blink100)
ghost.set(LedSpriteProperty.Brightness10)
food.set(LedSpriteProperty.Brightness5)
while (true) {
    basic.pause(400)
    if (ghost.get(LedSpriteProperty.X) < hero.get(LedSpriteProperty.X)) {
        ghost.change(LedSpriteProperty.X1)
    } else if (ghost.get(LedSpriteProperty.X) > hero.get(LedSpriteProperty.X)) {
        ghost.change(LedSpriteProperty.X, -1)
    } else if (ghost.get(LedSpriteProperty.Y) < hero.get(LedSpriteProperty.Y)) {
        ghost.change(LedSpriteProperty.Y1)
    } else if (ghost.get(LedSpriteProperty.Y) > hero.get(LedSpriteProperty.Y)) {
        ghost.change(LedSpriteProperty.Y, -1)
    }
    if (input.acceleration(Dimension.X) > 200) {
        hero.change(LedSpriteProperty.X1)
    } else if (input.acceleration(Dimension.X) < -200) {
        hero.change(LedSpriteProperty.X, -1)
    }
    if (input.acceleration(Dimension.Y) > 200) {
        hero.change(LedSpriteProperty.Y1)
    } else if (input.acceleration(Dimension.Y) < -200) {
        hero.change(LedSpriteProperty.Y, -1)
    }
    if (hero.isTouching(food)) {
        game.addScore(1)
        food.set(LedSpriteProperty.X, randint(05))
        food.set(LedSpriteProperty.Y, randint(05))
        if (food.get(LedSpriteProperty.X) < 2 && food.get(LedSpriteProperty.Y) < 2) {
            ghost.set(LedSpriteProperty.X4)
            ghost.set(LedSpriteProperty.Y4)
        } else if (food.get(LedSpriteProperty.X) > 2 && food.get(LedSpriteProperty.Y) < 2) {
            ghost.set(LedSpriteProperty.X0)
            ghost.set(LedSpriteProperty.Y4)
        } else if (food.get(LedSpriteProperty.X) < 2 && food.get(LedSpriteProperty.Y) > 2) {
            ghost.set(LedSpriteProperty.X4)
            ghost.set(LedSpriteProperty.Y0)
        } else {
            ghost.set(LedSpriteProperty.X0)
            ghost.set(LedSpriteProperty.Y0)
        }
    }
    if (hero.isTouching(ghost)) {
        game.gameOver()
    }
}
ghost.set(LedSpriteProperty.X4)



Download the .hex file

https://drive.google.com/file/d/1w-RO0K88EfGHtgg263UQGWF2EHffdLeV/view?usp=drive_link

Commentaires

Posts les plus consultés de ce blog

Create a Data Entry Form with Google HTML Service and Submit Data to Google Sheets