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@8:e6dd05393290, 2015-10-22 (annotated)
- Committer:
- jford38
- Date:
- Thu Oct 22 09:52:53 2015 +0000
- Revision:
- 8:e6dd05393290
- Parent:
- 7:9506f2d84162
- Child:
- 9:ee330b1ba394
Done for tonight. Tired. Right now the student side can run independently in single-player mode. The student side LEDs also turn on when you push the buttons on either side. Started some timing stuff. Didn't get far.
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 | 6:3be57cf4bd33 | 5 | |
| jford38 | 6:3be57cf4bd33 | 6 | DigitalOut led1(LED1); |
| jford38 | 6:3be57cf4bd33 | 7 | DigitalOut led2(LED2); |
| jford38 | 6:3be57cf4bd33 | 8 | DigitalOut led3(LED3); |
| jford38 | 6:3be57cf4bd33 | 9 | DigitalOut led4(LED4); |
| jford38 | 0:899c85cd266f | 10 | |
| jford38 | 6:3be57cf4bd33 | 11 | DigitalIn pb_l(p24); // left button |
| jford38 | 6:3be57cf4bd33 | 12 | DigitalIn pb_r(p21); // right button |
| jford38 | 6:3be57cf4bd33 | 13 | DigitalIn pb_u(p22); // up button |
| jford38 | 6:3be57cf4bd33 | 14 | DigitalIn pb_d(p23); // down button |
| jford38 | 6:3be57cf4bd33 | 15 | |
| jford38 | 8:e6dd05393290 | 16 | uLCD_4DGL uLCD(p9,p10,p11); // LCD (serial tx, serial rx, reset pin;) |
| jford38 | 8:e6dd05393290 | 17 | Game_Synchronizer sync(PLAYER1); // (tx, rx, rst, player mode) |
| jford38 | 0:899c85cd266f | 18 | |
| jford38 | 6:3be57cf4bd33 | 19 | // For debug only. Don't use in production code. It will slow your game down a lot. |
| jford38 | 6:3be57cf4bd33 | 20 | Serial pc(USBTX, USBRX); |
| jford38 | 0:899c85cd266f | 21 | |
| jford38 | 7:9506f2d84162 | 22 | |
| jford38 | 7:9506f2d84162 | 23 | int game_menu(void) { |
| jford38 | 7:9506f2d84162 | 24 | |
| jford38 | 7:9506f2d84162 | 25 | // Figure out what mode the game will be run in. |
| jford38 | 7:9506f2d84162 | 26 | // Right button -> Multiplayer |
| jford38 | 7:9506f2d84162 | 27 | // Left button -> Single player |
| jford38 | 7:9506f2d84162 | 28 | while(1) { |
| jford38 | 7:9506f2d84162 | 29 | if(!pb_r) { wait(1); return MULTI_PLAYER; } // Delay to allow user time to stop pushing the button before the game starts! |
| jford38 | 7:9506f2d84162 | 30 | if(!pb_l) { wait(1); return SINGLE_PLAYER; } // return whichever game mode the user selected. |
| jford38 | 7:9506f2d84162 | 31 | } |
| jford38 | 7:9506f2d84162 | 32 | } |
| jford38 | 7:9506f2d84162 | 33 | |
| jford38 | 6:3be57cf4bd33 | 34 | void game_init(void) { |
| jford38 | 6:3be57cf4bd33 | 35 | |
| jford38 | 6:3be57cf4bd33 | 36 | led1 = 0; led2 = 0; led3 = 0; led4 = 0; |
| jford38 | 6:3be57cf4bd33 | 37 | |
| jford38 | 6:3be57cf4bd33 | 38 | pb_l.mode(PullUp); |
| jford38 | 6:3be57cf4bd33 | 39 | pb_r.mode(PullUp); |
| jford38 | 6:3be57cf4bd33 | 40 | pb_u.mode(PullUp); |
| jford38 | 6:3be57cf4bd33 | 41 | pb_d.mode(PullUp); |
| jford38 | 6:3be57cf4bd33 | 42 | |
| jford38 | 6:3be57cf4bd33 | 43 | pc.printf("\033[2J\033[0;0H"); // Clear the terminal screen. |
| jford38 | 6:3be57cf4bd33 | 44 | pc.printf("I'm alive! Player 1\n"); // Let us know you made it this far. |
| jford38 | 7:9506f2d84162 | 45 | int mode = game_menu(); |
| jford38 | 8:e6dd05393290 | 46 | sync.init(&uLCD, mode); // Connect to the other player. |
| jford38 | 6:3be57cf4bd33 | 47 | pc.printf("Initialized...\n"); // Let us know you finished initializing. |
| jford38 | 6:3be57cf4bd33 | 48 | } |
| jford38 | 6:3be57cf4bd33 | 49 | |
| jford38 | 0:899c85cd266f | 50 | int main (void) { |
| jford38 | 8:e6dd05393290 | 51 | int* p2_buttons; |
| jford38 | 8:e6dd05393290 | 52 | |
| jford38 | 8:e6dd05393290 | 53 | long int tick, pre_tick; |
| jford38 | 8:e6dd05393290 | 54 | srand (time(NULL)); |
| jford38 | 8:e6dd05393290 | 55 | Timer timer; |
| jford38 | 8:e6dd05393290 | 56 | |
| jford38 | 8:e6dd05393290 | 57 | |
| jford38 | 8:e6dd05393290 | 58 | game_init(); |
| jford38 | 8:e6dd05393290 | 59 | |
| jford38 | 8:e6dd05393290 | 60 | timer.start(); |
| jford38 | 8:e6dd05393290 | 61 | tick = timer.read_ms(); |
| jford38 | 8:e6dd05393290 | 62 | pre_tick = tick; |
| jford38 | 0:899c85cd266f | 63 | |
| jford38 | 5:cfec780c935b | 64 | |
| jford38 | 5:cfec780c935b | 65 | float theta = 0; |
| jford38 | 0:899c85cd266f | 66 | while(1) { |
| jford38 | 3:3ddefff03cb2 | 67 | |
| jford38 | 6:3be57cf4bd33 | 68 | sync.line(64,64,64+50*cos(theta),64+50*sin(theta), BLACK); |
| jford38 | 5:cfec780c935b | 69 | theta += 0.05; |
| jford38 | 6:3be57cf4bd33 | 70 | sync.line(64,64,64+50*cos(theta),64+50*sin(theta), RED); |
| jford38 | 6:3be57cf4bd33 | 71 | sync.circle(10,10,100, GREEN); |
| jford38 | 6:3be57cf4bd33 | 72 | sync.filled_circle(4,4,10, 0xAB); |
| jford38 | 6:3be57cf4bd33 | 73 | sync.triangle(10,10, 20,20, 20,40, 0xAB); |
| jford38 | 6:3be57cf4bd33 | 74 | sync.rectangle(100,100, 90,90, GREEN); |
| jford38 | 6:3be57cf4bd33 | 75 | sync.filled_rectangle(100,100, 110,110, 0xAB); |
| jford38 | 6:3be57cf4bd33 | 76 | sync.pixel(40, 40, WHITE); |
| jford38 | 3:3ddefff03cb2 | 77 | |
| jford38 | 8:e6dd05393290 | 78 | |
| jford38 | 8:e6dd05393290 | 79 | tick = timer.read_ms(); // Read current time |
| jford38 | 8:e6dd05393290 | 80 | if(tick-pre_tick < 100){;} |
| jford38 | 8:e6dd05393290 | 81 | pre_tick = tick; |
| jford38 | 7:9506f2d84162 | 82 | |
| jford38 | 8:e6dd05393290 | 83 | sync.update(); |
| jford38 | 8:e6dd05393290 | 84 | p2_buttons = sync.get_button_state(); |
| jford38 | 8:e6dd05393290 | 85 | |
| jford38 | 8:e6dd05393290 | 86 | led1 = p2_buttons[0] ^ !pb_u; |
| jford38 | 8:e6dd05393290 | 87 | led2 = p2_buttons[1] ^ !pb_r; |
| jford38 | 8:e6dd05393290 | 88 | led3 = p2_buttons[2] ^ !pb_d; |
| jford38 | 8:e6dd05393290 | 89 | led4 = p2_buttons[3] ^ !pb_l; |
| jford38 | 8:e6dd05393290 | 90 | |
| jford38 | 5:cfec780c935b | 91 | //pc.printf("Button State: %x %x %x %x %x\n", buttons[0], buttons[1], buttons[2], buttons[3], buttons[4]); |
| jford38 | 5:cfec780c935b | 92 | //pc.printf("\033[2J\033[0;0H"); |
| jford38 | 3:3ddefff03cb2 | 93 | } |
| jford38 | 0:899c85cd266f | 94 | } |
