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:
Thu Oct 29 03:56:30 2015 +0000
Revision:
21:edfeb289b21f
Parent:
20:6a58052b0140
Child:
22:3c68eea5a609
Final Version (I hope). Gonna start stripping out the code to make the shell I reckon.

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 20:6a58052b0140 10 #include "misc.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 20:6a58052b0140 32 // Global variables go here.
jford38 18:18dfc9fb33b5 33 int winner = -1;
jford38 19:7aa3af04d6a8 34 int whose_turn = PLAYER1;
jford38 17:7bc7127782e4 35
jford38 20:6a58052b0140 36
jford38 20:6a58052b0140 37
jford38 17:7bc7127782e4 38 // Ask the user whether to run the game in Single- or Multi-Player mode.
jford38 17:7bc7127782e4 39 // Note that this function uses uLCD instead of sync because it is only
jford38 17:7bc7127782e4 40 // writing to the local (Player1) lcd. Sync hasn't been initialized yet,
jford38 17:7bc7127782e4 41 // so you can't use it anyway. For the same reason, you must access
jford38 17:7bc7127782e4 42 // the buttons directly e.g. if( !pb_r ) { do something; }.
jford38 7:9506f2d84162 43 int game_menu(void) {
jford38 7:9506f2d84162 44
jford38 18:18dfc9fb33b5 45 uLCD.baudrate(BAUD_3000000);
jford38 9:ee330b1ba394 46 uLCD.locate(0,1);
jford38 9:ee330b1ba394 47 uLCD.puts("Select Mode:");
jford38 9:ee330b1ba394 48 uLCD.locate(0,3);
jford38 9:ee330b1ba394 49 uLCD.puts(" Single-Player:");
jford38 9:ee330b1ba394 50 uLCD.locate(0,4);
jford38 9:ee330b1ba394 51 uLCD.puts(" Left Button");
jford38 9:ee330b1ba394 52 uLCD.locate(0,6);
jford38 9:ee330b1ba394 53 uLCD.puts(" Multi-Player:");
jford38 9:ee330b1ba394 54 uLCD.locate(0,7);
jford38 9:ee330b1ba394 55 uLCD.puts(" Right Button");
jford38 17:7bc7127782e4 56
jford38 7:9506f2d84162 57 while(1) {
jford38 17:7bc7127782e4 58 if(!pb_r) { return MULTI_PLAYER; } // Right button -> Multiplayer
jford38 17:7bc7127782e4 59 if(!pb_l) { return SINGLE_PLAYER; } // Left button -> Single player
jford38 7:9506f2d84162 60 }
jford38 7:9506f2d84162 61 }
jford38 7:9506f2d84162 62
jford38 17:7bc7127782e4 63 // Initialize the world map. I've provided a basic map here,
jford38 17:7bc7127782e4 64 // but as part of the assignment you must create more
jford38 17:7bc7127782e4 65 // interesting map(s).
jford38 17:7bc7127782e4 66 // Note that calls to sync.function() will run function()
jford38 17:7bc7127782e4 67 // on both players' LCDs (assuming you are in multiplayer mode).
jford38 17:7bc7127782e4 68 // In single player mode, only your lcd will be modified. (Makes sense, right?)
jford38 10:5da9b27e050e 69 void map_init() {
jford38 17:7bc7127782e4 70
jford38 17:7bc7127782e4 71 // Fill the entire screen with sky blue.
jford38 10:5da9b27e050e 72 sync.background_color(SKY_COLOR);
jford38 17:7bc7127782e4 73
jford38 17:7bc7127782e4 74 // Call the clear screen function to force the LCD to redraw the screen
jford38 17:7bc7127782e4 75 // with the new background color.
jford38 10:5da9b27e050e 76 sync.cls();
jford38 17:7bc7127782e4 77
jford38 17:7bc7127782e4 78 // Draw the ground in green.
jford38 10:5da9b27e050e 79 sync.filled_rectangle(0,0,128,20, GND_COLOR);
jford38 17:7bc7127782e4 80
jford38 17:7bc7127782e4 81 // Draw a wall in the middle of the map. It doesn't have to be black,
jford38 17:7bc7127782e4 82 // but it shouldn't be the same color as the sky or your tanks.
jford38 17:7bc7127782e4 83 // Get creative here. You could use brown and grey to draw a mountain
jford38 17:7bc7127782e4 84 // or something cool like that.
jford38 10:5da9b27e050e 85 sync.filled_rectangle(59, 20, 69, 60, BLACK);
jford38 17:7bc7127782e4 86
jford38 17:7bc7127782e4 87 // Before you write text on the screens, tell the LCD where to put it.
jford38 17:7bc7127782e4 88 sync.locate(0,15);
jford38 17:7bc7127782e4 89
jford38 20:6a58052b0140 90 // Set the text background color to match the sky. Just for looks.
jford38 14:36c306e26317 91 sync.textbackground_color(SKY_COLOR);
jford38 20:6a58052b0140 92 // Display the game title.
jford38 17:7bc7127782e4 93 char title[] = " ECE 2035 Tanks";
jford38 17:7bc7127782e4 94 sync.puts(title, sizeof(title));
jford38 10:5da9b27e050e 95 sync.update();
jford38 10:5da9b27e050e 96 }
jford38 10:5da9b27e050e 97
jford38 21:edfeb289b21f 98
jford38 21:edfeb289b21f 99 // Initialize the game hardware.
jford38 21:edfeb289b21f 100 // Call game_menu to find out which mode to play the game in (Single- or Multi-Player)
jford38 21:edfeb289b21f 101 // Initialize the game synchronizer.
jford38 6:3be57cf4bd33 102 void game_init(void) {
jford38 6:3be57cf4bd33 103
jford38 6:3be57cf4bd33 104 led1 = 0; led2 = 0; led3 = 0; led4 = 0;
jford38 6:3be57cf4bd33 105
jford38 17:7bc7127782e4 106 pb_u.mode(PullUp);
jford38 6:3be57cf4bd33 107 pb_r.mode(PullUp);
jford38 17:7bc7127782e4 108 pb_d.mode(PullUp);
jford38 17:7bc7127782e4 109 pb_l.mode(PullUp);
jford38 6:3be57cf4bd33 110
jford38 6:3be57cf4bd33 111 pc.printf("\033[2J\033[0;0H"); // Clear the terminal screen.
jford38 6:3be57cf4bd33 112 pc.printf("I'm alive! Player 1\n"); // Let us know you made it this far.
jford38 7:9506f2d84162 113 int mode = game_menu();
jford38 17:7bc7127782e4 114 sync.init(&uLCD, &acc, &pb_u, &pb_r, &pb_d, &pb_l, mode); // Connect to the other player.
jford38 10:5da9b27e050e 115 map_init();
jford38 6:3be57cf4bd33 116 pc.printf("Initialized...\n"); // Let us know you finished initializing.
jford38 6:3be57cf4bd33 117 }
jford38 6:3be57cf4bd33 118
jford38 21:edfeb289b21f 119 // Given the filename of a .wav file in the SD card, play the file over the speaker.
jford38 19:7aa3af04d6a8 120 void playSound(char * wav)
jford38 19:7aa3af04d6a8 121 {
jford38 19:7aa3af04d6a8 122 // open wav file
jford38 19:7aa3af04d6a8 123 FILE *wave_file;
jford38 19:7aa3af04d6a8 124 wave_file=fopen(wav,"r");
jford38 19:7aa3af04d6a8 125
jford38 19:7aa3af04d6a8 126 if(wave_file == NULL){
jford38 19:7aa3af04d6a8 127 uLCD.locate(0,4);
jford38 19:7aa3af04d6a8 128 uLCD.printf("Error in SD");
jford38 19:7aa3af04d6a8 129 return;
jford38 19:7aa3af04d6a8 130 }
jford38 19:7aa3af04d6a8 131 // play wav file
jford38 19:7aa3af04d6a8 132 player.play(wave_file);
jford38 19:7aa3af04d6a8 133
jford38 19:7aa3af04d6a8 134 // close wav file
jford38 19:7aa3af04d6a8 135 fclose(wave_file);
jford38 19:7aa3af04d6a8 136 }
jford38 19:7aa3af04d6a8 137
jford38 21:edfeb289b21f 138 // Display some kind of game over screen which lets us know who won.
jford38 21:edfeb289b21f 139 void game_over(int winner) {
jford38 21:edfeb289b21f 140 if(winner == PLAYER1) { pc.printf("Player 1 wins!\n"); }
jford38 21:edfeb289b21f 141 else if(winner == PLAYER2) { pc.printf("Player 2 wins!\n"); }
jford38 21:edfeb289b21f 142
jford38 21:edfeb289b21f 143 int i = 0;
jford38 21:edfeb289b21f 144 while(1) {
jford38 21:edfeb289b21f 145 sync.cls();
jford38 21:edfeb289b21f 146 sync.locate(i, 9);
jford38 21:edfeb289b21f 147 if(winner == PLAYER1) {
jford38 21:edfeb289b21f 148 char msg[] = "P1 Wins!";
jford38 21:edfeb289b21f 149 sync.puts(msg, sizeof(msg));
jford38 21:edfeb289b21f 150 } else if(winner == PLAYER2) {
jford38 21:edfeb289b21f 151 char msg[] = "P2 Wins!";
jford38 21:edfeb289b21f 152 sync.puts(msg, sizeof(msg));
jford38 21:edfeb289b21f 153 }
jford38 21:edfeb289b21f 154
jford38 21:edfeb289b21f 155 sync.update();
jford38 21:edfeb289b21f 156 i = (i+1)%11;
jford38 21:edfeb289b21f 157 wait(0.1);
jford38 21:edfeb289b21f 158 }
jford38 21:edfeb289b21f 159
jford38 21:edfeb289b21f 160 }
jford38 0:899c85cd266f 161 int main (void) {
jford38 20:6a58052b0140 162
jford38 20:6a58052b0140 163 int* p1_buttons;
jford38 20:6a58052b0140 164 int* p2_buttons;
jford38 20:6a58052b0140 165
jford38 20:6a58052b0140 166 float ax1, ay1, az1;
jford38 20:6a58052b0140 167 float ax2, ay2, az2;
jford38 8:e6dd05393290 168
jford38 8:e6dd05393290 169 game_init();
jford38 8:e6dd05393290 170
jford38 18:18dfc9fb33b5 171 Tank t1(4, 21, 12, 8, TANK_RED); // (min_x, min_y, width, height, color)
jford38 18:18dfc9fb33b5 172 Tank t2(111, 21, 12, 8, TANK_BLUE); // (min_x, min_y, width, height, color)
jford38 12:088a8203a9bb 173 Bullet b1(&t1);
jford38 12:088a8203a9bb 174 Bullet b2(&t2);
jford38 12:088a8203a9bb 175
jford38 5:cfec780c935b 176
jford38 12:088a8203a9bb 177 frame_timer.start();
jford38 20:6a58052b0140 178 while(true) {
jford38 12:088a8203a9bb 179
jford38 17:7bc7127782e4 180 sync.set_p1_inputs();
jford38 17:7bc7127782e4 181
jford38 20:6a58052b0140 182 p1_buttons = sync.get_p1_buttons();
jford38 20:6a58052b0140 183 p2_buttons = sync.get_p2_buttons();
jford38 20:6a58052b0140 184
jford38 20:6a58052b0140 185 sync.get_p1_accel_data(ax1, ay1, az1);
jford38 21:edfeb289b21f 186 sync.get_p2_accel_data(ax2, ay2, az2);
jford38 8:e6dd05393290 187
jford38 20:6a58052b0140 188 led1 = p2_buttons[0] ^ p2_buttons[0];
jford38 20:6a58052b0140 189 led2 = p2_buttons[1] ^ p2_buttons[1];
jford38 20:6a58052b0140 190 led3 = p2_buttons[2] ^ p2_buttons[2];
jford38 20:6a58052b0140 191 led4 = p2_buttons[3] ^ p2_buttons[3];
jford38 20:6a58052b0140 192
jford38 17:7bc7127782e4 193
jford38 19:7aa3af04d6a8 194 if(whose_turn == PLAYER1) {
jford38 19:7aa3af04d6a8 195 if(ax1 > ACC_THRESHOLD) { t1.reposition(-1, 0, 0); }
jford38 19:7aa3af04d6a8 196 if(ax1 < -ACC_THRESHOLD) { t1.reposition(+1, 0, 0); }
jford38 19:7aa3af04d6a8 197
jford38 19:7aa3af04d6a8 198 if(ay1 > ACC_THRESHOLD) { t1.reposition(0, 0, +PI/30.0); }
jford38 19:7aa3af04d6a8 199 if(ay1 < -ACC_THRESHOLD) { t1.reposition(0, 0, -PI/30.0); }
jford38 19:7aa3af04d6a8 200
jford38 20:6a58052b0140 201 if(p1_buttons[D_BUTTON]) {
jford38 19:7aa3af04d6a8 202 b1.shoot();
jford38 19:7aa3af04d6a8 203 }
jford38 19:7aa3af04d6a8 204
jford38 20:6a58052b0140 205 float dt = frame_timer.read();
jford38 21:edfeb289b21f 206 int intersection_code = b1.time_step(dt);
jford38 21:edfeb289b21f 207
jford38 21:edfeb289b21f 208 if(intersection_code != BULLET_NO_COLLISION || intersection_code == BULLET_OFF_SCREEN) { pc.printf("Now it's P2's turn!\n"); whose_turn = PLAYER2; }
jford38 21:edfeb289b21f 209
jford38 21:edfeb289b21f 210 // If you have shot yourself, you lost.
jford38 21:edfeb289b21f 211 if(intersection_code == CONVERT_24_TO_16_BPP(t1.tank_color)) {
jford38 21:edfeb289b21f 212 sync.update(); // Is this necessary?
jford38 21:edfeb289b21f 213 winner = PLAYER2;
jford38 21:edfeb289b21f 214 break;
jford38 21:edfeb289b21f 215 }
jford38 21:edfeb289b21f 216
jford38 21:edfeb289b21f 217 // If you have shot the other guy, you won!
jford38 21:edfeb289b21f 218 if(intersection_code == CONVERT_24_TO_16_BPP(t2.tank_color)) {
jford38 19:7aa3af04d6a8 219 sync.update();
jford38 19:7aa3af04d6a8 220 winner = PLAYER1;
jford38 19:7aa3af04d6a8 221 break;
jford38 19:7aa3af04d6a8 222 }
jford38 19:7aa3af04d6a8 223 } else if(whose_turn == PLAYER2) {
jford38 19:7aa3af04d6a8 224
jford38 19:7aa3af04d6a8 225 if((sync.play_mode == MULTI_PLAYER && ax2 > ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ax1 > ACC_THRESHOLD)) {
jford38 19:7aa3af04d6a8 226 t2.reposition(-1, 0, 0);
jford38 19:7aa3af04d6a8 227 }
jford38 19:7aa3af04d6a8 228 if((sync.play_mode == MULTI_PLAYER && ax2 < -ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ax1 < -ACC_THRESHOLD)) {
jford38 19:7aa3af04d6a8 229 t2.reposition(+1, 0, 0);
jford38 19:7aa3af04d6a8 230 }
jford38 19:7aa3af04d6a8 231 if((sync.play_mode == MULTI_PLAYER && ay2 > ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ay1 > ACC_THRESHOLD)) {
jford38 19:7aa3af04d6a8 232 t2.reposition(0, 0, -PI/30.0);
jford38 19:7aa3af04d6a8 233 }
jford38 19:7aa3af04d6a8 234 if((sync.play_mode == MULTI_PLAYER && ay2 < -ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ay1 < -ACC_THRESHOLD)) {
jford38 19:7aa3af04d6a8 235 t2.reposition(0, 0, +PI/30.0);
jford38 19:7aa3af04d6a8 236 }
jford38 20:6a58052b0140 237 if((sync.play_mode == MULTI_PLAYER && p2_buttons[D_BUTTON]) || (sync.play_mode == SINGLE_PLAYER && p1_buttons[D_BUTTON])) {
jford38 19:7aa3af04d6a8 238 b2.shoot();
jford38 19:7aa3af04d6a8 239 }
jford38 19:7aa3af04d6a8 240
jford38 20:6a58052b0140 241 float dt = frame_timer.read();
jford38 21:edfeb289b21f 242 int intersection_code = b2.time_step(dt);
jford38 21:edfeb289b21f 243
jford38 21:edfeb289b21f 244 if(intersection_code != BULLET_NO_COLLISION || intersection_code == BULLET_OFF_SCREEN) { pc.printf("Now it's P1's turn!\n"); whose_turn = PLAYER1; }
jford38 21:edfeb289b21f 245
jford38 21:edfeb289b21f 246 // If you shot yourself, you lost.
jford38 21:edfeb289b21f 247 if(intersection_code == CONVERT_24_TO_16_BPP(t2.tank_color)) {
jford38 21:edfeb289b21f 248 sync.update(); // Is this necessary?
jford38 21:edfeb289b21f 249 winner = PLAYER1;
jford38 21:edfeb289b21f 250 break;
jford38 21:edfeb289b21f 251 }
jford38 21:edfeb289b21f 252
jford38 21:edfeb289b21f 253 // If you shot the other guy, you won!
jford38 21:edfeb289b21f 254 if(intersection_code == CONVERT_24_TO_16_BPP(t1.tank_color)) {
jford38 19:7aa3af04d6a8 255 sync.update();
jford38 19:7aa3af04d6a8 256 winner = PLAYER2;
jford38 19:7aa3af04d6a8 257 break;
jford38 19:7aa3af04d6a8 258 }
jford38 19:7aa3af04d6a8 259 }
jford38 12:088a8203a9bb 260
jford38 18:18dfc9fb33b5 261 frame_timer.reset();
jford38 12:088a8203a9bb 262 sync.update();
jford38 3:3ddefff03cb2 263 }
jford38 15:4b27a3a95772 264
jford38 20:6a58052b0140 265 playSound("/sd/wavfiles/BUZZER.wav");
jford38 21:edfeb289b21f 266
jford38 21:edfeb289b21f 267 game_over(winner);
jford38 21:edfeb289b21f 268
jford38 0:899c85cd266f 269 }