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:
Fri Oct 30 08:54:10 2015 +0000
Parent:
22:3c68eea5a609
Child:
24:2100c9e8ec81
Commit message:
Final final final version. Maybe. For now. Ugh.

Changed in this revision

Bullet/bullet.cpp Show annotated file Show diff for this revision Revisions of this file
Game_Synchronizer.lib Show annotated file Show diff for this revision Revisions of this file
Tank/tank.cpp Show annotated file Show diff for this revision Revisions of this file
Tank/tank.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
misc/misc.cpp Show annotated file Show diff for this revision Revisions of this file
misc/misc.h Show annotated file Show diff for this revision Revisions of this file
--- a/Bullet/bullet.cpp	Thu Oct 29 05:14:49 2015 +0000
+++ b/Bullet/bullet.cpp	Fri Oct 30 08:54:10 2015 +0000
@@ -7,16 +7,20 @@
 extern Game_Synchronizer sync;
 
 // Initialize the bullet. Don't have to do much here.
-// Just set the speed.
+// Keep a pointer to this bullet's tank.
+// Set the speed, and default the bullet to not in_flight.
 Bullet::Bullet(Tank* t) {
     tank = t;
     //speed = ???;
     in_flight = false;
 }
 
-// Set the in_flight flag. Initialize values needed for
-// the trajectory calculations.
+// If in_flight, do nothing. Otherwise,
+// set the in_flight flag, and initialize values needed for
+// the trajectory calculations. (x0, y0), (vx0, vy0), time
+// Hint: tank->barrel_end(...) is useful here.
 void Bullet::shoot(void) {
+    in_flight = true;
 }
 
 // If the bullet is in flight, calculate its new position
@@ -25,14 +29,15 @@
 
 }
 
-// return codes:
-//      BULLET_NO_COLLISION: no collision
-//      BULLET_OFF_SCREEN:   off the side of the screen
-//      Otherwise, returns the color you've hit in 16bpp format. 
-
 int Bullet::time_step(float dt) {
     // If the bullet hasn't hit anything, 
     // redraw the bullet at its new location. 
-    // If it has hit something (obstacle, tank, edge of the screen), return
-    // a descriptive error code. 
+    // If it has hit something (obstacle, tank, edge of the screen), 
+    // set the in_flight flag back to false and return
+    // one of the following codes.
+    //
+    // return codes:
+    //      BULLET_NO_COLLISION: no collision
+    //      BULLET_OFF_SCREEN:   off the side of the screen
+    //      Otherwise, return the color you've hit in 16bpp format. 
 }
\ No newline at end of file
--- a/Game_Synchronizer.lib	Thu Oct 29 05:14:49 2015 +0000
+++ b/Game_Synchronizer.lib	Fri Oct 30 08:54:10 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/ECE2035-Spring-2015-TA/code/Game_Synchronizer/#36c95208edb8
+http://developer.mbed.org/teams/ECE2035-Spring-2015-TA/code/Game_Synchronizer/#a5f9bb626f38
--- a/Tank/tank.cpp	Thu Oct 29 05:14:49 2015 +0000
+++ b/Tank/tank.cpp	Fri Oct 30 08:54:10 2015 +0000
@@ -46,13 +46,17 @@
 }
 
 void Tank::reposition(int dx, int dy, float dtheta) {
-    // Move the tank dx pixels in the x direction.
-    // Move the tank dy pixels in the y direction.
-    // Move the tank barrel by an angle dtheta. Don't allow it to go below parallel.
+    // Blank out the old tank position, and
+    //      Move the tank dx pixels in the x direction.
+    //      Move the tank dy pixels in the y direction.
+    //      Move the tank barrel by an angle dtheta. Don't allow it to go below parallel.
     
-    // Do collision detection to prevent the tank from hitting things.
+    // Do collision detection to prevent the tank from hitting things. 
+    // (obstacles, side of the screen, other tanks, etc.)
 }
 
+
+// Example tank draw function. We expect you to get creative on this one!
 void Tank::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);
--- a/Tank/tank.h	Thu Oct 29 05:14:49 2015 +0000
+++ b/Tank/tank.h	Fri Oct 30 08:54:10 2015 +0000
@@ -1,6 +1,9 @@
 #ifndef TANK_H__
 #define TANK_H__
 
+// This class describes a tank. You may need to add
+// additional member variables (and maybe even member functions)
+// to draw your super cool new tank. 
 
 class Tank {
     public:
--- a/main.cpp	Thu Oct 29 05:14:49 2015 +0000
+++ b/main.cpp	Fri Oct 30 08:54:10 2015 +0000
@@ -31,7 +31,8 @@
 
 // Global variables go here.
 
-
+int winner = -1;
+int whose_turn = PLAYER1;
 
 
 // Ask the user whether to run the game in Single- or Multi-Player mode.
@@ -52,8 +53,12 @@
 
     // Button Example:
     // Use !pb_r to access the player1 right button value.
-    wait(2);
-    return SINGLE_PLAYER;
+    
+    wait(2);        // Eventually you can take this out.
+    
+    // Eventually you should return SINGLE_PLAYER or MULTI_PLAYER
+    // depending on the user's choice.
+    return SINGLE_PLAYER;       
 }
 
 // Initialize the world map. I've provided a basic map here,
@@ -89,10 +94,11 @@
     // Display the game title.
     char title[] = "  Title";
     sync.puts(title, sizeof(title));
-    sync.update();
+    
+    // Flush the draw buffer and execute all the accumulated draw commands.
+    sync.update();          
 }
 
-
 // Initialize the game hardware. 
 // Call game_menu to find out which mode to play the game in (Single- or Multi-Player)
 // Initialize the game synchronizer.
@@ -138,42 +144,79 @@
     Bullet b1(&t1);
     Bullet b2(&t2);
     
+    frame_timer.start();
     
+    // Your code starts here...
     while(true) {
+        s
+        // Get a pointer to the buttons for both sides.
+        // From now on, you can access the buttons for player x using
+        //
+        // px_buttons[U_BUTTON] 
+        // px_buttons[R_BUTTON] 
+        // px_buttons[D_BUTTON] 
+        // px_buttons[L_BUTTON]
         
-        // Read the buttons/accelerometer and store the values
-        // in the synchronizer's internal array.
-        sync.set_p1_inputs();
-        
-        // Get a pointer to the buttons for both sides.
         p1_buttons = sync.get_p1_buttons();
         p2_buttons = sync.get_p2_buttons();
+        
+        led1 = p1_buttons[0] ^ p2_buttons[0];
+        led2 = p1_buttons[1] ^ p2_buttons[1];
+        led3 = p1_buttons[2] ^ p2_buttons[2];
+        led4 = p1_buttons[3] ^ p2_buttons[3];
 
         // Get the accelerometer values.
         sync.get_p1_accel_data(&ax1, &ay1, &az1);
         sync.get_p2_accel_data(&ax2, &ay2, &az2);
 
-        // Game logic goes here.
-        
-        // Depending on whose turn it is, 
-        //  - update their tank's position using the accelerometer state for that player.
-        //  - update their bullet's position using the time elapsed since the previous frame.
-        
-        // You have to handle the case where sync.play_mode == SINGLE_PLAYER 
-        // as well as the case where sync.play_mode == MULTI_PLAYER
-        
-        // Useful functions:
-        // t1.reposition(...);
-        // t2.reposition(...);
-        // b1.timestep(...);
-        // b2.timestep(...);
-        
-        // End of game logic
-        
-        // sync.update() flushes the internal buffer and performs all the draw commands on both sides.
-        // This must be called at the end of every frame, or things won't be drawn and buttons won't get 
-        // updated.
-        sync.update();      
+        if(whose_turn == PLAYER1) {
+            
+            // Accelerometer example
+            if(ax1 >  ACC_THRESHOLD) { 
+                // Move the tank to the right if the accelerometer is tipped far enough to the right.
+                 t1.reposition(+1, 0, 0);          
+            }
+            
+            // Button example
+            if(p1_buttons[D_BUTTON]) { 
+                b1.shoot(); 
+            }
+            
+            float dt = frame_timer.read();
+            int intersection_code = b1.time_step(dt); 
+            
+            if(intersection_code != BULLET_NO_COLLISION || intersection_code == BULLET_OFF_SCREEN) { 
+                pc.printf("Now it's P2's turn!\n");
+                whose_turn = PLAYER2;
+            }
+            
+            // If you shot yourself, you lost.
+            if(sync.pixel_eq(intersection_code, t1.tank_color)) { 
+                sync.update();  // Is this necessary?
+                winner = PLAYER2;
+                break;
+            }
+            
+            // If you shot the other guy, you won!
+            if(sync.pixel_eq(intersection_code, t2.tank_color)) { 
+                sync.update();
+                winner = PLAYER1;
+                break;
+            }
+        } else if(whose_turn == PLAYER2) {
+            
+            // I gave you a lot of the logic for Player1. It's up to you to figure out Player2!
+            // If you are in SINGLE_PLAYER mode, you should use the p1 inputs to control p2.
+            // In MULTI_PLAYER mode, you should use the p2 inputs to control p2.
+            //
+            // Hint: 
+            //         sync.play_mode will tell you if you are in MULTI_PLAYER or SINGLE_PLAYER mode.
+            //         
+
+        }
+
+        frame_timer.reset();
+        sync.update();     
     } 
     
     game_over();
--- a/misc/misc.cpp	Thu Oct 29 05:14:49 2015 +0000
+++ b/misc/misc.cpp	Fri Oct 30 08:54:10 2015 +0000
@@ -6,19 +6,6 @@
 extern uLCD_4DGL uLCD;
 extern wave_player player;
 
-
-int CONVERT_24_TO_16_BPP(int col_24) {
-    int b = col_24 & 0xFF;
-    int g = (col_24 >> 8) & 0xFF;
-    int r = (col_24 >> 16)& 0xFF;
-    
-    r >>= 3;
-    g >>= 2;
-    b >>= 3;
-    
-    return r<<11 | g<<5 | b;
-}
-
 // Given the filename of a .wav file in the SD card, play the file over the speaker.
 void playSound(char * wav)
 {
--- a/misc/misc.h	Thu Oct 29 05:14:49 2015 +0000
+++ b/misc/misc.h	Fri Oct 30 08:54:10 2015 +0000
@@ -20,7 +20,6 @@
 
 #define PI  3.1415926535797
 
-int CONVERT_24_TO_16_BPP(int col_24);
 void playSound(char * wav);
 
 #endif //GLOBAL_H__
\ No newline at end of file