![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Shell code for the Tanks game
Dependencies: 4DGL-uLCD-SE EthernetInterface Game_Synchronizer MMA8452 SDFileSystem mbed-rtos mbed wave_player
main.cpp
- Committer:
- jford38
- Date:
- 2015-10-28
- Revision:
- 19:7aa3af04d6a8
- Parent:
- 18:18dfc9fb33b5
- Child:
- 20:6a58052b0140
File content as of revision 19:7aa3af04d6a8:
// Student Side. #include "mbed.h" #include "SDFileSystem.h" #include "wave_player.h" #include "game_synchronizer.h" #include "tank.h" #include "bullet.h" #include "globals.h" DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); DigitalIn pb_u(p21); // Up Button DigitalIn pb_r(p22); // Right Button DigitalIn pb_d(p23); // Down Button DigitalIn pb_l(p24); // Left Button Serial pc(USBTX, USBRX); // Serial connection to PC. Useful for debugging! MMA8452 acc(p28, p27, 100000); // Accelerometer (SDA, SCL, Baudrate) uLCD_4DGL uLCD(p9,p10,p11); // LCD (tx, rx, reset) SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD (mosi, miso, sck, cs) AnalogOut DACout(p18); // speaker wave_player player(&DACout); // wav player Game_Synchronizer sync(PLAYER1); // Game_Synchronizer (PLAYER) Timer frame_timer; // Timer int winner = -1; int whose_turn = PLAYER1; // Ask the user whether to run the game in Single- or Multi-Player mode. // Note that this function uses uLCD instead of sync because it is only // writing to the local (Player1) lcd. Sync hasn't been initialized yet, // so you can't use it anyway. For the same reason, you must access // the buttons directly e.g. if( !pb_r ) { do something; }. int game_menu(void) { uLCD.baudrate(BAUD_3000000); uLCD.locate(0,1); uLCD.puts("Select Mode:"); uLCD.locate(0,3); uLCD.puts(" Single-Player:"); uLCD.locate(0,4); uLCD.puts(" Left Button"); uLCD.locate(0,6); uLCD.puts(" Multi-Player:"); uLCD.locate(0,7); uLCD.puts(" Right Button"); while(1) { if(!pb_r) { return MULTI_PLAYER; } // Right button -> Multiplayer if(!pb_l) { return SINGLE_PLAYER; } // Left button -> Single player } } // Initialize the world map. I've provided a basic map here, // but as part of the assignment you must create more // interesting map(s). // Note that calls to sync.function() will run function() // on both players' LCDs (assuming you are in multiplayer mode). // In single player mode, only your lcd will be modified. (Makes sense, right?) void map_init() { // Fill the entire screen with sky blue. sync.background_color(SKY_COLOR); // Call the clear screen function to force the LCD to redraw the screen // with the new background color. sync.cls(); // Draw the ground in green. sync.filled_rectangle(0,0,128,20, GND_COLOR); // Draw a wall in the middle of the map. It doesn't have to be black, // but it shouldn't be the same color as the sky or your tanks. // Get creative here. You could use brown and grey to draw a mountain // or something cool like that. sync.filled_rectangle(59, 20, 69, 60, BLACK); // Before you write text on the screens, tell the LCD where to put it. sync.locate(0,15); sync.textbackground_color(SKY_COLOR); char title[] = " ECE 2035 Tanks"; sync.puts(title, sizeof(title)); sync.update(); } void game_init(void) { led1 = 0; led2 = 0; led3 = 0; led4 = 0; pb_u.mode(PullUp); pb_r.mode(PullUp); pb_d.mode(PullUp); pb_l.mode(PullUp); pc.printf("\033[2J\033[0;0H"); // Clear the terminal screen. pc.printf("I'm alive! Player 1\n"); // Let us know you made it this far. int mode = game_menu(); sync.init(&uLCD, &acc, &pb_u, &pb_r, &pb_d, &pb_l, mode); // Connect to the other player. map_init(); pc.printf("Initialized...\n"); // Let us know you finished initializing. } void playSound(char * wav) { // open wav file FILE *wave_file; wave_file=fopen(wav,"r"); if(wave_file == NULL){ uLCD.locate(0,4); uLCD.printf("Error in SD"); return; } // play wav file player.play(wave_file); // close wav file fclose(wave_file); } int main (void) { int* p1_inputs; int* p2_inputs; game_init(); Tank t1(4, 21, 12, 8, TANK_RED); // (min_x, min_y, width, height, color) Tank t2(111, 21, 12, 8, TANK_BLUE); // (min_x, min_y, width, height, color) Bullet b1(&t1); Bullet b2(&t2); frame_timer.start(); while(1) { sync.set_p1_inputs(); p1_inputs = sync.get_p1_inputs(); p2_inputs = sync.get_p2_inputs(); led1 = p2_inputs[0] ^ p1_inputs[0]; led2 = p2_inputs[1] ^ p1_inputs[1]; led3 = p2_inputs[2] ^ p1_inputs[2]; led4 = p2_inputs[3] ^ p1_inputs[3]; float ax1 = (float) p1_inputs[4] / 65536.0; float ay1 = (float) p1_inputs[5] / 65536.0; float az1 = (float) p1_inputs[6] / 65536.0; //pc.printf("P1 X: %f\tY: %f\tZ: %f\n", ax1, ay1, az1); float ax2 = (float) p2_inputs[4] / 65536.0; float ay2 = (float) p2_inputs[5] / 65536.0; float az2 = (float) p2_inputs[6] / 65536.0; //pc.printf("P2 X: %f\tY: %f\tZ: %f\n", ax2, ay2, az2); if(whose_turn == PLAYER1) { if(ax1 > ACC_THRESHOLD) { t1.reposition(-1, 0, 0); } if(ax1 < -ACC_THRESHOLD) { t1.reposition(+1, 0, 0); } if(ay1 > ACC_THRESHOLD) { t1.reposition(0, 0, +PI/30.0); } if(ay1 < -ACC_THRESHOLD) { t1.reposition(0, 0, -PI/30.0); } if(p1_inputs[D_BUTTON]) { b1.shoot(); } float time = frame_timer.read(); int pix_color = b1.time_step(time); if(pix_color != CONVERT_24_TO_16_BPP(SKY_COLOR)) { pc.printf("Now it's P1's turn!\n"); whose_turn = PLAYER2; } if(pix_color == CONVERT_24_TO_16_BPP(t1.tank_color) || pix_color == CONVERT_24_TO_16_BPP(t2.tank_color)) { sync.update(); pc.printf("P2 wins!"); winner = PLAYER1; break; } } else if(whose_turn == PLAYER2) { if((sync.play_mode == MULTI_PLAYER && ax2 > ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ax1 > ACC_THRESHOLD)) { t2.reposition(-1, 0, 0); } if((sync.play_mode == MULTI_PLAYER && ax2 < -ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ax1 < -ACC_THRESHOLD)) { t2.reposition(+1, 0, 0); } if((sync.play_mode == MULTI_PLAYER && ay2 > ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ay1 > ACC_THRESHOLD)) { t2.reposition(0, 0, -PI/30.0); } if((sync.play_mode == MULTI_PLAYER && ay2 < -ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ay1 < -ACC_THRESHOLD)) { t2.reposition(0, 0, +PI/30.0); } if((sync.play_mode == MULTI_PLAYER && p2_inputs[D_BUTTON]) || (sync.play_mode == SINGLE_PLAYER && p1_inputs[D_BUTTON])) { b2.shoot(); } float time = frame_timer.read(); int pix_color = b2.time_step(time); if(pix_color != CONVERT_24_TO_16_BPP(SKY_COLOR)) { pc.printf("Now it's P1's turn!\n"); whose_turn = PLAYER1; } if(pix_color == CONVERT_24_TO_16_BPP(t1.tank_color) || pix_color == CONVERT_24_TO_16_BPP(t2.tank_color)) { sync.update(); pc.printf("P1 wins!"); winner = PLAYER2; break; } } frame_timer.reset(); sync.update(); } playSound("/sd/BUZZER.wav"); // This dies after a while, and I have NO IDEA WHY. int i = 0; while(1) { sync.cls(); sync.locate(i, 9); if(winner == PLAYER1) { char msg[] = "P1 Wins!"; sync.puts(msg, sizeof(msg)); } else if(winner == PLAYER2) { char msg[] = "P2 Wins!"; sync.puts(msg, sizeof(msg)); } sync.update(); i = (i+1)%10; wait(0.1); } }