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
Diff: Tank/tank.cpp
- Revision:
- 28:8dbb85f35be6
- Parent:
- 27:bd55ab4d137c
diff -r bd55ab4d137c -r 8dbb85f35be6 Tank/tank.cpp --- a/Tank/tank.cpp Fri Oct 30 11:08:31 2015 +0000 +++ b/Tank/tank.cpp Mon Mar 13 00:15:36 2017 +0000 @@ -2,6 +2,7 @@ #include "globals.h" #include "math.h" #include "game_synchronizer.h" +#include "uLCD_4DGL.h" extern Game_Synchronizer sync; @@ -14,52 +15,98 @@ w = width; h = height; tank_color = color; barrel_theta = PI/4.0; - barrel_length = w; + barrel_length = .75*w; wheel_rad = 2.0; draw(); } // Return the minimum x-coord of your tank's bounding box. -int Tank::min_x(void) { - return 0; +int Tank::min_x(void) { + if(w/2.0+x+barrel_length*cos(barrel_theta)<x){ + return w/2.0+x+barrel_length*cos(barrel_theta); + } + else{ + return x; + } } // Return the minimum y-coord of your tank's bounding box. int Tank::min_y(void) { - return 0; + return y+wheel_rad; } // Return the maximum x-coord of your tank's bounding box. int Tank::max_x(void) { - return 0; + if(w/2.0+w+barrel_length*cos(barrel_theta)>x+w){ + return x+w/2+barrel_length*cos(barrel_theta);; + } + else{ + return x+w; + } } + // Return the maximum y-coord of your tank's bounding box. int Tank::max_y(void) { - return 1; + return y+h+wheel_rad+barrel_length*sin(barrel_theta); } void Tank::barrel_end(int* bx, int* by) { // Set the x and y coords of the end of the barrel. - // *bx = ??? - // *by = ??? + *bx = x + w/2.0 + barrel_length*cos(barrel_theta); + *by = y+h+wheel_rad + barrel_length*sin(barrel_theta); } void Tank::reposition(int dx, int dy, float dtheta) { - // Blank out the old tank position, and - // 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(dx!=0 || dy!=0 || dtheta!=0){ + x=x+dx; + y=y+dy; + sync.filled_rectangle(x-4, y-1, x+20, y+21, SKY_COLOR);// Blank out the old tank position, and + + sync.filled_circle(53, 26, 6, TREE); + draw(); // Move the tank dx pixels in the x direction. + sync.filled_circle(53, 26, 6, TREE); + //sync.line(76,23,76,50, BLACK); + //sync.rectangle(74, 20, 75, 50, WHITE); + sync.filled_circle(78, 19, 4, TREE); // Move the tank dy pixels in the y direction. + sync.update(); + if(0 <= barrel_theta+dtheta && barrel_theta+dtheta <= PI){ // Move the tank barrel by an angle dtheta. + sync.line(x + w/2.0, y+h+wheel_rad, x + w/2.0 + barrel_length*cos(barrel_theta), y+h+wheel_rad + barrel_length*sin(barrel_theta), SKY_COLOR);// Don't allow it to go below parallel. + barrel_theta += dtheta; + sync.line(x + w/2.0, y+h+wheel_rad, x + w/2.0 + barrel_length*cos(barrel_theta), y+h+wheel_rad + barrel_length*sin(barrel_theta), BLACK); + sync.filled_rectangle(x+2*wheel_rad, y+3*wheel_rad, x+.65*w, y+h+3*wheel_rad, tank_color); + sync.update(); + } - // Do collision detection to prevent the tank from hitting things. - // (obstacles, side of the screen, other tanks, etc.) + + //if((x){ //Do collision detection to prevent the tank from hitting things. + //(x)=x+1; + //} + + //if(x+16>127){ //Do collision detection to prevent the tank from hitting things. + //x=111; + //} + + //int obsticle=sync.read_pixel(x+20,y); + + //if(sync.pixel_eq(obsticle, SKY_COLOR)!=1){ + //x=x-1; + //} + // (obstacles, side of the screen, other tanks, etc.) + } } - - -// Example tank draw function. We expect you to get creative on this one! + // Example tank draw function. We expect you to get creative on this one! void Tank::draw() { - sync.line(x + w/2.0, y+h+wheel_rad, x + w/2.0 + barrel_length*cos(barrel_theta), y+h+wheel_rad + barrel_length*sin(barrel_theta), BLACK); - sync.filled_rectangle(x, y+wheel_rad, x+w, y+h+wheel_rad, tank_color); - sync.filled_circle(x+wheel_rad, y+wheel_rad, wheel_rad, BLACK); - sync.filled_circle(x+w-wheel_rad, y+wheel_rad, wheel_rad, BLACK); + //if(x>4 || x<111){ + sync.line(x + w/2.0, y+h+wheel_rad, x + w/2.0 + barrel_length*cos(barrel_theta), y+h+wheel_rad + barrel_length*sin(barrel_theta), BARREL_GREY); + sync.filled_circle(x+wheel_rad, y+wheel_rad, wheel_rad, BARREL_GREY); + sync.filled_circle(x+w-wheel_rad, y+wheel_rad, wheel_rad, BARREL_GREY); + sync.filled_circle(x+(w/1.475)-wheel_rad,y+wheel_rad, wheel_rad, BARREL_GREY); + sync.filled_rectangle(x, y+(2*wheel_rad), x+w, y+h+wheel_rad, tank_color); + sync.filled_rectangle(x+2*wheel_rad, y+3*wheel_rad, x+.65*w, y+h+3*wheel_rad, tank_color); + sync.line(x+wheel_rad,y-.5*wheel_rad, x+w-wheel_rad, y-.5*wheel_rad, BARREL_GREY); + sync.line(x+(2*wheel_rad), y+h+wheel_rad, x+.65*w, y+h+wheel_rad, BLACK); + + //} } \ No newline at end of file