Shell code for the Tanks game

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

Revision:
10:5da9b27e050e
Parent:
9:ee330b1ba394
Child:
11:f455e907baba
--- a/main.cpp	Thu Oct 22 21:34:00 2015 +0000
+++ b/main.cpp	Fri Oct 23 03:33:35 2015 +0000
@@ -2,6 +2,13 @@
 
 #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);
@@ -13,6 +20,7 @@
 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)
 
@@ -24,6 +32,7 @@
     
     // 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);
@@ -42,6 +51,52 @@
     }
 }
 
+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; }
+        int max_y(void) { return y + h + wheel_rad + ceil(barrel_length*sin(barrel_theta)); }
+        
+        void reposition(int new_x, int new_y, int 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 + 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;
@@ -55,7 +110,7 @@
     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();
+    map_init();
     pc.printf("Initialized...\n");              // Let us know you finished initializing.
 }
 
@@ -64,30 +119,11 @@
     
     game_init();
     
-    float theta = 0;
-    bool first = true;
+    Tank t1(4, 20, 12, 8, TANK_RED);
+    Tank t2(111, 20, 12, 8, TANK_BLUE);
     
     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.line(0,0, 5,5, BLACK);
         sync.update();
         
         p2_buttons = sync.get_button_state();
@@ -96,8 +132,5 @@
         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");
     } 
 }
\ No newline at end of file