Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE PinDetect mbed
Fork of Pac-Man by
main.cpp
00001 /******************************************************* 00002 * Author: Yuanzhe Xu 00003 * Institution: Georgia Institute of Technology 00004 * Date: October 18, 2015 00005 * 00006 * This main program simulates a game of Pac-Man that 00007 * allows for multiplayer. One player controls Pac-Man, 00008 * while two other players control a red and yellow 00009 * ghost using different tactile switches. Pac-Man 00010 * starts with three lives, and the ghosts have 00011 * infinite lives. Pac-Man's objective is to eat all of 00012 * the pac dots before running out of lives. Eating a 00013 * regular pac dot adds 10 points to Pac-Man's score; 00014 * eating a big pac dot adds 50 points to Pac-Man's 00015 * score. The ghosts' objective is to prevent Pac-Man 00016 * from doing so. If Pac-Man comes into contact with a 00017 * ghost, he dies and loses a life, causing Pac-Man and 00018 * both of the ghosts to reset. If Pac-Man runs out of 00019 * lives, he loses the game. As a countermeasure to the 00020 * ghosts, the big pac dots are used as power-ups. The 00021 * power-ups allow Pac-Man to be invincible for a fixed 00022 * amount of time, during which Pac-Man can also eat the 00023 * ghosts and force them to reset to their starting 00024 * position. During each power-up period, the first 00025 * ghost Pac-Man eats adds 200 points to Pac-Man's 00026 * score, the second adds 400 points, the third adds 600 00027 * points, and so on. The ghosts have two movement 00028 * constraints: they must be moving at all times and 00029 * they cannot move backwards in the direction they came 00030 * (for example, when a ghost is moving to the right, it 00031 * cannot change directions and move to the left). 00032 * Failure to comply to the mobility constraint causes 00033 * the ghost to reset to its original starting position. 00034 * When the game ends (Pac-Man completes his objective 00035 * or runs out of lives), a final screen is displayed 00036 * saying whether Pac-Man wins or loses and prints out 00037 * Pac-Man's final score. 00038 *******************************************************/ 00039 00040 #include "mbed.h" 00041 #include "uLCD_4DGL.h" 00042 #include "PinDetect.h" 00043 #include "Stage.h" 00044 #include "Pacman.h" 00045 #include "Ghost.h" 00046 00047 // Pac-Man controller 00048 PinDetect PacmanRight(p9); 00049 PinDetect PacmanDown(p10); 00050 PinDetect PacmanLeft(p11); 00051 PinDetect PacmanClick(p12); 00052 PinDetect PacmanUp(p13); 00053 00054 // Red ghost controller 00055 PinDetect redGhostRight(p14); 00056 PinDetect redGhostDown(p15); 00057 PinDetect redGhostLeft(p16); 00058 PinDetect redGhostClick(p17); 00059 PinDetect redGhostUp(p19); 00060 00061 // Yellow ghost controller 00062 PinDetect yellowGhostRight(p25); 00063 PinDetect yellowGhostDown(p24); 00064 PinDetect yellowGhostLeft(p23); 00065 PinDetect yellowGhostClick(p22); 00066 PinDetect yellowGhostUp(p21); 00067 00068 // uLCD display 00069 uLCD_4DGL uLCD(p28, p27, p30); 00070 00071 00072 // Direction inputs 00073 int volatile nextPacmanDirection = FACEUP; 00074 int volatile nextRedGhostDirection = FACELEFT; 00075 int volatile nextYellowGhostDirection = FACERIGHT; 00076 00077 00078 // Set the next Pac-Man direction to be right 00079 void PacmanRightTrigger() { 00080 nextPacmanDirection = FACERIGHT; 00081 } 00082 00083 // Set the next Pac-Man direction to be down 00084 void PacmanDownTrigger() { 00085 nextPacmanDirection = FACEDOWN; 00086 } 00087 00088 // Set the next Pac-Man direction to be left 00089 void PacmanLeftTrigger() { 00090 nextPacmanDirection = FACELEFT; 00091 } 00092 00093 // Resume/Pause the game 00094 void PacmanClickTrigger() { 00095 wait(0.5); 00096 00097 while (PacmanClick == 1) { 00098 } 00099 } 00100 00101 // Set the next Pac-Man direction to be up 00102 void PacmanUpTrigger() { 00103 nextPacmanDirection = FACEUP; 00104 } 00105 00106 00107 // Set the next red ghost direction to be right 00108 void redGhostRightTrigger() { 00109 nextRedGhostDirection = FACERIGHT; 00110 } 00111 00112 // Set the next red ghost direction to be down 00113 void redGhostDownTrigger() { 00114 nextRedGhostDirection = FACEDOWN; 00115 } 00116 00117 // Set the next red ghost direction to be left 00118 void redGhostLeftTrigger() { 00119 nextRedGhostDirection = FACELEFT; 00120 } 00121 00122 // Resume/Pause the game 00123 void redGhostClickTrigger() { 00124 wait(0.5); 00125 00126 while (redGhostClick == 1) { 00127 } 00128 } 00129 00130 // Set the next red ghost direction to be up 00131 void redGhostUpTrigger() { 00132 nextRedGhostDirection = FACEUP; 00133 } 00134 00135 00136 // Move the yellow ghost right 00137 void yellowGhostRightTrigger() { 00138 nextYellowGhostDirection = FACERIGHT; 00139 } 00140 00141 // Set the next yellow ghost direction to be down 00142 void yellowGhostDownTrigger() { 00143 nextYellowGhostDirection = FACEDOWN; 00144 } 00145 00146 // Set the next yellow ghost direction to be left 00147 void yellowGhostLeftTrigger() { 00148 nextYellowGhostDirection = FACELEFT; 00149 } 00150 00151 // Resume/Pause the game 00152 void yellowGhostClickTrigger() { 00153 wait(0.5); 00154 00155 while (yellowGhostClick == 1) { 00156 } 00157 } 00158 00159 // Set the next yellow ghost direction to be up 00160 void yellowGhostUpTrigger() { 00161 nextYellowGhostDirection = FACEUP; 00162 } 00163 00164 00165 int main() { 00166 // Maximize the baud rate 00167 uLCD.baudrate(3000000); 00168 00169 // Use internal pullups 00170 PacmanRight.mode(PullUp); 00171 PacmanDown.mode(PullUp); 00172 PacmanLeft.mode(PullUp); 00173 PacmanClick.mode(PullUp); 00174 PacmanUp.mode(PullUp); 00175 redGhostRight.mode(PullUp); 00176 redGhostDown.mode(PullUp); 00177 redGhostLeft.mode(PullUp); 00178 redGhostClick.mode(PullUp); 00179 redGhostUp.mode(PullUp); 00180 yellowGhostRight.mode(PullUp); 00181 yellowGhostDown.mode(PullUp); 00182 yellowGhostLeft.mode(PullUp); 00183 yellowGhostClick.mode(PullUp); 00184 yellowGhostUp.mode(PullUp); 00185 wait(0.01); 00186 // Interrupt callback functions 00187 PacmanRight.attach_deasserted(&PacmanRightTrigger); 00188 PacmanDown.attach_deasserted(&PacmanDownTrigger); 00189 PacmanLeft.attach_deasserted(&PacmanLeftTrigger); 00190 PacmanClick.attach_deasserted(&PacmanClickTrigger); 00191 PacmanUp.attach_deasserted(&PacmanUpTrigger); 00192 redGhostRight.attach_deasserted(&redGhostRightTrigger); 00193 redGhostDown.attach_deasserted(&redGhostDownTrigger); 00194 redGhostLeft.attach_deasserted(&redGhostLeftTrigger); 00195 redGhostClick.attach_deasserted(&redGhostClickTrigger); 00196 redGhostUp.attach_deasserted(&redGhostUpTrigger); 00197 yellowGhostRight.attach_deasserted(&yellowGhostRightTrigger); 00198 yellowGhostDown.attach_deasserted(&yellowGhostDownTrigger); 00199 yellowGhostLeft.attach_deasserted(&yellowGhostLeftTrigger); 00200 yellowGhostClick.attach_deasserted(&yellowGhostClickTrigger); 00201 yellowGhostUp.attach_deasserted(&yellowGhostUpTrigger); 00202 // Set sampling frequency 00203 PacmanRight.setSampleFrequency(); 00204 PacmanDown.setSampleFrequency(); 00205 PacmanLeft.setSampleFrequency(); 00206 PacmanClick.setSampleFrequency(); 00207 PacmanUp.setSampleFrequency(); 00208 redGhostRight.setSampleFrequency(); 00209 redGhostDown.setSampleFrequency(); 00210 redGhostLeft.setSampleFrequency(); 00211 redGhostClick.setSampleFrequency(); 00212 redGhostUp.setSampleFrequency(); 00213 yellowGhostRight.setSampleFrequency(); 00214 yellowGhostDown.setSampleFrequency(); 00215 yellowGhostLeft.setSampleFrequency(); 00216 yellowGhostClick.setSampleFrequency(); 00217 yellowGhostUp.setSampleFrequency(); 00218 00219 // Set up the stage 00220 Stage stage(uLCD); 00221 stage.initialize(); 00222 00223 // Set up Pac-Man 00224 Pacman pacman(uLCD, stage); 00225 pacman.initialize(); 00226 00227 // Set up the red ghost 00228 Ghost redGhost(RED, uLCD, stage, pacman); 00229 redGhost.initialize(60, 60, nextRedGhostDirection); 00230 00231 // Set up the yellow ghost 00232 Ghost yellowGhost(YELLOW, uLCD, stage, pacman); 00233 yellowGhost.initialize(68, 60, nextYellowGhostDirection); 00234 00235 // Wait 3 seconds 00236 wait(3); 00237 00238 // Checks to see whether the game is over 00239 bool gameOver; 00240 00241 // Loop through the game 00242 while (1) { 00243 pacman.setNextDirection(nextPacmanDirection); 00244 pacman.displayStatus(); 00245 gameOver = pacman.move(); 00246 00247 // If all pac dots are eaten or Pac-Man runs out of lives 00248 if (gameOver == true) { 00249 // Break out of the loop 00250 break; 00251 } 00252 00253 redGhost.setNextDirection(nextRedGhostDirection); 00254 redGhost.move(); 00255 00256 yellowGhost.setNextDirection(nextYellowGhostDirection); 00257 yellowGhost.move(); 00258 } 00259 00260 // Game over display 00261 pacman.gameOver(); 00262 }
Generated on Thu Jul 14 2022 07:08:37 by
1.7.2
