For Nikhil
Dependencies: 4DGL-uLCD-SE EthernetInterface Game_Synchronizer MMA8452 SDFileSystem mbed-rtos mbed wave_player
Fork of 2035_Tanks_Shell by
Revision 19:7aa3af04d6a8, committed 2015-10-28
- Comitter:
- jford38
- Date:
- Wed Oct 28 08:40:55 2015 +0000
- Parent:
- 18:18dfc9fb33b5
- Child:
- 20:6a58052b0140
- Commit message:
- Finished! I think. Haha :D
Changed in this revision
--- a/Bullet/bullet.cpp Wed Oct 28 06:36:24 2015 +0000 +++ b/Bullet/bullet.cpp Wed Oct 28 08:40:55 2015 +0000 @@ -22,18 +22,19 @@ } int Bullet::time_step(float dt) { - if(!in_flight) {return 0;} + if(!in_flight) {return CONVERT_24_TO_16_BPP(SKY_COLOR);} 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 0; + return CONVERT_24_TO_16_BPP(SKY_COLOR); } if(new_x < 0 || new_x > 128 || new_y < 0 || new_y > 128) { + sync.pixel(x, y, SKY_COLOR); in_flight = false; - return 0; + return CONVERT_24_TO_16_BPP(BLACK); } int col = sync.read_pixel(new_x, new_y); @@ -43,19 +44,19 @@ in_flight = false; if(col == CONVERT_24_TO_16_BPP(TANK_BLUE)) { - return TANK_BLUE; + return col; } if( col == CONVERT_24_TO_16_BPP(TANK_RED)) { - return TANK_RED; + return col; } - return 0; + return CONVERT_24_TO_16_BPP(BLACK); } sync.pixel(x, y, SKY_COLOR); x = new_x; y = new_y; sync.pixel(x, y, BLACK); - return 0; + return CONVERT_24_TO_16_BPP(SKY_COLOR); }
--- a/Game_Synchronizer.lib Wed Oct 28 06:36:24 2015 +0000 +++ b/Game_Synchronizer.lib Wed Oct 28 08:40:55 2015 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/ECE2035-Spring-2015-TA/code/Game_Synchronizer/#a44cfc9dfe58 +http://developer.mbed.org/teams/ECE2035-Spring-2015-TA/code/Game_Synchronizer/#8cad51d2f763
--- a/Speaker.h Wed Oct 28 06:36:24 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,19 +0,0 @@ -#include "mbed.h" -// new class to play a note on Speaker based on PwmOut class -class Speaker -{ -public: - Speaker(PinName pin) : _pin(pin) { -// _pin(pin) means pass pin to the Speaker Constructor - } -// class method to play a note based on PwmOut class - void PlayNote(float frequency, float duration, float volume) { - _pin.period(1.0/frequency); - _pin = volume/2.0; - wait(duration); - _pin = 0.0; - } - -private: - PwmOut _pin; -}; \ No newline at end of file
--- a/Tank/tank.cpp Wed Oct 28 06:36:24 2015 +0000 +++ b/Tank/tank.cpp Wed Oct 28 08:40:55 2015 +0000 @@ -36,7 +36,12 @@ by = y+h+wheel_rad + (barrel_length+1)*sin(barrel_theta); } -void Tank::reposition(int new_x, int new_y, float new_theta) { +void Tank::reposition(int dx, int dy, float dtheta) { + + int new_x = x+dx; + int new_y = y+dy; + float new_theta = abs(barrel_theta+dtheta); + float old_theta = barrel_theta; int old_x = x; int old_y = y; @@ -45,7 +50,7 @@ int maxx = max_x(); int maxy = max_y(); - x = new_x; y = new_y; barrel_theta = new_theta - (int)(new_theta/PI)*PI; + x = new_x; y = new_y; barrel_theta = (new_theta > PI) ? PI : new_theta; if(min_x() < 0 || max_x() > 128) { x = old_x; y = old_y; barrel_theta = old_theta;
--- 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) {