Revenge of the Mouse

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

Fork of 2035_Tanks_Shell by ECE2035 Spring 2015 TA

Files at this revision

API Documentation at this revision

Comitter:
jford38
Date:
Mon Oct 26 04:12:35 2015 +0000
Parent:
14:36c306e26317
Child:
16:b73f0842d3cd
Commit message:
Updates. Idk. Leave me alone. It's late.

Changed in this revision

Game_Synchronizer.lib Show annotated file Show diff for this revision Revisions of this file
bullet.cpp Show annotated file Show diff for this revision Revisions of this file
bullet.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Game_Synchronizer.lib	Mon Oct 26 02:58:37 2015 +0000
+++ b/Game_Synchronizer.lib	Mon Oct 26 04:12:35 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/ECE2035-Spring-2015-TA/code/Game_Synchronizer/#e34b35474115
+http://developer.mbed.org/teams/ECE2035-Spring-2015-TA/code/Game_Synchronizer/#f0e9bc7f1421
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bullet.cpp	Mon Oct 26 04:12:35 2015 +0000
@@ -0,0 +1,55 @@
+#include "bullet.h"
+#include "game_synchronizer.h"
+#include "globals.h"
+#include "math.h"
+
+extern Game_Synchronizer sync;
+
+Bullet::Bullet(Tank* t) {
+    tank = t;
+    speed = 30;
+    in_flight = false;
+}
+void Bullet::shoot(void) {
+    if(in_flight) {return;}
+    time = 0;
+    tank->barrel_end(x0, y0);
+    x = x0; y = y0;
+    vx0 = speed*cos(tank->barrel_theta);
+    vy0 = speed*sin(tank->barrel_theta);
+    in_flight = true; 
+}
+
+int Bullet::time_step(float dt) {
+    if(!in_flight) {return 0;}
+    time += dt;
+    int new_x = x0 + vx0*time;
+    int new_y = y0 + vy0*time + 0.5*(-9.81)*time*time;
+    
+    if(new_x == x && new_y == y) {      // Didn't move!
+        return 0;
+    }
+    
+    if(new_x < 0 || new_x > 128 || new_y < 0 || new_y > 128) {
+        in_flight = false;
+        return 0;
+    }
+    
+    int col = sync.read_pixel(new_x, new_y);
+    if(col != CONVERT_24_TO_16_BPP(SKY_COLOR)) {
+        sync.pixel(x, y, SKY_COLOR);
+        sync.filled_circle(new_x, new_y, 4, SKY_COLOR);
+        in_flight = false;
+        
+        if(col == CONVERT_24_TO_16_BPP(TANK_BLUE)) {
+            return 1;
+        }
+        return 0;
+    }
+    sync.pixel(x, y, SKY_COLOR);
+            
+    x = new_x;
+    y = new_y;            
+    sync.pixel(x, y, BLACK);
+    return 0;
+}      
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bullet.h	Mon Oct 26 04:12:35 2015 +0000
@@ -0,0 +1,25 @@
+#ifndef BULLET_H__
+#define BULLET_H__
+
+#include "tank.h"
+
+class Bullet{
+    public:
+        int x0;
+        int y0;
+        float vx0;
+        float vy0;
+        int x;
+        int y;
+        int speed;
+        float time;
+        bool in_flight;
+        
+        Tank* tank;
+        
+        Bullet(Tank* t);
+        void shoot(void);
+        int time_step(float dt);    
+};
+
+#endif
\ No newline at end of file
--- a/main.cpp	Mon Oct 26 02:58:37 2015 +0000
+++ b/main.cpp	Mon Oct 26 04:12:35 2015 +0000
@@ -1,11 +1,11 @@
 // Student Side.
 
 #include "mbed.h"
+
 #include "game_synchronizer.h"
-
 #include "tank.h"
+#include "bullet.h"
 #include "globals.h"
-#include <vector>
 
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
@@ -61,67 +61,6 @@
     sync.update();
 }
 
-class Bullet{
-    public:
-        int x0;
-        int y0;
-        float vx0;
-        float vy0;
-        int x;
-        int y;
-        int speed;
-        float time;
-        bool in_flight;
-        
-        Tank* tank;
-        
-        Bullet(Tank* t) {
-            tank = t;
-            speed = 30;
-            in_flight = false;
-        }
-        void shoot(void) {
-            if(in_flight) {return;}
-            time = 0;
-            tank->barrel_end(x0, y0);
-            x = x0; y = y0;
-            vx0 = speed*cos(tank->barrel_theta);
-            vy0 = speed*sin(tank->barrel_theta);
-            in_flight = true; 
-        }
-        
-        void time_step(float dt) {
-            if(!in_flight) {return;}
-            time += dt;
-            int new_x = x0 + vx0*time;
-            int new_y = y0 + vy0*time + 0.5*(-9.81)*time*time;
-            
-            if(new_x == x && new_y == y) {      // Didn't move!
-                return;
-            }
-            
-            if(new_x < 0 || new_x > 128 || new_y < 0 || new_y > 128) {
-                in_flight = false;
-                return;
-            }
-            
-            int col = sync.read_pixel(new_x, new_y);
-            if(col != CONVERT_24_TO_16_BPP(SKY_COLOR)) {
-                if(col == CONVERT_24_TO_16_BPP(TANK_BLUE)) {
-                    pc.printf("Player 1 wins!\n");
-                }
-                sync.pixel(x, y, SKY_COLOR);
-                sync.filled_circle(new_x, new_y, 4, SKY_COLOR);
-                in_flight = false;
-                return;
-            }
-            sync.pixel(x, y, SKY_COLOR);
-                    
-            x = new_x;
-            y = new_y;            
-            sync.pixel(x, y, BLACK);
-        }      
-};
 
 
 void game_init(void) {
@@ -178,10 +117,31 @@
         if(!pb_d) { b1.shoot(); }
         if(p2_buttons[2]) { b2.shoot(); }
 
-        b1.time_step(frame_timer.read());
-        b2.time_step(frame_timer.read());
+        int retval = b1.time_step(frame_timer.read());
+        if(retval) { 
+            sync.update();
+            pc.printf("P1 wins!"); 
+            break;
+        }
+        retval = b2.time_step(frame_timer.read());
+        if(retval) { 
+            sync.update();
+            pc.printf("P2 wins!"); 
+            break;
+        }
         frame_timer.reset();
         
         sync.update();
     } 
+    
+    
+    int i = 0;
+    while(1) {
+        sync.cls();
+        sync.locate(i, 6);
+        sync.puts("GAME OVER!\n", sizeof("GAME OVER!\n")); 
+        sync.update();  
+        (++i) %= 10; 
+        wait(0.1);
+    }
 }
\ No newline at end of file