![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Shell code for the Tanks game
Dependencies: 4DGL-uLCD-SE EthernetInterface Game_Synchronizer MMA8452 SDFileSystem mbed-rtos mbed wave_player
Bullet/bullet.cpp
- Committer:
- jford38
- Date:
- 2015-10-29
- Revision:
- 20:6a58052b0140
- Parent:
- 19:7aa3af04d6a8
- Child:
- 21:edfeb289b21f
File content as of revision 20:6a58052b0140:
#include "uLCD_4DGL.h" #include "bullet.h" #include "game_synchronizer.h" #include "misc.h" #include "math.h" extern Game_Synchronizer sync; Bullet::Bullet(Tank* t) { tank = t; speed = 30; in_flight = false; } 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; } int Bullet::time_step(float dt) { if(!in_flight) {return CONVERT_24_TO_16_BPP(SKY_COLOR);} time += dt; int new_x = x0 + vx0*time; int new_y = y0 + vy0*time + 0.5*(-9.81)*time*time; if(new_x == x && new_y == y) { // Didn't move! return CONVERT_24_TO_16_BPP(SKY_COLOR); } if(new_x < 0 || new_x > 128 || new_y < 0 || new_y > 128) { sync.pixel(x, y, SKY_COLOR); in_flight = false; return CONVERT_24_TO_16_BPP(BLACK); } int col = sync.read_pixel(new_x, new_y); if(col != CONVERT_24_TO_16_BPP(SKY_COLOR)) { sync.pixel(x, y, SKY_COLOR); sync.filled_circle(new_x, new_y, 4, SKY_COLOR); in_flight = false; if(col == CONVERT_24_TO_16_BPP(TANK_BLUE)) { return col; } if( col == CONVERT_24_TO_16_BPP(TANK_RED)) { return col; } return CONVERT_24_TO_16_BPP(BLACK); } sync.pixel(x, y, SKY_COLOR); x = new_x; y = new_y; sync.pixel(x, y, BLACK); return CONVERT_24_TO_16_BPP(SKY_COLOR); }