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:
- 21:edfeb289b21f
- Parent:
- 20:6a58052b0140
- Child:
- 22:3c68eea5a609
--- a/main.cpp Thu Oct 29 02:21:11 2015 +0000
+++ b/main.cpp Thu Oct 29 03:56:30 2015 +0000
@@ -95,6 +95,10 @@
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.
void game_init(void) {
led1 = 0; led2 = 0; led3 = 0; led4 = 0;
@@ -112,6 +116,7 @@
pc.printf("Initialized...\n"); // Let us know you finished initializing.
}
+// Given the filename of a .wav file in the SD card, play the file over the speaker.
void playSound(char * wav)
{
// open wav file
@@ -130,6 +135,29 @@
fclose(wave_file);
}
+// Display some kind of game over screen which lets us know who won.
+void game_over(int winner) {
+ if(winner == PLAYER1) { pc.printf("Player 1 wins!\n"); }
+ else if(winner == PLAYER2) { pc.printf("Player 2 wins!\n"); }
+
+ 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)%11;
+ wait(0.1);
+ }
+
+}
int main (void) {
int* p1_buttons;
@@ -155,7 +183,7 @@
p2_buttons = sync.get_p2_buttons();
sync.get_p1_accel_data(ax1, ay1, az1);
- sync.get_p1_accel_data(ax2, ay2, az2);
+ sync.get_p2_accel_data(ax2, ay2, az2);
led1 = p2_buttons[0] ^ p2_buttons[0];
led2 = p2_buttons[1] ^ p2_buttons[1];
@@ -175,11 +203,20 @@
}
float dt = frame_timer.read();
- int pix_color = b1.time_step(dt);
- 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)) {
+ 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 have shot yourself, you lost.
+ if(intersection_code == CONVERT_24_TO_16_BPP(t1.tank_color)) {
+ sync.update(); // Is this necessary?
+ winner = PLAYER2;
+ break;
+ }
+
+ // If you have shot the other guy, you won!
+ if(intersection_code == CONVERT_24_TO_16_BPP(t2.tank_color)) {
sync.update();
- pc.printf("P2 wins!");
winner = PLAYER1;
break;
}
@@ -202,11 +239,20 @@
}
float dt = frame_timer.read();
- int pix_color = b2.time_step(dt);
- 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)) {
+ int intersection_code = b2.time_step(dt);
+
+ if(intersection_code != BULLET_NO_COLLISION || intersection_code == BULLET_OFF_SCREEN) { pc.printf("Now it's P1's turn!\n"); whose_turn = PLAYER1; }
+
+ // If you shot yourself, you lost.
+ if(intersection_code == CONVERT_24_TO_16_BPP(t2.tank_color)) {
+ sync.update(); // Is this necessary?
+ winner = PLAYER1;
+ break;
+ }
+
+ // If you shot the other guy, you won!
+ if(intersection_code == CONVERT_24_TO_16_BPP(t1.tank_color)) {
sync.update();
- pc.printf("P1 wins!");
winner = PLAYER2;
break;
}
@@ -217,22 +263,7 @@
}
playSound("/sd/wavfiles/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);
- }
+
+ game_over(winner);
+
}
\ No newline at end of file
