Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE DRV2605 EthernetInterface Game_Synchronizer MMA8452 SDFileSystem SparkfunAnalogJoystick mbed-rtos mbed wave_player
Fork of 2035_Tanks_Shell by
bullet.cpp
00001 #include "uLCD_4DGL.h" 00002 #include "bullet.h" 00003 #include "game_synchronizer.h" 00004 #include "globals.h" 00005 #include "math.h" 00006 #include "SDFileSystem.h" 00007 #include "wave_player.h" 00008 #include "playSound.h" 00009 00010 extern Game_Synchronizer sync; 00011 float et = 0; 00012 int pixelcolor; 00013 //int bouncy 00014 00015 // Initialize the bullet. Don't have to do much here. 00016 // Keep a pointer to this bullet's tank. 00017 // Set the speed, and default the bullet to not in_flight. 00018 Bullet::Bullet(Tank* t) { 00019 tank = t; 00020 speed = 0; 00021 in_flight = false; 00022 } 00023 //Bullet::Bouncy(Tank* t) { 00024 //tank=t; 00025 //speed=0; 00026 //in_flight=false; 00027 //} 00028 // If in_flight, do nothing. Otherwise, 00029 // set the in_flight flag, and initialize values needed for 00030 // the trajectory calculations. (x0, y0), (vx0, vy0), time 00031 // Hint: tank->barrel_end(...) is useful here. 00032 void Bullet::shoot(void) { 00033 if (in_flight==false){ 00034 in_flight = true; 00035 tank->barrel_end(&x0,&y0); 00036 vx0=cos(tank->barrel_theta)*speed; 00037 vy0=sin(tank->barrel_theta)*speed; 00038 x=x0; 00039 y=y0; 00040 sync.filled_circle(x,y,2,YELLOW); 00041 sync.filled_circle(x,y,1,ORANGE); 00042 wait(.5); 00043 sync.filled_circle(x,y,2,SKY_COLOR); 00044 sync.filled_circle(x,y,1,BLT_COLOR); 00045 } 00046 } 00047 00048 // If the bullet is in flight, calculate its new position 00049 // after a time delta dt. 00050 void Bullet::update_position(float dt) { 00051 if (in_flight == true){ 00052 x=floor(x0+(vx0*dt)); 00053 y=floor(y0+(vy0*dt)-(4.9*dt*dt)); 00054 sync.filled_circle(x,y,1,BLT_COLOR); 00055 } 00056 } 00057 00058 int Bullet::time_step(float dt) { 00059 // If the bullet hasn't hit anything, 00060 // redraw the bullet at its new location. 00061 00062 if (in_flight == true){ 00063 sync.filled_circle(x,y,1,SKY_COLOR); 00064 et += dt; 00065 update_position(et); 00066 00067 // If it has hit something (obstacle, tank, edge of the screen), 00068 // set the in_flight flag back to false, explode the nearby area, 00069 // and return one of the following codes. 00070 //if(bouncy==1){ 00071 //if(x>128 || y<0 || y>128){ 00072 00073 //} 00074 //} 00075 //else{ 00076 if(x>128 || y<0 || y>128){ 00077 in_flight=false; 00078 et=0; 00079 return BULLET_OFF_SCREEN; 00080 } 00081 //} 00082 00083 else{ 00084 pixelcolor=sync.read_pixel(x,y); 00085 00086 if(sync.pixel_eq(pixelcolor,BLACK)==1){ 00087 sync.filled_circle(x,y,6, ORANGE); 00088 sync.filled_circle(x,y,4, YELLOW); 00089 sync.filled_circle(x,y,2, TANK_RED); 00090 playSound("/sd/wavfiles/Hit.wav"); 00091 sync.update(); 00092 sync.filled_circle(x,y,5, ORANGE); 00093 sync.filled_circle(x,y,3, TANK_RED); 00094 playSound("/sd/wavfiles/Hit.wav"); 00095 sync.update(); 00096 sync.filled_circle(x,y,6, SKY_COLOR); 00097 in_flight=false; 00098 et=0; 00099 return pixelcolor; 00100 00101 } 00102 if(sync.pixel_eq(pixelcolor,TREE)==1){ 00103 sync.filled_circle(x,y,6, ORANGE); 00104 sync.filled_circle(x,y,4, YELLOW); 00105 sync.filled_circle(x,y,2, TANK_RED); 00106 playSound("/sd/wavfiles/Hit.wav"); 00107 sync.update(); 00108 sync.filled_circle(x,y,5, ORANGE); 00109 sync.filled_circle(x,y,3, TANK_RED); 00110 playSound("/sd/wavfiles/Hit.wav"); 00111 sync.update(); 00112 sync.filled_circle(x,y,6, SKY_COLOR); 00113 in_flight=false; 00114 et=0; 00115 return pixelcolor; 00116 } 00117 if(sync.pixel_eq(pixelcolor,GND_COLOR)==1){ 00118 sync.filled_circle(x,y,6, ORANGE); 00119 sync.filled_circle(x,y,4, YELLOW); 00120 sync.filled_circle(x,y,2, TANK_RED); 00121 playSound("/sd/wavfiles/Hit.wav"); 00122 sync.update(); 00123 sync.filled_circle(x,y,5, ORANGE); 00124 sync.filled_circle(x,y,3, TANK_RED); 00125 playSound("/sd/wavfiles/Hit.wav"); 00126 sync.update(); 00127 sync.filled_circle(x,y,6, SKY_COLOR); 00128 sync.filled_circle(x,y,6,BROWN); 00129 sync.filled_circle(x,y+2,6, SKY_COLOR); 00130 in_flight=false; 00131 et=0; 00132 return pixelcolor; 00133 } 00134 if(sync.pixel_eq(pixelcolor,WHITE)==1){ 00135 sync.filled_circle(x,y,6, ORANGE); 00136 sync.filled_circle(x,y,4, YELLOW); 00137 sync.filled_circle(x,y,2, TANK_RED); 00138 playSound("/sd/wavfiles/Hit.wav"); 00139 sync.update(); 00140 sync.filled_circle(x,y,5, ORANGE); 00141 sync.filled_circle(x,y,3, TANK_RED); 00142 playSound("/sd/wavfiles/Hit.wav"); 00143 sync.update(); 00144 sync.filled_circle(x,y,6, SKY_COLOR); 00145 in_flight=false; 00146 et=0; 00147 return pixelcolor; 00148 } 00149 if(sync.pixel_eq(pixelcolor,TANK_RED)==1){ 00150 sync.filled_circle(x,y,6, ORANGE); 00151 sync.filled_circle(x,y,4, YELLOW); 00152 sync.filled_circle(x,y,2, TANK_RED); 00153 playSound("/sd/wavfiles/Hit.wav"); 00154 sync.update(); 00155 sync.filled_circle(x,y,5, ORANGE); 00156 sync.filled_circle(x,y,3, TANK_RED); 00157 playSound("/sd/wavfiles/Hit.wav"); 00158 sync.update(); 00159 sync.filled_circle(x,y,6, SKY_COLOR); 00160 in_flight=false; 00161 et=0; 00162 return pixelcolor; 00163 } 00164 if(sync.pixel_eq(pixelcolor,TANK_BLUE)==1){ 00165 sync.filled_circle(x,y,6, ORANGE); 00166 sync.filled_circle(x,y,4, YELLOW); 00167 sync.filled_circle(x,y,2, TANK_RED); 00168 playSound("/sd/wavfiles/Hit.wav"); 00169 sync.update(); 00170 sync.filled_circle(x,y,5, ORANGE); 00171 sync.filled_circle(x,y,3, TANK_RED); 00172 playSound("/sd/wavfiles/Hit.wav"); 00173 sync.update(); 00174 sync.filled_circle(x,y,6, SKY_COLOR); 00175 in_flight=false; 00176 et=0; 00177 return pixelcolor; 00178 } 00179 //if(sync.pixel_eq(pixelcolor,BARREL_GREY)==1){ 00180 //sync.filled_circle(x,y,6, ORANGE); 00181 //sync.filled_circle(x,y,4, YELLOW); 00182 //sync.filled_circle(x,y,2, TANK_RED); 00183 //wait(.15); 00184 //sync.update(); 00185 //sync.filled_circle(x,y,5, ORANGE); 00186 //sync.filled_circle(x,y,3, TANK_RED); 00187 //wait(.15); 00188 //sync.update(); 00189 //sync.filled_circle(x,y,6, SKY_COLOR); 00190 //in_flight=false; 00191 //et=0; 00192 //return pixelcolor; 00193 } 00194 00195 00196 } 00197 // return codes: 00198 // BULLET_NO_COLLISION: no collision 00199 // BULLET_OFF_SCREEN: off the side of the screen 00200 // Otherwise, return the color you've hit in 16bpp format. 00201 return BULLET_NO_COLLISION; 00202 }
Generated on Mon Jul 18 2022 22:47:15 by
1.7.2
