Shell code for the Tanks game

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

Revision:
18:18dfc9fb33b5
Parent:
17:7bc7127782e4
Child:
19:7aa3af04d6a8
--- a/main.cpp	Wed Oct 28 04:30:22 2015 +0000
+++ b/main.cpp	Wed Oct 28 06:36:24 2015 +0000
@@ -31,7 +31,7 @@
 Game_Synchronizer sync(PLAYER1);            // Game_Synchronizer (PLAYER)
 Timer frame_timer;                          // Timer
 
-
+int winner = -1;
 
 // Ask the user whether to run the game in Single- or Multi-Player mode.
 // Note that this function uses uLCD instead of sync because it is only 
@@ -40,6 +40,7 @@
 // the buttons directly e.g. if( !pb_r ) { do something; }.
 int game_menu(void) {
     
+    uLCD.baudrate(BAUD_3000000);
     uLCD.locate(0,1);
     uLCD.puts("Select Mode:");
     uLCD.locate(0,3);
@@ -113,8 +114,8 @@
     
     game_init();
     
-    Tank t1(4, 21, 12, 8, TANK_RED);
-    Tank t2(111, 21, 12, 8, TANK_BLUE);
+    Tank t1(4, 21, 12, 8, TANK_RED);            // (min_x, min_y, width, height, color)
+    Tank t2(111, 21, 12, 8, TANK_BLUE);         // (min_x, min_y, width, height, color)
     Bullet b1(&t1);
     Bullet b2(&t2);
     
@@ -136,42 +137,45 @@
         float ax1 = (float) p1_inputs[4] / 65536.0;
         float ay1 = (float) p1_inputs[5] / 65536.0;
         float az1 = (float) p1_inputs[6] / 65536.0;
-        pc.printf("P1 X: %f\tY: %f\tZ: %f\n", ax1, ay1, az1);
+        //pc.printf("P1 X: %f\tY: %f\tZ: %f\n", ax1, ay1, az1);
         
         float ax2 = (float) p2_inputs[4] / 65536.0;
         float ay2 = (float) p2_inputs[5] / 65536.0;
         float az2 = (float) p2_inputs[6] / 65536.0;
-        pc.printf("P2 X: %f\tY: %f\tZ: %f\n", ax2, ay2, az2);
+        //pc.printf("P2 X: %f\tY: %f\tZ: %f\n", ax2, ay2, az2);
         
-        /*       
-        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_inputs[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(ax1 >  ACC_THRESHOLD) { t1.reposition(t1.x-1, t1.y, t1.barrel_theta); }
+        if(ax1 < -ACC_THRESHOLD) { t1.reposition(t1.x+1, t1.y, t1.barrel_theta); }
+        if(ax2 >  ACC_THRESHOLD) { t2.reposition(t2.x-1, t2.y, t2.barrel_theta); }
+        if(ax2 < -ACC_THRESHOLD) { 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);
+        if(ay1 >  ACC_THRESHOLD) { t1.reposition(t1.x, t1.y, t1.barrel_theta+PI/30.0); }
+        if(ay1 < -ACC_THRESHOLD) { t1.reposition(t1.x, t1.y, t1.barrel_theta-PI/30.0); }
+        
+        if(ay2 >  ACC_THRESHOLD) { t2.reposition(t2.x, t2.y, t2.barrel_theta+PI/30.0); }
+        if(ay2 < -ACC_THRESHOLD) { t2.reposition(t2.x, t2.y, t2.barrel_theta-PI/30.0); }
         
-        if(!pb_d) { b1.shoot(); }
-        if(p2_buttons[2]) { b2.shoot(); }
+        if(p1_inputs[D_BUTTON]) { b1.shoot(); }
+        if(p2_inputs[D_BUTTON]) { b2.shoot(); }
 
-        int retval = b1.time_step(frame_timer.read());
-        if(retval) { 
+        float time = frame_timer.read();
+        int retval = b1.time_step(time);
+        if(retval == t2.tank_color) { 
             sync.update();
             pc.printf("P1 wins!"); 
+            winner = PLAYER1;
             break;
         }
-        retval = b2.time_step(frame_timer.read());
-        if(retval) { 
+        retval = b2.time_step(time);
+        if(retval == t1.tank_color) { 
             sync.update();
             pc.printf("P2 wins!"); 
+            winner = PLAYER2;
             break;
         }
-        */
-        //frame_timer.reset();
         
+        frame_timer.reset();
         sync.update();
     } 
     
@@ -180,8 +184,14 @@
     while(1) {
         sync.cls();
         sync.locate(i, 9);
-        char msg[] = "GAME OVER!";
-        sync.puts(msg, sizeof(msg)); 
+        if(winner == PLAYER1) {
+            char msg[] = "P1 Wins!";
+            sync.puts(msg, sizeof(msg)); 
+        } else if(winner == PLAYER2) {
+            char msg[] = "P2 Wins!";
+            sync.puts(msg, sizeof(msg)); 
+        }
+        
         sync.update();  
         i = (i+1)%10;
         wait(0.1);