Multi-game multiplayer arcade gaming system meant for the red X when playing Super Tic-Tac-Toe.

Dependencies:   uLCD_4DGL_SE PinDetect SDFileSystem mbed wave_player

main.cpp

Committer:
soapy12312
Date:
2015-11-26
Revision:
0:218d3fb75950
Child:
1:2a7e2f5aeda4

File content as of revision 0:218d3fb75950:

#include "mbed.h"
#include "PinDetect.h"
#include "SDFileSystem.h"
#include "uLCD_4DGL.h"
#include "wave_player.h"
#include <mpr121.h>
#include "RGBLED.h"
#include "SimonSays.h"
#include "SuperTicTacToe.h"
#include "Stage.h"
#include "Pacman.h"
#include "Ghost.h"


/* --------------------------------------------------------------------------------------------
   --------------------Global components used in all games with no overlap---------------------
   --------------------------------------------------------------------------------------------*/

// Speaker
AnalogOut DACout(p18);
wave_player waver(&DACout);

// SD card
SDFileSystem sd(p5, p6, p7, p8, "sd");

// uLCD display
uLCD_4DGL uLCD(p28, p27, p30);


/* --------------------------------------------------------------------------------------------
   -----------------Variables and functions prototypes used in multiple games------------------
   --------------------------------------------------------------------------------------------*/

// Select game to play
int selectGame();


/* --------------------------------------------------------------------------------------------
   ----------Rock, Paper, Scissors, Lizard, Spock variables and function prototypes------------
   --------------------------------------------------------------------------------------------*/


/* --------------------------------------------------------------------------------------------
   --------------------------------------Main function-----------------------------------------
   --------------------------------------------------------------------------------------------*/

int main() {
    start:
    int game = selectGame();

    // Play Simon Says
    if (game == 0) {
        // Analog noise
        AnalogIn noise(p15);

        // Pushbutton interrupts
        PinDetect button1(p17);
        PinDetect button2(p19);
        PinDetect button3(p20);
        PinDetect button4(p16);

        SimonSays simon = SimonSays(noise, button1, button2, button3, button4, sd, uLCD, waver);
        simon.playSimonSays();
    // Play Super Tic-Tac-Toe
    } else if (game == 1) {
        // Speaker
        AnalogOut DACout(p18);
        wave_player waver(&DACout);

        // RGB LED
        RGBLED RGB(p25, p23, p22);
        
        // Touch keypad
        InterruptIn input(p21);
        I2C i2c(p9, p10);
        Mpr121 MPR121(&i2c, Mpr121::ADD_VSS);

        SuperTicTacToe super = SuperTicTacToe(input, MPR121, RGB, sd, uLCD, waver);
        super.playSuperTicTacToe();
    // Play Pac-Man
    } else if (game == 2) {
        // Pac-Man controller
        PinDetect PacmanRight(p9);
        PinDetect PacmanDown(p10);
        PinDetect PacmanLeft(p11);
        PinDetect PacmanUp(p29);
        
        // Red ghost controller
        PinDetect redGhostRight(p26);
        PinDetect redGhostDown(p15);
        PinDetect redGhostLeft(p16);
        PinDetect redGhostUp(p19);
        
        // Yellow ghost controller
        PinDetect yellowGhostRight(p25);
        PinDetect yellowGhostDown(p24);
        PinDetect yellowGhostLeft(p23);
        PinDetect yellowGhostUp(p21);

        // Maximize the baud rate
        uLCD.baudrate(MAXBAUDRATE);
        
        // Set up the stage
        Stage stage(uLCD);
        stage.initialize();
    
        // Set up Pac-Man
        Pacman pacman(PacmanRight, PacmanDown, PacmanLeft, PacmanUp, stage, uLCD);
        pacman.initialize();
    
        // Set up the red ghost
        Ghost redGhost(RED, pacman, redGhostRight, redGhostDown, redGhostLeft, redGhostUp, stage, uLCD);
        redGhost.initialize(60, 60, FACELEFT);
    
        // Set up the yellow ghost
        Ghost yellowGhost(YELLOW, pacman, yellowGhostRight, yellowGhostDown, yellowGhostLeft, yellowGhostUp, stage, uLCD);
        yellowGhost.initialize(68, 60, FACERIGHT);
    
        // Wait 3 seconds
        wait(3);
    
        // Checks to see whether the game is over
        bool gameOver;
    
        // Loop through the game
        while (1) {
            pacman.displayStatus();
            gameOver = pacman.move();
    
            // If all pac dots are eaten or Pac-Man runs out of lives
            if (gameOver == true) {
                // Break out of the loop
                break;
            }

            redGhost.move();

            yellowGhost.move();
        }
    
        // Game over display
        pacman.gameOver();
    } else {
        // PLAY RPSLK
        wait(1);
        uLCD.cls();
    }

    wait(3);
    goto start;
}


/* --------------------------------------------------------------------------------------------
   ------------------------Functions used in multiple games------------------------------------
   --------------------------------------------------------------------------------------------*/

// Select game to play
int selectGame() {
    DigitalIn button1(p17);
    DigitalIn button2(p19);
    DigitalIn button3(p20);
    DigitalIn button4(p16);

    button1.mode(PullUp);
    button2.mode(PullUp);
    button3.mode(PullUp);
    button4.mode(PullUp);
    wait(0.01);

    uLCD.baudrate(MAXBAUDRATE);

    uLCD.text_width(1);
    uLCD.text_height(1);
    uLCD.textbackground_color(BLACK);
    uLCD.filled_rectangle(0, 0, 127, 127, BLACK);
    uLCD.background_color(BLACK);
    uLCD.color(GREEN);
    uLCD.locate(0, 0);

    uLCD.printf("Select game:\n\n");
    uLCD.printf("pb1: Simon Says\n");
    uLCD.printf("pb2: Super Tic-\n     Tac-Toe\n");
    uLCD.printf("pb3: Pac-Man\n");
    uLCD.printf("pb4: Rock, Paper,\n     Scissors,\n     Lizard, Spock");
    wait(0.1);

    int gameNumber = -1;

    while (1) {
        if (button1 != 1) {
            gameNumber = 0;
            break;
        } else if (button2 != 1) {
            gameNumber = 1;
            break;
        } else if (button3 != 1) {
            gameNumber = 2;
            break;
        } else if (button4 != 1) {
            gameNumber = 3;
            break;
        }
    }            

    uLCD.cls();
    return gameNumber;
}