IWKS 3400 Homework 6 Game Development (I encourage you to work on this HW as a guild, perhaps Spring 2016 talking it through in front of a whiteboard. However, if John K. Bennett you are so inclined, you may work on this HW as an individual.)

Introduction This homework brings together several of the individual skills that you have developed so far, using a classic game as our vehicle for doing so. As you may know, Pac-Man was one of the most successful games of all time. It enjoyed this success in spite of being very simple, at least on the surface.

In Pac-Man, the player guides Pac-Man—up, down, left, and right—through a maze filled with dots (called “crumps”) that are consumed along the way. Four “ghosts” are also in the maze and chase after our hero; contact with a ghost is usually fatal (see # 1 below). The goal is to clear the maze of dots while avoiding the deadly ghosts. Each round starts with the ghosts in the “home” at the center of the maze, emerging from it to join in the chase. If Pac-Man is captured by a ghost, a life is lost, the ghosts are returned to their home, and a new Pac-Man is placed at the starting position before play continues. When the maze is cleared of all dots, the board is reset, and play resumes at a new Level (which is slightly harder than the previous level). If Pac-Man gets caught by a ghost when he has no extra lives, the game is over.

There are 244 dots in the maze, and Pac-Man must eat them all in order to proceed to the next round. The 240 small dots are worth ten points each, and the four strategically located flashing dots—known as energizers—are worth 50 points each. This yields a total of 2,600 points for clearing the maze of dots each round. Players have two ways to increase their score beyond what is earned from eating dots:

1) Consuming ghosts (this is only possible when the ghosts are frightened). Whenever Pac-Man eats one of the four energizer dots, the ghosts reverse their direction and, in early levels, turn blue for a short period of time (whose length varies by level) before returning to normal. While blue, they are vulnerable to Pac-Man and can be consumed for extra points, providing they are caught before the time expires. After being eaten, a ghost's eyes will return to the monster pen where it is resurrected, exiting to chase Pac-Man once again. The first ghost captured after an energizer has been eaten is always worth 200 points. Each additional ghost captured from the same energizer will then be worth twice as many points as the one before it—400, 800, and 1,600 points, respectively. If all four ghosts are captured at all four energizers, an additional 12,000 points can be earned on these earlier levels. The ghosts initially remain blue for several seconds. The ghosts' “blue time” is reduced (and eventually eliminated) at higher levels of play.

2) Consuming the bonus symbols (commonly known as “fruit”) that appear below the ghost home twice each round. The first bonus fruit appears after 70 dots have been cleared from the maze; the second one appears after 170 dots are cleared. Each fruit is worth anywhere from 100 to 5,000 points, depending on the current level. Whenever a fruit appears, the amount of time it stays on the screen before disappearing is a random time between nine and ten seconds.

The Pac-Man Ghosts (a short summary of their behavior)1

Blinky Blinky starts just outside of the ghost house. He is described his personality as a shadow in English, and in Japanese his personality is referred to as 追いかけ (oikake), which translates as “pursuer” or “chaser”. Blinky’s target tile in Chase mode is defined as Pac-Man’s current tile. Blinky has a unique targeting idiosyncrasy; at two defined points in each level

1 This section borrows heavily from http://gameinternals.com/post/2072558330/understanding-pac-man-ghost-behavior. (based on the number of crumps remaining), his speed increases by 5% and his behavior in Scatter mode changes. The timing of the speed change varies based on the level, with the change occurring earlier and earlier as the player progresses. The change to Scatter targeting is perhaps more significant than the speed increases, since it causes Blinky’s target tile to remain as Pac-Man’s position even while in Scatter mode, instead of his regular fixed tile in the upper-right corner. This effectively keeps Blinky in Chase mode permanently, although he is still forced to reverse direction as a result of a mode switch. When in this enhanced state, Blinky is generally referred to as “Cruise Elroy”, though the origin of this term is unknown. If Pac-Man dies while Blinky is in Cruise Elroy mode, he reverts back to normal behavior temporarily, but returns to Elroy mode as soon as all other ghosts have exited the ghost house.

Pinky Pinky starts inside the ghost house, but exits immediately, even on the first level. Pinky is known as speedy in English and in Japanese as 待ち伏せ (machibuse), which translates to “ambusher”. Pinky does not actually move faster than any of the other ghosts, but his targeting scheme attempts to move him to the place four tiles straight ahead of Pac-Man. This works when Pac-Man is facing to the left, down, or right, but when Pac-Man is facing upwards, a bug in the original game’s code causes Pinky’s target tile to actually be four tiles ahead of Pac-Man and four tiles to the left of him.

Inky Inky remains inside the ghost house for a short time on the first level, not joining the chase until Pac-Man has managed to consume at least 30 crumps. In English, Inky is known as bashful, while in Japanese he is referred to as 気紛れ (kimagure), or “whimsical.” Inky uses both Pac-Man’s position/facing as well as Blinky’s (the red ghost’s) position in targeting calculations. To locate Inky’s target, we first start by selecting the position two tiles in front of Pac-Man in his current direction of travel, similar to Pinky’s targeting method. From there, imagine drawing a vector from Blinky’s position to this tile, and then doubling the length of the vector. The tile that this new, extended vector ends on will be Inky’s actual target. Inky’s “two tiles in front of Pac-Man” calculation has the same overflow error as Pinky’s four-tile equivalent, so if Pac-Man is heading upwards, the endpoint of the initial vector from Blinky (before doubling) will actually be two tiles up and two tiles left of Pac-Man.

Clyde Clyde is the last to leave the ghost house, and does not exit at all in the first level until over a third of the dots have been eaten. Clyde’s English personality is described as pokey, whereas the Japanese description is お惚け (otoboke), or “feigning ignorance.” Clyde’s targeting has two separate modes: He first calculates his distance from Pac-Man. If he is farther than eight tiles away (Euclidian distance), his targeting is identical to Blinky’s, using Pac-Man’s current tile as his target. However, as soon as his distance to Pac-Man becomes less than eight tiles, Clyde’s target is set to the same tile as his fixed one in Scatter mode, just outside the bottom-left corner of the maze. The combination of these two methods has the overall effect of Clyde alternating between coming directly towards Pac-Man, and then changing his mind and heading back to his corner whenever he gets too close.

Your Assignment 1) Read the interviews with (the creator of Pac-Man) and Masaya Nakamura (founder of ).

2) Play Pac-Man long enough to gain some personal intuition about how the characters interact.

3) Carefully research the design of Pac-Man ghost behavior. Here are some helpful starting points: The Links page of The Pac-Man Museum, and in particular, the “Pac-Man Dossier” found there. You may also be interested in the Pac-Man Wiki. Not for the faint of heart, you can also find the original Pac-Man Z80 assembly code (in some cases with disassembly and comments) online.

4) Write the C# method GhostTargets(), a skeleton of which is shown below, that is intended to be called once every 1/60 of a second. GhostTargets() takes the arguments shown and returns an array of ghost target positions (i.e., where on the grid the ghosts should be headed next) within the 28x31 grid of tiles that represent the Pac-Man maze. You may make any assumptions you wish, and can modify the way that this method works in any way that you wish as long as you preserve its basic functionality. enum GhostIdentity {Blinky, Pinky, Inky, Clyde}; enum GhostState {Home, Chase, Scatter, Blue, Dead}; public Vector2[] GhostTargets (Vector2[] ghost_positions, GhostState[] ghostStates, Vector2 PacMan_position, int current_level) { Vector2[] newGhostTargets = new Vector2[4];

// ...

// do some computing // replace 0,0 with Blinky's new target tile newGhostTargets[(int)GhostIdentity.Blinky] = new Vector2 (0,0);

// do some computing // replace 0,0 with Inky's new target tile newGhostTargets[(int)GhostIdentity.Inky] = new Vector2 (0,0);

// do some computing // replace 0,0 with Pinky's new target tile newGhostTargets[(int)GhostIdentity.Pinky] = new Vector2 (0,0);

// do some computing // replace 0,0 with Clyde's new target tile newGhostTargets[(int)GhostIdentity.Clyde] = new Vector2 (0,0);

//... return (newGhostTargets); }

5) Write the C# method FindNextTile() that is intended to be called after GhostTargets(), and used to determine the next tile adjacent to its current tile to which the ghost should move, given its current target. Think of this as a constrained path-planning problem.

6) Write the C# method NextGhostState() that, based upon current game parameters, computes the correct next state of each ghost (which of course may be the same state that it is in).

Extra Credit: 7) Write the C# method GhostSpeed() that, based upon current game parameters, computes the correct speed of each ghost.

Note: You are free to combine any parts of this assignment in any way that might make it easier to complete the assignment. You are also free turn this assignment into your final project (in which case you will get credit for both HW6 and Lab12). Your overall goal for HW6 is to write code that manages the ghosts’ behavior. Please try to do this assignment without referring to the many working code examples online; talk to me if your guild is stuck.

Possibly Useful Resources: 1) An Excel Spreadsheet of Pac-Man game parameters by level (created by me, so there might be errors) 2) An Excel Spreadsheet of the Pac-Man game board showing which tiles are open (1), which have crumps (3), which are closed (0), and which are the ghosts’ home (2) (also created by me, same warning) 3) Me. I have developed a working XNA 4.0 implementation of Pac-Man that attempts to emulate the original game as closely as possible. I might be persuaded to reveal some implementation ideas if asked.