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
Diff: main.cpp
- Revision:
- 23:77049670cae6
- Parent:
- 22:3c68eea5a609
- Child:
- 24:2100c9e8ec81
--- a/main.cpp Thu Oct 29 05:14:49 2015 +0000
+++ b/main.cpp Fri Oct 30 08:54:10 2015 +0000
@@ -31,7 +31,8 @@
// Global variables go here.
-
+int winner = -1;
+int whose_turn = PLAYER1;
// Ask the user whether to run the game in Single- or Multi-Player mode.
@@ -52,8 +53,12 @@
// Button Example:
// Use !pb_r to access the player1 right button value.
- wait(2);
- return SINGLE_PLAYER;
+
+ wait(2); // Eventually you can take this out.
+
+ // Eventually you should return SINGLE_PLAYER or MULTI_PLAYER
+ // depending on the user's choice.
+ return SINGLE_PLAYER;
}
// Initialize the world map. I've provided a basic map here,
@@ -89,10 +94,11 @@
// Display the game title.
char title[] = " Title";
sync.puts(title, sizeof(title));
- sync.update();
+
+ // Flush the draw buffer and execute all the accumulated draw commands.
+ sync.update();
}
-
// Initialize the game hardware.
// Call game_menu to find out which mode to play the game in (Single- or Multi-Player)
// Initialize the game synchronizer.
@@ -138,42 +144,79 @@
Bullet b1(&t1);
Bullet b2(&t2);
+ frame_timer.start();
+ // Your code starts here...
while(true) {
+ s
+ // Get a pointer to the buttons for both sides.
+ // From now on, you can access the buttons for player x using
+ //
+ // px_buttons[U_BUTTON]
+ // px_buttons[R_BUTTON]
+ // px_buttons[D_BUTTON]
+ // px_buttons[L_BUTTON]
- // Read the buttons/accelerometer and store the values
- // in the synchronizer's internal array.
- sync.set_p1_inputs();
-
- // Get a pointer to the buttons for both sides.
p1_buttons = sync.get_p1_buttons();
p2_buttons = sync.get_p2_buttons();
+
+ led1 = p1_buttons[0] ^ p2_buttons[0];
+ led2 = p1_buttons[1] ^ p2_buttons[1];
+ led3 = p1_buttons[2] ^ p2_buttons[2];
+ led4 = p1_buttons[3] ^ p2_buttons[3];
// Get the accelerometer values.
sync.get_p1_accel_data(&ax1, &ay1, &az1);
sync.get_p2_accel_data(&ax2, &ay2, &az2);
- // Game logic goes here.
-
- // Depending on whose turn it is,
- // - update their tank's position using the accelerometer state for that player.
- // - update their bullet's position using the time elapsed since the previous frame.
-
- // You have to handle the case where sync.play_mode == SINGLE_PLAYER
- // as well as the case where sync.play_mode == MULTI_PLAYER
-
- // Useful functions:
- // t1.reposition(...);
- // t2.reposition(...);
- // b1.timestep(...);
- // b2.timestep(...);
-
- // End of game logic
-
- // sync.update() flushes the internal buffer and performs all the draw commands on both sides.
- // This must be called at the end of every frame, or things won't be drawn and buttons won't get
- // updated.
- sync.update();
+ if(whose_turn == PLAYER1) {
+
+ // Accelerometer example
+ if(ax1 > ACC_THRESHOLD) {
+ // Move the tank to the right if the accelerometer is tipped far enough to the right.
+ t1.reposition(+1, 0, 0);
+ }
+
+ // Button example
+ if(p1_buttons[D_BUTTON]) {
+ b1.shoot();
+ }
+
+ float dt = frame_timer.read();
+ int intersection_code = b1.time_step(dt);
+
+ if(intersection_code != BULLET_NO_COLLISION || intersection_code == BULLET_OFF_SCREEN) {
+ pc.printf("Now it's P2's turn!\n");
+ whose_turn = PLAYER2;
+ }
+
+ // If you shot yourself, you lost.
+ if(sync.pixel_eq(intersection_code, t1.tank_color)) {
+ sync.update(); // Is this necessary?
+ winner = PLAYER2;
+ break;
+ }
+
+ // If you shot the other guy, you won!
+ if(sync.pixel_eq(intersection_code, t2.tank_color)) {
+ sync.update();
+ winner = PLAYER1;
+ break;
+ }
+ } else if(whose_turn == PLAYER2) {
+
+ // I gave you a lot of the logic for Player1. It's up to you to figure out Player2!
+ // If you are in SINGLE_PLAYER mode, you should use the p1 inputs to control p2.
+ // In MULTI_PLAYER mode, you should use the p2 inputs to control p2.
+ //
+ // Hint:
+ // sync.play_mode will tell you if you are in MULTI_PLAYER or SINGLE_PLAYER mode.
+ //
+
+ }
+
+ frame_timer.reset();
+ sync.update();
}
game_over();
