Pacman Tutorial 1

From Scalable Game Design wiki
Jump to: navigation, search
Pacman Screen.png

Overview

The purpose of this tutorial is to create a Pac-Man style game.


  • Pac-Man is a 1980s arcade game made by Namco, a Japanese company which also made Pole Position and Galaxian. When Pac-Man was distributed in the U.S. by Midway Games, it created a new genre, as most arcade games involved space shooting, like Space Invaders, or sports, like Pong. Wikipedia says that "Pac-Man is often credited with being a landmark in video game history, and is among the most famous arcade games of all time" (Wikipedia, 2010).
  • Pacman - The player guides the Pac-Man about the maze munching up the Dots in his path. Four Ghost Monsters - Inky, Blinky, Pinky, and Clyde - chase after the Pac-Man trying to capture and deflate him. The Pac-Man can counter attack by eating the big, Power Capsule that enables him to overpower the Monsters for additional score. After all the dots are gobbled up, the screen is cleared, and the Pac-Man continues for another round. Each rack features a special Fruit Target in the maze, which if eaten, earns Bonus Points. Players start with three Pac-Men. An additional Pac-Man is awarded for 10,000 points. (Excerpt from the original Midway Manual)
  • An extra feature one can add to the game for increasing the challenge and excitement is to program the ghosts using Collaborative Diffusion, which simulates ghosts chasing Pac-Man.
  • Follow the instructions below to create the game. In building this game you will quickly learn many programming and logic concepts and will eventually be able to create your own original games using your imagination!

Pac-man-arcade2.JPG


Getting Started

If this is your first time using Agentsheets, please follow the links to commonly used tools.

Make a New Project: The first thing you should do is create a new project and give it a meaningful name, such as "Pacman". When creating the project use 32 x 32 sized Agents.

Creating Agents: Create Agents for Pacman, Maze, Dots, Path, Ghost, and Power Capsule

Use the depiction editor to create the look and feel of your agents.

Have fun creating these agents, and use your imagination!


Agent Depictions


In addition to these main agents you will need to create the following agents to help in other functions of the game, such as creating new lives and refreshing the maze. They will be explained in more detail below.

  • Pacman Lives (for the 3 chances the player gets at winning)
  • Lives Counter (to keep track of how many lives are left)
  • New Pacman (to generate the new Pacman life)
  • New Ghost (to generate the new ghosts in their initial positions in the maze when you die)


Note for Maze Agent:

Look at the screen shot at the top of the page. There are many different icons used to make the make the maze. You will need to make several depictions for the maze agent.


Creating the Worksheet

  • Make a new worksheet and use the filled rectangle tool to make a rectangular area of Path agents.
  • Then use the many depictions of the maze agent to build a maze for your game.
  • When you save your worksheet, it will prompt you to enter the name. Keep in mind that any changes to your worksheet must be saved. If you press the "reset" button, it will revert back to the configuration of your last save.


Programming the Pacman Agent Pacman.gif

These are the behaviors of Pacman. Try to create the rules before looking at the screenshots!


While running

"While running" is a continuously running trigger for each agent. Pacman's "While running" trigger has two rules which contain the following:

Rule 1 runs every 0.1 secs calling the following methods to keep the Pacman moving and check if it's in ghost killing mode:

  • Decide which way to move based on user input of arrow keys
  • Move over certain agents (floor, pills, ghosts, etc)
  • Kill ghosts if the "heat" value is set to -1000 rather than +1000 (meaning a power pill has been eaten)
  • Heat can be explained more here.

Rule 2 runs every 5 secs if Pacman is in ghost killing mode (heat = -1000):

  • If the Pacman has eaten a power pill, then set to chase mode to last for 5 seconds
  • After 5 seconds has passed, set the heat back to +1000 (which is the original mode where ghosts chase Pacman)

Decide which way to move

(method: move_direction )


  1. User inputs arrow key press
  2. Pacman changes depiction to that direction (up, down, left, right)


Move over certain agents

1. Ground/path (method: move_ground)

  • Checks to see which depiction Pacman has (from user input of direction)
  • Checks to see if a Ground agent is in that direction
  • Makes a specific movement sound
  • Moves that direction

2. Points/dots (method: move_point)

  • Checks to see which depiction Pacman has (from user input of direction)
  • Checks to see if a Point agent is in that direction
  • Erases the point
  • Makes a specific point-eating sound
  • Moves that direction
  • Calls the method to update the score

3. Power pills/capsules (method: move_power_pill)

  • Checks to see which depiction Pacman has (from user input of direction)
  • Checks to see if a Power pill agent is in that direction
  • Erases the pill
  • Makes a specific pill-eating sound
  • Moves that direction
  • Calls the method to make invincible for 5 seconds
    • Changes to -1000 Heat(Pacman chases ghosts)
    • Broadcasts a message to ghosts to escape Pacman


Kill ghosts

(method: kill_ghost)


  1. Check to make sure Heat is -1000 and that there is a ghost agent in that direction
  2. Call the ghost's method for dying

Die if killed by ghost

(method: die)


  1. If heat = 1000 (meaning Pacman's vulnerable mode), then first make dying sound
  2. Keep changing depictions so Pacman spins into death, or just change to one dead depiction

Check to see if invincible

  1. Do this once every 5 secs in the "while_running" method to end the effects of the Powerpill
  2. Broadcast a message to Ghosts to chase Pacman again
  3. Change Pacman's Heat back to +1000

You can see this screenshot in the "while_running" screenshot above


Set Heat to 1000 when creating new agent

Explanation of Heat

  • Pacman has the highest Heat value of all agents, which makes Ghosts want to seek it.
  • The Heat disperses to agents surrounding Pacman, getting "cooler" the farther away they are from Pacman. Think of a campfire as warmer the closer you are to it and colder the farther away you are
  • The Ghost plans its movements based on which agent has the highest heat (only agents the Ghost can move on, such as Path, Points, Powerpills)
  • The Path, Points, and Powerpill agents continuously calculate their Heat values to be the average of the surrounding Heat values
  • If Pacman eats a Powerpill, his Heat changes to -1000 rather than +1000, so now all Heat values are inverted and the Ghosts flee Pacman


Initializing Heat

  • Create a new method
  • Right click at the top of the method where it says "On"
  • Select "Change Trigger"
  • Select "When creating new agent"
  • For the actions, set Heat to 1000

Programming the Ghost Agent Ghost red.gif

These are the behaviors of the ghost. Try to create the rules before looking at the screenshots!


"While running" is a continuously running trigger for each agent. The ghost's "While running" trigger has one rule which loops every 0.2 seconds and contains the following:

  • Choose direction to move based on highest Heat value
  • Move in that direction
  • Kill Pacman


Choose direction to move

(methods: choose_direction, max_heat_up, max_heat_down, max_heat_left, max_heat_right)


  • The Ghost chooses its direction to move based on which surrounding agent has the highest Heat value
  • A method called from the "while_running" trigger first initiates the "max_heat" variable to a low value, such as -1000
  • Then it iterates through all the directions of surrounding agents (in the form of methods) checking the following:
    • Does the agent in that direction have the attribute "heat"
    • Is that heat value greater than the max_heat value?
    • If so, change the max_heat to that value and set the Ghost depiction to that direction


Make Ghost move

(method: move)


  • Based on which depiction the Ghost has, make it move in that direction

Kill Pacman

(method: kill_pacman)


  1. Check to see if there's a Pacman agent in any surrounding direction (note: use "See a" instead of "See" since Pacman has many depictions)
  2. If so, call Pacman's "die" method
    1. This method changes Pacman's depiction
    2. Then it calls the Lives agent's "new_pacman" method, which assures there are lives left and resets for the next round


Die

(method: die)


  1. Changes the Ghost's depiction
  2. Erases the Ghost instance


Refresh Ghosts

  1. This method is called from the Lives agent when Pacman dies and still has lives left
  2. The only action is takes is "erase" so the Ghosts will erase
  3. When the Lives agent decrements a Pacman life, it does the following:
    1. Starts Pacman in the original starting position
    2. Erases the current ghosts on the screen by broadcasting to all Ghost agents: refresh_ghosts
    3. Calls the New_Ghost agent's "new_ghost" method, which creates new Ghosts in the original starting positions


Chase and Escape Pacman

(methods: chase_pacman and escape_pacman)


  1. The method "chase_pacman" sets the Ghost's color to red (angry) and is called from Pacman's while_running method as the default status color
  2. The method "escape_pacman" sets the Ghost's color to blue (scared) and is called from Pacman's invincible method when he eats the Power Pill


Programming the Path Agent Ground.gif

These are the behaviors of the Path (Ground/Floor) agent. Try to create the rules before looking at the screenshots!


Set Heat value to average of agents' Heat variables surrounding it

  • Pacman has the highest Heat value of all agents, which makes Ghosts want to seek it.
  • The Heat disperses to agents surrounding Pacman, getting "cooler" the farther away they are from Pacman
  • Each Path agent must continually calculate the Heat while the program runs, as Pacman's movement changes the Heat values
  • Find the Heat of the Path agent by finding the average of the agents' Heats surrounding it
  • Here's the equation to enter into the rule: 0.25*(heat[up]+heat[down]+heat[left]+heat[right])
    • heat[up] is the notation for "the Heat of the agent directly above the current agent"
    • Don't forget the end parenthesis, as the 0.25 needs to be multiplied by the sum of all four Heat values


Programming the Point (Dot) Agent Point.gif

These are the behaviors of the Point (Dot) agent. They are the exactly the same as the Path and the Powerpill behaviors.


Set Heat value to average of agents' Heat variables surrounding it

  • Pacman has the highest Heat value of all agents, which makes Ghosts want to seek it.
  • The Heat disperses to agents surrounding Pacman, getting "cooler" the farther away they are from Pacman
  • Each Point agent must continually calculate the Heat while the program runs, as Pacman's movement changes the Heat values
  • Find the Heat of the Point agent by finding the average of the agents' Heats surrounding it
  • Here's the equation to enter into the rule: 0.25*(heat[up]+heat[down]+heat[left]+heat[right])
    • heat[up] is the notation for "the Heat of the agent directly above the current agent"
    • Don't forget the end parenthesis, as the 0.25 needs to be multiplied by the sum of all four Heat values


Programming the Power Pill (Capsule) Agent Power pill.gif

These are the behaviors of the Power Pill (Capsule) agent. They are the exactly the same as the Path and the Point behaviors.


Set Heat value to average of agents' Heat variables surrounding it

  • Pacman has the highest Heat value of all agents, which makes Ghosts want to seek it.
  • The Heat disperses to agents surrounding Pacman, getting "cooler" the farther away they are from Pacman
  • Each Power Pill agent must continually calculate the Heat while the program runs, as Pacman's movement changes the Heat values
  • Find the Heat of the Power Pill agent by finding the average of the agents' Heats surrounding it
  • Here's the equation to enter into the rule: 0.25*(heat[up]+heat[down]+heat[left]+heat[right])
    • heat[up] is the notation for "the Heat of the agent directly above the current agent"
    • Don't forget the end parenthesis, as the 0.25 needs to be multiplied by the sum of all four Heat values


Programming the Lives (Lives_Counter) Agent Lives.gif

These are the behaviors of the Lives (Lives_Counter) agent. Try to create the rules before looking at the screenshots!


This agent only has one method "new_pacman" which is called from Pacman's "die" method and does the following:


  1. If there are lives left, it decrements a life (erases the current life's picture in the counter area at the bottom of the screen)
  2. Calls the Pacman_Lives agent's "decrease_life" method, which just moves the agent over one space
  3. Calls the Ghosts's "refresh_ghosts" method, which erases the Ghosts
  4. Calls the New_Ghost's "new_ghost" method, which creates new Ghosts in the original starting position
  5. Calls the New_Pacman's "new_pacman" method, which creates a new Pacman in the original starting position
  6. If there are no lives left, it shows a message, then resets the simulation


Programming the Pacman_Lives Agent

These are the behaviors of the Pacman_Lives agent. Try to create the rules before looking at the screenshots!


  • This agent only has one method "decrease_lives" which is called from the Lives agent's "new_pacman" method.
  • The "decrease_lives" method has a single action: to move the agent over left one space
  • The Lives and Pacman_Lives agents work together to help keep track of the how many lives are left and refreshing the board upon new life


Programming the New_Pacman Agent

These are the behaviors of the New_Pacman agent. Try to create the rules before looking at the screenshots!


  • This agent only has one method "new_pacman" which is called from the Lives agent's "new_pacman" method.
  • The "new_pacman" method has a single action: to create a new Pacman in the original starting position


Programming the New_Ghost Agent

These are the behaviors of the New_Ghost agent. Try to create the rules before looking at the screenshots!

  • This agent only has one method "new_ghosts" which is called from the Lives agent's "new_pacman" method.
  • The "new_ghosts" method has a single action: to create new Ghosts in original starting positions


Credits

All images and ideas are from AgentSheets