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
main.cpp@14:36c306e26317, 2015-10-26 (annotated)
- Committer:
- jford38
- Date:
- Mon Oct 26 02:58:37 2015 +0000
- Revision:
- 14:36c306e26317
- Parent:
- 12:088a8203a9bb
- Child:
- 15:4b27a3a95772
Split things into multiple files. Working pretty well now!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jford38 | 6:3be57cf4bd33 | 1 | // Student Side. |
jford38 | 0:899c85cd266f | 2 | |
jford38 | 0:899c85cd266f | 3 | #include "mbed.h" |
jford38 | 6:3be57cf4bd33 | 4 | #include "game_synchronizer.h" |
jford38 | 14:36c306e26317 | 5 | |
jford38 | 14:36c306e26317 | 6 | #include "tank.h" |
jford38 | 14:36c306e26317 | 7 | #include "globals.h" |
jford38 | 12:088a8203a9bb | 8 | #include <vector> |
jford38 | 10:5da9b27e050e | 9 | |
jford38 | 6:3be57cf4bd33 | 10 | DigitalOut led1(LED1); |
jford38 | 6:3be57cf4bd33 | 11 | DigitalOut led2(LED2); |
jford38 | 6:3be57cf4bd33 | 12 | DigitalOut led3(LED3); |
jford38 | 6:3be57cf4bd33 | 13 | DigitalOut led4(LED4); |
jford38 | 0:899c85cd266f | 14 | |
jford38 | 6:3be57cf4bd33 | 15 | DigitalIn pb_l(p24); // left button |
jford38 | 6:3be57cf4bd33 | 16 | DigitalIn pb_r(p21); // right button |
jford38 | 6:3be57cf4bd33 | 17 | DigitalIn pb_u(p22); // up button |
jford38 | 6:3be57cf4bd33 | 18 | DigitalIn pb_d(p23); // down button |
jford38 | 6:3be57cf4bd33 | 19 | |
jford38 | 12:088a8203a9bb | 20 | //MMA8452 acc(p28, p27, 100000); // Accelerometer |
jford38 | 8:e6dd05393290 | 21 | uLCD_4DGL uLCD(p9,p10,p11); // LCD (serial tx, serial rx, reset pin;) |
jford38 | 8:e6dd05393290 | 22 | Game_Synchronizer sync(PLAYER1); // (tx, rx, rst, player mode) |
jford38 | 0:899c85cd266f | 23 | |
jford38 | 12:088a8203a9bb | 24 | Timer frame_timer; |
jford38 | 12:088a8203a9bb | 25 | |
jford38 | 6:3be57cf4bd33 | 26 | // For debug only. Don't use in production code. It will slow your game down a lot. |
jford38 | 6:3be57cf4bd33 | 27 | Serial pc(USBTX, USBRX); |
jford38 | 0:899c85cd266f | 28 | |
jford38 | 7:9506f2d84162 | 29 | |
jford38 | 7:9506f2d84162 | 30 | int game_menu(void) { |
jford38 | 7:9506f2d84162 | 31 | |
jford38 | 7:9506f2d84162 | 32 | // Figure out what mode the game will be run in. |
jford38 | 9:ee330b1ba394 | 33 | |
jford38 | 10:5da9b27e050e | 34 | uLCD.current_orientation = IS_LANDSCAPE; |
jford38 | 9:ee330b1ba394 | 35 | uLCD.locate(0,1); |
jford38 | 9:ee330b1ba394 | 36 | uLCD.puts("Select Mode:"); |
jford38 | 9:ee330b1ba394 | 37 | uLCD.locate(0,3); |
jford38 | 9:ee330b1ba394 | 38 | uLCD.puts(" Single-Player:"); |
jford38 | 9:ee330b1ba394 | 39 | uLCD.locate(0,4); |
jford38 | 9:ee330b1ba394 | 40 | uLCD.puts(" Left Button"); |
jford38 | 9:ee330b1ba394 | 41 | uLCD.locate(0,6); |
jford38 | 9:ee330b1ba394 | 42 | uLCD.puts(" Multi-Player:"); |
jford38 | 9:ee330b1ba394 | 43 | uLCD.locate(0,7); |
jford38 | 9:ee330b1ba394 | 44 | uLCD.puts(" Right Button"); |
jford38 | 7:9506f2d84162 | 45 | // Right button -> Multiplayer |
jford38 | 7:9506f2d84162 | 46 | // Left button -> Single player |
jford38 | 7:9506f2d84162 | 47 | while(1) { |
jford38 | 9:ee330b1ba394 | 48 | if(!pb_r) { return MULTI_PLAYER; } |
jford38 | 9:ee330b1ba394 | 49 | if(!pb_l) { return SINGLE_PLAYER; } |
jford38 | 7:9506f2d84162 | 50 | } |
jford38 | 7:9506f2d84162 | 51 | } |
jford38 | 7:9506f2d84162 | 52 | |
jford38 | 10:5da9b27e050e | 53 | void map_init() { |
jford38 | 10:5da9b27e050e | 54 | sync.background_color(SKY_COLOR); |
jford38 | 10:5da9b27e050e | 55 | sync.cls(); |
jford38 | 10:5da9b27e050e | 56 | sync.filled_rectangle(0,0,128,20, GND_COLOR); |
jford38 | 10:5da9b27e050e | 57 | sync.filled_rectangle(59, 20, 69, 60, BLACK); |
jford38 | 14:36c306e26317 | 58 | sync.locate(0,0); |
jford38 | 14:36c306e26317 | 59 | sync.textbackground_color(SKY_COLOR); |
jford38 | 14:36c306e26317 | 60 | sync.puts("Pocket Tanks 2035", sizeof("Pocket Tanks 2035")); |
jford38 | 10:5da9b27e050e | 61 | sync.update(); |
jford38 | 10:5da9b27e050e | 62 | } |
jford38 | 10:5da9b27e050e | 63 | |
jford38 | 12:088a8203a9bb | 64 | class Bullet{ |
jford38 | 12:088a8203a9bb | 65 | public: |
jford38 | 12:088a8203a9bb | 66 | int x0; |
jford38 | 12:088a8203a9bb | 67 | int y0; |
jford38 | 12:088a8203a9bb | 68 | float vx0; |
jford38 | 12:088a8203a9bb | 69 | float vy0; |
jford38 | 12:088a8203a9bb | 70 | int x; |
jford38 | 12:088a8203a9bb | 71 | int y; |
jford38 | 12:088a8203a9bb | 72 | int speed; |
jford38 | 12:088a8203a9bb | 73 | float time; |
jford38 | 12:088a8203a9bb | 74 | bool in_flight; |
jford38 | 12:088a8203a9bb | 75 | |
jford38 | 12:088a8203a9bb | 76 | Tank* tank; |
jford38 | 12:088a8203a9bb | 77 | |
jford38 | 12:088a8203a9bb | 78 | Bullet(Tank* t) { |
jford38 | 12:088a8203a9bb | 79 | tank = t; |
jford38 | 12:088a8203a9bb | 80 | speed = 30; |
jford38 | 12:088a8203a9bb | 81 | in_flight = false; |
jford38 | 10:5da9b27e050e | 82 | } |
jford38 | 12:088a8203a9bb | 83 | void shoot(void) { |
jford38 | 12:088a8203a9bb | 84 | if(in_flight) {return;} |
jford38 | 12:088a8203a9bb | 85 | time = 0; |
jford38 | 12:088a8203a9bb | 86 | tank->barrel_end(x0, y0); |
jford38 | 12:088a8203a9bb | 87 | x = x0; y = y0; |
jford38 | 12:088a8203a9bb | 88 | vx0 = speed*cos(tank->barrel_theta); |
jford38 | 12:088a8203a9bb | 89 | vy0 = speed*sin(tank->barrel_theta); |
jford38 | 12:088a8203a9bb | 90 | in_flight = true; |
jford38 | 12:088a8203a9bb | 91 | } |
jford38 | 12:088a8203a9bb | 92 | |
jford38 | 12:088a8203a9bb | 93 | void time_step(float dt) { |
jford38 | 12:088a8203a9bb | 94 | if(!in_flight) {return;} |
jford38 | 12:088a8203a9bb | 95 | time += dt; |
jford38 | 12:088a8203a9bb | 96 | int new_x = x0 + vx0*time; |
jford38 | 12:088a8203a9bb | 97 | int new_y = y0 + vy0*time + 0.5*(-9.81)*time*time; |
jford38 | 12:088a8203a9bb | 98 | |
jford38 | 14:36c306e26317 | 99 | if(new_x == x && new_y == y) { // Didn't move! |
jford38 | 12:088a8203a9bb | 100 | return; |
jford38 | 12:088a8203a9bb | 101 | } |
jford38 | 12:088a8203a9bb | 102 | |
jford38 | 12:088a8203a9bb | 103 | if(new_x < 0 || new_x > 128 || new_y < 0 || new_y > 128) { |
jford38 | 12:088a8203a9bb | 104 | in_flight = false; |
jford38 | 12:088a8203a9bb | 105 | return; |
jford38 | 12:088a8203a9bb | 106 | } |
jford38 | 12:088a8203a9bb | 107 | |
jford38 | 12:088a8203a9bb | 108 | int col = sync.read_pixel(new_x, new_y); |
jford38 | 14:36c306e26317 | 109 | if(col != CONVERT_24_TO_16_BPP(SKY_COLOR)) { |
jford38 | 14:36c306e26317 | 110 | if(col == CONVERT_24_TO_16_BPP(TANK_BLUE)) { |
jford38 | 12:088a8203a9bb | 111 | pc.printf("Player 1 wins!\n"); |
jford38 | 12:088a8203a9bb | 112 | } |
jford38 | 12:088a8203a9bb | 113 | sync.pixel(x, y, SKY_COLOR); |
jford38 | 12:088a8203a9bb | 114 | sync.filled_circle(new_x, new_y, 4, SKY_COLOR); |
jford38 | 12:088a8203a9bb | 115 | in_flight = false; |
jford38 | 12:088a8203a9bb | 116 | return; |
jford38 | 12:088a8203a9bb | 117 | } |
jford38 | 12:088a8203a9bb | 118 | sync.pixel(x, y, SKY_COLOR); |
jford38 | 12:088a8203a9bb | 119 | |
jford38 | 12:088a8203a9bb | 120 | x = new_x; |
jford38 | 12:088a8203a9bb | 121 | y = new_y; |
jford38 | 12:088a8203a9bb | 122 | sync.pixel(x, y, BLACK); |
jford38 | 12:088a8203a9bb | 123 | } |
jford38 | 10:5da9b27e050e | 124 | }; |
jford38 | 10:5da9b27e050e | 125 | |
jford38 | 12:088a8203a9bb | 126 | |
jford38 | 6:3be57cf4bd33 | 127 | void game_init(void) { |
jford38 | 6:3be57cf4bd33 | 128 | |
jford38 | 6:3be57cf4bd33 | 129 | led1 = 0; led2 = 0; led3 = 0; led4 = 0; |
jford38 | 6:3be57cf4bd33 | 130 | |
jford38 | 6:3be57cf4bd33 | 131 | pb_l.mode(PullUp); |
jford38 | 6:3be57cf4bd33 | 132 | pb_r.mode(PullUp); |
jford38 | 6:3be57cf4bd33 | 133 | pb_u.mode(PullUp); |
jford38 | 6:3be57cf4bd33 | 134 | pb_d.mode(PullUp); |
jford38 | 6:3be57cf4bd33 | 135 | |
jford38 | 6:3be57cf4bd33 | 136 | pc.printf("\033[2J\033[0;0H"); // Clear the terminal screen. |
jford38 | 6:3be57cf4bd33 | 137 | pc.printf("I'm alive! Player 1\n"); // Let us know you made it this far. |
jford38 | 7:9506f2d84162 | 138 | int mode = game_menu(); |
jford38 | 9:ee330b1ba394 | 139 | sync.init(&uLCD, mode); // Connect to the other player. |
jford38 | 10:5da9b27e050e | 140 | map_init(); |
jford38 | 6:3be57cf4bd33 | 141 | pc.printf("Initialized...\n"); // Let us know you finished initializing. |
jford38 | 6:3be57cf4bd33 | 142 | } |
jford38 | 6:3be57cf4bd33 | 143 | |
jford38 | 0:899c85cd266f | 144 | int main (void) { |
jford38 | 8:e6dd05393290 | 145 | int* p2_buttons; |
jford38 | 8:e6dd05393290 | 146 | |
jford38 | 8:e6dd05393290 | 147 | game_init(); |
jford38 | 8:e6dd05393290 | 148 | |
jford38 | 12:088a8203a9bb | 149 | Tank t1(4, 21, 12, 8, TANK_RED); |
jford38 | 12:088a8203a9bb | 150 | Tank t2(111, 21, 12, 8, TANK_BLUE); |
jford38 | 12:088a8203a9bb | 151 | Bullet b1(&t1); |
jford38 | 12:088a8203a9bb | 152 | Bullet b2(&t2); |
jford38 | 12:088a8203a9bb | 153 | |
jford38 | 5:cfec780c935b | 154 | |
jford38 | 12:088a8203a9bb | 155 | frame_timer.start(); |
jford38 | 0:899c85cd266f | 156 | while(1) { |
jford38 | 12:088a8203a9bb | 157 | |
jford38 | 12:088a8203a9bb | 158 | //double acc_x, acc_y, acc_z; |
jford38 | 12:088a8203a9bb | 159 | //acc.readXYZGravity(&acc_x,&acc_y,&acc_z); //read accelerometer |
jford38 | 12:088a8203a9bb | 160 | //pc.printf("ACCELERATION X:%f Y:%f Z:%f\n", acc_x, acc_y, acc_z); |
jford38 | 8:e6dd05393290 | 161 | |
jford38 | 8:e6dd05393290 | 162 | p2_buttons = sync.get_button_state(); |
jford38 | 8:e6dd05393290 | 163 | led1 = p2_buttons[0] ^ !pb_u; |
jford38 | 8:e6dd05393290 | 164 | led2 = p2_buttons[1] ^ !pb_r; |
jford38 | 8:e6dd05393290 | 165 | led3 = p2_buttons[2] ^ !pb_d; |
jford38 | 8:e6dd05393290 | 166 | led4 = p2_buttons[3] ^ !pb_l; |
jford38 | 11:f455e907baba | 167 | |
jford38 | 11:f455e907baba | 168 | if(!pb_l) t1.reposition(t1.x-1, t1.y, t1.barrel_theta); |
jford38 | 11:f455e907baba | 169 | if(!pb_r) t1.reposition(t1.x+1, t1.y, t1.barrel_theta); |
jford38 | 11:f455e907baba | 170 | if(p2_buttons[3]) t2.reposition(t2.x-1, t2.y, t2.barrel_theta); |
jford38 | 11:f455e907baba | 171 | if(p2_buttons[1]) t2.reposition(t2.x+1, t2.y, t2.barrel_theta); |
jford38 | 11:f455e907baba | 172 | |
jford38 | 12:088a8203a9bb | 173 | //if(!pb_d) t1.reposition(t1.x, t1.y, t1.barrel_theta-PI/30.0); |
jford38 | 12:088a8203a9bb | 174 | if(!pb_u) t1.reposition(t1.x, t1.y, t1.barrel_theta+PI/30.0); |
jford38 | 11:f455e907baba | 175 | if(p2_buttons[0]) t2.reposition(t2.x, t2.y, t2.barrel_theta+PI/30.0); |
jford38 | 14:36c306e26317 | 176 | //if(p2_buttons[2]) t2.reposition(t2.x, t2.y, t2.barrel_theta-PI/30.0); |
jford38 | 11:f455e907baba | 177 | |
jford38 | 12:088a8203a9bb | 178 | if(!pb_d) { b1.shoot(); } |
jford38 | 14:36c306e26317 | 179 | if(p2_buttons[2]) { b2.shoot(); } |
jford38 | 12:088a8203a9bb | 180 | |
jford38 | 14:36c306e26317 | 181 | b1.time_step(frame_timer.read()); |
jford38 | 14:36c306e26317 | 182 | b2.time_step(frame_timer.read()); |
jford38 | 12:088a8203a9bb | 183 | frame_timer.reset(); |
jford38 | 11:f455e907baba | 184 | |
jford38 | 12:088a8203a9bb | 185 | sync.update(); |
jford38 | 3:3ddefff03cb2 | 186 | } |
jford38 | 0:899c85cd266f | 187 | } |