PAC-MAN

Recreate the classic arcade game PAC-MAN on your micro:bit!


You'll need

1 x game:bit OR
{1 x micro:bit +
1 x ADKeyboard +
1 x breakout board }
1 x micro USB cable
1 x laptop

Step 1

Instructions

Assemble your game:bit! (Refer to game:bit tutorial)

Or simply connect the micro:bit to the breakout board, and connect the ADKeyboard to pin 1 on the breakout board.




Materials


Step 2

Instructions

Let’s start by creating the hero, the food and the ghost!

Start with the on start block.
Create three variables named hero, food and ghost. Then, create sprite with x, y coordinates and store it in each variable. The create sprite function returns a LED Sprite on the micro:bit on a specific position indicated by the coordinates you input.

We will ensure that the ghost, food, and the hero are set apart by putting them into three different quadrants.

Now, let’s differentiate ghost, hero and food. Adjust the brightness of hero and food, and make the ghost blink.




Materials


Step 3

Instructions

Now, we are going to create the ghost’s movement. The ghost’s aim is to catch the hero, so it will always follow the hero and move one unit nearer to him.

Let’s create a while loop first. Then set the loop to True. This while loop will continue to run and repeat as long as the condition is True. The pause (ms) block here indicates the speed of the ghost, so you can change the value to adjust the speed of the ghost.

Inside the while loop, you need to use the if-else condition to evaluate the relative position of the ghost to the hero and make necessary movements. If the x coordinate of the ghost is smaller than that of the hero, the ghost must move right by 1 unit. This is achieved by changing x of ghost by 1. Try to finish the other three cases(x of ghost is greater than that of hero, y of ghost is smaller than that of hero, y of ghost is greater than that of hero) yourselves!




Materials


Step 4

Instructions

Let’s enable Pac-Man to move in the x direction and y direction with acceleration using the micro:bit accelerometer. Note that this piece of code is still inside the while loop!

This piece of code ensures that when you tilt to a certain direction, Pac-Man can move in the corresponding direction.




Materials


Step 5

Instructions

Now, we can proceed to the code where your Pac-Man successfully eats a blob of food.

Create an if conditional for this and put the condition as 'hero touching food?'. This condition will check whether the hero is on the same spot as the food.

Inside the if block, show a smiley face and set the food to another random spot using the random function.




Materials


Step 6

Instructions

After resetting the food, we also need to reset the ghost.

Create another if-else conditional inside the previous one.

To make sure that the ghost does not appear too near to the food at its default position, we will evaluate the position of the food and set the ghost to a corner that is furthest away from the food.




Materials


Step 7

Instructions

This piece of code ends the game when the ghost catches Pac-Man.

The game over function is an in-built function to clear the screen and display the string “game over”.




Materials


Step 8

Instructions

You have now finished coding the game. Want to add some flair to your game?

Display Pac-Man and it’s logo on the screen before the game starts. Make sure that this block comes first in your entire programme.




Materials


Step 9

Instructions

Having fun playing the game? Want to add more features into the game? Accept this challenge!

  1. Let the game start by pressing button A. (Hint: Input)
  2. Insert three different pieces of music at the start, when your Pac-Man successfully eat a food and when the game is over.
  3. Add a scoring system.
  4. Make the ghost move after every time your Pac-Man successfully eat a food to increase the difficulty of the game.

Try them out!




Materials


Code and References

Completed this tutorial?

Spread the word! Let's get this trending on social media with #letsgethacking #PAC-MAN