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
- Committer:
- jford38
- Date:
- 2015-10-26
- Revision:
- 14:36c306e26317
- Parent:
- 12:088a8203a9bb
- Child:
- 15:4b27a3a95772
File content as of revision 14:36c306e26317:
// Student Side. #include "mbed.h" #include "game_synchronizer.h" #include "tank.h" #include "globals.h" #include <vector> DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); DigitalIn pb_l(p24); // left button DigitalIn pb_r(p21); // right button DigitalIn pb_u(p22); // up button DigitalIn pb_d(p23); // down button //MMA8452 acc(p28, p27, 100000); // Accelerometer uLCD_4DGL uLCD(p9,p10,p11); // LCD (serial tx, serial rx, reset pin;) Game_Synchronizer sync(PLAYER1); // (tx, rx, rst, player mode) Timer frame_timer; // For debug only. Don't use in production code. It will slow your game down a lot. Serial pc(USBTX, USBRX); int game_menu(void) { // Figure out what mode the game will be run in. uLCD.current_orientation = IS_LANDSCAPE; 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"); // Right button -> Multiplayer // Left button -> Single player while(1) { if(!pb_r) { return MULTI_PLAYER; } if(!pb_l) { return SINGLE_PLAYER; } } } void map_init() { sync.background_color(SKY_COLOR); sync.cls(); sync.filled_rectangle(0,0,128,20, GND_COLOR); sync.filled_rectangle(59, 20, 69, 60, BLACK); sync.locate(0,0); sync.textbackground_color(SKY_COLOR); sync.puts("Pocket Tanks 2035", sizeof("Pocket Tanks 2035")); sync.update(); } class Bullet{ public: int x0; int y0; float vx0; float vy0; int x; int y; int speed; float time; bool in_flight; Tank* tank; Bullet(Tank* t) { tank = t; speed = 30; in_flight = false; } void shoot(void) { if(in_flight) {return;} time = 0; tank->barrel_end(x0, y0); x = x0; y = y0; vx0 = speed*cos(tank->barrel_theta); vy0 = speed*sin(tank->barrel_theta); in_flight = true; } void time_step(float dt) { if(!in_flight) {return;} time += dt; int new_x = x0 + vx0*time; int new_y = y0 + vy0*time + 0.5*(-9.81)*time*time; if(new_x == x && new_y == y) { // Didn't move! return; } if(new_x < 0 || new_x > 128 || new_y < 0 || new_y > 128) { in_flight = false; return; } int col = sync.read_pixel(new_x, new_y); if(col != CONVERT_24_TO_16_BPP(SKY_COLOR)) { if(col == CONVERT_24_TO_16_BPP(TANK_BLUE)) { pc.printf("Player 1 wins!\n"); } sync.pixel(x, y, SKY_COLOR); sync.filled_circle(new_x, new_y, 4, SKY_COLOR); in_flight = false; return; } sync.pixel(x, y, SKY_COLOR); x = new_x; y = new_y; sync.pixel(x, y, BLACK); } }; void game_init(void) { led1 = 0; led2 = 0; led3 = 0; led4 = 0; pb_l.mode(PullUp); pb_r.mode(PullUp); pb_u.mode(PullUp); pb_d.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, mode); // Connect to the other player. map_init(); pc.printf("Initialized...\n"); // Let us know you finished initializing. } int main (void) { int* p2_buttons; game_init(); Tank t1(4, 21, 12, 8, TANK_RED); Tank t2(111, 21, 12, 8, TANK_BLUE); Bullet b1(&t1); Bullet b2(&t2); frame_timer.start(); while(1) { //double acc_x, acc_y, acc_z; //acc.readXYZGravity(&acc_x,&acc_y,&acc_z); //read accelerometer //pc.printf("ACCELERATION X:%f Y:%f Z:%f\n", acc_x, acc_y, acc_z); p2_buttons = sync.get_button_state(); led1 = p2_buttons[0] ^ !pb_u; led2 = p2_buttons[1] ^ !pb_r; led3 = p2_buttons[2] ^ !pb_d; led4 = p2_buttons[3] ^ !pb_l; if(!pb_l) t1.reposition(t1.x-1, t1.y, t1.barrel_theta); if(!pb_r) t1.reposition(t1.x+1, t1.y, t1.barrel_theta); if(p2_buttons[3]) t2.reposition(t2.x-1, t2.y, t2.barrel_theta); if(p2_buttons[1]) t2.reposition(t2.x+1, t2.y, t2.barrel_theta); //if(!pb_d) t1.reposition(t1.x, t1.y, t1.barrel_theta-PI/30.0); if(!pb_u) t1.reposition(t1.x, t1.y, t1.barrel_theta+PI/30.0); if(p2_buttons[0]) t2.reposition(t2.x, t2.y, t2.barrel_theta+PI/30.0); //if(p2_buttons[2]) t2.reposition(t2.x, t2.y, t2.barrel_theta-PI/30.0); if(!pb_d) { b1.shoot(); } if(p2_buttons[2]) { b2.shoot(); } b1.time_step(frame_timer.read()); b2.time_step(frame_timer.read()); frame_timer.reset(); sync.update(); } }