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:
- 19:7aa3af04d6a8
- Parent:
- 18:18dfc9fb33b5
- Child:
- 20:6a58052b0140
--- a/main.cpp Wed Oct 28 06:36:24 2015 +0000
+++ b/main.cpp Wed Oct 28 08:40:55 2015 +0000
@@ -3,9 +3,7 @@
#include "mbed.h"
#include "SDFileSystem.h"
-#include "Speaker.h"
#include "wave_player.h"
-
#include "game_synchronizer.h"
#include "tank.h"
#include "bullet.h"
@@ -32,6 +30,7 @@
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
@@ -108,6 +107,24 @@
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;
@@ -144,41 +161,61 @@
float az2 = (float) p2_inputs[6] / 65536.0;
//pc.printf("P2 X: %f\tY: %f\tZ: %f\n", ax2, ay2, az2);
-
- if(ax1 > ACC_THRESHOLD) { t1.reposition(t1.x-1, t1.y, t1.barrel_theta); }
- if(ax1 < -ACC_THRESHOLD) { t1.reposition(t1.x+1, t1.y, t1.barrel_theta); }
- if(ax2 > ACC_THRESHOLD) { t2.reposition(t2.x-1, t2.y, t2.barrel_theta); }
- if(ax2 < -ACC_THRESHOLD) { t2.reposition(t2.x+1, t2.y, t2.barrel_theta); }
-
- if(ay1 > ACC_THRESHOLD) { t1.reposition(t1.x, t1.y, t1.barrel_theta+PI/30.0); }
- if(ay1 < -ACC_THRESHOLD) { t1.reposition(t1.x, t1.y, t1.barrel_theta-PI/30.0); }
-
- if(ay2 > ACC_THRESHOLD) { t2.reposition(t2.x, t2.y, t2.barrel_theta+PI/30.0); }
- if(ay2 < -ACC_THRESHOLD) { t2.reposition(t2.x, t2.y, t2.barrel_theta-PI/30.0); }
-
- if(p1_inputs[D_BUTTON]) { b1.shoot(); }
- if(p2_inputs[D_BUTTON]) { b2.shoot(); }
+ 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;
+ }
+ }
- float time = frame_timer.read();
- int retval = b1.time_step(time);
- if(retval == t2.tank_color) {
- sync.update();
- pc.printf("P1 wins!");
- winner = PLAYER1;
- break;
- }
- retval = b2.time_step(time);
- if(retval == t1.tank_color) {
- sync.update();
- pc.printf("P2 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) {
