UnicMinds

Coding the Pacman Game in Scratch

Coding the Pacman Game in Scratch

The Pacman game doesn’t need any introduction. The player controls the eponymous character Pac-man throughout a maze. It is undoubtedly one of the most recognized maze games.

How to play the Pacman game in Scratch?

The player needs to collect coins spread across the maze. There are some bigger coins which give bonus points if collected. But the player needs to dodge the ghosts which are moving across the maze. If the player touches any of the ghosts, the game gets over.

The player can control Pacman using up, down, left and right keys.

Steps to code the Pacman game in Scratch

Step1: Create the backgrounds in the game

First background is the Pacman maze. 

Pacman Game Maze - Scratch

Second background is the winning background, this background will be effective or shown when the player wins the game after collecting all the coins.

Pacman You Won Screen

Third background is the losing background, this background will be effective or shown when the player loses the game when the player is eaten by one of the ghosts.

Pacman Game Over Screen

Step 2: Create sprites in the game 

First sprite is the Pacman character, click on the add a sprite option and upload the below sprites for the Pacman.

Costume 1                          costume 2

Pacman Game
Pacman Game

Second sprite is the red ghost , click on add a sprite option and upload the below sprite.

Pacman Game Background

Third sprite is the green ghost,  click on add a sprite option and upload the below sprite.

Pacman Game Background

Another sprite is the dot that we need to put on the first background.

Pacman Game background

Create 88 sprites like above with similar picture

Bonus sprite is the big dot that we need to put on the first background.

Create 4 bonus sprites like below.

Pacman Game Background

Step 3: Add code to set different attributes for the Pacman when the game starts

Scratch Code for Pacman Game

When the flag is clicked, show the Pacman sprite. Make it point in the direction of 90. The original position of the Pacman should be x value 0 and y value -10, which is in the middle of the maze.

Step 4: Add code to move the Pacman using the left, right, up, and down keys

Pacman code in Scratch

Add the above code below when the flag is clicked. In this code, it is checked whether any of the keys is pressed. If any of the keys is pressed, change the costume every 0.1 seconds, which makes it animate whenever the key is pressed.

Pacman Scratch code

Whenever the left arrow is pressed, the Pacman will change the direction to -90,

Pacman code in Scratch

Whenever the right arrow is pressed, the Pacman will change the direction to 90,

Pacman code in Scratch

Whenever the up arrow is pressed, the Pacman will change the direction to 0, 

Pacman Scratch Program

Whenever the down arrow is pressed, the Pacman will change the direction to -90,

Step 5: Add code for Pacman touching any wall of the maze

Pacman Scratch Game

Add the above code in every forever loop associated with all the four key events.

This code checks if the black color is touching the black color, which is the background color of the maze, then only the Pacman should move 2 steps whenever any key is pressed.

Step 6: Add code to make the red ghost move randomly in the Pacman maze

Pacman Game code
Code for Pacman Game

When the flag is clicked , broadcast the message respawn1 Whenever the red ghost receives the message, move the red ghost up. If the ghost touches the wall of the maze, it moves down. Otherwise it keeps moving horizontally. Ghost variable is the speed with which the ghost is moving horizontally. The value is 2, which means the ghost keeps moving left until it touches the wall. On touching the wall, it changes direction and starts moving right and vice versa.

Step 7: Add code to make the green ghost move randomly in the Pacman maze

Write code for Pacman Game
Pacman Game in Scratch

When the flag is clicked , broadcast the message respawn1 Whenever the red ghost receives the message, move the red ghost up. If the ghost touches the wall of the maze, it moves down. Otherwise it keeps moving horizontally. Ghost2 variable is the speed with which the ghost is moving horizontally. The value is 2, which means the ghost keeps moving left until it touches the wall. On touching the wall, it changes direction and starts moving right and vice versa.

Step 8: Add code for each dot in the maze

Pacman Game Coded in Scratch
Pacman Code in Scratch Programming

When the flag is clicked , the dot is created at x position of 24 and y position of -10. Please change these values for each dot so that they are arranged in a pattern as shown below. When the Pacman touches these dots, they hide showing that the Pacman has collected the dots and 10 points are scored.

Step 9: Add code for bonus dot in the maze

Pacman Code

It’s the same code as the small dot. Please change the x and y value for each bonus dot. Whenever pacman touches the dot, 100 points are scored and they hide.

Step 10: Add code for winning the game

Pacman Game in Scratch
Pacman in Scratch

When the value of variable dots is 92, there is a win. The backdrop changes to winning backdrop.

Step 11: Add code for losing the game

Pacman Game in Scratch
Pacman Game in MIT Scratch Programming

Add the when flag clicked code to both the ghosts. Add the when I receive code to the game backdrop. 

Hope this is useful for you to make your own Pacman game on the Scratch platform, thank you.

You may like to read: VBA Programming for Kids, Coding Flappy Bird Game in Scratch, and Kangaroo Math Classes for Kids

BOOK A FREE TRIAL