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
Tank/tank.cpp@28:8dbb85f35be6, 2017-03-13 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
jford38 | 14:36c306e26317 | 1 | #include "tank.h" |
jford38 | 27:bd55ab4d137c | 2 | #include "globals.h" |
jford38 | 14:36c306e26317 | 3 | #include "math.h" |
jford38 | 14:36c306e26317 | 4 | #include "game_synchronizer.h" |
macenzofan | 28:8dbb85f35be6 | 5 | #include "uLCD_4DGL.h" |
jford38 | 14:36c306e26317 | 6 | |
jford38 | 14:36c306e26317 | 7 | extern Game_Synchronizer sync; |
jford38 | 14:36c306e26317 | 8 | |
jford38 | 20:6a58052b0140 | 9 | // sx is the x-coord of the bottom left corner of the tank |
jford38 | 20:6a58052b0140 | 10 | // sy is the y-coord of the same corner |
jford38 | 20:6a58052b0140 | 11 | // width is the width of the tank |
jford38 | 20:6a58052b0140 | 12 | // height is the height of the tank |
jford38 | 14:36c306e26317 | 13 | Tank::Tank(int sx, int sy, int width, int height, int color) { |
jford38 | 14:36c306e26317 | 14 | x = sx; y = sy; |
jford38 | 14:36c306e26317 | 15 | w = width; h = height; |
jford38 | 14:36c306e26317 | 16 | tank_color = color; |
jford38 | 14:36c306e26317 | 17 | barrel_theta = PI/4.0; |
macenzofan | 28:8dbb85f35be6 | 18 | barrel_length = .75*w; |
jford38 | 14:36c306e26317 | 19 | wheel_rad = 2.0; |
jford38 | 14:36c306e26317 | 20 | draw(); |
jford38 | 14:36c306e26317 | 21 | } |
jford38 | 22:3c68eea5a609 | 22 | |
jford38 | 22:3c68eea5a609 | 23 | // Return the minimum x-coord of your tank's bounding box. |
macenzofan | 28:8dbb85f35be6 | 24 | int Tank::min_x(void) { |
macenzofan | 28:8dbb85f35be6 | 25 | if(w/2.0+x+barrel_length*cos(barrel_theta)<x){ |
macenzofan | 28:8dbb85f35be6 | 26 | return w/2.0+x+barrel_length*cos(barrel_theta); |
macenzofan | 28:8dbb85f35be6 | 27 | } |
macenzofan | 28:8dbb85f35be6 | 28 | else{ |
macenzofan | 28:8dbb85f35be6 | 29 | return x; |
macenzofan | 28:8dbb85f35be6 | 30 | } |
jford38 | 22:3c68eea5a609 | 31 | } |
jford38 | 22:3c68eea5a609 | 32 | |
jford38 | 22:3c68eea5a609 | 33 | // Return the minimum y-coord of your tank's bounding box. |
jford38 | 22:3c68eea5a609 | 34 | int Tank::min_y(void) { |
macenzofan | 28:8dbb85f35be6 | 35 | return y+wheel_rad; |
jford38 | 14:36c306e26317 | 36 | } |
jford38 | 14:36c306e26317 | 37 | |
jford38 | 22:3c68eea5a609 | 38 | // Return the maximum x-coord of your tank's bounding box. |
jford38 | 22:3c68eea5a609 | 39 | int Tank::max_x(void) { |
macenzofan | 28:8dbb85f35be6 | 40 | if(w/2.0+w+barrel_length*cos(barrel_theta)>x+w){ |
macenzofan | 28:8dbb85f35be6 | 41 | return x+w/2+barrel_length*cos(barrel_theta);; |
macenzofan | 28:8dbb85f35be6 | 42 | } |
macenzofan | 28:8dbb85f35be6 | 43 | else{ |
macenzofan | 28:8dbb85f35be6 | 44 | return x+w; |
macenzofan | 28:8dbb85f35be6 | 45 | } |
jford38 | 14:36c306e26317 | 46 | } |
jford38 | 14:36c306e26317 | 47 | |
macenzofan | 28:8dbb85f35be6 | 48 | |
jford38 | 22:3c68eea5a609 | 49 | // Return the maximum y-coord of your tank's bounding box. |
jford38 | 22:3c68eea5a609 | 50 | int Tank::max_y(void) { |
macenzofan | 28:8dbb85f35be6 | 51 | return y+h+wheel_rad+barrel_length*sin(barrel_theta); |
jford38 | 22:3c68eea5a609 | 52 | } |
jford38 | 22:3c68eea5a609 | 53 | |
jford38 | 22:3c68eea5a609 | 54 | void Tank::barrel_end(int* bx, int* by) { |
jford38 | 25:b3c075a2f2c7 | 55 | // Set the x and y coords of the end of the barrel. |
macenzofan | 28:8dbb85f35be6 | 56 | *bx = x + w/2.0 + barrel_length*cos(barrel_theta); |
macenzofan | 28:8dbb85f35be6 | 57 | *by = y+h+wheel_rad + barrel_length*sin(barrel_theta); |
jford38 | 14:36c306e26317 | 58 | } |
jford38 | 14:36c306e26317 | 59 | |
jford38 | 19:7aa3af04d6a8 | 60 | void Tank::reposition(int dx, int dy, float dtheta) { |
macenzofan | 28:8dbb85f35be6 | 61 | |
macenzofan | 28:8dbb85f35be6 | 62 | if(dx!=0 || dy!=0 || dtheta!=0){ |
macenzofan | 28:8dbb85f35be6 | 63 | x=x+dx; |
macenzofan | 28:8dbb85f35be6 | 64 | y=y+dy; |
macenzofan | 28:8dbb85f35be6 | 65 | sync.filled_rectangle(x-4, y-1, x+20, y+21, SKY_COLOR);// Blank out the old tank position, and |
macenzofan | 28:8dbb85f35be6 | 66 | |
macenzofan | 28:8dbb85f35be6 | 67 | sync.filled_circle(53, 26, 6, TREE); |
macenzofan | 28:8dbb85f35be6 | 68 | draw(); // Move the tank dx pixels in the x direction. |
macenzofan | 28:8dbb85f35be6 | 69 | sync.filled_circle(53, 26, 6, TREE); |
macenzofan | 28:8dbb85f35be6 | 70 | //sync.line(76,23,76,50, BLACK); |
macenzofan | 28:8dbb85f35be6 | 71 | //sync.rectangle(74, 20, 75, 50, WHITE); |
macenzofan | 28:8dbb85f35be6 | 72 | sync.filled_circle(78, 19, 4, TREE); // Move the tank dy pixels in the y direction. |
macenzofan | 28:8dbb85f35be6 | 73 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 74 | if(0 <= barrel_theta+dtheta && barrel_theta+dtheta <= PI){ // Move the tank barrel by an angle dtheta. |
macenzofan | 28:8dbb85f35be6 | 75 | 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. |
macenzofan | 28:8dbb85f35be6 | 76 | barrel_theta += dtheta; |
macenzofan | 28:8dbb85f35be6 | 77 | 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); |
macenzofan | 28:8dbb85f35be6 | 78 | sync.filled_rectangle(x+2*wheel_rad, y+3*wheel_rad, x+.65*w, y+h+3*wheel_rad, tank_color); |
macenzofan | 28:8dbb85f35be6 | 79 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 80 | } |
jford38 | 14:36c306e26317 | 81 | |
macenzofan | 28:8dbb85f35be6 | 82 | |
macenzofan | 28:8dbb85f35be6 | 83 | //if((x){ //Do collision detection to prevent the tank from hitting things. |
macenzofan | 28:8dbb85f35be6 | 84 | //(x)=x+1; |
macenzofan | 28:8dbb85f35be6 | 85 | //} |
macenzofan | 28:8dbb85f35be6 | 86 | |
macenzofan | 28:8dbb85f35be6 | 87 | //if(x+16>127){ //Do collision detection to prevent the tank from hitting things. |
macenzofan | 28:8dbb85f35be6 | 88 | //x=111; |
macenzofan | 28:8dbb85f35be6 | 89 | //} |
macenzofan | 28:8dbb85f35be6 | 90 | |
macenzofan | 28:8dbb85f35be6 | 91 | //int obsticle=sync.read_pixel(x+20,y); |
macenzofan | 28:8dbb85f35be6 | 92 | |
macenzofan | 28:8dbb85f35be6 | 93 | //if(sync.pixel_eq(obsticle, SKY_COLOR)!=1){ |
macenzofan | 28:8dbb85f35be6 | 94 | //x=x-1; |
macenzofan | 28:8dbb85f35be6 | 95 | //} |
macenzofan | 28:8dbb85f35be6 | 96 | // (obstacles, side of the screen, other tanks, etc.) |
macenzofan | 28:8dbb85f35be6 | 97 | } |
jford38 | 14:36c306e26317 | 98 | } |
macenzofan | 28:8dbb85f35be6 | 99 | // Example tank draw function. We expect you to get creative on this one! |
jford38 | 14:36c306e26317 | 100 | void Tank::draw() { |
macenzofan | 28:8dbb85f35be6 | 101 | //if(x>4 || x<111){ |
macenzofan | 28:8dbb85f35be6 | 102 | 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); |
macenzofan | 28:8dbb85f35be6 | 103 | sync.filled_circle(x+wheel_rad, y+wheel_rad, wheel_rad, BARREL_GREY); |
macenzofan | 28:8dbb85f35be6 | 104 | sync.filled_circle(x+w-wheel_rad, y+wheel_rad, wheel_rad, BARREL_GREY); |
macenzofan | 28:8dbb85f35be6 | 105 | sync.filled_circle(x+(w/1.475)-wheel_rad,y+wheel_rad, wheel_rad, BARREL_GREY); |
macenzofan | 28:8dbb85f35be6 | 106 | sync.filled_rectangle(x, y+(2*wheel_rad), x+w, y+h+wheel_rad, tank_color); |
macenzofan | 28:8dbb85f35be6 | 107 | sync.filled_rectangle(x+2*wheel_rad, y+3*wheel_rad, x+.65*w, y+h+3*wheel_rad, tank_color); |
macenzofan | 28:8dbb85f35be6 | 108 | sync.line(x+wheel_rad,y-.5*wheel_rad, x+w-wheel_rad, y-.5*wheel_rad, BARREL_GREY); |
macenzofan | 28:8dbb85f35be6 | 109 | sync.line(x+(2*wheel_rad), y+h+wheel_rad, x+.65*w, y+h+wheel_rad, BLACK); |
macenzofan | 28:8dbb85f35be6 | 110 | |
macenzofan | 28:8dbb85f35be6 | 111 | //} |
jford38 | 14:36c306e26317 | 112 | } |