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:
Thu Oct 29 05:14:49 2015 +0000
Parent:
21:edfeb289b21f
Child:
23:77049670cae6
Commit message:
The Shell for students to fill in to play their tanks game.

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 03:56:30 2015 +0000
+++ b/Bullet/bullet.cpp	Thu Oct 29 05:14:49 2015 +0000
@@ -6,26 +6,23 @@
 
 extern Game_Synchronizer sync;
 
+// Initialize the bullet. Don't have to do much here.
+// Just set the speed.
 Bullet::Bullet(Tank* t) {
     tank = t;
-    speed = 30;
+    //speed = ???;
     in_flight = false;
 }
+
+// Set the in_flight flag. Initialize values needed for
+// the trajectory calculations.
 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; 
 }
 
+// If the bullet is in flight, calculate its new position
+// after a time delta dt.
 void Bullet::update_position(float dt) {
-    if(!in_flight) { return; }
-    time += dt;
-    x = x0 + vx0*time;
-    y = y0 + vy0*time + 0.5*(-9.81)*time*time;
+
 }
 
 // return codes:
@@ -34,31 +31,8 @@
 //      Otherwise, returns the color you've hit in 16bpp format. 
 
 int Bullet::time_step(float dt) {
-    if(!in_flight) { return BULLET_NO_COLLISION; }
-    int old_x = x;
-    int old_y = y;
-    
-    update_position(dt);
-    
-    if(x == old_x && y == old_y) {
-        return BULLET_NO_COLLISION;
-    }
-    
-    if(x < 0 || x > 128 || y < 0 || y > 128) {
-        sync.pixel(x, y, SKY_COLOR);
-        in_flight = false;
-        return BULLET_OFF_SCREEN;        
-    }
-    
-    int col = sync.read_pixel(x, y);
-    if(col != CONVERT_24_TO_16_BPP(SKY_COLOR)) {
-        sync.pixel(old_x, old_y, SKY_COLOR);
-        sync.filled_circle(x, y, 4, SKY_COLOR);
-        in_flight = false;
-        return col;
-    }
-    sync.pixel(old_x, old_y, SKY_COLOR);
-    sync.pixel(x, y, BLACK);
-    return BULLET_NO_COLLISION;
-    
+    // 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. 
 }
\ No newline at end of file
--- a/Game_Synchronizer.lib	Thu Oct 29 03:56:30 2015 +0000
+++ b/Game_Synchronizer.lib	Thu Oct 29 05:14:49 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/ECE2035-Spring-2015-TA/code/Game_Synchronizer/#5807272c35b5
+http://developer.mbed.org/teams/ECE2035-Spring-2015-TA/code/Game_Synchronizer/#36c95208edb8
--- a/Tank/tank.cpp	Thu Oct 29 03:56:30 2015 +0000
+++ b/Tank/tank.cpp	Thu Oct 29 05:14:49 2015 +0000
@@ -18,63 +18,39 @@
     wheel_rad = 2.0;
     draw();
 }
+
+// Return the minimum x-coord of your tank's bounding box.
 int Tank::min_x(void) { 
-    int barrel_extent = (-barrel_length*cos(barrel_theta) > w/2.0) ? barrel_length*cos(barrel_theta) + w/2.0 : 0;
-    return x + barrel_extent;
-}
-    
-int Tank::min_y(void) { return y; }
-int Tank::max_x(void) { 
-    int barrel_extent = (barrel_length*cos(barrel_theta) > w/2.0) ? barrel_length*cos(barrel_theta) : w/2.0;
-    return x + w/2.0 + barrel_extent; 
+    return 0;
+}    
+
+// Return the minimum y-coord of your tank's bounding box.
+int Tank::min_y(void) {
+    return 0;
 }
 
-int Tank::max_y(void) { 
-    int barrel_extent = (sin(barrel_theta) > 0) ? barrel_length*sin(barrel_theta) : 0;
-    return y + h + wheel_rad + barrel_extent;
+// Return the maximum x-coord of your tank's bounding box.
+int Tank::max_x(void) { 
+    return 0;
 }
 
-void Tank::barrel_end(int& bx, int& by) {
-    bx = x + w/2.0 + (barrel_length+1)*cos(barrel_theta);
-    by = y+h+wheel_rad + (barrel_length+1)*sin(barrel_theta);
+// Return the maximum y-coord of your tank's bounding box.
+int Tank::max_y(void) {  
+    return 1;
+}
+
+void Tank::barrel_end(int* bx, int* by) {
+    // Return the x and y coords of the end of the barrel.
+    // *bx = ???
+    // *by = ???
 }
 
 void Tank::reposition(int dx, int dy, float dtheta) {
-    
-    int new_x = x+dx;
-    int new_y = y+dy;
-    float new_theta = abs(barrel_theta+dtheta);
-    
-    float old_theta = barrel_theta;
-    int old_x = x;
-    int old_y = y;
-    int minx = min_x();
-    int miny = min_y();
-    int maxx = max_x();
-    int maxy = max_y();
-    
-    x = new_x; y = new_y; barrel_theta = (new_theta > PI) ? PI : new_theta;
+    // 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.
     
-    if(min_x() < 0 || max_x() > 128) { 
-        x = old_x; y = old_y; barrel_theta = old_theta;
-        draw();
-        return;
-    }
-    
-    int right_edge = sync.read_pixel(min_x()-1, (min_y()+max_y())/2.0);
-    int left_edge  = sync.read_pixel(max_x()+1, (min_y()+max_y())/2.0);
-    
-    if( ( right_edge != CONVERT_24_TO_16_BPP(SKY_COLOR) && new_x < old_x ) ||
-        ( left_edge  != CONVERT_24_TO_16_BPP(SKY_COLOR) && new_x > old_x ) ) {
-        x = old_x; y = old_y; barrel_theta = old_theta;
-        draw();
-        return;  
-    } 
-    
-    
-    sync.filled_rectangle(minx, miny, maxx, maxy, SKY_COLOR);
-    sync.line(old_x + w/2.0, old_y+h+wheel_rad, old_x + w/2.0 + 1 + barrel_length*cos(old_theta), old_y+h + barrel_length*sin(old_theta), SKY_COLOR);
-    draw();
+    // Do collision detection to prevent the tank from hitting things.
 }
 
 void Tank::draw() {
--- a/Tank/tank.h	Thu Oct 29 03:56:30 2015 +0000
+++ b/Tank/tank.h	Thu Oct 29 05:14:49 2015 +0000
@@ -25,7 +25,7 @@
         int max_y(void);
         
         // Calculate the position of the end of the barrel.
-        void barrel_end(int& bx, int& by);
+        void barrel_end(int* bx, int* by);
         
         // Reposition the tank!
         void reposition(int dx, int dy, float dtheta);
--- a/main.cpp	Thu Oct 29 03:56:30 2015 +0000
+++ b/main.cpp	Thu Oct 29 05:14:49 2015 +0000
@@ -30,8 +30,7 @@
 Timer frame_timer;                          // Timer
 
 // Global variables go here.
-int winner = -1;
-int whose_turn = PLAYER1;
+
 
 
 
@@ -40,24 +39,21 @@
 // writing to the local (Player1) lcd. Sync hasn't been initialized yet,
 // so you can't use it anyway. For the same reason, you must access
 // the buttons directly e.g. if( !pb_r ) { do something; }.
+
+// return MULTI_PLAYER if the user selects multi-player.
+// return SINGLE_PLAYER if the user selects single-player.
 int game_menu(void) {
     
     uLCD.baudrate(BAUD_3000000);
-    uLCD.locate(0,1);
-    uLCD.puts("Select Mode:");
-    uLCD.locate(0,3);
-    uLCD.puts("  Single-Player:");
-    uLCD.locate(0,4);
-    uLCD.puts("   Left Button");
-    uLCD.locate(0,6);
-    uLCD.puts("  Multi-Player:");
-    uLCD.locate(0,7);
-    uLCD.puts("   Right Button");
+    
+    // the locate command tells the screen where to place the text.
+    uLCD.locate(0,15);
+    uLCD.puts("Replace me");
 
-    while(1) {
-        if(!pb_r) { return  MULTI_PLAYER; }    // Right button -> Multiplayer
-        if(!pb_l) { return SINGLE_PLAYER; }    // Left button -> Single player
-    }
+    // Button Example:
+    // Use !pb_r to access the player1 right button value.
+    wait(2);
+    return SINGLE_PLAYER;
 }
 
 // Initialize the world map. I've provided a basic map here,
@@ -78,8 +74,8 @@
     // Draw the ground in green.
     sync.filled_rectangle(0,0,128,20, GND_COLOR);
     
-    // Draw a wall in the middle of the map. It doesn't have to be black, 
-    // but it shouldn't be the same color as the sky or your tanks.
+    // Draw some obstacles. They don't have to be black, 
+    // but they shouldn't be the same color as the sky or your tanks.
     // Get creative here. You could use brown and grey to draw a mountain
     // or something cool like that.
     sync.filled_rectangle(59, 20, 69, 60, BLACK);
@@ -89,8 +85,9 @@
     
     // Set the text background color to match the sky. Just for looks.
     sync.textbackground_color(SKY_COLOR);
+    
     // Display the game title.
-    char title[] = "  ECE 2035 Tanks";
+    char title[] = "  Title";
     sync.puts(title, sizeof(title));
     sync.update();
 }
@@ -116,48 +113,13 @@
     pc.printf("Initialized...\n");              // Let us know you finished initializing.
 }
 
-// Given the filename of a .wav file in the SD card, play the file over the speaker.
-void playSound(char * wav)
-{
-    // open wav file
-    FILE *wave_file;
-    wave_file=fopen(wav,"r");
-
-    if(wave_file == NULL){
-        uLCD.locate(0,4);
-        uLCD.printf("Error in SD");
-        return;
-    }
-    // play wav file
-    player.play(wave_file);
-
-    // close wav file
-    fclose(wave_file);
+// Display some kind of game over screen which lets us know who won.
+// Play a cool sound!
+void game_over() {
+    
 }
 
-// Display some kind of game over screen which lets us know who won.
-void game_over(int winner) {
-    if(winner == PLAYER1)      { pc.printf("Player 1 wins!\n"); }
-    else if(winner == PLAYER2) { pc.printf("Player 2 wins!\n"); }
-        
-    int i = 0;
-    while(1) {
-        sync.cls();
-        sync.locate(i, 9);
-        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)%11;
-        wait(0.1);
-    }
 
-}
 int main (void) {
     
     int* p1_buttons;
@@ -168,102 +130,52 @@
     
     game_init();
     
+    // Create your tanks.
     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)
+    
+    // For each tank, create a bullet.
     Bullet b1(&t1);
     Bullet b2(&t2);
     
     
-    frame_timer.start();
     while(true) {
         
+        // 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();
 
-        sync.get_p1_accel_data(ax1, ay1, az1);
-        sync.get_p2_accel_data(ax2, ay2, az2);
+        // Get the accelerometer values.
+        sync.get_p1_accel_data(&ax1, &ay1, &az1);
+        sync.get_p2_accel_data(&ax2, &ay2, &az2);
+
+        // Game logic goes here.
         
-        led1 = p2_buttons[0] ^ p2_buttons[0];
-        led2 = p2_buttons[1] ^ p2_buttons[1];
-        led3 = p2_buttons[2] ^ p2_buttons[2];
-        led4 = p2_buttons[3] ^ p2_buttons[3];
-
+        // 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
         
-        if(whose_turn == PLAYER1) {
-            if(ax1 >  ACC_THRESHOLD) { t1.reposition(-1, 0, 0); }
-            if(ax1 < -ACC_THRESHOLD) { t1.reposition(+1, 0, 0); }
-            
-            if(ay1 >  ACC_THRESHOLD) { t1.reposition(0, 0, +PI/30.0); }
-            if(ay1 < -ACC_THRESHOLD) { t1.reposition(0, 0, -PI/30.0); }
-            
-            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 have shot yourself, you lost.
-            if(intersection_code == CONVERT_24_TO_16_BPP(t1.tank_color)) { 
-                sync.update();  // Is this necessary?
-                winner = PLAYER2;
-                break;
-            }
-            
-            // If you have shot the other guy, you won!
-            if(intersection_code == CONVERT_24_TO_16_BPP(t2.tank_color)) { 
-                sync.update();
-                winner = PLAYER1;
-                break;
-            }
-        } else if(whose_turn == PLAYER2) {
-            
-            if((sync.play_mode == MULTI_PLAYER && ax2 >  ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ax1 >  ACC_THRESHOLD)) { 
-                t2.reposition(-1, 0, 0);
-            }
-            if((sync.play_mode == MULTI_PLAYER && ax2 < -ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ax1 < -ACC_THRESHOLD)) {
-                t2.reposition(+1, 0, 0);
-            }
-            if((sync.play_mode == MULTI_PLAYER && ay2 >  ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ay1 >  ACC_THRESHOLD)) { 
-                t2.reposition(0, 0, -PI/30.0);
-            }
-            if((sync.play_mode == MULTI_PLAYER && ay2 < -ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ay1 < -ACC_THRESHOLD)) { 
-                t2.reposition(0, 0, +PI/30.0);
-            }
-            if((sync.play_mode == MULTI_PLAYER && p2_buttons[D_BUTTON]) || (sync.play_mode == SINGLE_PLAYER && p1_buttons[D_BUTTON])) { 
-                b2.shoot(); 
-            }
-                
-            float dt = frame_timer.read();
-            int intersection_code = b2.time_step(dt);
-            
-            if(intersection_code != BULLET_NO_COLLISION || intersection_code == BULLET_OFF_SCREEN) { pc.printf("Now it's P1's turn!\n"); whose_turn = PLAYER1; }
-            
-            // If you shot yourself, you lost.
-            if(intersection_code == CONVERT_24_TO_16_BPP(t2.tank_color)) { 
-                sync.update();  // Is this necessary?
-                winner = PLAYER1;
-                break;
-            }
-            
-            // If you shot the other guy, you won!
-            if(intersection_code == CONVERT_24_TO_16_BPP(t1.tank_color)) { 
-                sync.update();
-                winner = PLAYER2;
-                break;
-            }
-        }
-
-        frame_timer.reset();
-        sync.update();
+        // 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();      
     } 
     
-    playSound("/sd/wavfiles/BUZZER.wav");
-    
-    game_over(winner);
+    game_over();
     
 }
\ No newline at end of file
--- a/misc/misc.cpp	Thu Oct 29 03:56:30 2015 +0000
+++ b/misc/misc.cpp	Thu Oct 29 05:14:49 2015 +0000
@@ -1,4 +1,11 @@
 #include "misc.h"
+#include "uLCD_4DGL.h"
+#include "SDFileSystem.h"
+#include "wave_player.h"
+
+extern uLCD_4DGL uLCD;
+extern wave_player player;
+
 
 int CONVERT_24_TO_16_BPP(int col_24) {
     int b = col_24 & 0xFF;
@@ -10,4 +17,23 @@
     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)
+{
+    // open wav file
+    FILE *wave_file;
+    wave_file=fopen(wav,"r");
+
+    if(wave_file == NULL){
+        uLCD.locate(0,4);
+        uLCD.printf("Error in SD");
+        return;
+    }
+    // play wav file
+    player.play(wave_file);
+
+    // close wav file
+    fclose(wave_file);
 }
\ No newline at end of file
--- a/misc/misc.h	Thu Oct 29 03:56:30 2015 +0000
+++ b/misc/misc.h	Thu Oct 29 05:14:49 2015 +0000
@@ -21,5 +21,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