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@7:9506f2d84162, 2015-10-22 (annotated)
- Committer:
- jford38
- Date:
- Thu Oct 22 09:14:47 2015 +0000
- Revision:
- 7:9506f2d84162
- Parent:
- 6:3be57cf4bd33
- Child:
- 8:e6dd05393290
Now contains a switch for single vs. multiplayer modes. ; At the beginning of the game, the master must select the play mode.
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 | 6:3be57cf4bd33 | 16 | Game_Synchronizer sync(p9,p10,p11, PLAYER1); // (tx, rx, rst, player mode) |
jford38 | 0:899c85cd266f | 17 | |
jford38 | 6:3be57cf4bd33 | 18 | // For debug only. Don't use in production code. It will slow your game down a lot. |
jford38 | 6:3be57cf4bd33 | 19 | Serial pc(USBTX, USBRX); |
jford38 | 0:899c85cd266f | 20 | |
jford38 | 7:9506f2d84162 | 21 | |
jford38 | 7:9506f2d84162 | 22 | int game_menu(void) { |
jford38 | 7:9506f2d84162 | 23 | |
jford38 | 7:9506f2d84162 | 24 | // Figure out what mode the game will be run in. |
jford38 | 7:9506f2d84162 | 25 | // Right button -> Multiplayer |
jford38 | 7:9506f2d84162 | 26 | // Left button -> Single player |
jford38 | 7:9506f2d84162 | 27 | while(1) { |
jford38 | 7:9506f2d84162 | 28 | 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 | 29 | if(!pb_l) { wait(1); return SINGLE_PLAYER; } // return whichever game mode the user selected. |
jford38 | 7:9506f2d84162 | 30 | } |
jford38 | 7:9506f2d84162 | 31 | } |
jford38 | 7:9506f2d84162 | 32 | |
jford38 | 6:3be57cf4bd33 | 33 | void game_init(void) { |
jford38 | 6:3be57cf4bd33 | 34 | |
jford38 | 6:3be57cf4bd33 | 35 | led1 = 0; led2 = 0; led3 = 0; led4 = 0; |
jford38 | 6:3be57cf4bd33 | 36 | |
jford38 | 6:3be57cf4bd33 | 37 | pb_l.mode(PullUp); |
jford38 | 6:3be57cf4bd33 | 38 | pb_r.mode(PullUp); |
jford38 | 6:3be57cf4bd33 | 39 | pb_u.mode(PullUp); |
jford38 | 6:3be57cf4bd33 | 40 | pb_d.mode(PullUp); |
jford38 | 6:3be57cf4bd33 | 41 | |
jford38 | 6:3be57cf4bd33 | 42 | pc.printf("\033[2J\033[0;0H"); // Clear the terminal screen. |
jford38 | 6:3be57cf4bd33 | 43 | pc.printf("I'm alive! Player 1\n"); // Let us know you made it this far. |
jford38 | 7:9506f2d84162 | 44 | int mode = game_menu(); |
jford38 | 7:9506f2d84162 | 45 | sync.init(mode); // Connect to the other player. |
jford38 | 6:3be57cf4bd33 | 46 | pc.printf("Initialized...\n"); // Let us know you finished initializing. |
jford38 | 6:3be57cf4bd33 | 47 | } |
jford38 | 6:3be57cf4bd33 | 48 | |
jford38 | 0:899c85cd266f | 49 | int main (void) { |
jford38 | 1:0589ea36661b | 50 | int* buttons; |
jford38 | 0:899c85cd266f | 51 | |
jford38 | 6:3be57cf4bd33 | 52 | game_init(); |
jford38 | 5:cfec780c935b | 53 | |
jford38 | 5:cfec780c935b | 54 | float theta = 0; |
jford38 | 0:899c85cd266f | 55 | while(1) { |
jford38 | 3:3ddefff03cb2 | 56 | |
jford38 | 6:3be57cf4bd33 | 57 | sync.line(64,64,64+50*cos(theta),64+50*sin(theta), BLACK); |
jford38 | 5:cfec780c935b | 58 | theta += 0.05; |
jford38 | 6:3be57cf4bd33 | 59 | sync.line(64,64,64+50*cos(theta),64+50*sin(theta), RED); |
jford38 | 6:3be57cf4bd33 | 60 | sync.circle(10,10,100, GREEN); |
jford38 | 6:3be57cf4bd33 | 61 | sync.filled_circle(4,4,10, 0xAB); |
jford38 | 6:3be57cf4bd33 | 62 | sync.triangle(10,10, 20,20, 20,40, 0xAB); |
jford38 | 6:3be57cf4bd33 | 63 | sync.rectangle(100,100, 90,90, GREEN); |
jford38 | 6:3be57cf4bd33 | 64 | sync.filled_rectangle(100,100, 110,110, 0xAB); |
jford38 | 6:3be57cf4bd33 | 65 | sync.pixel(40, 40, WHITE); |
jford38 | 3:3ddefff03cb2 | 66 | |
jford38 | 6:3be57cf4bd33 | 67 | sync.update(); |
jford38 | 6:3be57cf4bd33 | 68 | buttons = sync.get_button_state(); |
jford38 | 7:9506f2d84162 | 69 | |
jford38 | 6:3be57cf4bd33 | 70 | led1 = buttons[0]; |
jford38 | 6:3be57cf4bd33 | 71 | led2 = buttons[1]; |
jford38 | 6:3be57cf4bd33 | 72 | led3 = buttons[2]; |
jford38 | 6:3be57cf4bd33 | 73 | led4 = buttons[3]; |
jford38 | 5:cfec780c935b | 74 | //pc.printf("Button State: %x %x %x %x %x\n", buttons[0], buttons[1], buttons[2], buttons[3], buttons[4]); |
jford38 | 5:cfec780c935b | 75 | //pc.printf("\033[2J\033[0;0H"); |
jford38 | 3:3ddefff03cb2 | 76 | } |
jford38 | 0:899c85cd266f | 77 | } |