Shell code for the Tanks game

Dependencies:   4DGL-uLCD-SE EthernetInterface Game_Synchronizer MMA8452 SDFileSystem mbed-rtos mbed wave_player

Committer:
jford38
Date:
Thu Oct 22 21:34:00 2015 +0000
Revision:
9:ee330b1ba394
Parent:
8:e6dd05393290
Child:
10:5da9b27e050e
Added game_init() to set up buttons & leds (and eventually other stuff); Added game_menu() to display a splash screen and ask whether to play in single- or multi-player mode.; Fixed a bug in background_color().

Who changed what in which revision?

UserRevisionLine numberNew 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 9:ee330b1ba394 26
jford38 9:ee330b1ba394 27 uLCD.locate(0,1);
jford38 9:ee330b1ba394 28 uLCD.puts("Select Mode:");
jford38 9:ee330b1ba394 29 uLCD.locate(0,3);
jford38 9:ee330b1ba394 30 uLCD.puts(" Single-Player:");
jford38 9:ee330b1ba394 31 uLCD.locate(0,4);
jford38 9:ee330b1ba394 32 uLCD.puts(" Left Button");
jford38 9:ee330b1ba394 33 uLCD.locate(0,6);
jford38 9:ee330b1ba394 34 uLCD.puts(" Multi-Player:");
jford38 9:ee330b1ba394 35 uLCD.locate(0,7);
jford38 9:ee330b1ba394 36 uLCD.puts(" Right Button");
jford38 7:9506f2d84162 37 // Right button -> Multiplayer
jford38 7:9506f2d84162 38 // Left button -> Single player
jford38 7:9506f2d84162 39 while(1) {
jford38 9:ee330b1ba394 40 if(!pb_r) { return MULTI_PLAYER; }
jford38 9:ee330b1ba394 41 if(!pb_l) { return SINGLE_PLAYER; }
jford38 7:9506f2d84162 42 }
jford38 7:9506f2d84162 43 }
jford38 7:9506f2d84162 44
jford38 6:3be57cf4bd33 45 void game_init(void) {
jford38 6:3be57cf4bd33 46
jford38 6:3be57cf4bd33 47 led1 = 0; led2 = 0; led3 = 0; led4 = 0;
jford38 6:3be57cf4bd33 48
jford38 6:3be57cf4bd33 49 pb_l.mode(PullUp);
jford38 6:3be57cf4bd33 50 pb_r.mode(PullUp);
jford38 6:3be57cf4bd33 51 pb_u.mode(PullUp);
jford38 6:3be57cf4bd33 52 pb_d.mode(PullUp);
jford38 6:3be57cf4bd33 53
jford38 6:3be57cf4bd33 54 pc.printf("\033[2J\033[0;0H"); // Clear the terminal screen.
jford38 6:3be57cf4bd33 55 pc.printf("I'm alive! Player 1\n"); // Let us know you made it this far.
jford38 7:9506f2d84162 56 int mode = game_menu();
jford38 9:ee330b1ba394 57 sync.init(&uLCD, mode); // Connect to the other player.
jford38 9:ee330b1ba394 58 //sync.cls();
jford38 6:3be57cf4bd33 59 pc.printf("Initialized...\n"); // Let us know you finished initializing.
jford38 6:3be57cf4bd33 60 }
jford38 6:3be57cf4bd33 61
jford38 0:899c85cd266f 62 int main (void) {
jford38 8:e6dd05393290 63 int* p2_buttons;
jford38 8:e6dd05393290 64
jford38 8:e6dd05393290 65 game_init();
jford38 8:e6dd05393290 66
jford38 9:ee330b1ba394 67 float theta = 0;
jford38 9:ee330b1ba394 68 bool first = true;
jford38 5:cfec780c935b 69
jford38 0:899c85cd266f 70 while(1) {
jford38 3:3ddefff03cb2 71
jford38 9:ee330b1ba394 72 char howdy[] = "Howdy!";
jford38 9:ee330b1ba394 73 if(first) {
jford38 9:ee330b1ba394 74 sync.background_color(BLUE);
jford38 9:ee330b1ba394 75 sync.cls();
jford38 9:ee330b1ba394 76
jford38 9:ee330b1ba394 77 sync.locate(5,0);
jford38 9:ee330b1ba394 78 sync.puts(howdy, sizeof(howdy));
jford38 9:ee330b1ba394 79 first = false;
jford38 9:ee330b1ba394 80 }
jford38 9:ee330b1ba394 81 sync.line(64,64,64+50*cos(theta),64+50*sin(theta), BLUE);
jford38 5:cfec780c935b 82 theta += 0.05;
jford38 6:3be57cf4bd33 83 sync.line(64,64,64+50*cos(theta),64+50*sin(theta), RED);
jford38 6:3be57cf4bd33 84 sync.circle(10,10,100, GREEN);
jford38 9:ee330b1ba394 85 sync.filled_circle(4,4,10, RED);
jford38 9:ee330b1ba394 86 sync.triangle(10,10, 20,20, 20,40, RED);
jford38 6:3be57cf4bd33 87 sync.rectangle(100,100, 90,90, GREEN);
jford38 9:ee330b1ba394 88 sync.filled_rectangle(100,100, 110,110, RED);
jford38 6:3be57cf4bd33 89 sync.pixel(40, 40, WHITE);
jford38 3:3ddefff03cb2 90
jford38 9:ee330b1ba394 91 sync.update();
jford38 8:e6dd05393290 92
jford38 8:e6dd05393290 93 p2_buttons = sync.get_button_state();
jford38 8:e6dd05393290 94
jford38 8:e6dd05393290 95 led1 = p2_buttons[0] ^ !pb_u;
jford38 8:e6dd05393290 96 led2 = p2_buttons[1] ^ !pb_r;
jford38 8:e6dd05393290 97 led3 = p2_buttons[2] ^ !pb_d;
jford38 8:e6dd05393290 98 led4 = p2_buttons[3] ^ !pb_l;
jford38 8:e6dd05393290 99
jford38 5:cfec780c935b 100 //pc.printf("Button State: %x %x %x %x %x\n", buttons[0], buttons[1], buttons[2], buttons[3], buttons[4]);
jford38 5:cfec780c935b 101 //pc.printf("\033[2J\033[0;0H");
jford38 3:3ddefff03cb2 102 }
jford38 0:899c85cd266f 103 }