The classic dueling tanks game for mbed.

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

Committer:
macenzofan
Date:
Mon Mar 13 00:15:36 2017 +0000
Revision:
28:8dbb85f35be6
Parent:
27:bd55ab4d137c
Classic dueling tanks game for bed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jford38 17:7bc7127782e4 1 #include "uLCD_4DGL.h"
jford38 15:4b27a3a95772 2 #include "bullet.h"
jford38 15:4b27a3a95772 3 #include "game_synchronizer.h"
jford38 27:bd55ab4d137c 4 #include "globals.h"
jford38 15:4b27a3a95772 5 #include "math.h"
macenzofan 28:8dbb85f35be6 6 #include "SDFileSystem.h"
macenzofan 28:8dbb85f35be6 7 #include "wave_player.h"
macenzofan 28:8dbb85f35be6 8 #include "playSound.h"
jford38 15:4b27a3a95772 9
jford38 15:4b27a3a95772 10 extern Game_Synchronizer sync;
macenzofan 28:8dbb85f35be6 11 float et = 0;
macenzofan 28:8dbb85f35be6 12 int pixelcolor;
macenzofan 28:8dbb85f35be6 13 //int bouncy
jford38 15:4b27a3a95772 14
jford38 22:3c68eea5a609 15 // Initialize the bullet. Don't have to do much here.
jford38 23:77049670cae6 16 // Keep a pointer to this bullet's tank.
jford38 23:77049670cae6 17 // Set the speed, and default the bullet to not in_flight.
jford38 15:4b27a3a95772 18 Bullet::Bullet(Tank* t) {
jford38 15:4b27a3a95772 19 tank = t;
macenzofan 28:8dbb85f35be6 20 speed = 0;
jford38 15:4b27a3a95772 21 in_flight = false;
jford38 15:4b27a3a95772 22 }
macenzofan 28:8dbb85f35be6 23 //Bullet::Bouncy(Tank* t) {
macenzofan 28:8dbb85f35be6 24 //tank=t;
macenzofan 28:8dbb85f35be6 25 //speed=0;
macenzofan 28:8dbb85f35be6 26 //in_flight=false;
macenzofan 28:8dbb85f35be6 27 //}
jford38 23:77049670cae6 28 // If in_flight, do nothing. Otherwise,
jford38 23:77049670cae6 29 // set the in_flight flag, and initialize values needed for
jford38 23:77049670cae6 30 // the trajectory calculations. (x0, y0), (vx0, vy0), time
jford38 23:77049670cae6 31 // Hint: tank->barrel_end(...) is useful here.
jford38 15:4b27a3a95772 32 void Bullet::shoot(void) {
macenzofan 28:8dbb85f35be6 33 if (in_flight==false){
macenzofan 28:8dbb85f35be6 34 in_flight = true;
macenzofan 28:8dbb85f35be6 35 tank->barrel_end(&x0,&y0);
macenzofan 28:8dbb85f35be6 36 vx0=cos(tank->barrel_theta)*speed;
macenzofan 28:8dbb85f35be6 37 vy0=sin(tank->barrel_theta)*speed;
macenzofan 28:8dbb85f35be6 38 x=x0;
macenzofan 28:8dbb85f35be6 39 y=y0;
macenzofan 28:8dbb85f35be6 40 sync.filled_circle(x,y,2,YELLOW);
macenzofan 28:8dbb85f35be6 41 sync.filled_circle(x,y,1,ORANGE);
macenzofan 28:8dbb85f35be6 42 wait(.5);
macenzofan 28:8dbb85f35be6 43 sync.filled_circle(x,y,2,SKY_COLOR);
macenzofan 28:8dbb85f35be6 44 sync.filled_circle(x,y,1,BLT_COLOR);
macenzofan 28:8dbb85f35be6 45 }
jford38 15:4b27a3a95772 46 }
jford38 15:4b27a3a95772 47
jford38 22:3c68eea5a609 48 // If the bullet is in flight, calculate its new position
jford38 22:3c68eea5a609 49 // after a time delta dt.
jford38 21:edfeb289b21f 50 void Bullet::update_position(float dt) {
macenzofan 28:8dbb85f35be6 51 if (in_flight == true){
macenzofan 28:8dbb85f35be6 52 x=floor(x0+(vx0*dt));
macenzofan 28:8dbb85f35be6 53 y=floor(y0+(vy0*dt)-(4.9*dt*dt));
macenzofan 28:8dbb85f35be6 54 sync.filled_circle(x,y,1,BLT_COLOR);
macenzofan 28:8dbb85f35be6 55 }
jford38 21:edfeb289b21f 56 }
jford38 21:edfeb289b21f 57
jford38 21:edfeb289b21f 58 int Bullet::time_step(float dt) {
jford38 22:3c68eea5a609 59 // If the bullet hasn't hit anything,
jford38 22:3c68eea5a609 60 // redraw the bullet at its new location.
macenzofan 28:8dbb85f35be6 61
macenzofan 28:8dbb85f35be6 62 if (in_flight == true){
macenzofan 28:8dbb85f35be6 63 sync.filled_circle(x,y,1,SKY_COLOR);
macenzofan 28:8dbb85f35be6 64 et += dt;
macenzofan 28:8dbb85f35be6 65 update_position(et);
macenzofan 28:8dbb85f35be6 66
jford38 23:77049670cae6 67 // If it has hit something (obstacle, tank, edge of the screen),
jford38 26:317310d660b2 68 // set the in_flight flag back to false, explode the nearby area,
jford38 26:317310d660b2 69 // and return one of the following codes.
macenzofan 28:8dbb85f35be6 70 //if(bouncy==1){
macenzofan 28:8dbb85f35be6 71 //if(x>128 || y<0 || y>128){
macenzofan 28:8dbb85f35be6 72
macenzofan 28:8dbb85f35be6 73 //}
macenzofan 28:8dbb85f35be6 74 //}
macenzofan 28:8dbb85f35be6 75 //else{
macenzofan 28:8dbb85f35be6 76 if(x>128 || y<0 || y>128){
macenzofan 28:8dbb85f35be6 77 in_flight=false;
macenzofan 28:8dbb85f35be6 78 et=0;
macenzofan 28:8dbb85f35be6 79 return BULLET_OFF_SCREEN;
macenzofan 28:8dbb85f35be6 80 }
macenzofan 28:8dbb85f35be6 81 //}
macenzofan 28:8dbb85f35be6 82
macenzofan 28:8dbb85f35be6 83 else{
macenzofan 28:8dbb85f35be6 84 pixelcolor=sync.read_pixel(x,y);
macenzofan 28:8dbb85f35be6 85
macenzofan 28:8dbb85f35be6 86 if(sync.pixel_eq(pixelcolor,BLACK)==1){
macenzofan 28:8dbb85f35be6 87 sync.filled_circle(x,y,6, ORANGE);
macenzofan 28:8dbb85f35be6 88 sync.filled_circle(x,y,4, YELLOW);
macenzofan 28:8dbb85f35be6 89 sync.filled_circle(x,y,2, TANK_RED);
macenzofan 28:8dbb85f35be6 90 playSound("/sd/wavfiles/Hit.wav");
macenzofan 28:8dbb85f35be6 91 sync.update();
macenzofan 28:8dbb85f35be6 92 sync.filled_circle(x,y,5, ORANGE);
macenzofan 28:8dbb85f35be6 93 sync.filled_circle(x,y,3, TANK_RED);
macenzofan 28:8dbb85f35be6 94 playSound("/sd/wavfiles/Hit.wav");
macenzofan 28:8dbb85f35be6 95 sync.update();
macenzofan 28:8dbb85f35be6 96 sync.filled_circle(x,y,6, SKY_COLOR);
macenzofan 28:8dbb85f35be6 97 in_flight=false;
macenzofan 28:8dbb85f35be6 98 et=0;
macenzofan 28:8dbb85f35be6 99 return pixelcolor;
macenzofan 28:8dbb85f35be6 100
macenzofan 28:8dbb85f35be6 101 }
macenzofan 28:8dbb85f35be6 102 if(sync.pixel_eq(pixelcolor,TREE)==1){
macenzofan 28:8dbb85f35be6 103 sync.filled_circle(x,y,6, ORANGE);
macenzofan 28:8dbb85f35be6 104 sync.filled_circle(x,y,4, YELLOW);
macenzofan 28:8dbb85f35be6 105 sync.filled_circle(x,y,2, TANK_RED);
macenzofan 28:8dbb85f35be6 106 playSound("/sd/wavfiles/Hit.wav");
macenzofan 28:8dbb85f35be6 107 sync.update();
macenzofan 28:8dbb85f35be6 108 sync.filled_circle(x,y,5, ORANGE);
macenzofan 28:8dbb85f35be6 109 sync.filled_circle(x,y,3, TANK_RED);
macenzofan 28:8dbb85f35be6 110 playSound("/sd/wavfiles/Hit.wav");
macenzofan 28:8dbb85f35be6 111 sync.update();
macenzofan 28:8dbb85f35be6 112 sync.filled_circle(x,y,6, SKY_COLOR);
macenzofan 28:8dbb85f35be6 113 in_flight=false;
macenzofan 28:8dbb85f35be6 114 et=0;
macenzofan 28:8dbb85f35be6 115 return pixelcolor;
macenzofan 28:8dbb85f35be6 116 }
macenzofan 28:8dbb85f35be6 117 if(sync.pixel_eq(pixelcolor,GND_COLOR)==1){
macenzofan 28:8dbb85f35be6 118 sync.filled_circle(x,y,6, ORANGE);
macenzofan 28:8dbb85f35be6 119 sync.filled_circle(x,y,4, YELLOW);
macenzofan 28:8dbb85f35be6 120 sync.filled_circle(x,y,2, TANK_RED);
macenzofan 28:8dbb85f35be6 121 playSound("/sd/wavfiles/Hit.wav");
macenzofan 28:8dbb85f35be6 122 sync.update();
macenzofan 28:8dbb85f35be6 123 sync.filled_circle(x,y,5, ORANGE);
macenzofan 28:8dbb85f35be6 124 sync.filled_circle(x,y,3, TANK_RED);
macenzofan 28:8dbb85f35be6 125 playSound("/sd/wavfiles/Hit.wav");
macenzofan 28:8dbb85f35be6 126 sync.update();
macenzofan 28:8dbb85f35be6 127 sync.filled_circle(x,y,6, SKY_COLOR);
macenzofan 28:8dbb85f35be6 128 sync.filled_circle(x,y,6,BROWN);
macenzofan 28:8dbb85f35be6 129 sync.filled_circle(x,y+2,6, SKY_COLOR);
macenzofan 28:8dbb85f35be6 130 in_flight=false;
macenzofan 28:8dbb85f35be6 131 et=0;
macenzofan 28:8dbb85f35be6 132 return pixelcolor;
macenzofan 28:8dbb85f35be6 133 }
macenzofan 28:8dbb85f35be6 134 if(sync.pixel_eq(pixelcolor,WHITE)==1){
macenzofan 28:8dbb85f35be6 135 sync.filled_circle(x,y,6, ORANGE);
macenzofan 28:8dbb85f35be6 136 sync.filled_circle(x,y,4, YELLOW);
macenzofan 28:8dbb85f35be6 137 sync.filled_circle(x,y,2, TANK_RED);
macenzofan 28:8dbb85f35be6 138 playSound("/sd/wavfiles/Hit.wav");
macenzofan 28:8dbb85f35be6 139 sync.update();
macenzofan 28:8dbb85f35be6 140 sync.filled_circle(x,y,5, ORANGE);
macenzofan 28:8dbb85f35be6 141 sync.filled_circle(x,y,3, TANK_RED);
macenzofan 28:8dbb85f35be6 142 playSound("/sd/wavfiles/Hit.wav");
macenzofan 28:8dbb85f35be6 143 sync.update();
macenzofan 28:8dbb85f35be6 144 sync.filled_circle(x,y,6, SKY_COLOR);
macenzofan 28:8dbb85f35be6 145 in_flight=false;
macenzofan 28:8dbb85f35be6 146 et=0;
macenzofan 28:8dbb85f35be6 147 return pixelcolor;
macenzofan 28:8dbb85f35be6 148 }
macenzofan 28:8dbb85f35be6 149 if(sync.pixel_eq(pixelcolor,TANK_RED)==1){
macenzofan 28:8dbb85f35be6 150 sync.filled_circle(x,y,6, ORANGE);
macenzofan 28:8dbb85f35be6 151 sync.filled_circle(x,y,4, YELLOW);
macenzofan 28:8dbb85f35be6 152 sync.filled_circle(x,y,2, TANK_RED);
macenzofan 28:8dbb85f35be6 153 playSound("/sd/wavfiles/Hit.wav");
macenzofan 28:8dbb85f35be6 154 sync.update();
macenzofan 28:8dbb85f35be6 155 sync.filled_circle(x,y,5, ORANGE);
macenzofan 28:8dbb85f35be6 156 sync.filled_circle(x,y,3, TANK_RED);
macenzofan 28:8dbb85f35be6 157 playSound("/sd/wavfiles/Hit.wav");
macenzofan 28:8dbb85f35be6 158 sync.update();
macenzofan 28:8dbb85f35be6 159 sync.filled_circle(x,y,6, SKY_COLOR);
macenzofan 28:8dbb85f35be6 160 in_flight=false;
macenzofan 28:8dbb85f35be6 161 et=0;
macenzofan 28:8dbb85f35be6 162 return pixelcolor;
macenzofan 28:8dbb85f35be6 163 }
macenzofan 28:8dbb85f35be6 164 if(sync.pixel_eq(pixelcolor,TANK_BLUE)==1){
macenzofan 28:8dbb85f35be6 165 sync.filled_circle(x,y,6, ORANGE);
macenzofan 28:8dbb85f35be6 166 sync.filled_circle(x,y,4, YELLOW);
macenzofan 28:8dbb85f35be6 167 sync.filled_circle(x,y,2, TANK_RED);
macenzofan 28:8dbb85f35be6 168 playSound("/sd/wavfiles/Hit.wav");
macenzofan 28:8dbb85f35be6 169 sync.update();
macenzofan 28:8dbb85f35be6 170 sync.filled_circle(x,y,5, ORANGE);
macenzofan 28:8dbb85f35be6 171 sync.filled_circle(x,y,3, TANK_RED);
macenzofan 28:8dbb85f35be6 172 playSound("/sd/wavfiles/Hit.wav");
macenzofan 28:8dbb85f35be6 173 sync.update();
macenzofan 28:8dbb85f35be6 174 sync.filled_circle(x,y,6, SKY_COLOR);
macenzofan 28:8dbb85f35be6 175 in_flight=false;
macenzofan 28:8dbb85f35be6 176 et=0;
macenzofan 28:8dbb85f35be6 177 return pixelcolor;
macenzofan 28:8dbb85f35be6 178 }
macenzofan 28:8dbb85f35be6 179 //if(sync.pixel_eq(pixelcolor,BARREL_GREY)==1){
macenzofan 28:8dbb85f35be6 180 //sync.filled_circle(x,y,6, ORANGE);
macenzofan 28:8dbb85f35be6 181 //sync.filled_circle(x,y,4, YELLOW);
macenzofan 28:8dbb85f35be6 182 //sync.filled_circle(x,y,2, TANK_RED);
macenzofan 28:8dbb85f35be6 183 //wait(.15);
macenzofan 28:8dbb85f35be6 184 //sync.update();
macenzofan 28:8dbb85f35be6 185 //sync.filled_circle(x,y,5, ORANGE);
macenzofan 28:8dbb85f35be6 186 //sync.filled_circle(x,y,3, TANK_RED);
macenzofan 28:8dbb85f35be6 187 //wait(.15);
macenzofan 28:8dbb85f35be6 188 //sync.update();
macenzofan 28:8dbb85f35be6 189 //sync.filled_circle(x,y,6, SKY_COLOR);
macenzofan 28:8dbb85f35be6 190 //in_flight=false;
macenzofan 28:8dbb85f35be6 191 //et=0;
macenzofan 28:8dbb85f35be6 192 //return pixelcolor;
macenzofan 28:8dbb85f35be6 193 }
macenzofan 28:8dbb85f35be6 194
macenzofan 28:8dbb85f35be6 195
macenzofan 28:8dbb85f35be6 196 }
jford38 23:77049670cae6 197 // return codes:
jford38 23:77049670cae6 198 // BULLET_NO_COLLISION: no collision
jford38 23:77049670cae6 199 // BULLET_OFF_SCREEN: off the side of the screen
jford38 23:77049670cae6 200 // Otherwise, return the color you've hit in 16bpp format.
jford38 27:bd55ab4d137c 201 return BULLET_NO_COLLISION;
macenzofan 28:8dbb85f35be6 202 }