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:
- 20:6a58052b0140
- Parent:
- 19:7aa3af04d6a8
- Child:
- 21:edfeb289b21f
--- a/main.cpp Wed Oct 28 08:40:55 2015 +0000
+++ b/main.cpp Thu Oct 29 02:21:11 2015 +0000
@@ -7,7 +7,7 @@
#include "game_synchronizer.h"
#include "tank.h"
#include "bullet.h"
-#include "globals.h"
+#include "misc.h"
DigitalOut led1(LED1);
@@ -29,9 +29,12 @@
Game_Synchronizer sync(PLAYER1); // Game_Synchronizer (PLAYER)
Timer frame_timer; // Timer
+// 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.
// 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,
@@ -84,7 +87,9 @@
// Before you write text on the screens, tell the LCD where to put it.
sync.locate(0,15);
+ // Set the text background color to match the sky. Just for looks.
sync.textbackground_color(SKY_COLOR);
+ // Display the game title.
char title[] = " ECE 2035 Tanks";
sync.puts(title, sizeof(title));
sync.update();
@@ -126,8 +131,12 @@
}
int main (void) {
- int* p1_inputs;
- int* p2_inputs;
+
+ int* p1_buttons;
+ int* p2_buttons;
+
+ float ax1, ay1, az1;
+ float ax2, ay2, az2;
game_init();
@@ -138,28 +147,21 @@
frame_timer.start();
- while(1) {
+ while(true) {
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];
+ p1_buttons = sync.get_p1_buttons();
+ p2_buttons = sync.get_p2_buttons();
+
+ sync.get_p1_accel_data(ax1, ay1, az1);
+ sync.get_p1_accel_data(ax2, ay2, az2);
- 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);
+ led1 = p2_buttons[0] ^ p2_buttons[0];
+ led2 = p2_buttons[1] ^ p2_buttons[1];
+ led3 = p2_buttons[2] ^ p2_buttons[2];
+ led4 = p2_buttons[3] ^ p2_buttons[3];
+
if(whose_turn == PLAYER1) {
if(ax1 > ACC_THRESHOLD) { t1.reposition(-1, 0, 0); }
@@ -168,12 +170,12 @@
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]) {
+ if(p1_buttons[D_BUTTON]) {
b1.shoot();
}
- float time = frame_timer.read();
- int pix_color = b1.time_step(time);
+ 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)) {
sync.update();
@@ -195,12 +197,12 @@
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])) {
+ if((sync.play_mode == MULTI_PLAYER && p2_buttons[D_BUTTON]) || (sync.play_mode == SINGLE_PLAYER && p1_buttons[D_BUTTON])) {
b2.shoot();
}
- float time = frame_timer.read();
- int pix_color = b2.time_step(time);
+ 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)) {
sync.update();
@@ -214,7 +216,7 @@
sync.update();
}
- playSound("/sd/BUZZER.wav");
+ playSound("/sd/wavfiles/BUZZER.wav");
// This dies after a while, and I have NO IDEA WHY.
int i = 0;
