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-23
Revision:
11:f455e907baba
Parent:
10:5da9b27e050e
Child:
12:088a8203a9bb

File content as of revision 11:f455e907baba:

// Student Side.

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

#define SKY_COLOR  0x7EC0EE
#define GND_COLOR  0x66CD00
#define TANK_RED   0xCD0000
#define TANK_BLUE  0x000080
#define PI  3.1415926535797

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.current_orientation = IS_LANDSCAPE;
    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 map_init() {
    sync.background_color(SKY_COLOR);
    sync.cls();
    sync.filled_rectangle(0,0,128,20, GND_COLOR);
    sync.filled_rectangle(59, 20, 69, 60, BLACK);
    sync.update();
}

class Tank {
    public:
        int x, y;
        int w;
        int h;
        int tank_color;
        float barrel_theta;
        int barrel_length;
        int wheel_rad;
        
        Tank(int sx, int sy, int width, int height, int color) {
            x = sx; y = sy;
            w = width; h = height;
            tank_color = color;
            barrel_theta = PI/4.0;
            barrel_length = w;
            wheel_rad = 2.0;
            draw();
        }
        int min_x(void) { return x; }
        int min_y(void) { return y; }
        int max_x(void) { return x + w + ceil(barrel_length*cos(barrel_theta)); }
        int max_y(void) { return y + h + wheel_rad + ceil(barrel_length*sin(barrel_theta)); }
        
        void reposition(int new_x, int new_y, float new_theta) {
            sync.filled_rectangle(min_x(), min_y(), max_x(), max_y(), SKY_COLOR);
            sync.line(x + w/2.0, y+h+wheel_rad, x + w/2.0 + 1 + barrel_length*cos(barrel_theta), y+h + barrel_length*sin(barrel_theta), SKY_COLOR);
            x = new_x; y = new_y; barrel_theta = new_theta;
            draw();
        }
        void draw() {
            sync.line(x + w/2.0, y+h+wheel_rad, x + w/2.0 + barrel_length*cos(barrel_theta), y+h+wheel_rad + barrel_length*sin(barrel_theta), BLACK);
            sync.filled_rectangle(x, y+wheel_rad, x+w, y+h+wheel_rad, tank_color);
            sync.filled_circle(x+wheel_rad, y+wheel_rad, wheel_rad, BLACK);
            sync.filled_circle(x+w-wheel_rad, y+wheel_rad, wheel_rad, BLACK);
        }
};

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.
    map_init();
    pc.printf("Initialized...\n");              // Let us know you finished initializing.
}

int main (void) {
    int* p2_buttons;
    
    game_init();
    
    Tank t1(4, 20, 12, 8, TANK_RED);
    Tank t2(111, 20, 12, 8, TANK_BLUE);
    
    while(1) {
        sync.line(0,0, 5,5, BLACK);             // Update pukes if you try to send nothing :P
        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;
        
        if(!pb_l) t1.reposition(t1.x-1, t1.y, t1.barrel_theta);
        if(!pb_r) t1.reposition(t1.x+1, t1.y, t1.barrel_theta);
        if(p2_buttons[3]) t2.reposition(t2.x-1, t2.y, t2.barrel_theta);
        if(p2_buttons[1]) t2.reposition(t2.x+1, t2.y, t2.barrel_theta);
        
        if(!pb_d) t1.reposition(t1.x, t1.y, t1.barrel_theta+PI/30.0);
        if(!pb_u) t1.reposition(t1.x, t1.y, t1.barrel_theta-PI/30.0);
        if(p2_buttons[0]) t2.reposition(t2.x, t2.y, t2.barrel_theta+PI/30.0);
        if(p2_buttons[2]) t2.reposition(t2.x, t2.y, t2.barrel_theta-PI/30.0);
        
        
    } 
}