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

Committer:
jford38
Date:
Fri Oct 23 03:33:35 2015 +0000
Revision:
10:5da9b27e050e
Parent:
9:ee330b1ba394
Child:
11:f455e907baba
Draws tanks! And terrain and junk. Idk. I'm tired.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jford38 6:3be57cf4bd33 1 // Student Side.
jford38 0:899c85cd266f 2
jford38 0:899c85cd266f 3 #include "mbed.h"
jford38 6:3be57cf4bd33 4 #include "game_synchronizer.h"
jford38 10:5da9b27e050e 5 #include "math.h"
jford38 10:5da9b27e050e 6
jford38 10:5da9b27e050e 7 #define SKY_COLOR 0x7EC0EE
jford38 10:5da9b27e050e 8 #define GND_COLOR 0x66CD00
jford38 10:5da9b27e050e 9 #define TANK_RED 0xCD0000
jford38 10:5da9b27e050e 10 #define TANK_BLUE 0x000080
jford38 10:5da9b27e050e 11 #define PI 3.1415926535797
jford38 6:3be57cf4bd33 12
jford38 6:3be57cf4bd33 13 DigitalOut led1(LED1);
jford38 6:3be57cf4bd33 14 DigitalOut led2(LED2);
jford38 6:3be57cf4bd33 15 DigitalOut led3(LED3);
jford38 6:3be57cf4bd33 16 DigitalOut led4(LED4);
jford38 0:899c85cd266f 17
jford38 6:3be57cf4bd33 18 DigitalIn pb_l(p24); // left button
jford38 6:3be57cf4bd33 19 DigitalIn pb_r(p21); // right button
jford38 6:3be57cf4bd33 20 DigitalIn pb_u(p22); // up button
jford38 6:3be57cf4bd33 21 DigitalIn pb_d(p23); // down button
jford38 6:3be57cf4bd33 22
jford38 10:5da9b27e050e 23
jford38 8:e6dd05393290 24 uLCD_4DGL uLCD(p9,p10,p11); // LCD (serial tx, serial rx, reset pin;)
jford38 8:e6dd05393290 25 Game_Synchronizer sync(PLAYER1); // (tx, rx, rst, player mode)
jford38 0:899c85cd266f 26
jford38 6:3be57cf4bd33 27 // For debug only. Don't use in production code. It will slow your game down a lot.
jford38 6:3be57cf4bd33 28 Serial pc(USBTX, USBRX);
jford38 0:899c85cd266f 29
jford38 7:9506f2d84162 30
jford38 7:9506f2d84162 31 int game_menu(void) {
jford38 7:9506f2d84162 32
jford38 7:9506f2d84162 33 // Figure out what mode the game will be run in.
jford38 9:ee330b1ba394 34
jford38 10:5da9b27e050e 35 uLCD.current_orientation = IS_LANDSCAPE;
jford38 9:ee330b1ba394 36 uLCD.locate(0,1);
jford38 9:ee330b1ba394 37 uLCD.puts("Select Mode:");
jford38 9:ee330b1ba394 38 uLCD.locate(0,3);
jford38 9:ee330b1ba394 39 uLCD.puts(" Single-Player:");
jford38 9:ee330b1ba394 40 uLCD.locate(0,4);
jford38 9:ee330b1ba394 41 uLCD.puts(" Left Button");
jford38 9:ee330b1ba394 42 uLCD.locate(0,6);
jford38 9:ee330b1ba394 43 uLCD.puts(" Multi-Player:");
jford38 9:ee330b1ba394 44 uLCD.locate(0,7);
jford38 9:ee330b1ba394 45 uLCD.puts(" Right Button");
jford38 7:9506f2d84162 46 // Right button -> Multiplayer
jford38 7:9506f2d84162 47 // Left button -> Single player
jford38 7:9506f2d84162 48 while(1) {
jford38 9:ee330b1ba394 49 if(!pb_r) { return MULTI_PLAYER; }
jford38 9:ee330b1ba394 50 if(!pb_l) { return SINGLE_PLAYER; }
jford38 7:9506f2d84162 51 }
jford38 7:9506f2d84162 52 }
jford38 7:9506f2d84162 53
jford38 10:5da9b27e050e 54 void map_init() {
jford38 10:5da9b27e050e 55 sync.background_color(SKY_COLOR);
jford38 10:5da9b27e050e 56 sync.cls();
jford38 10:5da9b27e050e 57 sync.filled_rectangle(0,0,128,20, GND_COLOR);
jford38 10:5da9b27e050e 58 sync.filled_rectangle(59, 20, 69, 60, BLACK);
jford38 10:5da9b27e050e 59 sync.update();
jford38 10:5da9b27e050e 60 }
jford38 10:5da9b27e050e 61
jford38 10:5da9b27e050e 62 class Tank {
jford38 10:5da9b27e050e 63 public:
jford38 10:5da9b27e050e 64 int x, y;
jford38 10:5da9b27e050e 65 int w;
jford38 10:5da9b27e050e 66 int h;
jford38 10:5da9b27e050e 67 int tank_color;
jford38 10:5da9b27e050e 68 float barrel_theta;
jford38 10:5da9b27e050e 69 int barrel_length;
jford38 10:5da9b27e050e 70 int wheel_rad;
jford38 10:5da9b27e050e 71
jford38 10:5da9b27e050e 72 Tank(int sx, int sy, int width, int height, int color) {
jford38 10:5da9b27e050e 73 x = sx; y = sy;
jford38 10:5da9b27e050e 74 w = width; h = height;
jford38 10:5da9b27e050e 75 tank_color = color;
jford38 10:5da9b27e050e 76 barrel_theta = PI/4.0;
jford38 10:5da9b27e050e 77 barrel_length = w;
jford38 10:5da9b27e050e 78 wheel_rad = 2.0;
jford38 10:5da9b27e050e 79 draw();
jford38 10:5da9b27e050e 80 }
jford38 10:5da9b27e050e 81 int min_x(void) { return x; }
jford38 10:5da9b27e050e 82 int min_y(void) { return y; }
jford38 10:5da9b27e050e 83 int max_x(void) { return x + w; }
jford38 10:5da9b27e050e 84 int max_y(void) { return y + h + wheel_rad + ceil(barrel_length*sin(barrel_theta)); }
jford38 10:5da9b27e050e 85
jford38 10:5da9b27e050e 86 void reposition(int new_x, int new_y, int new_theta) {
jford38 10:5da9b27e050e 87 sync.filled_rectangle(min_x(), min_y(), max_x(), max_y(), SKY_COLOR);
jford38 10:5da9b27e050e 88 sync.line(x + w/2.0, y+h+wheel_rad, x + w/2.0 + barrel_length*cos(barrel_theta), y+h + barrel_length*sin(barrel_theta), SKY_COLOR);
jford38 10:5da9b27e050e 89 x = new_x; y = new_y; barrel_theta = new_theta;
jford38 10:5da9b27e050e 90 draw();
jford38 10:5da9b27e050e 91 }
jford38 10:5da9b27e050e 92 void draw() {
jford38 10:5da9b27e050e 93 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);
jford38 10:5da9b27e050e 94 sync.filled_rectangle(x, y+wheel_rad, x+w, y+h+wheel_rad, tank_color);
jford38 10:5da9b27e050e 95 sync.filled_circle(x+wheel_rad, y+wheel_rad, wheel_rad, BLACK);
jford38 10:5da9b27e050e 96 sync.filled_circle(x+w-wheel_rad, y+wheel_rad, wheel_rad, BLACK);
jford38 10:5da9b27e050e 97 }
jford38 10:5da9b27e050e 98 };
jford38 10:5da9b27e050e 99
jford38 6:3be57cf4bd33 100 void game_init(void) {
jford38 6:3be57cf4bd33 101
jford38 6:3be57cf4bd33 102 led1 = 0; led2 = 0; led3 = 0; led4 = 0;
jford38 6:3be57cf4bd33 103
jford38 6:3be57cf4bd33 104 pb_l.mode(PullUp);
jford38 6:3be57cf4bd33 105 pb_r.mode(PullUp);
jford38 6:3be57cf4bd33 106 pb_u.mode(PullUp);
jford38 6:3be57cf4bd33 107 pb_d.mode(PullUp);
jford38 6:3be57cf4bd33 108
jford38 6:3be57cf4bd33 109 pc.printf("\033[2J\033[0;0H"); // Clear the terminal screen.
jford38 6:3be57cf4bd33 110 pc.printf("I'm alive! Player 1\n"); // Let us know you made it this far.
jford38 7:9506f2d84162 111 int mode = game_menu();
jford38 9:ee330b1ba394 112 sync.init(&uLCD, mode); // Connect to the other player.
jford38 10:5da9b27e050e 113 map_init();
jford38 6:3be57cf4bd33 114 pc.printf("Initialized...\n"); // Let us know you finished initializing.
jford38 6:3be57cf4bd33 115 }
jford38 6:3be57cf4bd33 116
jford38 0:899c85cd266f 117 int main (void) {
jford38 8:e6dd05393290 118 int* p2_buttons;
jford38 8:e6dd05393290 119
jford38 8:e6dd05393290 120 game_init();
jford38 8:e6dd05393290 121
jford38 10:5da9b27e050e 122 Tank t1(4, 20, 12, 8, TANK_RED);
jford38 10:5da9b27e050e 123 Tank t2(111, 20, 12, 8, TANK_BLUE);
jford38 5:cfec780c935b 124
jford38 0:899c85cd266f 125 while(1) {
jford38 10:5da9b27e050e 126 sync.line(0,0, 5,5, BLACK);
jford38 9:ee330b1ba394 127 sync.update();
jford38 8:e6dd05393290 128
jford38 8:e6dd05393290 129 p2_buttons = sync.get_button_state();
jford38 8:e6dd05393290 130
jford38 8:e6dd05393290 131 led1 = p2_buttons[0] ^ !pb_u;
jford38 8:e6dd05393290 132 led2 = p2_buttons[1] ^ !pb_r;
jford38 8:e6dd05393290 133 led3 = p2_buttons[2] ^ !pb_d;
jford38 8:e6dd05393290 134 led4 = p2_buttons[3] ^ !pb_l;
jford38 3:3ddefff03cb2 135 }
jford38 0:899c85cd266f 136 }