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 ECE2035 Spring 2015 TA

Committer:
jford38
Date:
Wed Oct 28 08:40:55 2015 +0000
Revision:
19:7aa3af04d6a8
Parent:
18:18dfc9fb33b5
Child:
20:6a58052b0140
Finished! I think. Haha :D

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jford38 6:3be57cf4bd33 1 // Student Side.
jford38 0:899c85cd266f 2
jford38 0:899c85cd266f 3 #include "mbed.h"
jford38 15:4b27a3a95772 4
jford38 17:7bc7127782e4 5 #include "SDFileSystem.h"
jford38 17:7bc7127782e4 6 #include "wave_player.h"
jford38 6:3be57cf4bd33 7 #include "game_synchronizer.h"
jford38 14:36c306e26317 8 #include "tank.h"
jford38 15:4b27a3a95772 9 #include "bullet.h"
jford38 14:36c306e26317 10 #include "globals.h"
jford38 10:5da9b27e050e 11
jford38 17:7bc7127782e4 12
jford38 6:3be57cf4bd33 13 DigitalOut led1(LED1);
jford38 6:3be57cf4bd33 14 DigitalOut led2(LED2);
jford38 6:3be57cf4bd33 15 DigitalOut led3(LED3);
jford38 6:3be57cf4bd33 16 DigitalOut led4(LED4);
jford38 0:899c85cd266f 17
jford38 17:7bc7127782e4 18 DigitalIn pb_u(p21); // Up Button
jford38 17:7bc7127782e4 19 DigitalIn pb_r(p22); // Right Button
jford38 17:7bc7127782e4 20 DigitalIn pb_d(p23); // Down Button
jford38 17:7bc7127782e4 21 DigitalIn pb_l(p24); // Left Button
jford38 6:3be57cf4bd33 22
jford38 17:7bc7127782e4 23 Serial pc(USBTX, USBRX); // Serial connection to PC. Useful for debugging!
jford38 17:7bc7127782e4 24 MMA8452 acc(p28, p27, 100000); // Accelerometer (SDA, SCL, Baudrate)
jford38 17:7bc7127782e4 25 uLCD_4DGL uLCD(p9,p10,p11); // LCD (tx, rx, reset)
jford38 17:7bc7127782e4 26 SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD (mosi, miso, sck, cs)
jford38 17:7bc7127782e4 27 AnalogOut DACout(p18); // speaker
jford38 17:7bc7127782e4 28 wave_player player(&DACout); // wav player
jford38 17:7bc7127782e4 29 Game_Synchronizer sync(PLAYER1); // Game_Synchronizer (PLAYER)
jford38 17:7bc7127782e4 30 Timer frame_timer; // Timer
jford38 0:899c85cd266f 31
jford38 18:18dfc9fb33b5 32 int winner = -1;
jford38 19:7aa3af04d6a8 33 int whose_turn = PLAYER1;
jford38 17:7bc7127782e4 34
jford38 17:7bc7127782e4 35 // Ask the user whether to run the game in Single- or Multi-Player mode.
jford38 17:7bc7127782e4 36 // Note that this function uses uLCD instead of sync because it is only
jford38 17:7bc7127782e4 37 // writing to the local (Player1) lcd. Sync hasn't been initialized yet,
jford38 17:7bc7127782e4 38 // so you can't use it anyway. For the same reason, you must access
jford38 17:7bc7127782e4 39 // the buttons directly e.g. if( !pb_r ) { do something; }.
jford38 7:9506f2d84162 40 int game_menu(void) {
jford38 7:9506f2d84162 41
jford38 18:18dfc9fb33b5 42 uLCD.baudrate(BAUD_3000000);
jford38 9:ee330b1ba394 43 uLCD.locate(0,1);
jford38 9:ee330b1ba394 44 uLCD.puts("Select Mode:");
jford38 9:ee330b1ba394 45 uLCD.locate(0,3);
jford38 9:ee330b1ba394 46 uLCD.puts(" Single-Player:");
jford38 9:ee330b1ba394 47 uLCD.locate(0,4);
jford38 9:ee330b1ba394 48 uLCD.puts(" Left Button");
jford38 9:ee330b1ba394 49 uLCD.locate(0,6);
jford38 9:ee330b1ba394 50 uLCD.puts(" Multi-Player:");
jford38 9:ee330b1ba394 51 uLCD.locate(0,7);
jford38 9:ee330b1ba394 52 uLCD.puts(" Right Button");
jford38 17:7bc7127782e4 53
jford38 7:9506f2d84162 54 while(1) {
jford38 17:7bc7127782e4 55 if(!pb_r) { return MULTI_PLAYER; } // Right button -> Multiplayer
jford38 17:7bc7127782e4 56 if(!pb_l) { return SINGLE_PLAYER; } // Left button -> Single player
jford38 7:9506f2d84162 57 }
jford38 7:9506f2d84162 58 }
jford38 7:9506f2d84162 59
jford38 17:7bc7127782e4 60 // Initialize the world map. I've provided a basic map here,
jford38 17:7bc7127782e4 61 // but as part of the assignment you must create more
jford38 17:7bc7127782e4 62 // interesting map(s).
jford38 17:7bc7127782e4 63 // Note that calls to sync.function() will run function()
jford38 17:7bc7127782e4 64 // on both players' LCDs (assuming you are in multiplayer mode).
jford38 17:7bc7127782e4 65 // In single player mode, only your lcd will be modified. (Makes sense, right?)
jford38 10:5da9b27e050e 66 void map_init() {
jford38 17:7bc7127782e4 67
jford38 17:7bc7127782e4 68 // Fill the entire screen with sky blue.
jford38 10:5da9b27e050e 69 sync.background_color(SKY_COLOR);
jford38 17:7bc7127782e4 70
jford38 17:7bc7127782e4 71 // Call the clear screen function to force the LCD to redraw the screen
jford38 17:7bc7127782e4 72 // with the new background color.
jford38 10:5da9b27e050e 73 sync.cls();
jford38 17:7bc7127782e4 74
jford38 17:7bc7127782e4 75 // Draw the ground in green.
jford38 10:5da9b27e050e 76 sync.filled_rectangle(0,0,128,20, GND_COLOR);
jford38 17:7bc7127782e4 77
jford38 17:7bc7127782e4 78 // Draw a wall in the middle of the map. It doesn't have to be black,
jford38 17:7bc7127782e4 79 // but it shouldn't be the same color as the sky or your tanks.
jford38 17:7bc7127782e4 80 // Get creative here. You could use brown and grey to draw a mountain
jford38 17:7bc7127782e4 81 // or something cool like that.
jford38 10:5da9b27e050e 82 sync.filled_rectangle(59, 20, 69, 60, BLACK);
jford38 17:7bc7127782e4 83
jford38 17:7bc7127782e4 84 // Before you write text on the screens, tell the LCD where to put it.
jford38 17:7bc7127782e4 85 sync.locate(0,15);
jford38 17:7bc7127782e4 86
jford38 14:36c306e26317 87 sync.textbackground_color(SKY_COLOR);
jford38 17:7bc7127782e4 88 char title[] = " ECE 2035 Tanks";
jford38 17:7bc7127782e4 89 sync.puts(title, sizeof(title));
jford38 10:5da9b27e050e 90 sync.update();
jford38 10:5da9b27e050e 91 }
jford38 10:5da9b27e050e 92
jford38 6:3be57cf4bd33 93 void game_init(void) {
jford38 6:3be57cf4bd33 94
jford38 6:3be57cf4bd33 95 led1 = 0; led2 = 0; led3 = 0; led4 = 0;
jford38 6:3be57cf4bd33 96
jford38 17:7bc7127782e4 97 pb_u.mode(PullUp);
jford38 6:3be57cf4bd33 98 pb_r.mode(PullUp);
jford38 17:7bc7127782e4 99 pb_d.mode(PullUp);
jford38 17:7bc7127782e4 100 pb_l.mode(PullUp);
jford38 6:3be57cf4bd33 101
jford38 6:3be57cf4bd33 102 pc.printf("\033[2J\033[0;0H"); // Clear the terminal screen.
jford38 6:3be57cf4bd33 103 pc.printf("I'm alive! Player 1\n"); // Let us know you made it this far.
jford38 7:9506f2d84162 104 int mode = game_menu();
jford38 17:7bc7127782e4 105 sync.init(&uLCD, &acc, &pb_u, &pb_r, &pb_d, &pb_l, mode); // Connect to the other player.
jford38 10:5da9b27e050e 106 map_init();
jford38 6:3be57cf4bd33 107 pc.printf("Initialized...\n"); // Let us know you finished initializing.
jford38 6:3be57cf4bd33 108 }
jford38 6:3be57cf4bd33 109
jford38 19:7aa3af04d6a8 110 void playSound(char * wav)
jford38 19:7aa3af04d6a8 111 {
jford38 19:7aa3af04d6a8 112 // open wav file
jford38 19:7aa3af04d6a8 113 FILE *wave_file;
jford38 19:7aa3af04d6a8 114 wave_file=fopen(wav,"r");
jford38 19:7aa3af04d6a8 115
jford38 19:7aa3af04d6a8 116 if(wave_file == NULL){
jford38 19:7aa3af04d6a8 117 uLCD.locate(0,4);
jford38 19:7aa3af04d6a8 118 uLCD.printf("Error in SD");
jford38 19:7aa3af04d6a8 119 return;
jford38 19:7aa3af04d6a8 120 }
jford38 19:7aa3af04d6a8 121 // play wav file
jford38 19:7aa3af04d6a8 122 player.play(wave_file);
jford38 19:7aa3af04d6a8 123
jford38 19:7aa3af04d6a8 124 // close wav file
jford38 19:7aa3af04d6a8 125 fclose(wave_file);
jford38 19:7aa3af04d6a8 126 }
jford38 19:7aa3af04d6a8 127
jford38 0:899c85cd266f 128 int main (void) {
jford38 17:7bc7127782e4 129 int* p1_inputs;
jford38 17:7bc7127782e4 130 int* p2_inputs;
jford38 8:e6dd05393290 131
jford38 8:e6dd05393290 132 game_init();
jford38 8:e6dd05393290 133
jford38 18:18dfc9fb33b5 134 Tank t1(4, 21, 12, 8, TANK_RED); // (min_x, min_y, width, height, color)
jford38 18:18dfc9fb33b5 135 Tank t2(111, 21, 12, 8, TANK_BLUE); // (min_x, min_y, width, height, color)
jford38 12:088a8203a9bb 136 Bullet b1(&t1);
jford38 12:088a8203a9bb 137 Bullet b2(&t2);
jford38 12:088a8203a9bb 138
jford38 5:cfec780c935b 139
jford38 12:088a8203a9bb 140 frame_timer.start();
jford38 0:899c85cd266f 141 while(1) {
jford38 12:088a8203a9bb 142
jford38 17:7bc7127782e4 143 sync.set_p1_inputs();
jford38 17:7bc7127782e4 144
jford38 17:7bc7127782e4 145 p1_inputs = sync.get_p1_inputs();
jford38 17:7bc7127782e4 146 p2_inputs = sync.get_p2_inputs();
jford38 17:7bc7127782e4 147
jford38 17:7bc7127782e4 148
jford38 17:7bc7127782e4 149 led1 = p2_inputs[0] ^ p1_inputs[0];
jford38 17:7bc7127782e4 150 led2 = p2_inputs[1] ^ p1_inputs[1];
jford38 17:7bc7127782e4 151 led3 = p2_inputs[2] ^ p1_inputs[2];
jford38 17:7bc7127782e4 152 led4 = p2_inputs[3] ^ p1_inputs[3];
jford38 8:e6dd05393290 153
jford38 17:7bc7127782e4 154 float ax1 = (float) p1_inputs[4] / 65536.0;
jford38 17:7bc7127782e4 155 float ay1 = (float) p1_inputs[5] / 65536.0;
jford38 17:7bc7127782e4 156 float az1 = (float) p1_inputs[6] / 65536.0;
jford38 18:18dfc9fb33b5 157 //pc.printf("P1 X: %f\tY: %f\tZ: %f\n", ax1, ay1, az1);
jford38 11:f455e907baba 158
jford38 17:7bc7127782e4 159 float ax2 = (float) p2_inputs[4] / 65536.0;
jford38 17:7bc7127782e4 160 float ay2 = (float) p2_inputs[5] / 65536.0;
jford38 17:7bc7127782e4 161 float az2 = (float) p2_inputs[6] / 65536.0;
jford38 18:18dfc9fb33b5 162 //pc.printf("P2 X: %f\tY: %f\tZ: %f\n", ax2, ay2, az2);
jford38 17:7bc7127782e4 163
jford38 19:7aa3af04d6a8 164 if(whose_turn == PLAYER1) {
jford38 19:7aa3af04d6a8 165 if(ax1 > ACC_THRESHOLD) { t1.reposition(-1, 0, 0); }
jford38 19:7aa3af04d6a8 166 if(ax1 < -ACC_THRESHOLD) { t1.reposition(+1, 0, 0); }
jford38 19:7aa3af04d6a8 167
jford38 19:7aa3af04d6a8 168 if(ay1 > ACC_THRESHOLD) { t1.reposition(0, 0, +PI/30.0); }
jford38 19:7aa3af04d6a8 169 if(ay1 < -ACC_THRESHOLD) { t1.reposition(0, 0, -PI/30.0); }
jford38 19:7aa3af04d6a8 170
jford38 19:7aa3af04d6a8 171 if(p1_inputs[D_BUTTON]) {
jford38 19:7aa3af04d6a8 172 b1.shoot();
jford38 19:7aa3af04d6a8 173 }
jford38 19:7aa3af04d6a8 174
jford38 19:7aa3af04d6a8 175 float time = frame_timer.read();
jford38 19:7aa3af04d6a8 176 int pix_color = b1.time_step(time);
jford38 19:7aa3af04d6a8 177 if(pix_color != CONVERT_24_TO_16_BPP(SKY_COLOR)) { pc.printf("Now it's P1's turn!\n"); whose_turn = PLAYER2; }
jford38 19:7aa3af04d6a8 178 if(pix_color == CONVERT_24_TO_16_BPP(t1.tank_color) || pix_color == CONVERT_24_TO_16_BPP(t2.tank_color)) {
jford38 19:7aa3af04d6a8 179 sync.update();
jford38 19:7aa3af04d6a8 180 pc.printf("P2 wins!");
jford38 19:7aa3af04d6a8 181 winner = PLAYER1;
jford38 19:7aa3af04d6a8 182 break;
jford38 19:7aa3af04d6a8 183 }
jford38 19:7aa3af04d6a8 184 } else if(whose_turn == PLAYER2) {
jford38 19:7aa3af04d6a8 185
jford38 19:7aa3af04d6a8 186 if((sync.play_mode == MULTI_PLAYER && ax2 > ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ax1 > ACC_THRESHOLD)) {
jford38 19:7aa3af04d6a8 187 t2.reposition(-1, 0, 0);
jford38 19:7aa3af04d6a8 188 }
jford38 19:7aa3af04d6a8 189 if((sync.play_mode == MULTI_PLAYER && ax2 < -ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ax1 < -ACC_THRESHOLD)) {
jford38 19:7aa3af04d6a8 190 t2.reposition(+1, 0, 0);
jford38 19:7aa3af04d6a8 191 }
jford38 19:7aa3af04d6a8 192 if((sync.play_mode == MULTI_PLAYER && ay2 > ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ay1 > ACC_THRESHOLD)) {
jford38 19:7aa3af04d6a8 193 t2.reposition(0, 0, -PI/30.0);
jford38 19:7aa3af04d6a8 194 }
jford38 19:7aa3af04d6a8 195 if((sync.play_mode == MULTI_PLAYER && ay2 < -ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ay1 < -ACC_THRESHOLD)) {
jford38 19:7aa3af04d6a8 196 t2.reposition(0, 0, +PI/30.0);
jford38 19:7aa3af04d6a8 197 }
jford38 19:7aa3af04d6a8 198 if((sync.play_mode == MULTI_PLAYER && p2_inputs[D_BUTTON]) || (sync.play_mode == SINGLE_PLAYER && p1_inputs[D_BUTTON])) {
jford38 19:7aa3af04d6a8 199 b2.shoot();
jford38 19:7aa3af04d6a8 200 }
jford38 19:7aa3af04d6a8 201
jford38 19:7aa3af04d6a8 202 float time = frame_timer.read();
jford38 19:7aa3af04d6a8 203 int pix_color = b2.time_step(time);
jford38 19:7aa3af04d6a8 204 if(pix_color != CONVERT_24_TO_16_BPP(SKY_COLOR)) { pc.printf("Now it's P1's turn!\n"); whose_turn = PLAYER1; }
jford38 19:7aa3af04d6a8 205 if(pix_color == CONVERT_24_TO_16_BPP(t1.tank_color) || pix_color == CONVERT_24_TO_16_BPP(t2.tank_color)) {
jford38 19:7aa3af04d6a8 206 sync.update();
jford38 19:7aa3af04d6a8 207 pc.printf("P1 wins!");
jford38 19:7aa3af04d6a8 208 winner = PLAYER2;
jford38 19:7aa3af04d6a8 209 break;
jford38 19:7aa3af04d6a8 210 }
jford38 19:7aa3af04d6a8 211 }
jford38 12:088a8203a9bb 212
jford38 18:18dfc9fb33b5 213 frame_timer.reset();
jford38 12:088a8203a9bb 214 sync.update();
jford38 3:3ddefff03cb2 215 }
jford38 15:4b27a3a95772 216
jford38 19:7aa3af04d6a8 217 playSound("/sd/BUZZER.wav");
jford38 19:7aa3af04d6a8 218
jford38 17:7bc7127782e4 219 // This dies after a while, and I have NO IDEA WHY.
jford38 15:4b27a3a95772 220 int i = 0;
jford38 15:4b27a3a95772 221 while(1) {
jford38 15:4b27a3a95772 222 sync.cls();
jford38 17:7bc7127782e4 223 sync.locate(i, 9);
jford38 18:18dfc9fb33b5 224 if(winner == PLAYER1) {
jford38 18:18dfc9fb33b5 225 char msg[] = "P1 Wins!";
jford38 18:18dfc9fb33b5 226 sync.puts(msg, sizeof(msg));
jford38 18:18dfc9fb33b5 227 } else if(winner == PLAYER2) {
jford38 18:18dfc9fb33b5 228 char msg[] = "P2 Wins!";
jford38 18:18dfc9fb33b5 229 sync.puts(msg, sizeof(msg));
jford38 18:18dfc9fb33b5 230 }
jford38 18:18dfc9fb33b5 231
jford38 15:4b27a3a95772 232 sync.update();
jford38 17:7bc7127782e4 233 i = (i+1)%10;
jford38 15:4b27a3a95772 234 wait(0.1);
jford38 15:4b27a3a95772 235 }
jford38 0:899c85cd266f 236 }