Revenge of the Mouse
Dependencies: 4DGL-uLCD-SE EthernetInterface Game_Synchronizer LCD_fonts MMA8452 SDFileSystem mbed-rtos mbed wave_player
Fork of 2035_Tanks_Shell by
main.cpp@16:b73f0842d3cd, 2015-10-27 (annotated)
- Committer:
- jford38
- Date:
- Tue Oct 27 18:10:03 2015 +0000
- Revision:
- 16:b73f0842d3cd
- Parent:
- 15:4b27a3a95772
- Child:
- 17:7bc7127782e4
Just making sure it's still up to date on the TA group.
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 | 15:4b27a3a95772 | 4 | |
jford38 | 6:3be57cf4bd33 | 5 | #include "game_synchronizer.h" |
jford38 | 14:36c306e26317 | 6 | #include "tank.h" |
jford38 | 15:4b27a3a95772 | 7 | #include "bullet.h" |
jford38 | 14:36c306e26317 | 8 | #include "globals.h" |
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 | 16:b73f0842d3cd | 60 | sync.puts("Pocket Tanks 2035"); |
jford38 | 10:5da9b27e050e | 61 | sync.update(); |
jford38 | 10:5da9b27e050e | 62 | } |
jford38 | 10:5da9b27e050e | 63 | |
jford38 | 10:5da9b27e050e | 64 | |
jford38 | 12:088a8203a9bb | 65 | |
jford38 | 6:3be57cf4bd33 | 66 | void game_init(void) { |
jford38 | 6:3be57cf4bd33 | 67 | |
jford38 | 6:3be57cf4bd33 | 68 | led1 = 0; led2 = 0; led3 = 0; led4 = 0; |
jford38 | 6:3be57cf4bd33 | 69 | |
jford38 | 6:3be57cf4bd33 | 70 | pb_l.mode(PullUp); |
jford38 | 6:3be57cf4bd33 | 71 | pb_r.mode(PullUp); |
jford38 | 6:3be57cf4bd33 | 72 | pb_u.mode(PullUp); |
jford38 | 6:3be57cf4bd33 | 73 | pb_d.mode(PullUp); |
jford38 | 6:3be57cf4bd33 | 74 | |
jford38 | 6:3be57cf4bd33 | 75 | pc.printf("\033[2J\033[0;0H"); // Clear the terminal screen. |
jford38 | 6:3be57cf4bd33 | 76 | pc.printf("I'm alive! Player 1\n"); // Let us know you made it this far. |
jford38 | 7:9506f2d84162 | 77 | int mode = game_menu(); |
jford38 | 9:ee330b1ba394 | 78 | sync.init(&uLCD, mode); // Connect to the other player. |
jford38 | 10:5da9b27e050e | 79 | map_init(); |
jford38 | 6:3be57cf4bd33 | 80 | pc.printf("Initialized...\n"); // Let us know you finished initializing. |
jford38 | 6:3be57cf4bd33 | 81 | } |
jford38 | 6:3be57cf4bd33 | 82 | |
jford38 | 0:899c85cd266f | 83 | int main (void) { |
jford38 | 8:e6dd05393290 | 84 | int* p2_buttons; |
jford38 | 8:e6dd05393290 | 85 | |
jford38 | 8:e6dd05393290 | 86 | game_init(); |
jford38 | 8:e6dd05393290 | 87 | |
jford38 | 12:088a8203a9bb | 88 | Tank t1(4, 21, 12, 8, TANK_RED); |
jford38 | 12:088a8203a9bb | 89 | Tank t2(111, 21, 12, 8, TANK_BLUE); |
jford38 | 12:088a8203a9bb | 90 | Bullet b1(&t1); |
jford38 | 12:088a8203a9bb | 91 | Bullet b2(&t2); |
jford38 | 12:088a8203a9bb | 92 | |
jford38 | 5:cfec780c935b | 93 | |
jford38 | 12:088a8203a9bb | 94 | frame_timer.start(); |
jford38 | 0:899c85cd266f | 95 | while(1) { |
jford38 | 12:088a8203a9bb | 96 | |
jford38 | 12:088a8203a9bb | 97 | //double acc_x, acc_y, acc_z; |
jford38 | 12:088a8203a9bb | 98 | //acc.readXYZGravity(&acc_x,&acc_y,&acc_z); //read accelerometer |
jford38 | 12:088a8203a9bb | 99 | //pc.printf("ACCELERATION X:%f Y:%f Z:%f\n", acc_x, acc_y, acc_z); |
jford38 | 8:e6dd05393290 | 100 | |
jford38 | 8:e6dd05393290 | 101 | p2_buttons = sync.get_button_state(); |
jford38 | 8:e6dd05393290 | 102 | led1 = p2_buttons[0] ^ !pb_u; |
jford38 | 8:e6dd05393290 | 103 | led2 = p2_buttons[1] ^ !pb_r; |
jford38 | 8:e6dd05393290 | 104 | led3 = p2_buttons[2] ^ !pb_d; |
jford38 | 8:e6dd05393290 | 105 | led4 = p2_buttons[3] ^ !pb_l; |
jford38 | 11:f455e907baba | 106 | |
jford38 | 11:f455e907baba | 107 | if(!pb_l) t1.reposition(t1.x-1, t1.y, t1.barrel_theta); |
jford38 | 11:f455e907baba | 108 | if(!pb_r) t1.reposition(t1.x+1, t1.y, t1.barrel_theta); |
jford38 | 11:f455e907baba | 109 | if(p2_buttons[3]) t2.reposition(t2.x-1, t2.y, t2.barrel_theta); |
jford38 | 11:f455e907baba | 110 | if(p2_buttons[1]) t2.reposition(t2.x+1, t2.y, t2.barrel_theta); |
jford38 | 11:f455e907baba | 111 | |
jford38 | 12:088a8203a9bb | 112 | //if(!pb_d) t1.reposition(t1.x, t1.y, t1.barrel_theta-PI/30.0); |
jford38 | 12:088a8203a9bb | 113 | if(!pb_u) t1.reposition(t1.x, t1.y, t1.barrel_theta+PI/30.0); |
jford38 | 11:f455e907baba | 114 | if(p2_buttons[0]) t2.reposition(t2.x, t2.y, t2.barrel_theta+PI/30.0); |
jford38 | 14:36c306e26317 | 115 | //if(p2_buttons[2]) t2.reposition(t2.x, t2.y, t2.barrel_theta-PI/30.0); |
jford38 | 11:f455e907baba | 116 | |
jford38 | 12:088a8203a9bb | 117 | if(!pb_d) { b1.shoot(); } |
jford38 | 14:36c306e26317 | 118 | if(p2_buttons[2]) { b2.shoot(); } |
jford38 | 12:088a8203a9bb | 119 | |
jford38 | 15:4b27a3a95772 | 120 | int retval = b1.time_step(frame_timer.read()); |
jford38 | 15:4b27a3a95772 | 121 | if(retval) { |
jford38 | 15:4b27a3a95772 | 122 | sync.update(); |
jford38 | 15:4b27a3a95772 | 123 | pc.printf("P1 wins!"); |
jford38 | 15:4b27a3a95772 | 124 | break; |
jford38 | 15:4b27a3a95772 | 125 | } |
jford38 | 15:4b27a3a95772 | 126 | retval = b2.time_step(frame_timer.read()); |
jford38 | 15:4b27a3a95772 | 127 | if(retval) { |
jford38 | 15:4b27a3a95772 | 128 | sync.update(); |
jford38 | 15:4b27a3a95772 | 129 | pc.printf("P2 wins!"); |
jford38 | 15:4b27a3a95772 | 130 | break; |
jford38 | 15:4b27a3a95772 | 131 | } |
jford38 | 12:088a8203a9bb | 132 | frame_timer.reset(); |
jford38 | 11:f455e907baba | 133 | |
jford38 | 12:088a8203a9bb | 134 | sync.update(); |
jford38 | 3:3ddefff03cb2 | 135 | } |
jford38 | 15:4b27a3a95772 | 136 | |
jford38 | 15:4b27a3a95772 | 137 | |
jford38 | 15:4b27a3a95772 | 138 | int i = 0; |
jford38 | 15:4b27a3a95772 | 139 | while(1) { |
jford38 | 15:4b27a3a95772 | 140 | sync.cls(); |
jford38 | 15:4b27a3a95772 | 141 | sync.locate(i, 6); |
jford38 | 16:b73f0842d3cd | 142 | sync.puts("GAME OVER!\n"); |
jford38 | 15:4b27a3a95772 | 143 | sync.update(); |
jford38 | 15:4b27a3a95772 | 144 | (++i) %= 10; |
jford38 | 15:4b27a3a95772 | 145 | wait(0.1); |
jford38 | 15:4b27a3a95772 | 146 | } |
jford38 | 0:899c85cd266f | 147 | } |