Jason McNeely / Mbed 2 deprecated DuelingTanks

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

Fork of 2035_Tanks_Shell by ECE2035 Spring 2015 TA

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Student Side.
00002 
00003 #include "mbed.h"
00004 #include "DRV2605.h"
00005 #include "SDFileSystem.h"
00006 #include "wave_player.h"
00007 #include "playSound.h"
00008 #include "SparkfunAnalogJoystick.h"
00009 #include "game_synchronizer.h"
00010 #include "tank.h"
00011 #include "bullet.h"
00012 #include "globals.h"
00013 
00014 SparkfunAnalogJoystick joystick(p15, p16, p17);
00015 DigitalOut led1(LED1);
00016 DigitalOut led2(LED2);
00017 DigitalOut led3(LED3);
00018 DigitalOut led4(LED4);
00019 DRV2605 haptics(p9, p10);
00020 
00021 DigitalIn pb_u(p21);                        // Up Button
00022 DigitalIn pb_r(p22);                        // Right Button
00023 DigitalIn pb_d(p23);                        // Down Button
00024 DigitalIn pb_l(p24);                        // Left Button
00025 
00026 Serial pc(USBTX, USBRX);                    // Serial connection to PC. Useful for debugging!
00027 MMA8452 acc(p28, p27, 100000);              // Accelerometer (SDA, SCL, Baudrate)
00028 uLCD_4DGL uLCD(p13,p14,p11);                 // LCD (tx, rx, reset)
00029 SDFileSystem sd(p5, p6, p7, p8, "sd");      // SD  (mosi, miso, sck, cs)
00030 AnalogOut DACout(p18);                      // speaker
00031 wave_player player(&DACout);                // wav player
00032 Game_Synchronizer sync(PLAYER1);            // Game_Synchronizer (PLAYER)
00033 Timer frame_timer;                          // Timer
00034 
00035 
00036 // Global variables go here.
00037 
00038 int winner = -1;
00039 int whose_turn = PLAYER1;
00040 int menu=1;
00041 int t2minpixelcolor;
00042 int t1maxpixelcolor;
00043 int t1y;
00044 int t2y;
00045 
00046 
00047 
00048 // Ask the user whether to run the game in Single- or Multi-Player mode.
00049 // Note that this function uses uLCD instead of sync because it is only 
00050 // writing to the local (Player1) lcd. Sync hasn't been initialized yet,
00051 // so you can't use it anyway. For the same reason, you must access
00052 // the buttons directly e.g. if( !pb_r ) { do something; }.
00053 
00054 // return MULTI_PLAYER if the user selects multi-player.
00055 // return SINGLE_PLAYER if the user selects single-player.
00056 int game_menu(void) {
00057     
00058     uLCD.baudrate(BAUD_3000000);
00059     
00060     // the locate command tells the screen where to place the text.
00061     uLCD.filled_rectangle(11,54,117,99,BLACK);
00062     uLCD.rectangle(10, 55, 118, 100, WHITE);
00063     uLCD.locate(6,8);
00064     uLCD.puts("1 Player");
00065     uLCD.locate(6,9);
00066     uLCD.puts("2 Player");
00067     uLCD.locate(6,10);
00068     uLCD.puts("Settings");
00069     uLCD.color(RED);
00070     uLCD.circle(25, 67, 1, RED);
00071     uLCD.circle(25, 75, 1, RED);
00072     uLCD.circle(25, 83, 1, RED);
00073     uLCD.circle(25, 67, 2, WHITE);
00074     uLCD.text_width(2);
00075     uLCD.text_height(2);
00076     uLCD.locate(1,1);
00077     uLCD.puts("DUELING");
00078     uLCD.locate(2,2);
00079     uLCD.text_bold(TEXTBOLD);
00080     uLCD.puts("TANKS");
00081     uLCD.color(WHITE);
00082     //playSound("/sd/wavfiles/Dueling Banjo.wav");
00083     
00084     
00085  while(true){ 
00086     if (menu==1){  
00087        if (!pb_d){ 
00088           uLCD.circle(25, 67, 2, BLACK);
00089           uLCD.circle(25, 75, 2, WHITE);
00090           uLCD.circle(25, 83, 2, BLACK);
00091           wait(.25);
00092           menu=2;
00093        }
00094        if (!pb_u){  
00095           uLCD.circle(25, 67, 2, BLACK);
00096           uLCD.circle(25, 75, 2, BLACK);
00097           uLCD.circle(25, 83, 2, WHITE);
00098           wait(.25);
00099           menu=3;
00100        }
00101        if (!pb_r){
00102          return SINGLE_PLAYER;
00103        }
00104     }
00105     if (menu==2){
00106        if (!pb_d){  
00107           uLCD.circle(25, 67, 2, BLACK);
00108           uLCD.circle(25, 75, 2, BLACK);
00109           uLCD.circle(25, 83, 2, WHITE);
00110           wait(.25);
00111           menu=3;
00112        } 
00113        if (!pb_u){
00114           uLCD.circle(25, 67, 2, WHITE);
00115           uLCD.circle(25, 75, 2, BLACK);
00116           uLCD.circle(25, 83, 2, BLACK);
00117           wait(.25);
00118           menu=1;
00119        }
00120        if(!pb_r){
00121          return MULTI_PLAYER;
00122        }
00123     }
00124     if (menu==3){
00125        if (!pb_d){  
00126           uLCD.circle(25, 67, 2, WHITE);
00127           uLCD.circle(25, 75, 2, BLACK);
00128           uLCD.circle(25, 83, 2, BLACK);
00129           wait(.25);
00130           menu=1;
00131        } 
00132        if (!pb_u){
00133           uLCD.circle(25, 67, 2, BLACK);
00134           uLCD.circle(25, 75, 2, WHITE);
00135           uLCD.circle(25, 83, 2, BLACK);
00136           wait(.25);
00137           menu=2;
00138        }
00139        if(!pb_r){
00140            //SETTINGS();
00141            }
00142     }
00143 }
00144 }
00145 
00146     //wait(10);        // Eventually you can take this out.
00147   
00148     // Eventually you should return SINGLE_PLAYER or MULTI_PLAYER
00149     // depending on the user's choice.
00150     //return SINGLE_PLAYER;       
00151 
00152 // Initialize the world map. I've provided a basic map here,
00153 // but as part of the assignment you must create more
00154 // interesting map(s).
00155 // Note that calls to sync.function() will run function()
00156 // on both players' LCDs (assuming you are in multiplayer mode).
00157 // In single player mode, only your lcd will be modified. (Makes sense, right?)
00158 void map_init() {
00159     
00160     // Fill the entire screen with sky blue.
00161     sync.background_color(SKY_COLOR);
00162     //uLCD.media_init();
00163     //uLCD.set_byte_address(0x00000, 0x3B5001);
00164     //uLCD.display_image(64,64);
00165     
00166     // Call the clear screen function to force the LCD to redraw the screen
00167     // with the new background color.
00168     sync.cls();
00169     
00170     // Draw the ground in green.
00171     sync.filled_rectangle(0,0,128,20, GND_COLOR);
00172     sync.line(0,20,128,20, TREE);
00173     
00174     // Draw some obstacles. They don't have to be black, 
00175     // but they shouldn't be the same color as the sky or your tanks.
00176     // Get creative here. You could use brown and grey to draw a mountain
00177     // or something cool like that.
00178     //sync.triangle(0, 20, 128, 20, 85, 55, BROWN);    
00179     sync.rectangle(53, 17, 76, 78, BLACK);
00180     sync.filled_rectangle(54, 17, 75, 77, WHITE);
00181     sync.filled_rectangle(65, 17, 69, 27, BLACK);
00182     sync.filled_rectangle(67, 32, 71, 37, BLACK);
00183     sync.filled_rectangle(67, 41, 71, 46, BLACK);
00184     sync.filled_rectangle(67, 50, 71, 55, BLACK);
00185     sync.filled_rectangle(67, 59, 71, 64, BLACK);
00186     sync.filled_rectangle(67, 68, 71, 73, BLACK);
00187     sync.filled_rectangle(58, 32, 62, 37, BLACK);
00188     sync.filled_rectangle(58, 41, 62, 46, BLACK);
00189     sync.filled_rectangle(58, 50, 62, 55, BLACK);
00190     sync.filled_rectangle(58, 59, 62, 64, BLACK);
00191     sync.filled_rectangle(58, 68, 62, 73, BLACK);
00192     sync.filled_rectangle(52, 16, 54, 20, BROWN);
00193     sync.update();
00194     sync.filled_circle(53, 26, 6, TREE);
00195     sync.filled_circle(78, 19, 4, TREE);
00196     sync.filled_circle(1, 12, 7, TREE);
00197     sync.filled_circle(6, 10, 4, TREE);
00198     sync.filled_circle(9, 10, 4, TREE);
00199     sync.filled_circle(14, 10, 2, TREE);
00200     sync.filled_circle(100, 12, 7, TREE);
00201     sync.circle(100, 12, 7, SHRUB);
00202     sync.filled_circle(106, 10, 4, TREE);
00203     sync.filled_circle(90, 10, 4, TREE);
00204     sync.filled_circle(110, 10, 2, TREE);
00205     sync.circle(1, 12, 7, SHRUB);
00206     sync.filled_circle(115, 115, 10, YELLOW);
00207     sync.circle(115, 115, 10, ORANGE);
00208     
00209     // Before you write text on the screens, tell the LCD where to put it.
00210     sync.locate(6,15);
00211     
00212     // Set the text background color to match the sky. Just for looks.
00213     sync.textbackground_color(SKY_COLOR);
00214     
00215     // Display the game title.
00216     char title[] = "ROUND 1\n  GO!";
00217     sync.puts(title, sizeof(title));
00218     
00219     // Flush the draw buffer and execute all the accumulated draw commands.
00220     sync.update();  
00221     playSound("/sd/wavfiles/boxingbell.wav");
00222     sync.filled_rectangle(25, 122, 100, 128, SKY_COLOR);
00223     sync.update();
00224     wait(1);
00225     sync.filled_rectangle(45, 114, 75, 122, SKY_COLOR);       
00226     
00227 }
00228 
00229 // Initialize the game hardware. 
00230 // Call game_menu to find out which mode to play the game in (Single- or Multi-Player)
00231 // Initialize the game synchronizer.
00232 void game_init(void) {
00233     
00234     led1 = 0; led2 = 0; led3 = 0; led4 = 0;
00235     
00236     pb_u.mode(PullUp);
00237     pb_r.mode(PullUp); 
00238     pb_d.mode(PullUp);    
00239     pb_l.mode(PullUp);
00240     
00241     pc.printf("\033[2J\033[0;0H");              // Clear the terminal screen.
00242     pc.printf("I'm alive! Player 1\n");         // Let us know you made it this far.
00243     int mode = game_menu();
00244     sync.init(&uLCD, &acc, &pb_u, &pb_r, &pb_d, &pb_l, mode); // Connect to the other player.
00245     map_init();
00246     pc.printf("Initialized...\n");              // Let us know you finished initializing.
00247 }
00248 
00249 // Display some kind of game over screen which lets us know who won.
00250 // Play a cool sound!
00251 void game_over();
00252     
00253 
00254 int main (void) {
00255     
00256     int* p1_buttons;
00257     int* p2_buttons;
00258     int p1lives=2;
00259     int p2lives=2;
00260     int i=0;
00261     
00262     float ax1, ay1, az1;
00263     float ax2, ay2, az2;
00264     ax1 = 0.0f;
00265     ay1 = 0.0f;
00266     ax2 = 0.0f;
00267     ay2 = 0.0f;
00268     
00269     game_init();
00270     
00271     // Create your tanks.
00272     Tank t1(4, 22, 16, 6, TANK_RED);            // (min_x, min_y, width, height, color)
00273     Tank t2(108, 22, 16, 6, TANK_BLUE);         // (min_x, min_y, width, height, color)
00274     
00275     // For each tank, create a bullet.
00276     Bullet b1(&t1);
00277     Bullet b2(&t2);
00278     //Bouncy b3(&t1);
00279     //Bouncy b4(&t2);
00280     
00281     frame_timer.start();
00282 
00283     // Your code starts here...
00284     while(true) {
00285         t1y=t1.min_y();
00286         t2y=t2.min_y();
00287         t2minpixelcolor=sync.read_pixel(t2.min_x()-5,t2y);
00288         t1maxpixelcolor=sync.read_pixel(t1.max_x()+1,t1y);
00289         // Get a pointer to the buttons for both sides.
00290         // From now on, you can access the buttons for player x using
00291         //
00292         // px_buttons[U_BUTTON] 
00293         // px_buttons[R_BUTTON] 
00294         // px_buttons[D_BUTTON] 
00295         // px_buttons[L_BUTTON]
00296         
00297         p1_buttons = sync.get_p1_buttons();
00298         p2_buttons = sync.get_p2_buttons();
00299         
00300         led1 = p1_buttons[0] ^ p2_buttons[0];
00301         led2 = p1_buttons[1] ^ p2_buttons[1];
00302         led3 = p1_buttons[2] ^ p2_buttons[2];
00303         led4 = p1_buttons[3] ^ p2_buttons[3];
00304 
00305         // Get the accelerometer values.
00306         sync.get_p1_accel_data(&ax1, &ay1, &az1);
00307         sync.get_p2_accel_data(&ax2, &ay2, &az2); 
00308         //ay1 = joystick.xAxis()*2 -1;
00309         ax1 = -joystick.yAxis();
00310         //ay2 = joystick.xAxis()*2 -1;
00311         ax2 = -joystick.yAxis();
00312         
00313         int pix1=sync.read_pixel(64,30);
00314         int pix2=sync.read_pixel(64,39);
00315         int pix3=sync.read_pixel(64,48);
00316         int pix4=sync.read_pixel(64,57);
00317         int pix5=sync.read_pixel(64,66);
00318         int j;
00319         
00320         //Building collapse animation:
00321         if(sync.pixel_eq(pix5,SKY_COLOR)==1){
00322             
00323             //If highest test point is hit, do the following for loop.
00324             for(j=0;j<10;j++){
00325                 
00326             sync.filled_rectangle(53, 78-j, 76, 80-j, SKY_COLOR);
00327             sync.line(53, 77-j, 76, 79-j, BLACK);
00328             sync.filled_rectangle(54, 74-j, 75, 77-j, WHITE);
00329             sync.filled_circle(53, 64,6, LT_GREY);
00330             sync.filled_circle(76, 64,5, LT_GREY);
00331             sync.filled_circle(56, 64,4, MED_GREY);
00332             sync.filled_circle(73, 64,4, DRK_GREY);
00333             //playSound("/sd/wavfiles/colapse.wav");
00334             haptics.play_waveform(1);
00335             sync.filled_circle(70, 64,5, MED_GREY);
00336             sync.filled_circle(64, 64,6, LT_GREY);
00337             
00338             sync.update();
00339             sync.filled_circle(52,65,6,SKY_COLOR);
00340             sync.filled_circle(77,65,6,SKY_COLOR);
00341             sync.filled_circle(52,65,6,SKY_COLOR);
00342             sync.filled_circle(55,65,6,SKY_COLOR);
00343             sync.filled_circle(70,69,6,SKY_COLOR);
00344             sync.filled_circle(76,67,6,SKY_COLOR);
00345             sync.filled_circle(65,65,6,SKY_COLOR);
00346             sync.filled_circle(64,66,3,PIX);
00347             //playSound("/sd/wavfiles/colapse.wav");
00348             haptics.play_waveform(1);
00349             }
00350  }
00351         if(sync.pixel_eq(pix4,SKY_COLOR)==1){
00352             
00353             //If the second highest point is hit, do the following for loop.
00354             for(j=0;j<20;j++){
00355                 
00356             sync.filled_rectangle(53, 78-j, 76, 80-j, SKY_COLOR);
00357             sync.line(53, 77-j, 76, 79-j, BLACK);
00358             sync.filled_rectangle(54, 74-j, 75, 77-j, WHITE);
00359             sync.filled_circle(53, 54,6, LT_GREY);
00360             sync.filled_circle(76, 54,5, LT_GREY);
00361             sync.filled_circle(56, 54,4, MED_GREY);
00362             sync.filled_circle(73, 54,4, DRK_GREY);
00363             //playSound("/sd/wavfiles/colapse.wav");
00364             haptics.play_waveform(1);
00365             sync.filled_circle(70, 54,5, MED_GREY);
00366             sync.filled_circle(64, 54,6, LT_GREY);
00367             
00368             sync.update();
00369             sync.filled_circle(52,55,6,SKY_COLOR);
00370             sync.filled_circle(77,55,6,SKY_COLOR);
00371             sync.filled_circle(52,55,6,SKY_COLOR);
00372             sync.filled_circle(60,55,6,SKY_COLOR);
00373             sync.filled_circle(70,59,6,SKY_COLOR);
00374             sync.filled_circle(76,57,6,SKY_COLOR);
00375             sync.filled_circle(65,55,6,SKY_COLOR);
00376             sync.filled_circle(64,56,3,PIX);
00377             sync.filled_circle(64,66,3,PIX);
00378             //playSound("/sd/wavfiles/colapse.wav");
00379             haptics.play_waveform(1);
00380             }
00381  }
00382         if(sync.pixel_eq(pix3,SKY_COLOR)==1){
00383             
00384             //If the third highest point is hit, do the following for loop.
00385             for(j=0;j<30;j++){
00386                 
00387             sync.filled_rectangle(53, 78-j, 76, 80-j, SKY_COLOR);
00388             sync.line(53, 77-j, 76, 79-j, BLACK);
00389             sync.filled_rectangle(54, 74-j, 75, 77-j, WHITE);
00390             sync.filled_circle(53, 44,6, LT_GREY);
00391             sync.filled_circle(76, 44,5, LT_GREY);
00392             haptics.play_waveform(1);
00393             sync.update();
00394             sync.filled_circle(56, 44,4, MED_GREY);
00395             sync.filled_circle(73, 44,4, DRK_GREY);
00396             //playSound("/sd/wavfiles/colapse.wav");
00397             sync.filled_circle(70, 44,5, MED_GREY);
00398             sync.filled_circle(64, 44,6, LT_GREY);
00399             
00400             sync.update();
00401             sync.filled_circle(52,45,6,SKY_COLOR);
00402             sync.filled_circle(77,45,6,SKY_COLOR);
00403             sync.filled_circle(52,45,6,SKY_COLOR);
00404             sync.filled_circle(60,45,6,SKY_COLOR);
00405             //playSound("/sd/wavfiles/colapse.wav");
00406             sync.filled_circle(70,49,6,SKY_COLOR);
00407             sync.filled_circle(76,47,6,SKY_COLOR);
00408             sync.filled_circle(65,45,6,SKY_COLOR);
00409             sync.update();
00410             haptics.play_waveform(1);
00411             sync.filled_circle(64,46,3,PIX);
00412             sync.filled_circle(64,56,3,PIX);
00413             sync.filled_circle(64,66,3,PIX);
00414             //wait(.5);
00415             haptics.play_waveform(1);
00416             }
00417  }
00418       if(sync.pixel_eq(pix2,SKY_COLOR)==1){
00419             
00420             //If the lowest test point is hit, do the following for loop.
00421             for(j=0;j<40;j++){
00422                 
00423             sync.filled_rectangle(53, 78-j, 76, 80-j, SKY_COLOR);
00424             sync.line(53, 77-j, 76, 79-j, BLACK);
00425             sync.filled_rectangle(54, 74-j, 75, 77-j, WHITE);
00426             sync.filled_circle(53, 34,6, LT_GREY);
00427             sync.filled_circle(76, 34,5, LT_GREY);
00428             sync.update();
00429             sync.filled_circle(56, 34,4, MED_GREY);
00430             sync.filled_circle(73, 34,4, DRK_GREY);
00431             sync.filled_circle(70, 34,5, MED_GREY);
00432             sync.filled_circle(64, 34,6, LT_GREY);
00433             
00434             
00435             sync.update();
00436             sync.filled_circle(52,35,6,SKY_COLOR);
00437             sync.filled_circle(77,35,6,SKY_COLOR);
00438             sync.filled_circle(52,35,6,SKY_COLOR);
00439             sync.filled_circle(60,35,6,SKY_COLOR);
00440             sync.filled_circle(70,39,6,SKY_COLOR);
00441             sync.filled_circle(76,37,6,SKY_COLOR);
00442             sync.filled_circle(65,35,6,SKY_COLOR);
00443             haptics.play_waveform(1);
00444             sync.update();
00445             sync.filled_circle(64,36,3,PIX);
00446             sync.filled_circle(64,46,3,PIX);
00447             sync.filled_circle(64,56,3,PIX);
00448             sync.filled_circle(64,66,3,PIX);
00449             haptics.play_waveform(1);
00450             }
00451  }
00452         if(whose_turn == PLAYER1) {
00453             
00454             //Draw the initial lives for player one and light up tank color to indicate turn
00455             sync.rectangle(45, 2, 84, 7, BLACK);
00456             if(p1lives==0){
00457             sync.filled_circle(4, 4, 2, TANK_RED);
00458             }
00459             if(p1lives==1){
00460             sync.filled_circle(10, 4, 2, TANK_RED);
00461             sync.filled_circle(4, 4, 2, TANK_RED);
00462             }
00463             if(p1lives==2){
00464             sync.filled_circle(10, 4, 2, TANK_RED);
00465             sync.filled_circle(4, 4, 2, TANK_RED);
00466             sync.filled_circle(16, 4, 2, TANK_RED);
00467             }
00468             //show light blue for player two showing its player ones turn.
00469             if(p2lives==0){
00470             sync.filled_circle(124, 4, 2, LT_BLUE);
00471             }
00472             if(p2lives==1){
00473             sync.filled_circle(118, 4, 2, LT_BLUE);
00474             sync.filled_circle(124, 4, 2, LT_BLUE);
00475             }
00476             if(p2lives==2){
00477             sync.filled_circle(118, 4, 2, LT_BLUE);
00478             sync.filled_circle(112, 4, 2, LT_BLUE);
00479             sync.filled_circle(124, 4, 2, LT_BLUE);
00480             }
00481 
00482              
00483             // Accelerometer example
00484             if(ax1 <  -ACC_THRESHOLD && sync.pixel_eq(t1maxpixelcolor,SKY_COLOR)==1) { 
00485                 // Move the tank to the right if the accelerometer is tipped far enough to the right.
00486                  t1.reposition(+1, 0, 0);         
00487             }
00488             if(ax1 <  -1.5*ACC_THRESHOLD && sync.pixel_eq(t1maxpixelcolor,SKY_COLOR)==1) { 
00489                 // Move the tank to the right if the accelerometer is tipped far enough to the right.
00490                  t1.reposition(+3, 0, 0);  
00491             }
00492             if(ax1 > ACC_THRESHOLD && t1.min_x()>0) {
00493                 // Move the tank to the left if the accelerometer is tipped far enough to the left.
00494                  t1.reposition(-1, 0, 0);
00495             }
00496             if(ax1 > 1.5*ACC_THRESHOLD && t1.min_x()>0) {
00497                 
00498                 // Move the tank to the left if the accelerometer is tipped far enough to the left.
00499                  t1.reposition(-3, 0, 0);
00500             }
00501             if(ay1 > ACC_THRESHOLD) {
00502                 //Move the barrel up.
00503                  t1.reposition(0, 0, PI/24);
00504             }
00505             if(ay1 < -ACC_THRESHOLD) {
00506                 //Move the barrel down.
00507                  t1.reposition(0, 0, -PI/24);
00508             }
00509             
00510             
00511             // Button example
00512             //if(p1_buttons[L_BUTTON]) { 
00513             //    b1.shoot(); 
00514             //}
00515             // hold down left button to power up the power bar.
00516             if(p1_buttons[L_BUTTON]){
00517                 b1.speed+=3;
00518                 i+=2;
00519                 sync.filled_rectangle(46, 3, 45+i, 6, TANK_RED);
00520                 haptics.play_waveform(1);
00521                 sync.update();
00522                 if(i>=38){
00523                    i=37;
00524                    b1.speed=56;
00525                    }  
00526             }
00527             else{
00528                 i=0;
00529             }
00530             //Press right button to fire.  
00531             if(p1_buttons[R_BUTTON]){
00532                 b1.shoot();
00533                 playSound("/sd/wavfiles/Fire.wav");
00534                 haptics.play_waveform(1);
00535                 b1.speed=0;
00536                 sync.filled_rectangle(46, 3, 83, 6, GND_COLOR);
00537                 sync.rectangle(45, 2, 84, 7, BLACK);
00538             
00539 
00540             }
00541             //if(p1_buttons[U_BUTTON]){
00542                // sync.filled_circle(24,9,2,BLACK);
00543                // bouncy=1;
00544                 
00545             
00546             //}
00547             float dt = frame_timer.read();
00548             int intersection_code = b1.time_step(dt); 
00549             
00550             if(intersection_code != BULLET_NO_COLLISION || intersection_code == BULLET_OFF_SCREEN) { 
00551                 pc.printf("Now it's P2's turn!\n");
00552                 whose_turn = PLAYER2;
00553             }
00554             
00555             // If you shot yourself, you lost a life.
00556             if(sync.pixel_eq(intersection_code, t1.tank_color)) {
00557                 if(p1lives==0){ 
00558                    sync.filled_circle(4, 4, 2, GND_COLOR);   
00559                    sync.update();  // Is this necessary?
00560                    winner = PLAYER2;
00561                    //game_over();
00562                    break;
00563                 } 
00564                 else if(p1lives!=0){
00565                     
00566                     if(p1lives==1){
00567                        p1lives+=-1;
00568                        sync.filled_circle(10, 4, 2, GND_COLOR);
00569                     }
00570                     if(p1lives==2){
00571                        p1lives+=-1;
00572                        sync.update();
00573                        sync.filled_circle(16, 4, 2, GND_COLOR);
00574                        
00575                     }
00576                 }
00577             }
00578             
00579             
00580             // If you shot the other guy, you took one life!
00581             if(sync.pixel_eq(intersection_code, t2.tank_color)) { 
00582                 if(p2lives==0){ 
00583                    sync.filled_circle(124, 4, 2, GND_COLOR);   
00584                    sync.update();  // Is this necessary?
00585                    winner = PLAYER1;
00586                    break;
00587                    }
00588                 else if(p2lives!=0){
00589                     
00590                      if(p2lives==1){
00591                         p2lives+=-1;
00592                         sync.filled_circle(118, 4, 2, GND_COLOR);
00593                      } 
00594                      if(p2lives==2){
00595                         p2lives+=-1;
00596                         sync.update();
00597                         sync.filled_circle(112, 4, 2, GND_COLOR);
00598                      }
00599                      
00600                 }
00601             }
00602         }
00603          else if(whose_turn == PLAYER2) {
00604            
00605             // Show player two's lives in tank color and player ones in light red.
00606             if(p2lives==0){
00607             sync.filled_circle(124, 4, 2, TANK_BLUE);
00608             }
00609             if(p2lives==1){
00610             sync.filled_circle(118, 4, 2, TANK_BLUE);
00611             sync.filled_circle(124, 4, 2, TANK_BLUE);
00612             }
00613             if(p2lives==2){
00614             sync.filled_circle(118, 4, 2, TANK_BLUE);
00615             sync.filled_circle(112, 4, 2, TANK_BLUE);
00616             sync.filled_circle(124, 4, 2, TANK_BLUE);
00617             }
00618             if(p1lives==0){
00619             sync.filled_circle(4, 4, 2, LT_RED);
00620             }
00621             if(p1lives==1){
00622             sync.filled_circle(10, 4, 2, LT_RED);
00623             sync.filled_circle(4, 4, 2, LT_RED);
00624             }
00625             if(p1lives==2){
00626             sync.filled_circle(10, 4, 2, LT_RED);
00627             sync.filled_circle(4, 4, 2, LT_RED);
00628             sync.filled_circle(16, 4, 2, LT_RED);
00629             }
00630             
00631             // Accelerometer example
00632             if(sync.play_mode==SINGLE_PLAYER){
00633             if(ax1 <  -ACC_THRESHOLD && t2.max_x()<127) { 
00634                 // Move the tank to the right if the accelerometer is tipped far enough to the right.
00635                  t2.reposition(+1, 0, 0);         
00636             }
00637             if(ax1 <  -1.5*ACC_THRESHOLD && t2.max_x()<127) { 
00638                 // Move the tank to the right if the accelerometer is tipped far enough to the right.
00639                  t2.reposition(+3, 0, 0);  
00640             }
00641             if(ax1 > ACC_THRESHOLD && sync.pixel_eq(t2minpixelcolor,SKY_COLOR)==1) {
00642                 // Move the tank to the left if the accelerometer is tipped far enough to the left.
00643                  t2.reposition(-1, 0, 0);
00644             }
00645             if(ax1 > 1.5*ACC_THRESHOLD && sync.pixel_eq(t2minpixelcolor,SKY_COLOR)==1) {
00646                 // Move the tank to the left if the accelerometer is tipped far enough to the left.
00647                  t2.reposition(-3, 0, 0);
00648             }
00649             if(ay1 > ACC_THRESHOLD) {
00650                 //Move the barrel up.
00651                  t2.reposition(0, 0, PI/24);
00652             }
00653             if(ay1 < -ACC_THRESHOLD) {
00654                 //Move the barrel down.
00655                  t2.reposition(0, 0, -PI/24);
00656             }
00657             }
00658             
00659             if(sync.play_mode==MULTI_PLAYER){
00660             if(ax2 <  -ACC_THRESHOLD && t2.max_x()<127) { 
00661                 // Move the tank to the right if the accelerometer is tipped far enough to the right.
00662                  t2.reposition(+1, 0, 0);         
00663             }
00664             if(ax2 <  -1.5*ACC_THRESHOLD && t2.max_x()<127) { 
00665                 // Move the tank to the right if the accelerometer is tipped far enough to the right.
00666                  t2.reposition(+3, 0, 0);  
00667             }
00668             if(ax2 > ACC_THRESHOLD && sync.pixel_eq(t2minpixelcolor,SKY_COLOR)==1) {
00669                 // Move the tank to the left if the accelerometer is tipped far enough to the left.
00670                  t2.reposition(-1, 0, 0);
00671             }
00672             if(ax2 > 1.5*ACC_THRESHOLD && sync.pixel_eq(t2minpixelcolor,SKY_COLOR)==1) {
00673                 // Move the tank to the left if the accelerometer is tipped far enough to the left.
00674                  t2.reposition(-3, 0, 0);
00675             }
00676             if(ay2 > ACC_THRESHOLD) {
00677                 //Move the barrel up.
00678                  t2.reposition(0, 0, PI/24);
00679             }
00680             if(ay2 < -ACC_THRESHOLD) {
00681                 //Move the barrel down.
00682                  t2.reposition(0, 0, -PI/24);
00683             }
00684             }
00685             //If in single player mode charge the power bar. 
00686             if(sync.play_mode==SINGLE_PLAYER){
00687             if(p1_buttons[L_BUTTON]){
00688                 b2.speed+=3;
00689                 i+=2;
00690                 sync.filled_rectangle(83, 3, 85-i, 6, TANK_BLUE);
00691                 haptics.play_waveform(1);
00692                 if(i>=38){
00693                    i=37;
00694                    b2.speed=56;
00695                    }  
00696             }
00697             else{
00698                 i=0;
00699             }
00700             // fire tank.
00701             if(p1_buttons[R_BUTTON]){
00702                 b2.shoot();
00703                 playSound("/sd/wavfiles/Fire.wav");
00704                 haptics.play_waveform(1);
00705                 
00706                 b2.speed=0;
00707                 sync.filled_rectangle(46, 3, 83, 6, GND_COLOR);
00708                 sync.rectangle(45, 2, 84, 7, BLACK);
00709                 }
00710             }
00711             if(sync.play_mode==MULTI_PLAYER){
00712             if(p2_buttons[L_BUTTON]){
00713                 b2.speed+=3;
00714                 i+=2;
00715                 sync.filled_rectangle(83, 3, 85-i, 6, TANK_BLUE);
00716                 sync.update();
00717                 if(i>=38){
00718                    i=37;
00719                    b2.speed=56;
00720                    }  
00721             }
00722             else{
00723                 i=0;
00724             }
00725             if(p2_buttons[R_BUTTON]){
00726                 b2.shoot();
00727                 playSound("/sd/wavfiles/Fire.wav");
00728                 haptics.play_waveform(1);
00729                 b2.speed=0;
00730                 sync.filled_rectangle(46, 3, 83, 6, GND_COLOR);
00731                 sync.rectangle(45, 2, 84, 7, BLACK);
00732 
00733             }
00734             }
00735             
00736             float dt = frame_timer.read();
00737             int intersection_code = b2.time_step(dt); 
00738             
00739             if(intersection_code != BULLET_NO_COLLISION || intersection_code == BULLET_OFF_SCREEN) { 
00740                 pc.printf("Now it's P2's turn!\n");
00741                 whose_turn = PLAYER1;
00742             }
00743             
00744             // If you shot yourself, you lost a life.
00745             if(sync.pixel_eq(intersection_code, t1.tank_color)) { 
00746                 if(p1lives==0){    
00747                     sync.update();  // Is this necessary?
00748                     winner = PLAYER2;
00749                     sync.filled_circle(4, 4, 2, GND_COLOR);
00750                     break;
00751                 }
00752                 else if(p1lives!=0){
00753     
00754                 if(p1lives==1){
00755                     p1lives+=-1;
00756                     sync.filled_circle(10, 4, 2, GND_COLOR);
00757                 } 
00758                 if(p1lives==2){
00759                     p1lives+=-1;
00760                     sync.filled_circle(16, 4, 2, GND_COLOR);
00761                     sync.update();
00762                 }
00763                 }
00764             
00765             // If you shot the other guy, took a life!
00766             if(sync.pixel_eq(intersection_code, t2.tank_color)) { 
00767                 if(p2lives==0){    
00768                 sync.update();  // Is this necessary?
00769                 winner = PLAYER1;
00770                 sync.filled_circle(124, 4, 2, GND_COLOR);
00771                 break;
00772                 }
00773                 else if(p2lives!=0){
00774                 
00775                 if(p2lives==1){
00776                     p2lives+=-1;
00777                     sync.filled_circle(118, 4, 2, GND_COLOR);
00778                 } 
00779                 if(p2lives==2){
00780                     p2lives+=-1;
00781                     sync.filled_circle(112, 4, 2, GND_COLOR);
00782                     sync.update();
00783                 }
00784                 }
00785             }
00786         }
00787             
00788 
00789         }
00790 
00791         frame_timer.reset();
00792         sync.update();     
00793     } 
00794     
00795     game_over();
00796   
00797 }
00798 void game_over() {
00799     
00800     if(winner==PLAYER1){
00801         
00802         char status[]= "GAME OVER!";
00803         char player[]="Player 1 \n  wins!";
00804         char play[]="Play again? \n\n Yes   No";
00805         sync.rectangle(13,56,115,99,BLACK);
00806         sync.filled_rectangle(15,58,113,97,TANK_RED);
00807         sync.textbackground_color(TANK_RED);
00808         sync.locate(4,10);
00809         sync.puts(status, sizeof(status));
00810         sync.update();
00811         wait(2);
00812         sync.filled_rectangle(15,62,113,97,TANK_RED);
00813         sync.locate(5,10);
00814         sync.puts(player, sizeof(player));
00815         sync.update();
00816         playSound("/sd/wavfiles/cheering.wav");
00817         sync.filled_rectangle(15,62,113,97,TANK_RED);
00818         sync.locate(4,10);
00819         sync.puts(play, sizeof(play));
00820         sync.update();
00821         while(2){
00822         if(!pb_l){
00823             sync.rectangle(33,63,60,76,WHITE);
00824             sync.update(); 
00825             wait(2);
00826             sync.cls();
00827             sync.textbackground_color(BLACK);
00828             sync.filled_rectangle(0,0,128,128,BLACK);
00829             sync.update();
00830             game_init();
00831             }
00832         else if(!pb_r){
00833             sync.rectangle(68,63,95,76,WHITE);
00834             sync.update(); 
00835             wait(2);
00836             sync.cls();
00837             sync.textbackground_color(BLACK);
00838             sync.filled_rectangle(0,0,128,128,BLACK);
00839             sync.update();
00840             main();
00841             }
00842             }
00843             
00844     }
00845     
00846     if(winner==PLAYER2){
00847         
00848         char status[]= "GAME OVER!";
00849         char player[]="Player 2 \n  wins!";
00850         char play[]="Play again? \n\n Yes   No";
00851         sync.rectangle(13,56,115,99,BLACK);
00852         sync.filled_rectangle(15,58,113,97,TANK_BLUE);
00853         sync.textbackground_color(TANK_BLUE);
00854         sync.locate(4,10);
00855         sync.puts(status, sizeof(status));
00856         sync.update();
00857         wait(2);
00858         sync.filled_rectangle(15,62,113,97,TANK_BLUE);
00859         sync.locate(5,10);
00860         sync.puts(player, sizeof(player));
00861         sync.update();
00862         playSound("/sd/wavfiles/cheering.wav");
00863         sync.filled_rectangle(15,62,113,97,TANK_BLUE);
00864         sync.locate(4,10);
00865         sync.puts(play, sizeof(play));
00866         sync.update();
00867         while(true){
00868         if(!pb_l){
00869             sync.rectangle(33,63,60,76,WHITE);
00870             sync.update(); 
00871             wait(2);
00872             sync.cls();
00873             sync.textbackground_color(BLACK);
00874             sync.filled_rectangle(0,0,128,128,BLACK);
00875             sync.update();
00876             game_init();
00877             }
00878         else if(!pb_r){
00879             sync.rectangle(68,63,95,76,WHITE);
00880             sync.update(); 
00881             wait(2);
00882             sync.cls();
00883             sync.textbackground_color(BLACK);
00884             sync.filled_rectangle(0,0,128,128,BLACK);
00885             sync.update();
00886             main();
00887             }
00888             }
00889     }
00890 }