Eric Xu / Mbed 2 deprecated Arcade_RedX

Dependencies:   uLCD_4DGL_SE PinDetect SDFileSystem mbed wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*******************************************************
00002  * This main program simulates a multi-game multiplayer
00003  * arcade gaming system. The program allows the player
00004  * to choose from four different games:
00005  *
00006  *  1) Simon
00007  *  2) Super Tic-Tac-Toe (first player, the red X)
00008  *  3) Pac-Man
00009  *  4) Rock, Paper, Scissors, Lizard, Spock
00010  * 
00011  * All games except Pac-Man communicate with another
00012  * gaming system via an XBee module to simulate
00013  * multiplayer. Pac-Man is multiplayer as well, but the
00014  * characters in the game are controlled using the three
00015  * tactile switches on one board.
00016  *******************************************************/
00017 
00018 
00019 #include "mbed.h"
00020 #include "PinDetect.h"
00021 #include "SDFileSystem.h"
00022 #include "uLCD_4DGL.h"
00023 #include "wave_player.h"
00024 #include <mpr121.h>
00025 #include "RGBLED.h"
00026 #include "RedX.h"
00027 #include "Simon.h"
00028 #include "Stage.h"
00029 #include "Pacman.h"
00030 #include "Ghost.h"
00031 #include "RPSLK.h"
00032 
00033 
00034 // Speaker
00035 AnalogOut DACout(p18);
00036 wave_player waver(&DACout);
00037 
00038 // XBee module
00039 DigitalOut reset(p12);
00040 Serial XBee(p13, p14);
00041 
00042 // SD card
00043 SDFileSystem sd(p5, p6, p7, p8, "sd");
00044 
00045 // uLCD display
00046 uLCD_4DGL uLCD(p28, p27, p30);
00047 
00048 
00049 // Select game to play
00050 int selectGame();
00051 
00052 
00053 int main() {
00054     int gameNumber = selectGame();
00055 
00056     // Play Simon Says
00057     if (gameNumber == 0) {
00058         // Analog noise
00059         AnalogIn noise(p15);
00060 
00061         // Pushbutton interrupts
00062         PinDetect button1(p17);
00063         PinDetect button2(p19);
00064         PinDetect button3(p20);
00065         PinDetect button4(p16);
00066 
00067         Simon simon = Simon(noise, reset, button1, button2, button3, button4, sd, XBee, uLCD, waver);
00068         simon.playSimon();
00069     // Play Super Tic-Tac-Toe
00070     } else if (gameNumber == 1) {
00071         // Touch keypad
00072         InterruptIn input(p21);
00073         I2C i2c(p9, p10);
00074         Mpr121 MPR121(&i2c, Mpr121::ADD_VSS);
00075 
00076         // RGB LED
00077         RGBLED RGB(p25, p23, p22);
00078 
00079         RedX player = RedX(reset, input, MPR121, RGB, sd, XBee, uLCD, waver);
00080         player.playSuperTicTacToe();
00081     // Play Pac-Man
00082     } else if (gameNumber == 2) {
00083         // Pac-Man controller
00084         PinDetect PacmanRight(p9);
00085         PinDetect PacmanDown(p10);
00086         PinDetect PacmanLeft(p11);
00087         PinDetect PacmanUp(p29);
00088         
00089         // Red ghost controller
00090         PinDetect redGhostRight(p26);
00091         PinDetect redGhostDown(p15);
00092         PinDetect redGhostLeft(p16);
00093         PinDetect redGhostUp(p19);
00094         
00095         // Yellow ghost controller
00096         PinDetect yellowGhostRight(p25);
00097         PinDetect yellowGhostDown(p24);
00098         PinDetect yellowGhostLeft(p23);
00099         PinDetect yellowGhostUp(p21);
00100 
00101         // Maximize the baud rate
00102         uLCD.baudrate(MAXBAUDRATE);
00103         
00104         // Set up the stage
00105         Stage stage(uLCD);
00106         stage.initialize();
00107     
00108         // Set up Pac-Man
00109         Pacman pacman(PacmanRight, PacmanDown, PacmanLeft, PacmanUp, stage, uLCD, waver);
00110         pacman.initialize();
00111     
00112         // Set up the red ghost
00113         Ghost redGhost(RED, pacman, redGhostRight, redGhostDown, redGhostLeft, redGhostUp, stage, uLCD);
00114         redGhost.initialize(60, 60, FACELEFT);
00115 
00116         // Set up the yellow ghost
00117         Ghost yellowGhost(YELLOW, pacman, yellowGhostRight, yellowGhostDown, yellowGhostLeft, yellowGhostUp, stage, uLCD);
00118         yellowGhost.initialize(68, 60, FACERIGHT);
00119 
00120         pacman.playSound(THEME);
00121     
00122         // Checks to see whether the game is over
00123         bool gameOver;
00124     
00125         // Loop through the game
00126         while (1) {
00127             pacman.displayStatus();
00128             gameOver = pacman.move();
00129     
00130             // If all pac dots are eaten or Pac-Man runs out of lives
00131             if (gameOver == true) {
00132                 // Break out of the loop
00133                 break;
00134             }
00135 
00136             redGhost.move();
00137 
00138             yellowGhost.move();
00139         }
00140     
00141         // Game over display
00142         pacman.gameOver();
00143     } else {
00144         // Tactile switch interrupts
00145         PinDetect rock(p21);
00146         PinDetect paper(p22);
00147         PinDetect scissors(p23);
00148         PinDetect lizard(p24);
00149         PinDetect spock(p25);
00150 
00151         RPSLK rpslk = RPSLK(reset, rock, paper, scissors, lizard, spock, sd, XBee, uLCD, waver);
00152         rpslk.playRPSLK();
00153     }
00154 }
00155 
00156 
00157 // Select game to play
00158 int selectGame() {
00159     // Set up XBee
00160     reset = 0;
00161     wait(0.001);
00162     reset = 1;
00163     wait(0.001);
00164 
00165     selection:
00166     DigitalIn button1(p17);
00167     DigitalIn button2(p19);
00168     DigitalIn button3(p20);
00169     DigitalIn button4(p16);
00170 
00171     button1.mode(PullUp);
00172     button2.mode(PullUp);
00173     button3.mode(PullUp);
00174     button4.mode(PullUp);
00175     wait(0.01);
00176 
00177     uLCD.baudrate(MAXBAUDRATE);
00178 
00179     uLCD.text_width(1);
00180     uLCD.text_height(1);
00181     uLCD.textbackground_color(BLACK);
00182     uLCD.filled_rectangle(0, 0, 127, 127, BLACK);
00183     uLCD.background_color(BLACK);
00184     uLCD.color(GREEN);
00185     uLCD.locate(0, 0);
00186 
00187     uLCD.printf("Select game:\n\n");
00188     uLCD.printf("pb1: Simon Says\n");
00189     uLCD.printf("pb2: Super Tic-\n     Tac-Toe\n");
00190     uLCD.printf("pb3: Pac-Man\n");
00191     uLCD.printf("pb4: Rock, Paper,\n     Scissors,\n     Lizard, Spock");
00192     wait(0.1);
00193 
00194     int gameNumber = -1;
00195 
00196     while (1) {
00197         if (button1 != 1) {
00198             gameNumber = 0;
00199             break;
00200         } else if (button2 != 1) {
00201             gameNumber = 1;
00202             break;
00203         } else if (button3 != 1) {
00204             gameNumber = 2;
00205             break;
00206         } else if (button4 != 1) {
00207             gameNumber = 3;
00208             break;
00209         }
00210     }
00211 
00212     uLCD.cls();
00213     XBee.putc(gameNumber);
00214     int otherGame = -1;
00215     bool print = true;
00216 
00217     if ((gameNumber == 0) || (gameNumber == 1) || (gameNumber == 3)) {
00218         while (otherGame == -1) {
00219             if (XBee.readable() == true) {
00220                 otherGame = XBee.getc();
00221                 print = false;
00222             }
00223 
00224             if (print == true) {
00225                 uLCD.printf("Waiting for other player...");
00226                 print = false;
00227             }
00228         }
00229 
00230         if (otherGame != gameNumber) {
00231             uLCD.cls();
00232             uLCD.printf("Sorry, but the    ");
00233             uLCD.printf("other player chose");
00234             uLCD.printf("a different game. ");
00235             uLCD.printf("Returning to game ");
00236             uLCD.printf("selection menu.");
00237             wait(2);
00238             uLCD.cls();
00239             goto selection;
00240         }
00241     }
00242 
00243     uLCD.cls();
00244     return gameNumber;
00245 }