This program simulates a game of Pac-Man that allows for multiplayer. One player controls Pac-Man, while two other players control a red and yellow ghost using different tactile switches.
Dependencies: 4DGL-uLCD-SE PinDetect mbed
Fork of Pac-Man by
Ghost.h@1:839f59c6d021, 2015-10-19 (annotated)
- Committer:
- soapy12312
- Date:
- Mon Oct 19 17:51:14 2015 +0000
- Revision:
- 1:839f59c6d021
- Parent:
- 0:8b6686f2d4be
Multiplayer Pac-Man.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
soapy12312 | 0:8b6686f2d4be | 1 | /****************************************************** |
soapy12312 | 0:8b6686f2d4be | 2 | * Author: Yuanzhe Xu |
soapy12312 | 0:8b6686f2d4be | 3 | * Institution: Georgia Institute of Technology |
soapy12312 | 0:8b6686f2d4be | 4 | * Date: October 18, 2015 |
soapy12312 | 0:8b6686f2d4be | 5 | * |
soapy12312 | 0:8b6686f2d4be | 6 | * This header file declares all the functions and |
soapy12312 | 0:8b6686f2d4be | 7 | * variables needed for the Ghost object. Specific |
soapy12312 | 0:8b6686f2d4be | 8 | * ghost functionalities can be found in main.cpp. |
soapy12312 | 0:8b6686f2d4be | 9 | ******************************************************/ |
soapy12312 | 0:8b6686f2d4be | 10 | |
soapy12312 | 0:8b6686f2d4be | 11 | // Ghost class |
soapy12312 | 0:8b6686f2d4be | 12 | class Ghost { |
soapy12312 | 0:8b6686f2d4be | 13 | public: |
soapy12312 | 0:8b6686f2d4be | 14 | // Ghost constructor |
soapy12312 | 0:8b6686f2d4be | 15 | Ghost(int color, uLCD_4DGL& uLCD, Stage& stage, Pacman& pacman) |
soapy12312 | 0:8b6686f2d4be | 16 | : color(color), uLCD(uLCD), stage(stage), pacman(pacman) { |
soapy12312 | 0:8b6686f2d4be | 17 | } |
soapy12312 | 0:8b6686f2d4be | 18 | |
soapy12312 | 0:8b6686f2d4be | 19 | // Initialize the ghost at the beginning of the game |
soapy12312 | 0:8b6686f2d4be | 20 | void initialize(int xCoordinate, int yCoordinate, int facingDirection) { |
soapy12312 | 0:8b6686f2d4be | 21 | startingX = xCoordinate; |
soapy12312 | 0:8b6686f2d4be | 22 | startingY = yCoordinate; |
soapy12312 | 0:8b6686f2d4be | 23 | x = xCoordinate; |
soapy12312 | 0:8b6686f2d4be | 24 | y = yCoordinate; |
soapy12312 | 0:8b6686f2d4be | 25 | direction = facingDirection; |
soapy12312 | 0:8b6686f2d4be | 26 | immobile = false; |
soapy12312 | 0:8b6686f2d4be | 27 | draw(x, y); |
soapy12312 | 0:8b6686f2d4be | 28 | } |
soapy12312 | 0:8b6686f2d4be | 29 | |
soapy12312 | 0:8b6686f2d4be | 30 | // Set the next direction |
soapy12312 | 0:8b6686f2d4be | 31 | void setNextDirection(int next) { |
soapy12312 | 0:8b6686f2d4be | 32 | previousDirection = (direction + 2) % 4; |
soapy12312 | 0:8b6686f2d4be | 33 | |
soapy12312 | 0:8b6686f2d4be | 34 | // If the next direction is not its previous direction |
soapy12312 | 0:8b6686f2d4be | 35 | if (next != previousDirection) { |
soapy12312 | 0:8b6686f2d4be | 36 | // Set the next direction to next |
soapy12312 | 0:8b6686f2d4be | 37 | nextDirection = next; |
soapy12312 | 0:8b6686f2d4be | 38 | } |
soapy12312 | 0:8b6686f2d4be | 39 | } |
soapy12312 | 0:8b6686f2d4be | 40 | |
soapy12312 | 0:8b6686f2d4be | 41 | // Move the ghost |
soapy12312 | 0:8b6686f2d4be | 42 | void move() { |
soapy12312 | 0:8b6686f2d4be | 43 | // If Pac-Man died |
soapy12312 | 0:8b6686f2d4be | 44 | if (pacman.dead == true) { |
soapy12312 | 0:8b6686f2d4be | 45 | // Reset the ghost |
soapy12312 | 0:8b6686f2d4be | 46 | erase(x, y); |
soapy12312 | 0:8b6686f2d4be | 47 | reinitialize(); |
soapy12312 | 0:8b6686f2d4be | 48 | pacman.dead = false; |
soapy12312 | 0:8b6686f2d4be | 49 | } |
soapy12312 | 0:8b6686f2d4be | 50 | |
soapy12312 | 0:8b6686f2d4be | 51 | // If the ghost is not moving |
soapy12312 | 0:8b6686f2d4be | 52 | if (immobile == true) { |
soapy12312 | 0:8b6686f2d4be | 53 | // Reset the ghost |
soapy12312 | 0:8b6686f2d4be | 54 | erase(x, y); |
soapy12312 | 0:8b6686f2d4be | 55 | stage.redrawPacDots(); |
soapy12312 | 0:8b6686f2d4be | 56 | reinitialize(); |
soapy12312 | 0:8b6686f2d4be | 57 | } |
soapy12312 | 0:8b6686f2d4be | 58 | |
soapy12312 | 0:8b6686f2d4be | 59 | // Move the ghost in its current direction |
soapy12312 | 0:8b6686f2d4be | 60 | switch (direction) { |
soapy12312 | 0:8b6686f2d4be | 61 | case FACERIGHT: |
soapy12312 | 0:8b6686f2d4be | 62 | moveRight(); |
soapy12312 | 0:8b6686f2d4be | 63 | break; |
soapy12312 | 0:8b6686f2d4be | 64 | case FACEDOWN: |
soapy12312 | 0:8b6686f2d4be | 65 | moveDown(); |
soapy12312 | 0:8b6686f2d4be | 66 | break; |
soapy12312 | 0:8b6686f2d4be | 67 | case FACELEFT: |
soapy12312 | 0:8b6686f2d4be | 68 | moveLeft(); |
soapy12312 | 0:8b6686f2d4be | 69 | break; |
soapy12312 | 0:8b6686f2d4be | 70 | case FACEUP: |
soapy12312 | 0:8b6686f2d4be | 71 | moveUp(); |
soapy12312 | 0:8b6686f2d4be | 72 | break; |
soapy12312 | 0:8b6686f2d4be | 73 | default: |
soapy12312 | 0:8b6686f2d4be | 74 | break; |
soapy12312 | 0:8b6686f2d4be | 75 | } |
soapy12312 | 0:8b6686f2d4be | 76 | |
soapy12312 | 0:8b6686f2d4be | 77 | // If Pac-Man and the ghost occupy the same position |
soapy12312 | 0:8b6686f2d4be | 78 | if ((abs(pacman.x - x) < 5) && (abs(pacman.y - y) < 5)) { |
soapy12312 | 0:8b6686f2d4be | 79 | // If Pac-Man is not invincible |
soapy12312 | 0:8b6686f2d4be | 80 | if (pacman.powerUp == 0) { |
soapy12312 | 0:8b6686f2d4be | 81 | // Reset Pac-Man and the ghost |
soapy12312 | 0:8b6686f2d4be | 82 | pacman.ghostsEaten = 0; |
soapy12312 | 0:8b6686f2d4be | 83 | pacman.erase(pacman.x, pacman.y); |
soapy12312 | 0:8b6686f2d4be | 84 | erase(x, y); |
soapy12312 | 0:8b6686f2d4be | 85 | pacman.draw(pacman.x, pacman.y); |
soapy12312 | 0:8b6686f2d4be | 86 | wait(0.5); |
soapy12312 | 0:8b6686f2d4be | 87 | pacman.die(); |
soapy12312 | 0:8b6686f2d4be | 88 | pacman.dead = true; |
soapy12312 | 0:8b6686f2d4be | 89 | reinitialize(); |
soapy12312 | 0:8b6686f2d4be | 90 | // If Pac-Man is invincible |
soapy12312 | 0:8b6686f2d4be | 91 | } else { |
soapy12312 | 0:8b6686f2d4be | 92 | // Reset the ghost |
soapy12312 | 0:8b6686f2d4be | 93 | pacman.ghostsEaten++; |
soapy12312 | 0:8b6686f2d4be | 94 | pacman.score += pacman.ghostsEaten * 200; |
soapy12312 | 0:8b6686f2d4be | 95 | pacman.scoreChanged = true; |
soapy12312 | 0:8b6686f2d4be | 96 | erase(x, y); |
soapy12312 | 0:8b6686f2d4be | 97 | stage.redrawPacDots(); |
soapy12312 | 0:8b6686f2d4be | 98 | pacman.draw(pacman.x, pacman.y); |
soapy12312 | 0:8b6686f2d4be | 99 | wait(0.5); |
soapy12312 | 0:8b6686f2d4be | 100 | reinitialize(); |
soapy12312 | 0:8b6686f2d4be | 101 | } |
soapy12312 | 0:8b6686f2d4be | 102 | } |
soapy12312 | 0:8b6686f2d4be | 103 | } |
soapy12312 | 0:8b6686f2d4be | 104 | |
soapy12312 | 0:8b6686f2d4be | 105 | |
soapy12312 | 0:8b6686f2d4be | 106 | private: |
soapy12312 | 0:8b6686f2d4be | 107 | // The ghost's color |
soapy12312 | 0:8b6686f2d4be | 108 | int color; |
soapy12312 | 0:8b6686f2d4be | 109 | // Starting horizontal position |
soapy12312 | 0:8b6686f2d4be | 110 | int startingX; |
soapy12312 | 0:8b6686f2d4be | 111 | // Starting vertical position |
soapy12312 | 0:8b6686f2d4be | 112 | int startingY; |
soapy12312 | 0:8b6686f2d4be | 113 | // Current horizontal position |
soapy12312 | 0:8b6686f2d4be | 114 | int x; |
soapy12312 | 0:8b6686f2d4be | 115 | // Current vertical position |
soapy12312 | 0:8b6686f2d4be | 116 | int y; |
soapy12312 | 0:8b6686f2d4be | 117 | // Previous direction |
soapy12312 | 0:8b6686f2d4be | 118 | int previousDirection; |
soapy12312 | 0:8b6686f2d4be | 119 | // Current direction |
soapy12312 | 0:8b6686f2d4be | 120 | int direction; |
soapy12312 | 0:8b6686f2d4be | 121 | // Next direction |
soapy12312 | 0:8b6686f2d4be | 122 | int nextDirection; |
soapy12312 | 0:8b6686f2d4be | 123 | // Ghost resets when immobile is true |
soapy12312 | 0:8b6686f2d4be | 124 | bool immobile; |
soapy12312 | 0:8b6686f2d4be | 125 | // uLCD display |
soapy12312 | 0:8b6686f2d4be | 126 | uLCD_4DGL& uLCD; |
soapy12312 | 0:8b6686f2d4be | 127 | // Stage reference |
soapy12312 | 0:8b6686f2d4be | 128 | Stage& stage; |
soapy12312 | 0:8b6686f2d4be | 129 | // Pacman reference |
soapy12312 | 0:8b6686f2d4be | 130 | Pacman& pacman; |
soapy12312 | 0:8b6686f2d4be | 131 | |
soapy12312 | 0:8b6686f2d4be | 132 | // Reset function for each ghost |
soapy12312 | 0:8b6686f2d4be | 133 | void reinitialize() { |
soapy12312 | 0:8b6686f2d4be | 134 | // Red ghost reset |
soapy12312 | 0:8b6686f2d4be | 135 | if (color == RED) { |
soapy12312 | 0:8b6686f2d4be | 136 | initialize(startingX, startingY, FACELEFT); |
soapy12312 | 0:8b6686f2d4be | 137 | // Yellow ghost reset |
soapy12312 | 0:8b6686f2d4be | 138 | } else if (color == YELLOW) { |
soapy12312 | 0:8b6686f2d4be | 139 | initialize(startingX, startingY, FACERIGHT); |
soapy12312 | 0:8b6686f2d4be | 140 | } |
soapy12312 | 0:8b6686f2d4be | 141 | } |
soapy12312 | 0:8b6686f2d4be | 142 | |
soapy12312 | 0:8b6686f2d4be | 143 | // Draw the ghost based on its direction |
soapy12312 | 0:8b6686f2d4be | 144 | void draw(int xCoordinate, int yCoordinate) { |
soapy12312 | 0:8b6686f2d4be | 145 | // Outer bound rectangular coordinates of the ghost |
soapy12312 | 0:8b6686f2d4be | 146 | int x1 = xCoordinate - 3; |
soapy12312 | 0:8b6686f2d4be | 147 | int x2 = xCoordinate + 3; |
soapy12312 | 0:8b6686f2d4be | 148 | int y1 = yCoordinate - 3; |
soapy12312 | 0:8b6686f2d4be | 149 | int y2 = yCoordinate + 3; |
soapy12312 | 0:8b6686f2d4be | 150 | |
soapy12312 | 0:8b6686f2d4be | 151 | // If Pac-Man is currently invincible |
soapy12312 | 0:8b6686f2d4be | 152 | if (pacman.powerUp > 0) { |
soapy12312 | 0:8b6686f2d4be | 153 | // Draw the ghost in blue |
soapy12312 | 0:8b6686f2d4be | 154 | uLCD.filled_rectangle(x1, y1, x2, y2, BLUE); |
soapy12312 | 0:8b6686f2d4be | 155 | // If Pac-Man is not currently invincible |
soapy12312 | 0:8b6686f2d4be | 156 | } else { |
soapy12312 | 0:8b6686f2d4be | 157 | // Draw the ghost in its original color |
soapy12312 | 0:8b6686f2d4be | 158 | uLCD.filled_rectangle(x1, y1, x2, y2, color); |
soapy12312 | 0:8b6686f2d4be | 159 | } |
soapy12312 | 0:8b6686f2d4be | 160 | |
soapy12312 | 0:8b6686f2d4be | 161 | // Specify the pixels |
soapy12312 | 0:8b6686f2d4be | 162 | uLCD.pixel(x1, y1, BLACK); |
soapy12312 | 0:8b6686f2d4be | 163 | uLCD.pixel(x2, y1, BLACK); |
soapy12312 | 0:8b6686f2d4be | 164 | uLCD.pixel(xCoordinate - 2, y2, BLACK); |
soapy12312 | 0:8b6686f2d4be | 165 | uLCD.pixel(xCoordinate, y2, BLACK); |
soapy12312 | 0:8b6686f2d4be | 166 | uLCD.pixel(xCoordinate + 2, y2, BLACK); |
soapy12312 | 0:8b6686f2d4be | 167 | uLCD.pixel(x1, yCoordinate - 1, WHITE); |
soapy12312 | 0:8b6686f2d4be | 168 | uLCD.pixel(xCoordinate - 2, yCoordinate - 2, WHITE); |
soapy12312 | 0:8b6686f2d4be | 169 | uLCD.pixel(xCoordinate - 2, yCoordinate - 1, WHITE); |
soapy12312 | 0:8b6686f2d4be | 170 | uLCD.pixel(xCoordinate - 2, yCoordinate, WHITE); |
soapy12312 | 0:8b6686f2d4be | 171 | uLCD.pixel(xCoordinate - 1, yCoordinate - 1, WHITE); |
soapy12312 | 0:8b6686f2d4be | 172 | uLCD.pixel(x2, yCoordinate - 1, WHITE); |
soapy12312 | 0:8b6686f2d4be | 173 | uLCD.pixel(xCoordinate + 2, yCoordinate - 2, WHITE); |
soapy12312 | 0:8b6686f2d4be | 174 | uLCD.pixel(xCoordinate + 2, yCoordinate - 1, WHITE); |
soapy12312 | 0:8b6686f2d4be | 175 | uLCD.pixel(xCoordinate + 2, yCoordinate, WHITE); |
soapy12312 | 0:8b6686f2d4be | 176 | uLCD.pixel(xCoordinate + 1, yCoordinate - 1, WHITE); |
soapy12312 | 0:8b6686f2d4be | 177 | |
soapy12312 | 0:8b6686f2d4be | 178 | // Draw the ghost's eyes based on its current direction |
soapy12312 | 0:8b6686f2d4be | 179 | switch (direction) { |
soapy12312 | 0:8b6686f2d4be | 180 | case FACERIGHT: |
soapy12312 | 0:8b6686f2d4be | 181 | uLCD.pixel(xCoordinate - 1, yCoordinate - 1, BLACK); |
soapy12312 | 0:8b6686f2d4be | 182 | uLCD.pixel(x2, yCoordinate - 1, BLACK); |
soapy12312 | 0:8b6686f2d4be | 183 | break; |
soapy12312 | 0:8b6686f2d4be | 184 | case FACEDOWN: |
soapy12312 | 0:8b6686f2d4be | 185 | uLCD.pixel(xCoordinate - 2, yCoordinate, BLACK); |
soapy12312 | 0:8b6686f2d4be | 186 | uLCD.pixel(xCoordinate + 2, yCoordinate, BLACK); |
soapy12312 | 0:8b6686f2d4be | 187 | break; |
soapy12312 | 0:8b6686f2d4be | 188 | case FACELEFT: |
soapy12312 | 0:8b6686f2d4be | 189 | uLCD.pixel(x1, yCoordinate - 1, BLACK); |
soapy12312 | 0:8b6686f2d4be | 190 | uLCD.pixel(xCoordinate + 1, yCoordinate - 1, BLACK); |
soapy12312 | 0:8b6686f2d4be | 191 | break; |
soapy12312 | 0:8b6686f2d4be | 192 | case FACEUP: |
soapy12312 | 0:8b6686f2d4be | 193 | uLCD.pixel(xCoordinate - 2, yCoordinate - 2, BLACK); |
soapy12312 | 0:8b6686f2d4be | 194 | uLCD.pixel(xCoordinate + 2, yCoordinate - 2, BLACK); |
soapy12312 | 0:8b6686f2d4be | 195 | break; |
soapy12312 | 0:8b6686f2d4be | 196 | default: |
soapy12312 | 0:8b6686f2d4be | 197 | break; |
soapy12312 | 0:8b6686f2d4be | 198 | } |
soapy12312 | 0:8b6686f2d4be | 199 | } |
soapy12312 | 0:8b6686f2d4be | 200 | |
soapy12312 | 0:8b6686f2d4be | 201 | // Erase the ghost from the given position |
soapy12312 | 0:8b6686f2d4be | 202 | void erase(int xCoordinate, int yCoordinate) { |
soapy12312 | 0:8b6686f2d4be | 203 | int x1 = xCoordinate - 3; |
soapy12312 | 0:8b6686f2d4be | 204 | int x2 = xCoordinate + 3; |
soapy12312 | 0:8b6686f2d4be | 205 | int y1 = yCoordinate - 3; |
soapy12312 | 0:8b6686f2d4be | 206 | int y2 = yCoordinate + 3; |
soapy12312 | 0:8b6686f2d4be | 207 | uLCD.filled_rectangle(x1, y1, x2, y2, BLACK); |
soapy12312 | 0:8b6686f2d4be | 208 | } |
soapy12312 | 0:8b6686f2d4be | 209 | |
soapy12312 | 0:8b6686f2d4be | 210 | // Move the ghost right |
soapy12312 | 0:8b6686f2d4be | 211 | void moveRight() { |
soapy12312 | 0:8b6686f2d4be | 212 | // If the space to the right is a valid position and is not the rightmost space |
soapy12312 | 0:8b6686f2d4be | 213 | if ((stage.positions[x + 1][y] != 0) && (x != 124)) { |
soapy12312 | 0:8b6686f2d4be | 214 | // The ghost is mobile |
soapy12312 | 0:8b6686f2d4be | 215 | immobile = false; |
soapy12312 | 0:8b6686f2d4be | 216 | // Erase the ghost from its current position |
soapy12312 | 0:8b6686f2d4be | 217 | erase(x, y); |
soapy12312 | 0:8b6686f2d4be | 218 | // Increment the ghost's x-coordinate |
soapy12312 | 0:8b6686f2d4be | 219 | x++; |
soapy12312 | 0:8b6686f2d4be | 220 | |
soapy12312 | 0:8b6686f2d4be | 221 | // If passed over a pac dot |
soapy12312 | 0:8b6686f2d4be | 222 | if (stage.positions[x - 4][y] == 2) { |
soapy12312 | 0:8b6686f2d4be | 223 | // Redraw the pac dot |
soapy12312 | 0:8b6686f2d4be | 224 | uLCD.pixel(x - 4, y, YELLOW); |
soapy12312 | 0:8b6686f2d4be | 225 | // If passed over a big pac dot |
soapy12312 | 0:8b6686f2d4be | 226 | } else { |
soapy12312 | 0:8b6686f2d4be | 227 | // Redraw the big pac dot |
soapy12312 | 0:8b6686f2d4be | 228 | for (int i = x - 2; i >= x - 5; i--) { |
soapy12312 | 0:8b6686f2d4be | 229 | if (stage.positions[i][y] == 3) { |
soapy12312 | 0:8b6686f2d4be | 230 | uLCD.filled_circle(i, y, 1, YELLOW); |
soapy12312 | 0:8b6686f2d4be | 231 | } |
soapy12312 | 0:8b6686f2d4be | 232 | } |
soapy12312 | 0:8b6686f2d4be | 233 | } |
soapy12312 | 0:8b6686f2d4be | 234 | |
soapy12312 | 0:8b6686f2d4be | 235 | // Draw the ghost in its new position |
soapy12312 | 0:8b6686f2d4be | 236 | draw(x, y); |
soapy12312 | 0:8b6686f2d4be | 237 | // If the space to the right is not a valid positions |
soapy12312 | 0:8b6686f2d4be | 238 | } else if (stage.positions[x + 1][y] == 0) { |
soapy12312 | 0:8b6686f2d4be | 239 | // The ghost is immobile |
soapy12312 | 0:8b6686f2d4be | 240 | immobile = true; |
soapy12312 | 0:8b6686f2d4be | 241 | wait(0.03); |
soapy12312 | 0:8b6686f2d4be | 242 | } |
soapy12312 | 0:8b6686f2d4be | 243 | |
soapy12312 | 0:8b6686f2d4be | 244 | // If the rightmost position is reached |
soapy12312 | 0:8b6686f2d4be | 245 | if (x == 124) { |
soapy12312 | 0:8b6686f2d4be | 246 | // The ghost is mobile |
soapy12312 | 0:8b6686f2d4be | 247 | immobile = false; |
soapy12312 | 0:8b6686f2d4be | 248 | // Erase the ghost from its current position |
soapy12312 | 0:8b6686f2d4be | 249 | erase(x, y); |
soapy12312 | 0:8b6686f2d4be | 250 | // Set the ghost's x-coordinate to 4 |
soapy12312 | 0:8b6686f2d4be | 251 | x = 4; |
soapy12312 | 0:8b6686f2d4be | 252 | |
soapy12312 | 0:8b6686f2d4be | 253 | // If passed over a pac dot |
soapy12312 | 0:8b6686f2d4be | 254 | if (stage.positions[124][68] == 2) { |
soapy12312 | 0:8b6686f2d4be | 255 | // Redraw the pac dot |
soapy12312 | 0:8b6686f2d4be | 256 | uLCD.pixel(124, 68, YELLOW); |
soapy12312 | 0:8b6686f2d4be | 257 | } |
soapy12312 | 0:8b6686f2d4be | 258 | |
soapy12312 | 0:8b6686f2d4be | 259 | // Draw the ghost in its new position |
soapy12312 | 0:8b6686f2d4be | 260 | draw(x, y); |
soapy12312 | 0:8b6686f2d4be | 261 | } |
soapy12312 | 0:8b6686f2d4be | 262 | |
soapy12312 | 0:8b6686f2d4be | 263 | // If Pac-Man's invincibility status changed |
soapy12312 | 0:8b6686f2d4be | 264 | if (pacman.powerUpChanged == true) { |
soapy12312 | 0:8b6686f2d4be | 265 | // Change the color of the ghost |
soapy12312 | 0:8b6686f2d4be | 266 | draw(x, y); |
soapy12312 | 0:8b6686f2d4be | 267 | // Reset Pac-Man's invincibility status change |
soapy12312 | 0:8b6686f2d4be | 268 | pacman.powerUpChanged = false; |
soapy12312 | 0:8b6686f2d4be | 269 | } |
soapy12312 | 0:8b6686f2d4be | 270 | |
soapy12312 | 0:8b6686f2d4be | 271 | // Set the ghost's next direction |
soapy12312 | 0:8b6686f2d4be | 272 | switch (nextDirection) { |
soapy12312 | 0:8b6686f2d4be | 273 | case FACEDOWN: |
soapy12312 | 0:8b6686f2d4be | 274 | if (stage.positions[x][y + 1] != 0) { |
soapy12312 | 0:8b6686f2d4be | 275 | direction = nextDirection; |
soapy12312 | 0:8b6686f2d4be | 276 | } |
soapy12312 | 0:8b6686f2d4be | 277 | |
soapy12312 | 0:8b6686f2d4be | 278 | break; |
soapy12312 | 0:8b6686f2d4be | 279 | case FACELEFT: |
soapy12312 | 0:8b6686f2d4be | 280 | if (stage.positions[x - 1][y] != 0) { |
soapy12312 | 0:8b6686f2d4be | 281 | direction = nextDirection; |
soapy12312 | 0:8b6686f2d4be | 282 | } |
soapy12312 | 0:8b6686f2d4be | 283 | |
soapy12312 | 0:8b6686f2d4be | 284 | break; |
soapy12312 | 0:8b6686f2d4be | 285 | case FACEUP: |
soapy12312 | 0:8b6686f2d4be | 286 | if (stage.positions[x][y - 1] != 0) { |
soapy12312 | 0:8b6686f2d4be | 287 | direction = nextDirection; |
soapy12312 | 0:8b6686f2d4be | 288 | } |
soapy12312 | 0:8b6686f2d4be | 289 | |
soapy12312 | 0:8b6686f2d4be | 290 | break; |
soapy12312 | 0:8b6686f2d4be | 291 | default: |
soapy12312 | 0:8b6686f2d4be | 292 | break; |
soapy12312 | 0:8b6686f2d4be | 293 | } |
soapy12312 | 0:8b6686f2d4be | 294 | } |
soapy12312 | 0:8b6686f2d4be | 295 | |
soapy12312 | 0:8b6686f2d4be | 296 | // Move the ghost down |
soapy12312 | 0:8b6686f2d4be | 297 | void moveDown() { |
soapy12312 | 0:8b6686f2d4be | 298 | // If the space in the downward direction is a valid position |
soapy12312 | 0:8b6686f2d4be | 299 | if (stage.positions[x][y + 1] != 0) { |
soapy12312 | 0:8b6686f2d4be | 300 | // The ghost is mobile |
soapy12312 | 0:8b6686f2d4be | 301 | immobile = false; |
soapy12312 | 0:8b6686f2d4be | 302 | // Erase the ghost from its current position |
soapy12312 | 0:8b6686f2d4be | 303 | erase(x, y); |
soapy12312 | 0:8b6686f2d4be | 304 | // Increment the ghost's y-coordinate |
soapy12312 | 0:8b6686f2d4be | 305 | y++; |
soapy12312 | 0:8b6686f2d4be | 306 | |
soapy12312 | 0:8b6686f2d4be | 307 | // If passed over a pac dot |
soapy12312 | 0:8b6686f2d4be | 308 | if (stage.positions[x][y - 4] == 2) { |
soapy12312 | 0:8b6686f2d4be | 309 | // Redraw the pac dot |
soapy12312 | 0:8b6686f2d4be | 310 | uLCD.pixel(x, y - 4, YELLOW); |
soapy12312 | 0:8b6686f2d4be | 311 | // If passed over a big pac dot |
soapy12312 | 0:8b6686f2d4be | 312 | } else { |
soapy12312 | 0:8b6686f2d4be | 313 | // Redraw the big pac dot |
soapy12312 | 0:8b6686f2d4be | 314 | for (int j = y - 2; j >= y - 5; j--) { |
soapy12312 | 0:8b6686f2d4be | 315 | if (stage.positions[x][j] == 3) { |
soapy12312 | 0:8b6686f2d4be | 316 | uLCD.filled_circle(x, j, 1, YELLOW); |
soapy12312 | 0:8b6686f2d4be | 317 | } |
soapy12312 | 0:8b6686f2d4be | 318 | } |
soapy12312 | 0:8b6686f2d4be | 319 | } |
soapy12312 | 0:8b6686f2d4be | 320 | |
soapy12312 | 0:8b6686f2d4be | 321 | // Draw the ghost in its new position |
soapy12312 | 0:8b6686f2d4be | 322 | draw(x, y); |
soapy12312 | 0:8b6686f2d4be | 323 | // If the space in the downward direction is not a valid position |
soapy12312 | 0:8b6686f2d4be | 324 | } else { |
soapy12312 | 0:8b6686f2d4be | 325 | // The ghost is immobile |
soapy12312 | 0:8b6686f2d4be | 326 | immobile = true; |
soapy12312 | 0:8b6686f2d4be | 327 | wait(0.03); |
soapy12312 | 0:8b6686f2d4be | 328 | } |
soapy12312 | 0:8b6686f2d4be | 329 | |
soapy12312 | 0:8b6686f2d4be | 330 | // If Pac-Man's invincibility status changed |
soapy12312 | 0:8b6686f2d4be | 331 | if (pacman.powerUpChanged == true) { |
soapy12312 | 0:8b6686f2d4be | 332 | // Change the color of the ghost |
soapy12312 | 0:8b6686f2d4be | 333 | draw(x, y); |
soapy12312 | 0:8b6686f2d4be | 334 | // Reset Pac-Man's invincibility status change |
soapy12312 | 0:8b6686f2d4be | 335 | pacman.powerUpChanged = false; |
soapy12312 | 0:8b6686f2d4be | 336 | } |
soapy12312 | 0:8b6686f2d4be | 337 | |
soapy12312 | 0:8b6686f2d4be | 338 | // Set the ghost's next direction |
soapy12312 | 0:8b6686f2d4be | 339 | switch (nextDirection) { |
soapy12312 | 0:8b6686f2d4be | 340 | case FACERIGHT: |
soapy12312 | 0:8b6686f2d4be | 341 | if (stage.positions[x + 1][y] != 0) { |
soapy12312 | 0:8b6686f2d4be | 342 | direction = nextDirection; |
soapy12312 | 0:8b6686f2d4be | 343 | } |
soapy12312 | 0:8b6686f2d4be | 344 | |
soapy12312 | 0:8b6686f2d4be | 345 | break; |
soapy12312 | 0:8b6686f2d4be | 346 | case FACELEFT: |
soapy12312 | 0:8b6686f2d4be | 347 | if (stage.positions[x - 1][y] != 0) { |
soapy12312 | 0:8b6686f2d4be | 348 | direction = nextDirection; |
soapy12312 | 0:8b6686f2d4be | 349 | } |
soapy12312 | 0:8b6686f2d4be | 350 | |
soapy12312 | 0:8b6686f2d4be | 351 | break; |
soapy12312 | 0:8b6686f2d4be | 352 | case FACEUP: |
soapy12312 | 0:8b6686f2d4be | 353 | if (stage.positions[x][y - 1] != 0) { |
soapy12312 | 0:8b6686f2d4be | 354 | direction = nextDirection; |
soapy12312 | 0:8b6686f2d4be | 355 | } |
soapy12312 | 0:8b6686f2d4be | 356 | |
soapy12312 | 0:8b6686f2d4be | 357 | break; |
soapy12312 | 0:8b6686f2d4be | 358 | default: |
soapy12312 | 0:8b6686f2d4be | 359 | break; |
soapy12312 | 0:8b6686f2d4be | 360 | } |
soapy12312 | 0:8b6686f2d4be | 361 | } |
soapy12312 | 0:8b6686f2d4be | 362 | |
soapy12312 | 0:8b6686f2d4be | 363 | // Move the ghost left |
soapy12312 | 0:8b6686f2d4be | 364 | void moveLeft() { |
soapy12312 | 0:8b6686f2d4be | 365 | // If the space to the left is a valid position and is not the leftmost space |
soapy12312 | 0:8b6686f2d4be | 366 | if ((stage.positions[x - 1][y] != 0) && (x != 4)) { |
soapy12312 | 0:8b6686f2d4be | 367 | // The ghost is mobile |
soapy12312 | 0:8b6686f2d4be | 368 | immobile = false; |
soapy12312 | 0:8b6686f2d4be | 369 | // Erase the ghost from its current position |
soapy12312 | 0:8b6686f2d4be | 370 | erase(x, y); |
soapy12312 | 0:8b6686f2d4be | 371 | // Decrement the ghost's x-coordinate |
soapy12312 | 0:8b6686f2d4be | 372 | x--; |
soapy12312 | 0:8b6686f2d4be | 373 | |
soapy12312 | 0:8b6686f2d4be | 374 | // If passed over a pac dot |
soapy12312 | 0:8b6686f2d4be | 375 | if (stage.positions[x + 4][y] == 2) { |
soapy12312 | 0:8b6686f2d4be | 376 | // Redraw the pac dot |
soapy12312 | 0:8b6686f2d4be | 377 | uLCD.pixel(x + 4, y, YELLOW); |
soapy12312 | 0:8b6686f2d4be | 378 | // If passed over a big pac dot |
soapy12312 | 0:8b6686f2d4be | 379 | } else { |
soapy12312 | 0:8b6686f2d4be | 380 | // Redraw the big pac dot |
soapy12312 | 0:8b6686f2d4be | 381 | for (int i = x + 2; i <= x + 5; i++) { |
soapy12312 | 0:8b6686f2d4be | 382 | if (stage.positions[i][y] == 3) { |
soapy12312 | 0:8b6686f2d4be | 383 | uLCD.filled_circle(i, y, 1, YELLOW); |
soapy12312 | 0:8b6686f2d4be | 384 | } |
soapy12312 | 0:8b6686f2d4be | 385 | } |
soapy12312 | 0:8b6686f2d4be | 386 | } |
soapy12312 | 0:8b6686f2d4be | 387 | |
soapy12312 | 0:8b6686f2d4be | 388 | // Draw the ghost in its new position |
soapy12312 | 0:8b6686f2d4be | 389 | draw(x, y); |
soapy12312 | 0:8b6686f2d4be | 390 | // If the space to the left is not a valid position |
soapy12312 | 0:8b6686f2d4be | 391 | } else if (stage.positions[x - 1][y] == 0) { |
soapy12312 | 0:8b6686f2d4be | 392 | // The ghost is immobile |
soapy12312 | 0:8b6686f2d4be | 393 | immobile = true; |
soapy12312 | 0:8b6686f2d4be | 394 | wait(0.03); |
soapy12312 | 0:8b6686f2d4be | 395 | } |
soapy12312 | 0:8b6686f2d4be | 396 | |
soapy12312 | 0:8b6686f2d4be | 397 | // If the leftmost position is reached |
soapy12312 | 0:8b6686f2d4be | 398 | if (x == 4) { |
soapy12312 | 0:8b6686f2d4be | 399 | // The ghost is mobile |
soapy12312 | 0:8b6686f2d4be | 400 | immobile = false; |
soapy12312 | 0:8b6686f2d4be | 401 | // Erase the ghost from its current position |
soapy12312 | 0:8b6686f2d4be | 402 | erase(x, y); |
soapy12312 | 0:8b6686f2d4be | 403 | // Set the ghost's x-coordinate to 124 |
soapy12312 | 0:8b6686f2d4be | 404 | x = 124; |
soapy12312 | 0:8b6686f2d4be | 405 | |
soapy12312 | 0:8b6686f2d4be | 406 | // If passed over a pac dot |
soapy12312 | 0:8b6686f2d4be | 407 | if (stage.positions[4][68] == 2) { |
soapy12312 | 0:8b6686f2d4be | 408 | // Redraw the pac dot |
soapy12312 | 0:8b6686f2d4be | 409 | uLCD.pixel(4, 68, YELLOW); |
soapy12312 | 0:8b6686f2d4be | 410 | } |
soapy12312 | 0:8b6686f2d4be | 411 | |
soapy12312 | 0:8b6686f2d4be | 412 | // Draw the ghost in its new position |
soapy12312 | 0:8b6686f2d4be | 413 | draw(x, y); |
soapy12312 | 0:8b6686f2d4be | 414 | } |
soapy12312 | 0:8b6686f2d4be | 415 | |
soapy12312 | 0:8b6686f2d4be | 416 | // If Pac-Man's invincibility status changed |
soapy12312 | 0:8b6686f2d4be | 417 | if (pacman.powerUpChanged == true) { |
soapy12312 | 0:8b6686f2d4be | 418 | // Change the color of the ghost |
soapy12312 | 0:8b6686f2d4be | 419 | draw(x, y); |
soapy12312 | 0:8b6686f2d4be | 420 | // Reset Pac-Man's invincibility status change |
soapy12312 | 0:8b6686f2d4be | 421 | pacman.powerUpChanged = false; |
soapy12312 | 0:8b6686f2d4be | 422 | } |
soapy12312 | 0:8b6686f2d4be | 423 | |
soapy12312 | 0:8b6686f2d4be | 424 | // Set the ghost's next direction |
soapy12312 | 0:8b6686f2d4be | 425 | switch (nextDirection) { |
soapy12312 | 0:8b6686f2d4be | 426 | case FACERIGHT: |
soapy12312 | 0:8b6686f2d4be | 427 | if (stage.positions[x + 1][y] != 0) { |
soapy12312 | 0:8b6686f2d4be | 428 | direction = nextDirection; |
soapy12312 | 0:8b6686f2d4be | 429 | } |
soapy12312 | 0:8b6686f2d4be | 430 | |
soapy12312 | 0:8b6686f2d4be | 431 | break; |
soapy12312 | 0:8b6686f2d4be | 432 | case FACEDOWN: |
soapy12312 | 0:8b6686f2d4be | 433 | if (stage.positions[x][y + 1] != 0) { |
soapy12312 | 0:8b6686f2d4be | 434 | direction = nextDirection; |
soapy12312 | 0:8b6686f2d4be | 435 | } |
soapy12312 | 0:8b6686f2d4be | 436 | |
soapy12312 | 0:8b6686f2d4be | 437 | break; |
soapy12312 | 0:8b6686f2d4be | 438 | case FACEUP: |
soapy12312 | 0:8b6686f2d4be | 439 | if (stage.positions[x][y - 1] != 0) { |
soapy12312 | 0:8b6686f2d4be | 440 | direction = nextDirection; |
soapy12312 | 0:8b6686f2d4be | 441 | } |
soapy12312 | 0:8b6686f2d4be | 442 | |
soapy12312 | 0:8b6686f2d4be | 443 | break; |
soapy12312 | 0:8b6686f2d4be | 444 | default: |
soapy12312 | 0:8b6686f2d4be | 445 | break; |
soapy12312 | 0:8b6686f2d4be | 446 | } |
soapy12312 | 0:8b6686f2d4be | 447 | } |
soapy12312 | 0:8b6686f2d4be | 448 | |
soapy12312 | 0:8b6686f2d4be | 449 | // Move the ghost up |
soapy12312 | 0:8b6686f2d4be | 450 | void moveUp() { |
soapy12312 | 0:8b6686f2d4be | 451 | // If the space in the upward direction is a valid position |
soapy12312 | 0:8b6686f2d4be | 452 | if (stage.positions[x][y - 1] != 0) { |
soapy12312 | 0:8b6686f2d4be | 453 | // The ghost is mobile |
soapy12312 | 0:8b6686f2d4be | 454 | immobile = false; |
soapy12312 | 0:8b6686f2d4be | 455 | // Erase the ghost from its current position |
soapy12312 | 0:8b6686f2d4be | 456 | erase(x, y); |
soapy12312 | 0:8b6686f2d4be | 457 | // Decrement the ghost's y-coordinate |
soapy12312 | 0:8b6686f2d4be | 458 | y--; |
soapy12312 | 0:8b6686f2d4be | 459 | |
soapy12312 | 0:8b6686f2d4be | 460 | // If passed over a pac dot |
soapy12312 | 0:8b6686f2d4be | 461 | if (stage.positions[x][y + 4] == 2) { |
soapy12312 | 0:8b6686f2d4be | 462 | // Redraw the pac dot |
soapy12312 | 0:8b6686f2d4be | 463 | uLCD.pixel(x, y + 4, YELLOW); |
soapy12312 | 0:8b6686f2d4be | 464 | // If passed over a big pac dot |
soapy12312 | 0:8b6686f2d4be | 465 | } else { |
soapy12312 | 0:8b6686f2d4be | 466 | // Redraw the big pac dot |
soapy12312 | 0:8b6686f2d4be | 467 | for (int j = y + 2; j <= y + 5; j++) { |
soapy12312 | 0:8b6686f2d4be | 468 | if (stage.positions[x][j] == 3) { |
soapy12312 | 0:8b6686f2d4be | 469 | uLCD.filled_circle(x, j, 1, YELLOW); |
soapy12312 | 0:8b6686f2d4be | 470 | } |
soapy12312 | 0:8b6686f2d4be | 471 | } |
soapy12312 | 0:8b6686f2d4be | 472 | } |
soapy12312 | 0:8b6686f2d4be | 473 | |
soapy12312 | 0:8b6686f2d4be | 474 | // Draw the ghost in its new position |
soapy12312 | 0:8b6686f2d4be | 475 | draw(x, y); |
soapy12312 | 0:8b6686f2d4be | 476 | // If the space in the upward direction is not a valid position |
soapy12312 | 0:8b6686f2d4be | 477 | } else { |
soapy12312 | 0:8b6686f2d4be | 478 | // The ghost is immobile |
soapy12312 | 0:8b6686f2d4be | 479 | immobile = true; |
soapy12312 | 0:8b6686f2d4be | 480 | wait(0.03); |
soapy12312 | 0:8b6686f2d4be | 481 | } |
soapy12312 | 0:8b6686f2d4be | 482 | |
soapy12312 | 0:8b6686f2d4be | 483 | // If Pac-Man's invincibility status changed |
soapy12312 | 0:8b6686f2d4be | 484 | if (pacman.powerUpChanged == true) { |
soapy12312 | 0:8b6686f2d4be | 485 | // Change the color of the ghost |
soapy12312 | 0:8b6686f2d4be | 486 | draw(x, y); |
soapy12312 | 0:8b6686f2d4be | 487 | // Reset Pac-Man's invincibility status change |
soapy12312 | 0:8b6686f2d4be | 488 | pacman.powerUpChanged = false; |
soapy12312 | 0:8b6686f2d4be | 489 | } |
soapy12312 | 0:8b6686f2d4be | 490 | |
soapy12312 | 0:8b6686f2d4be | 491 | // Set the ghost's next direction |
soapy12312 | 0:8b6686f2d4be | 492 | switch (nextDirection) { |
soapy12312 | 0:8b6686f2d4be | 493 | case FACERIGHT: |
soapy12312 | 0:8b6686f2d4be | 494 | if (stage.positions[x + 1][y] != 0) { |
soapy12312 | 0:8b6686f2d4be | 495 | direction = nextDirection; |
soapy12312 | 0:8b6686f2d4be | 496 | } |
soapy12312 | 0:8b6686f2d4be | 497 | |
soapy12312 | 0:8b6686f2d4be | 498 | break; |
soapy12312 | 0:8b6686f2d4be | 499 | case FACEDOWN: |
soapy12312 | 0:8b6686f2d4be | 500 | if (stage.positions[x][y + 1] != 0) { |
soapy12312 | 0:8b6686f2d4be | 501 | direction = nextDirection; |
soapy12312 | 0:8b6686f2d4be | 502 | } |
soapy12312 | 0:8b6686f2d4be | 503 | |
soapy12312 | 0:8b6686f2d4be | 504 | break; |
soapy12312 | 0:8b6686f2d4be | 505 | case FACELEFT: |
soapy12312 | 0:8b6686f2d4be | 506 | if (stage.positions[x - 1][y] != 0) { |
soapy12312 | 0:8b6686f2d4be | 507 | direction = nextDirection; |
soapy12312 | 0:8b6686f2d4be | 508 | } |
soapy12312 | 0:8b6686f2d4be | 509 | |
soapy12312 | 0:8b6686f2d4be | 510 | break; |
soapy12312 | 0:8b6686f2d4be | 511 | default: |
soapy12312 | 0:8b6686f2d4be | 512 | break; |
soapy12312 | 0:8b6686f2d4be | 513 | } |
soapy12312 | 0:8b6686f2d4be | 514 | } |
soapy12312 | 0:8b6686f2d4be | 515 | }; |