For Nikhil

Dependencies:   4DGL-uLCD-SE EthernetInterface Game_Synchronizer MMA8452 SDFileSystem mbed-rtos mbed wave_player

Fork of 2035_Tanks_Shell by ECE2035 Spring 2015 TA

main.cpp

Committer:
jford38
Date:
2015-10-22
Revision:
9:ee330b1ba394
Parent:
8:e6dd05393290
Child:
10:5da9b27e050e

File content as of revision 9:ee330b1ba394:

// Student Side.

#include "mbed.h"
#include "game_synchronizer.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

DigitalIn pb_l(p24);  // left button
DigitalIn pb_r(p21);  // right button
DigitalIn pb_u(p22);  // up button
DigitalIn pb_d(p23);  // down button

uLCD_4DGL uLCD(p9,p10,p11); // LCD (serial tx, serial rx, reset pin;)
Game_Synchronizer sync(PLAYER1); // (tx, rx, rst, player mode)

// For debug only. Don't use in production code. It will slow your game down a lot.
Serial pc(USBTX, USBRX);                     


int game_menu(void) {
    
    // Figure out what mode the game will be run in.
    
    uLCD.locate(0,1);
    uLCD.puts("Select Mode:");
    uLCD.locate(0,3);
    uLCD.puts("  Single-Player:");
    uLCD.locate(0,4);
    uLCD.puts("   Left Button");
    uLCD.locate(0,6);
    uLCD.puts("  Multi-Player:");
    uLCD.locate(0,7);
    uLCD.puts("   Right Button");
    // Right button -> Multiplayer
    // Left button -> Single player
    while(1) {
        if(!pb_r) { return  MULTI_PLAYER; }
        if(!pb_l) { return SINGLE_PLAYER; }
    }
}

void game_init(void) {
    
    led1 = 0; led2 = 0; led3 = 0; led4 = 0;
    
    pb_l.mode(PullUp);
    pb_r.mode(PullUp); 
    pb_u.mode(PullUp);    
    pb_d.mode(PullUp);
    
    pc.printf("\033[2J\033[0;0H");              // Clear the terminal screen.
    pc.printf("I'm alive! Player 1\n");         // Let us know you made it this far.
    int mode = game_menu();
    sync.init(&uLCD, mode);                     // Connect to the other player.
    //sync.cls();
    pc.printf("Initialized...\n");              // Let us know you finished initializing.
}

int main (void) {
    int* p2_buttons;
    
    game_init();
    
    float theta = 0;
    bool first = true;
    
    while(1) {
        
        char howdy[] = "Howdy!";
        if(first) {
            sync.background_color(BLUE);
            sync.cls();
            
            sync.locate(5,0);
            sync.puts(howdy, sizeof(howdy));
            first = false;
        }
        sync.line(64,64,64+50*cos(theta),64+50*sin(theta), BLUE);
        theta += 0.05;
        sync.line(64,64,64+50*cos(theta),64+50*sin(theta), RED);
        sync.circle(10,10,100, GREEN);
        sync.filled_circle(4,4,10, RED);
        sync.triangle(10,10, 20,20, 20,40, RED);
        sync.rectangle(100,100, 90,90, GREEN);
        sync.filled_rectangle(100,100, 110,110, RED);
        sync.pixel(40, 40, WHITE);
        
        sync.update();
        
        p2_buttons = sync.get_button_state();
        
        led1 = p2_buttons[0] ^ !pb_u;
        led2 = p2_buttons[1] ^ !pb_r;
        led3 = p2_buttons[2] ^ !pb_d;
        led4 = p2_buttons[3] ^ !pb_l;
        
        //pc.printf("Button State: %x %x %x %x %x\n", buttons[0], buttons[1], buttons[2], buttons[3], buttons[4]);
        //pc.printf("\033[2J\033[0;0H");
    } 
}