Revenge of the Mouse
Dependencies: 4DGL-uLCD-SE EthernetInterface Game_Synchronizer LCD_fonts MMA8452 SDFileSystem mbed-rtos mbed wave_player
Fork of 2035_Tanks_Shell by
Revision 17:7bc7127782e4, committed 2015-10-28
- Comitter:
- jford38
- Date:
- Wed Oct 28 04:30:22 2015 +0000
- Parent:
- 16:b73f0842d3cd
- Child:
- 18:18dfc9fb33b5
- Commit message:
- Works with updates to game synchronizer. The synchronizer now contains buttons/accelerometers.
Changed in this revision
--- a/4DGL-uLCD-SE.lib Tue Oct 27 18:10:03 2015 +0000 +++ b/4DGL-uLCD-SE.lib Wed Oct 28 04:30:22 2015 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/teams/ECE2035-Spring-2015-TA/code/4DGL-uLCD-SE/#58a2d5f16fcb +https://developer.mbed.org/teams/ECE2035-Spring-2015-TA/code/4DGL-uLCD-SE/#b41b53f1c366
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Bullet/bullet.cpp Wed Oct 28 04:30:22 2015 +0000 @@ -0,0 +1,56 @@ +#include "uLCD_4DGL.h" +#include "bullet.h" +#include "game_synchronizer.h" +#include "globals.h" +#include "math.h" + +extern Game_Synchronizer sync; + +Bullet::Bullet(Tank* t) { + tank = t; + speed = 30; + in_flight = false; +} +void Bullet::shoot(void) { + if(in_flight) {return;} + time = 0; + tank->barrel_end(x0, y0); + x = x0; y = y0; + vx0 = speed*cos(tank->barrel_theta); + vy0 = speed*sin(tank->barrel_theta); + in_flight = true; +} + +int Bullet::time_step(float dt) { + if(!in_flight) {return 0;} + 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; + } + + if(new_x < 0 || new_x > 128 || new_y < 0 || new_y > 128) { + in_flight = false; + return 0; + } + + int col = sync.read_pixel(new_x, new_y); + if(col != CONVERT_24_TO_16_BPP(SKY_COLOR)) { + sync.pixel(x, y, SKY_COLOR); + sync.filled_circle(new_x, new_y, 4, SKY_COLOR); + in_flight = false; + + if(col == CONVERT_24_TO_16_BPP(TANK_BLUE)) { + return 1; + } + return 0; + } + sync.pixel(x, y, SKY_COLOR); + + x = new_x; + y = new_y; + sync.pixel(x, y, BLACK); + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Bullet/bullet.h Wed Oct 28 04:30:22 2015 +0000 @@ -0,0 +1,25 @@ +#ifndef BULLET_H__ +#define BULLET_H__ + +#include "tank.h" + +class Bullet{ + public: + int x0; + int y0; + float vx0; + float vy0; + int x; + int y; + int speed; + float time; + bool in_flight; + + Tank* tank; + + Bullet(Tank* t); + void shoot(void); + int time_step(float dt); +}; + +#endif \ No newline at end of file
--- a/Game_Synchronizer.lib Tue Oct 27 18:10:03 2015 +0000 +++ b/Game_Synchronizer.lib Wed Oct 28 04:30:22 2015 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/ECE2035-Spring-2015-TA/code/Game_Synchronizer/#1c2e370464a9 +http://developer.mbed.org/teams/ECE2035-Spring-2015-TA/code/Game_Synchronizer/#3804888303c0
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA8452.lib Wed Oct 28 04:30:22 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/ashleymills/code/MMA8452/#a92a632a0cc7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Wed Oct 28 04:30:22 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/neilt6/code/SDFileSystem/#c2c1f0b16380
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Speaker.h Wed Oct 28 04:30:22 2015 +0000 @@ -0,0 +1,19 @@ +#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/bullet.cpp Tue Oct 27 18:10:03 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -#include "bullet.h" -#include "game_synchronizer.h" -#include "globals.h" -#include "math.h" - -extern Game_Synchronizer sync; - -Bullet::Bullet(Tank* t) { - tank = t; - speed = 30; - in_flight = false; -} -void Bullet::shoot(void) { - if(in_flight) {return;} - time = 0; - tank->barrel_end(x0, y0); - x = x0; y = y0; - vx0 = speed*cos(tank->barrel_theta); - vy0 = speed*sin(tank->barrel_theta); - in_flight = true; -} - -int Bullet::time_step(float dt) { - if(!in_flight) {return 0;} - 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; - } - - if(new_x < 0 || new_x > 128 || new_y < 0 || new_y > 128) { - in_flight = false; - return 0; - } - - int col = sync.read_pixel(new_x, new_y); - if(col != CONVERT_24_TO_16_BPP(SKY_COLOR)) { - sync.pixel(x, y, SKY_COLOR); - sync.filled_circle(new_x, new_y, 4, SKY_COLOR); - in_flight = false; - - if(col == CONVERT_24_TO_16_BPP(TANK_BLUE)) { - return 1; - } - return 0; - } - sync.pixel(x, y, SKY_COLOR); - - x = new_x; - y = new_y; - sync.pixel(x, y, BLACK); - return 0; -}
--- a/bullet.h Tue Oct 27 18:10:03 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -#ifndef BULLET_H__ -#define BULLET_H__ - -#include "tank.h" - -class Bullet{ - public: - int x0; - int y0; - float vx0; - float vy0; - int x; - int y; - int speed; - float time; - bool in_flight; - - Tank* tank; - - Bullet(Tank* t); - void shoot(void); - int time_step(float dt); -}; - -#endif \ No newline at end of file
--- a/main.cpp Tue Oct 27 18:10:03 2015 +0000 +++ b/main.cpp Wed Oct 28 04:30:22 2015 +0000 @@ -2,36 +2,44 @@ #include "mbed.h" +#include "SDFileSystem.h" +#include "Speaker.h" +#include "wave_player.h" + #include "game_synchronizer.h" #include "tank.h" #include "bullet.h" #include "globals.h" + DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); -DigitalIn pb_l(p24); // left button -DigitalIn pb_r(p21); // right button -DigitalIn pb_u(p22); // up button -DigitalIn pb_d(p23); // down button +DigitalIn pb_u(p21); // Up Button +DigitalIn pb_r(p22); // Right Button +DigitalIn pb_d(p23); // Down Button +DigitalIn pb_l(p24); // Left Button -//MMA8452 acc(p28, p27, 100000); // Accelerometer -uLCD_4DGL uLCD(p9,p10,p11); // LCD (serial tx, serial rx, reset pin;) -Game_Synchronizer sync(PLAYER1); // (tx, rx, rst, player mode) - -Timer frame_timer; - -// For debug only. Don't use in production code. It will slow your game down a lot. -Serial pc(USBTX, USBRX); +Serial pc(USBTX, USBRX); // Serial connection to PC. Useful for debugging! +MMA8452 acc(p28, p27, 100000); // Accelerometer (SDA, SCL, Baudrate) +uLCD_4DGL uLCD(p9,p10,p11); // LCD (tx, rx, reset) +SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD (mosi, miso, sck, cs) +AnalogOut DACout(p18); // speaker +wave_player player(&DACout); // wav player +Game_Synchronizer sync(PLAYER1); // Game_Synchronizer (PLAYER) +Timer frame_timer; // Timer + +// 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, +// so you can't use it anyway. For the same reason, you must access +// the buttons directly e.g. if( !pb_r ) { do something; }. int game_menu(void) { - // Figure out what mode the game will be run in. - - uLCD.current_orientation = IS_LANDSCAPE; uLCD.locate(0,1); uLCD.puts("Select Mode:"); uLCD.locate(0,3); @@ -42,46 +50,66 @@ uLCD.puts(" Multi-Player:"); uLCD.locate(0,7); uLCD.puts(" Right Button"); - // Right button -> Multiplayer - // Left button -> Single player + while(1) { - if(!pb_r) { return MULTI_PLAYER; } - if(!pb_l) { return SINGLE_PLAYER; } + if(!pb_r) { return MULTI_PLAYER; } // Right button -> Multiplayer + if(!pb_l) { return SINGLE_PLAYER; } // Left button -> Single player } } +// Initialize the world map. I've provided a basic map here, +// but as part of the assignment you must create more +// interesting map(s). +// Note that calls to sync.function() will run function() +// on both players' LCDs (assuming you are in multiplayer mode). +// In single player mode, only your lcd will be modified. (Makes sense, right?) void map_init() { + + // Fill the entire screen with sky blue. sync.background_color(SKY_COLOR); + + // Call the clear screen function to force the LCD to redraw the screen + // with the new background color. sync.cls(); + + // Draw the ground in green. sync.filled_rectangle(0,0,128,20, GND_COLOR); + + // Draw a wall in the middle of the map. It doesn't have to be black, + // but it shouldn't be the same color as the sky or your tanks. + // Get creative here. You could use brown and grey to draw a mountain + // or something cool like that. sync.filled_rectangle(59, 20, 69, 60, BLACK); - sync.locate(0,0); + + // Before you write text on the screens, tell the LCD where to put it. + sync.locate(0,15); + sync.textbackground_color(SKY_COLOR); - sync.puts("Pocket Tanks 2035"); + char title[] = " ECE 2035 Tanks"; + sync.puts(title, sizeof(title)); sync.update(); } - - void game_init(void) { led1 = 0; led2 = 0; led3 = 0; led4 = 0; - pb_l.mode(PullUp); + pb_u.mode(PullUp); pb_r.mode(PullUp); - pb_u.mode(PullUp); - pb_d.mode(PullUp); + pb_d.mode(PullUp); + pb_l.mode(PullUp); pc.printf("\033[2J\033[0;0H"); // Clear the terminal screen. pc.printf("I'm alive! Player 1\n"); // Let us know you made it this far. int mode = game_menu(); - sync.init(&uLCD, mode); // Connect to the other player. + sync.init(&uLCD, &acc, &pb_u, &pb_r, &pb_d, &pb_l, mode); // Connect to the other player. map_init(); pc.printf("Initialized...\n"); // Let us know you finished initializing. } int main (void) { - int* p2_buttons; + int* p1_inputs; + int* p2_inputs; game_init(); @@ -94,19 +122,31 @@ frame_timer.start(); while(1) { - //double acc_x, acc_y, acc_z; - //acc.readXYZGravity(&acc_x,&acc_y,&acc_z); //read accelerometer - //pc.printf("ACCELERATION X:%f Y:%f Z:%f\n", acc_x, acc_y, acc_z); + 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]; - p2_buttons = sync.get_button_state(); - led1 = p2_buttons[0] ^ !pb_u; - led2 = p2_buttons[1] ^ !pb_r; - led3 = p2_buttons[2] ^ !pb_d; - led4 = p2_buttons[3] ^ !pb_l; + 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); + + /* if(!pb_l) t1.reposition(t1.x-1, t1.y, t1.barrel_theta); if(!pb_r) t1.reposition(t1.x+1, t1.y, t1.barrel_theta); - if(p2_buttons[3]) t2.reposition(t2.x-1, t2.y, t2.barrel_theta); + if(p2_inputs[3]) t2.reposition(t2.x-1, t2.y, t2.barrel_theta); if(p2_buttons[1]) t2.reposition(t2.x+1, t2.y, t2.barrel_theta); //if(!pb_d) t1.reposition(t1.x, t1.y, t1.barrel_theta-PI/30.0); @@ -129,19 +169,21 @@ pc.printf("P2 wins!"); break; } - frame_timer.reset(); + */ + //frame_timer.reset(); sync.update(); } - + // This dies after a while, and I have NO IDEA WHY. int i = 0; while(1) { sync.cls(); - sync.locate(i, 6); - sync.puts("GAME OVER!\n"); + sync.locate(i, 9); + char msg[] = "GAME OVER!"; + sync.puts(msg, sizeof(msg)); sync.update(); - (++i) %= 10; + i = (i+1)%10; wait(0.1); } } \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wave_player.lib Wed Oct 28 04:30:22 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/sravet/code/wave_player/#acc3e18e77ad