How to create Pac-Man game using Scratch

Payal Udhani on October 14, 2022

Pac man game

How to create Pac-Man game using Scratch

Namco developed Pac-Man in 1980 for the arcades. It is a maze game where the player can control the main character, Pac-Man, a yellow ball. It eats all the dots, also known as pills, placed inside the maze and is enclosed from all sides. In this blog , we will learn how we can create the Pacman game on the Scratch platform. We will divide this into three parts – required background and sprites addition, adding necessary variables, and coding for each.

Adding background and sprites

Scratch programming platform for kids provides functionality to add different backgrounds and sprites that are nothing but characters in the project. For the Pac-Man game, we can go for drawing our maze for the background or upload any downloaded picture for the background.
Choose the paint option in the upload background popup to draw a background. Using the fill bucket option, we can easily color our background to any color. Then you can choose any fill color for the background and choose the fill option shown in the image. Click on the Convert to Bitmap option and then click on the blank space of the screen to apply color.

Now, choose the line option and color of the line in the outline option and start drawing a maze-like structure through which Pac-Man has to travel, as shown below.

Now, we will go for adding all our sprites. First, you can download the images from google search and then remove the background of each one using the https://www.remove.bg/upload site. After that, you can upload all the sprites to choose the sprite option of Scratch. You can also alternatively go for drawing your sprites using simple shape tools like circles, rectangles, lines, and erasers while painting your sprite.

Below is the list of all the sprites and images required for the Pacman game

  • Pacman sprite with two costumes of Pacman open and Pacman closed mouth to create the animation of eating Pac-Man                                                                                                              
  • Yellow pill – Pac- Man will eat this yellow pill. Every time Pac-Man eats one yellow pill, you will get 1 point and change the score by one. Later on, after completing the code of yellow pill, we will have to duplicate this sprite 109 times as we need a total of 110 yellow pills. Also, we will have to place each one of them correctly on the stage.
  • Red Pill – This is the Super pill. After eating it, Pac-Man gets a superpower. This superpower will make all the aliens blue in color for 20 seconds, and Pacman can eat these aliens when they are blue-colored. We will have 3-4 red pills and can duplicate the same sprite after completing its code. After completing the code, you can duplicate the sprite instead of writing the code again.
  • Game Over – We will need a sprite which we will show once Pacman loses all his three lives and gets caught up by an alien thrice. For this, we will add one sprite of the game over, which we will show once we get the broadcast message of the game over from the Pacman sprite once he loses all his lives, that is, lives = 0 
  • You Win – You will win the game when Pac-Man eats all the 110 yellow pills. To display the same, we have added one sprite of You Won, shown when we broadcast you won message, one pill left is zero.
  • Alien 1 – There are four aliens patrolling the maze. If Pac -Man touches any of the aliens, he loses one of his lives. Each alien sprite will have two costumes—one of his original color and another blue color. The alien changes his costume to blue whenever Pac-Man eats the red pill and gets a superpower for 20 seconds.
  • Alien 2 – Even this alien has two costumes – one red and the other blue color.
  • Alien 3 – This alien has two costumes – Purple color and the other blue color.
  • Alien 4 – This alien has two costumes – Cyan and blue color 

Note – You can change the position and size of the sprites as per your maze space. Pacman and aliens should be able to travel through the space available in the maze. Also, you need to hide the the game over, and you win, sprites in the beginning.

The final outcome of all the sprites and the background should look like the above image. Also, ensure that every sprite’s initial position is fixed. Place all of them in the proper position as per your requirement.

Adding required variables 

You will need to create 4 variables by going to the Variables block section of Scratch and clicking on the “Make a Variable” option. These variables are as listed below, along with the purpose for creating them.

  • Blue – This variable is hidden and is used to control whether the aliens will be in the standard or blue color. This variable will initially have a false value as aliens will have their standard colors. When Pacman eats the red pill, the variable is set as true, and depending on the value of this variable, all the aliens turn their costumes to blue for 20 seconds. During this time, Pacman can eat blue aliens and can get extra 100 points added to the score. After which, again blue variable is set to false, and aliens are back in their original colors where they can eat Pac-Man.
  • Lives – This is a visible variable and is shown on the top of the stage. This variable is responsible for maintaining how many lives are available with the PacMan. In the beginning, its value is 3. As and when Pacman gets eaten by the alien, its life decreases by one each time. The game gets over when all of the lives finish, i.e., Lives = 0.
  • Pills Left – It is a visible variable on the screen. This will be the total yellow pills remaining on the screen to be eaten by Pacman. Initially, it is set to 110 pills, as we have created 110 yellow pills. However, you can change it to as many yellow pills as you want to create. When Pac-Man eats any yellow pill, the pill left variable decreases by one. When the pill left variable reaches zero, Pac-Man wins the game.
  • Score – This is also a visible variable. This is the number of points gained by us. For eating every yellow pill, you get 1 point. For eating a red pill, you get 25 points. While eating an alien when it’s blue-colored gives you 100 points.

Code for the game

Code for the Pac Man

As our Pac-Man is continuously going to open and close his mouth, to create this animation, we will have two Pac-Man costumes: Pacman open, and Pacman closed. In the below code snippet, as soon as the green flag is clicked which means the game starts forever, which means continuously we are going to switch the costume of Pacman to the next costume every 0.5 seconds to create an animation effect of eating Pacman. The code snippet is shown below

Next, we need to set the initial position of Pac -Man using x and y coordinates and its direction. Along with that, we will also set the initial value of all the taken variables. Further, to control Pacman, we will forever move him by 4 steps and check whether the up, down, left, or right buttons are clicked on, which we will change the y and x positions accordingly; when any character on Scratch moves up the y increases and for down y decreases. When the sprite moves right x increases and y decrease. Nextly, if Pacman touches the blue color of the maze border, instead of moving forward by four steps, we will move him backward by four steps as he does not crosses those maze lines. For this, we will have to write the following Scratch code. –

Whenever aliens eat Pac-Man, they will broadcast one message called “Got Pacman”. When Pacman receives this message, it should hide and play a sound related to losing one life and then decrease the lives variable by one as he lost his one life. Further, depending on whether any lives are remaining for Pacman or not, we will decide to show him after 3 seconds at his original place or just game over if lives are zero.

Code for yellow pills 

When the green flag is clicked, all the yellow pills need to do is show on the screen and continuously check whether Pac-Man is touching the yellow pill. Once he touches, which means eat the yellow pill, we will hide our pill, increase the score by one and decrease the pill’s left variable by 1. Further, we will also check if all the pills are eaten which means the pills left are zero then, we will broadcast the message “You Won,” which will make you win sprite show on the screen.

Code for red pills 

When the green flag is clicked, we will show the red pill and check if Pac-Man is touching it. Once Pacman touches it, it is hidden, and the score is increased by 25. At the same time, we broadcast the message of Super pill and turn the blue variable to 1, as all aliens will now turn blue, and Pacman will get a superpower for 20 seconds.

Code for Game over sprite 

When we click on the green flag in the beginning, we will keep the game over sprite hidden, and as soon as it receives the broadcasted message of “Game Over”, we will show it back on the screen.

Code for You Won sprite 

When we click on the green flag at that time, we will keep the “You Won” sprite hidden, and as soon as it receives the broadcasted message of “You Won”, we will show it back on the screen.

Code for all the aliens 

All the aliens will have similar codes, just that their x and y positions at the beginning and their costume names and colors will change. First, we will discuss the code related to one of the aliens.
When the green flag is clicked, we will give the initial x and y position to the alien and then set his starting costume to his original color and show it. Further, as the movement of the aliens is entirely random, we will create one function named change direction, which will give random direction from up, down, left or right. We will also create a variable for distance, as the distance traveled will also be random. We will give the distance variable value as a random value between 20 to 50.
Whatever random number comes up in the distance, for that many times, we will rotate one repeat loop. In this, we will make our alien sprite move in 2 steps. In this repeat loop, we will check various conditions. Before that, let us see how the alien gets a random direction in the change direction function.

We will take one variable named direction in the change direction function’s definition. For this variable, we will set a random number between 1 to 4. Now depending on what number comes, we will set the direction of the alien by using a point in the direction to either 0, 90, -90, or 180 which stands for up, right, left, and down.

Returning to the repeat loop, we first check whether the alien is touching the blue color of the maze after every two steps. If so, instead of 2 steps forward, we will make him go two steps backward.
In the following condition, we will check whether an alien is touching Pac- Man and the blue variable has a 0 value which means aliens are in standard color and can eat Pacman if they touch it. In this case, we will broadcast the message of “Got Pacman” and hide our alien. Then, as our aliens are immortal, we will wait for 3 seconds and again place them at their initial position and show them back in the game.
The last condition that we check is whether the alien is touching Pacman and the blue variable is having 1 value which means aliens are blue-colored and can be eaten by Pacman. In this case, we will hide our alien as he dies for some time and change the score by 100 points. Further, aliens are immortal, waiting for 3 seconds and then bringing them back to their original position and shown on screen.

Next is to handle the message of the super pill aliens receive when Pac-Man eats the red pill. The aliens turn their costumes to blue colors and wait for 20 seconds, after that, the costumes are switched back to their original costumes, and the blue variable is set back to 0.

We can copy the same code for other aliens and change the costume name and the starting position everywhere inside the code.

Conclusion

In this way, you can follow the steps mentioned above: adding sprites and background, adding necessary variables, and coding for each and every sprite. You can create your own version of Pac-Man game on Scratch. All you need to do is add the required backgrounds and sprites and then logically do the coding for each. Further, you can check whether our game is working correctly or not for different scenarios and go for sharing with others.

Scratch programming serves as an introduction to coding for kids. It helps kids get a grip on the basics that support all kinds of coding languages.

Kids can use Scratch to code their own interactive stories, animations, and games. In the process, they learn to think creatively, reason systematically, and work collaboratively  — harnessing the essential 21st-century skills.

Learn to master the art making some best games in Scratch with our Scratch programming course for kids ages 8-14. Let kids explore their interests and grow; as a result, becoming better prepared for the future and all it holds!

Try a free class and get started on the perfect pathway for kids to explore and learn coding with Scratch.

 

 

Sign up to our newsletter

Get the latest blogs, articles, and updates delivered straight to your inbox.

Share with your friends

Try a free class