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
main.cpp@28:8dbb85f35be6, 2017-03-13 (annotated)
- Committer:
- macenzofan
- Date:
- Mon Mar 13 00:15:36 2017 +0000
- Revision:
- 28:8dbb85f35be6
- Parent:
- 27:bd55ab4d137c
Classic dueling tanks game for bed.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jford38 | 6:3be57cf4bd33 | 1 | // Student Side. |
jford38 | 0:899c85cd266f | 2 | |
jford38 | 0:899c85cd266f | 3 | #include "mbed.h" |
macenzofan | 28:8dbb85f35be6 | 4 | #include "DRV2605.h" |
jford38 | 17:7bc7127782e4 | 5 | #include "SDFileSystem.h" |
jford38 | 17:7bc7127782e4 | 6 | #include "wave_player.h" |
macenzofan | 28:8dbb85f35be6 | 7 | #include "playSound.h" |
macenzofan | 28:8dbb85f35be6 | 8 | #include "SparkfunAnalogJoystick.h" |
jford38 | 6:3be57cf4bd33 | 9 | #include "game_synchronizer.h" |
jford38 | 14:36c306e26317 | 10 | #include "tank.h" |
jford38 | 15:4b27a3a95772 | 11 | #include "bullet.h" |
jford38 | 27:bd55ab4d137c | 12 | #include "globals.h" |
jford38 | 10:5da9b27e050e | 13 | |
macenzofan | 28:8dbb85f35be6 | 14 | SparkfunAnalogJoystick joystick(p15, p16, p17); |
jford38 | 6:3be57cf4bd33 | 15 | DigitalOut led1(LED1); |
jford38 | 6:3be57cf4bd33 | 16 | DigitalOut led2(LED2); |
jford38 | 6:3be57cf4bd33 | 17 | DigitalOut led3(LED3); |
jford38 | 6:3be57cf4bd33 | 18 | DigitalOut led4(LED4); |
macenzofan | 28:8dbb85f35be6 | 19 | DRV2605 haptics(p9, p10); |
jford38 | 0:899c85cd266f | 20 | |
jford38 | 17:7bc7127782e4 | 21 | DigitalIn pb_u(p21); // Up Button |
jford38 | 17:7bc7127782e4 | 22 | DigitalIn pb_r(p22); // Right Button |
jford38 | 17:7bc7127782e4 | 23 | DigitalIn pb_d(p23); // Down Button |
jford38 | 17:7bc7127782e4 | 24 | DigitalIn pb_l(p24); // Left Button |
jford38 | 6:3be57cf4bd33 | 25 | |
jford38 | 17:7bc7127782e4 | 26 | Serial pc(USBTX, USBRX); // Serial connection to PC. Useful for debugging! |
jford38 | 17:7bc7127782e4 | 27 | MMA8452 acc(p28, p27, 100000); // Accelerometer (SDA, SCL, Baudrate) |
macenzofan | 28:8dbb85f35be6 | 28 | uLCD_4DGL uLCD(p13,p14,p11); // LCD (tx, rx, reset) |
jford38 | 17:7bc7127782e4 | 29 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD (mosi, miso, sck, cs) |
jford38 | 17:7bc7127782e4 | 30 | AnalogOut DACout(p18); // speaker |
jford38 | 17:7bc7127782e4 | 31 | wave_player player(&DACout); // wav player |
jford38 | 17:7bc7127782e4 | 32 | Game_Synchronizer sync(PLAYER1); // Game_Synchronizer (PLAYER) |
jford38 | 17:7bc7127782e4 | 33 | Timer frame_timer; // Timer |
jford38 | 0:899c85cd266f | 34 | |
macenzofan | 28:8dbb85f35be6 | 35 | |
jford38 | 20:6a58052b0140 | 36 | // Global variables go here. |
jford38 | 22:3c68eea5a609 | 37 | |
jford38 | 23:77049670cae6 | 38 | int winner = -1; |
jford38 | 23:77049670cae6 | 39 | int whose_turn = PLAYER1; |
macenzofan | 28:8dbb85f35be6 | 40 | int menu=1; |
macenzofan | 28:8dbb85f35be6 | 41 | int t2minpixelcolor; |
macenzofan | 28:8dbb85f35be6 | 42 | int t1maxpixelcolor; |
macenzofan | 28:8dbb85f35be6 | 43 | int t1y; |
macenzofan | 28:8dbb85f35be6 | 44 | int t2y; |
macenzofan | 28:8dbb85f35be6 | 45 | |
jford38 | 20:6a58052b0140 | 46 | |
jford38 | 20:6a58052b0140 | 47 | |
jford38 | 17:7bc7127782e4 | 48 | // Ask the user whether to run the game in Single- or Multi-Player mode. |
jford38 | 17:7bc7127782e4 | 49 | // Note that this function uses uLCD instead of sync because it is only |
jford38 | 17:7bc7127782e4 | 50 | // writing to the local (Player1) lcd. Sync hasn't been initialized yet, |
jford38 | 17:7bc7127782e4 | 51 | // so you can't use it anyway. For the same reason, you must access |
jford38 | 17:7bc7127782e4 | 52 | // the buttons directly e.g. if( !pb_r ) { do something; }. |
jford38 | 22:3c68eea5a609 | 53 | |
jford38 | 22:3c68eea5a609 | 54 | // return MULTI_PLAYER if the user selects multi-player. |
jford38 | 22:3c68eea5a609 | 55 | // return SINGLE_PLAYER if the user selects single-player. |
jford38 | 7:9506f2d84162 | 56 | int game_menu(void) { |
jford38 | 7:9506f2d84162 | 57 | |
jford38 | 18:18dfc9fb33b5 | 58 | uLCD.baudrate(BAUD_3000000); |
jford38 | 22:3c68eea5a609 | 59 | |
jford38 | 22:3c68eea5a609 | 60 | // the locate command tells the screen where to place the text. |
macenzofan | 28:8dbb85f35be6 | 61 | uLCD.filled_rectangle(11,54,117,99,BLACK); |
macenzofan | 28:8dbb85f35be6 | 62 | uLCD.rectangle(10, 55, 118, 100, WHITE); |
macenzofan | 28:8dbb85f35be6 | 63 | uLCD.locate(6,8); |
macenzofan | 28:8dbb85f35be6 | 64 | uLCD.puts("1 Player"); |
macenzofan | 28:8dbb85f35be6 | 65 | uLCD.locate(6,9); |
macenzofan | 28:8dbb85f35be6 | 66 | uLCD.puts("2 Player"); |
macenzofan | 28:8dbb85f35be6 | 67 | uLCD.locate(6,10); |
macenzofan | 28:8dbb85f35be6 | 68 | uLCD.puts("Settings"); |
macenzofan | 28:8dbb85f35be6 | 69 | uLCD.color(RED); |
macenzofan | 28:8dbb85f35be6 | 70 | uLCD.circle(25, 67, 1, RED); |
macenzofan | 28:8dbb85f35be6 | 71 | uLCD.circle(25, 75, 1, RED); |
macenzofan | 28:8dbb85f35be6 | 72 | uLCD.circle(25, 83, 1, RED); |
macenzofan | 28:8dbb85f35be6 | 73 | uLCD.circle(25, 67, 2, WHITE); |
macenzofan | 28:8dbb85f35be6 | 74 | uLCD.text_width(2); |
macenzofan | 28:8dbb85f35be6 | 75 | uLCD.text_height(2); |
macenzofan | 28:8dbb85f35be6 | 76 | uLCD.locate(1,1); |
macenzofan | 28:8dbb85f35be6 | 77 | uLCD.puts("DUELING"); |
macenzofan | 28:8dbb85f35be6 | 78 | uLCD.locate(2,2); |
macenzofan | 28:8dbb85f35be6 | 79 | uLCD.text_bold(TEXTBOLD); |
macenzofan | 28:8dbb85f35be6 | 80 | uLCD.puts("TANKS"); |
macenzofan | 28:8dbb85f35be6 | 81 | uLCD.color(WHITE); |
macenzofan | 28:8dbb85f35be6 | 82 | //playSound("/sd/wavfiles/Dueling Banjo.wav"); |
macenzofan | 28:8dbb85f35be6 | 83 | |
macenzofan | 28:8dbb85f35be6 | 84 | |
macenzofan | 28:8dbb85f35be6 | 85 | while(true){ |
macenzofan | 28:8dbb85f35be6 | 86 | if (menu==1){ |
macenzofan | 28:8dbb85f35be6 | 87 | if (!pb_d){ |
macenzofan | 28:8dbb85f35be6 | 88 | uLCD.circle(25, 67, 2, BLACK); |
macenzofan | 28:8dbb85f35be6 | 89 | uLCD.circle(25, 75, 2, WHITE); |
macenzofan | 28:8dbb85f35be6 | 90 | uLCD.circle(25, 83, 2, BLACK); |
macenzofan | 28:8dbb85f35be6 | 91 | wait(.25); |
macenzofan | 28:8dbb85f35be6 | 92 | menu=2; |
macenzofan | 28:8dbb85f35be6 | 93 | } |
macenzofan | 28:8dbb85f35be6 | 94 | if (!pb_u){ |
macenzofan | 28:8dbb85f35be6 | 95 | uLCD.circle(25, 67, 2, BLACK); |
macenzofan | 28:8dbb85f35be6 | 96 | uLCD.circle(25, 75, 2, BLACK); |
macenzofan | 28:8dbb85f35be6 | 97 | uLCD.circle(25, 83, 2, WHITE); |
macenzofan | 28:8dbb85f35be6 | 98 | wait(.25); |
macenzofan | 28:8dbb85f35be6 | 99 | menu=3; |
macenzofan | 28:8dbb85f35be6 | 100 | } |
macenzofan | 28:8dbb85f35be6 | 101 | if (!pb_r){ |
macenzofan | 28:8dbb85f35be6 | 102 | return SINGLE_PLAYER; |
macenzofan | 28:8dbb85f35be6 | 103 | } |
macenzofan | 28:8dbb85f35be6 | 104 | } |
macenzofan | 28:8dbb85f35be6 | 105 | if (menu==2){ |
macenzofan | 28:8dbb85f35be6 | 106 | if (!pb_d){ |
macenzofan | 28:8dbb85f35be6 | 107 | uLCD.circle(25, 67, 2, BLACK); |
macenzofan | 28:8dbb85f35be6 | 108 | uLCD.circle(25, 75, 2, BLACK); |
macenzofan | 28:8dbb85f35be6 | 109 | uLCD.circle(25, 83, 2, WHITE); |
macenzofan | 28:8dbb85f35be6 | 110 | wait(.25); |
macenzofan | 28:8dbb85f35be6 | 111 | menu=3; |
macenzofan | 28:8dbb85f35be6 | 112 | } |
macenzofan | 28:8dbb85f35be6 | 113 | if (!pb_u){ |
macenzofan | 28:8dbb85f35be6 | 114 | uLCD.circle(25, 67, 2, WHITE); |
macenzofan | 28:8dbb85f35be6 | 115 | uLCD.circle(25, 75, 2, BLACK); |
macenzofan | 28:8dbb85f35be6 | 116 | uLCD.circle(25, 83, 2, BLACK); |
macenzofan | 28:8dbb85f35be6 | 117 | wait(.25); |
macenzofan | 28:8dbb85f35be6 | 118 | menu=1; |
macenzofan | 28:8dbb85f35be6 | 119 | } |
macenzofan | 28:8dbb85f35be6 | 120 | if(!pb_r){ |
macenzofan | 28:8dbb85f35be6 | 121 | return MULTI_PLAYER; |
macenzofan | 28:8dbb85f35be6 | 122 | } |
macenzofan | 28:8dbb85f35be6 | 123 | } |
macenzofan | 28:8dbb85f35be6 | 124 | if (menu==3){ |
macenzofan | 28:8dbb85f35be6 | 125 | if (!pb_d){ |
macenzofan | 28:8dbb85f35be6 | 126 | uLCD.circle(25, 67, 2, WHITE); |
macenzofan | 28:8dbb85f35be6 | 127 | uLCD.circle(25, 75, 2, BLACK); |
macenzofan | 28:8dbb85f35be6 | 128 | uLCD.circle(25, 83, 2, BLACK); |
macenzofan | 28:8dbb85f35be6 | 129 | wait(.25); |
macenzofan | 28:8dbb85f35be6 | 130 | menu=1; |
macenzofan | 28:8dbb85f35be6 | 131 | } |
macenzofan | 28:8dbb85f35be6 | 132 | if (!pb_u){ |
macenzofan | 28:8dbb85f35be6 | 133 | uLCD.circle(25, 67, 2, BLACK); |
macenzofan | 28:8dbb85f35be6 | 134 | uLCD.circle(25, 75, 2, WHITE); |
macenzofan | 28:8dbb85f35be6 | 135 | uLCD.circle(25, 83, 2, BLACK); |
macenzofan | 28:8dbb85f35be6 | 136 | wait(.25); |
macenzofan | 28:8dbb85f35be6 | 137 | menu=2; |
macenzofan | 28:8dbb85f35be6 | 138 | } |
macenzofan | 28:8dbb85f35be6 | 139 | if(!pb_r){ |
macenzofan | 28:8dbb85f35be6 | 140 | //SETTINGS(); |
macenzofan | 28:8dbb85f35be6 | 141 | } |
macenzofan | 28:8dbb85f35be6 | 142 | } |
macenzofan | 28:8dbb85f35be6 | 143 | } |
macenzofan | 28:8dbb85f35be6 | 144 | } |
jford38 | 17:7bc7127782e4 | 145 | |
macenzofan | 28:8dbb85f35be6 | 146 | //wait(10); // Eventually you can take this out. |
macenzofan | 28:8dbb85f35be6 | 147 | |
jford38 | 23:77049670cae6 | 148 | // Eventually you should return SINGLE_PLAYER or MULTI_PLAYER |
jford38 | 23:77049670cae6 | 149 | // depending on the user's choice. |
macenzofan | 28:8dbb85f35be6 | 150 | //return SINGLE_PLAYER; |
jford38 | 7:9506f2d84162 | 151 | |
jford38 | 17:7bc7127782e4 | 152 | // Initialize the world map. I've provided a basic map here, |
jford38 | 17:7bc7127782e4 | 153 | // but as part of the assignment you must create more |
jford38 | 17:7bc7127782e4 | 154 | // interesting map(s). |
jford38 | 17:7bc7127782e4 | 155 | // Note that calls to sync.function() will run function() |
jford38 | 17:7bc7127782e4 | 156 | // on both players' LCDs (assuming you are in multiplayer mode). |
jford38 | 17:7bc7127782e4 | 157 | // In single player mode, only your lcd will be modified. (Makes sense, right?) |
jford38 | 10:5da9b27e050e | 158 | void map_init() { |
jford38 | 17:7bc7127782e4 | 159 | |
jford38 | 17:7bc7127782e4 | 160 | // Fill the entire screen with sky blue. |
jford38 | 10:5da9b27e050e | 161 | sync.background_color(SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 162 | //uLCD.media_init(); |
macenzofan | 28:8dbb85f35be6 | 163 | //uLCD.set_byte_address(0x00000, 0x3B5001); |
macenzofan | 28:8dbb85f35be6 | 164 | //uLCD.display_image(64,64); |
jford38 | 17:7bc7127782e4 | 165 | |
jford38 | 17:7bc7127782e4 | 166 | // Call the clear screen function to force the LCD to redraw the screen |
jford38 | 17:7bc7127782e4 | 167 | // with the new background color. |
jford38 | 10:5da9b27e050e | 168 | sync.cls(); |
jford38 | 17:7bc7127782e4 | 169 | |
jford38 | 17:7bc7127782e4 | 170 | // Draw the ground in green. |
jford38 | 10:5da9b27e050e | 171 | sync.filled_rectangle(0,0,128,20, GND_COLOR); |
macenzofan | 28:8dbb85f35be6 | 172 | sync.line(0,20,128,20, TREE); |
jford38 | 17:7bc7127782e4 | 173 | |
jford38 | 22:3c68eea5a609 | 174 | // Draw some obstacles. They don't have to be black, |
jford38 | 22:3c68eea5a609 | 175 | // but they shouldn't be the same color as the sky or your tanks. |
jford38 | 17:7bc7127782e4 | 176 | // Get creative here. You could use brown and grey to draw a mountain |
jford38 | 17:7bc7127782e4 | 177 | // or something cool like that. |
macenzofan | 28:8dbb85f35be6 | 178 | //sync.triangle(0, 20, 128, 20, 85, 55, BROWN); |
macenzofan | 28:8dbb85f35be6 | 179 | sync.rectangle(53, 17, 76, 78, BLACK); |
macenzofan | 28:8dbb85f35be6 | 180 | sync.filled_rectangle(54, 17, 75, 77, WHITE); |
macenzofan | 28:8dbb85f35be6 | 181 | sync.filled_rectangle(65, 17, 69, 27, BLACK); |
macenzofan | 28:8dbb85f35be6 | 182 | sync.filled_rectangle(67, 32, 71, 37, BLACK); |
macenzofan | 28:8dbb85f35be6 | 183 | sync.filled_rectangle(67, 41, 71, 46, BLACK); |
macenzofan | 28:8dbb85f35be6 | 184 | sync.filled_rectangle(67, 50, 71, 55, BLACK); |
macenzofan | 28:8dbb85f35be6 | 185 | sync.filled_rectangle(67, 59, 71, 64, BLACK); |
macenzofan | 28:8dbb85f35be6 | 186 | sync.filled_rectangle(67, 68, 71, 73, BLACK); |
macenzofan | 28:8dbb85f35be6 | 187 | sync.filled_rectangle(58, 32, 62, 37, BLACK); |
macenzofan | 28:8dbb85f35be6 | 188 | sync.filled_rectangle(58, 41, 62, 46, BLACK); |
macenzofan | 28:8dbb85f35be6 | 189 | sync.filled_rectangle(58, 50, 62, 55, BLACK); |
macenzofan | 28:8dbb85f35be6 | 190 | sync.filled_rectangle(58, 59, 62, 64, BLACK); |
macenzofan | 28:8dbb85f35be6 | 191 | sync.filled_rectangle(58, 68, 62, 73, BLACK); |
macenzofan | 28:8dbb85f35be6 | 192 | sync.filled_rectangle(52, 16, 54, 20, BROWN); |
macenzofan | 28:8dbb85f35be6 | 193 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 194 | sync.filled_circle(53, 26, 6, TREE); |
macenzofan | 28:8dbb85f35be6 | 195 | sync.filled_circle(78, 19, 4, TREE); |
macenzofan | 28:8dbb85f35be6 | 196 | sync.filled_circle(1, 12, 7, TREE); |
macenzofan | 28:8dbb85f35be6 | 197 | sync.filled_circle(6, 10, 4, TREE); |
macenzofan | 28:8dbb85f35be6 | 198 | sync.filled_circle(9, 10, 4, TREE); |
macenzofan | 28:8dbb85f35be6 | 199 | sync.filled_circle(14, 10, 2, TREE); |
macenzofan | 28:8dbb85f35be6 | 200 | sync.filled_circle(100, 12, 7, TREE); |
macenzofan | 28:8dbb85f35be6 | 201 | sync.circle(100, 12, 7, SHRUB); |
macenzofan | 28:8dbb85f35be6 | 202 | sync.filled_circle(106, 10, 4, TREE); |
macenzofan | 28:8dbb85f35be6 | 203 | sync.filled_circle(90, 10, 4, TREE); |
macenzofan | 28:8dbb85f35be6 | 204 | sync.filled_circle(110, 10, 2, TREE); |
macenzofan | 28:8dbb85f35be6 | 205 | sync.circle(1, 12, 7, SHRUB); |
macenzofan | 28:8dbb85f35be6 | 206 | sync.filled_circle(115, 115, 10, YELLOW); |
macenzofan | 28:8dbb85f35be6 | 207 | sync.circle(115, 115, 10, ORANGE); |
jford38 | 17:7bc7127782e4 | 208 | |
jford38 | 17:7bc7127782e4 | 209 | // Before you write text on the screens, tell the LCD where to put it. |
macenzofan | 28:8dbb85f35be6 | 210 | sync.locate(6,15); |
jford38 | 17:7bc7127782e4 | 211 | |
jford38 | 20:6a58052b0140 | 212 | // Set the text background color to match the sky. Just for looks. |
jford38 | 14:36c306e26317 | 213 | sync.textbackground_color(SKY_COLOR); |
jford38 | 22:3c68eea5a609 | 214 | |
jford38 | 20:6a58052b0140 | 215 | // Display the game title. |
macenzofan | 28:8dbb85f35be6 | 216 | char title[] = "ROUND 1\n GO!"; |
jford38 | 17:7bc7127782e4 | 217 | sync.puts(title, sizeof(title)); |
jford38 | 23:77049670cae6 | 218 | |
jford38 | 23:77049670cae6 | 219 | // Flush the draw buffer and execute all the accumulated draw commands. |
macenzofan | 28:8dbb85f35be6 | 220 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 221 | playSound("/sd/wavfiles/boxingbell.wav"); |
macenzofan | 28:8dbb85f35be6 | 222 | sync.filled_rectangle(25, 122, 100, 128, SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 223 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 224 | wait(1); |
macenzofan | 28:8dbb85f35be6 | 225 | sync.filled_rectangle(45, 114, 75, 122, SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 226 | |
jford38 | 10:5da9b27e050e | 227 | } |
jford38 | 10:5da9b27e050e | 228 | |
jford38 | 21:edfeb289b21f | 229 | // Initialize the game hardware. |
jford38 | 21:edfeb289b21f | 230 | // Call game_menu to find out which mode to play the game in (Single- or Multi-Player) |
jford38 | 21:edfeb289b21f | 231 | // Initialize the game synchronizer. |
jford38 | 6:3be57cf4bd33 | 232 | void game_init(void) { |
jford38 | 6:3be57cf4bd33 | 233 | |
jford38 | 6:3be57cf4bd33 | 234 | led1 = 0; led2 = 0; led3 = 0; led4 = 0; |
jford38 | 6:3be57cf4bd33 | 235 | |
jford38 | 17:7bc7127782e4 | 236 | pb_u.mode(PullUp); |
jford38 | 6:3be57cf4bd33 | 237 | pb_r.mode(PullUp); |
jford38 | 17:7bc7127782e4 | 238 | pb_d.mode(PullUp); |
jford38 | 17:7bc7127782e4 | 239 | pb_l.mode(PullUp); |
jford38 | 6:3be57cf4bd33 | 240 | |
jford38 | 6:3be57cf4bd33 | 241 | pc.printf("\033[2J\033[0;0H"); // Clear the terminal screen. |
jford38 | 6:3be57cf4bd33 | 242 | pc.printf("I'm alive! Player 1\n"); // Let us know you made it this far. |
jford38 | 7:9506f2d84162 | 243 | int mode = game_menu(); |
jford38 | 17:7bc7127782e4 | 244 | sync.init(&uLCD, &acc, &pb_u, &pb_r, &pb_d, &pb_l, mode); // Connect to the other player. |
jford38 | 10:5da9b27e050e | 245 | map_init(); |
jford38 | 6:3be57cf4bd33 | 246 | pc.printf("Initialized...\n"); // Let us know you finished initializing. |
jford38 | 6:3be57cf4bd33 | 247 | } |
jford38 | 6:3be57cf4bd33 | 248 | |
jford38 | 22:3c68eea5a609 | 249 | // Display some kind of game over screen which lets us know who won. |
jford38 | 22:3c68eea5a609 | 250 | // Play a cool sound! |
macenzofan | 28:8dbb85f35be6 | 251 | void game_over(); |
jford38 | 22:3c68eea5a609 | 252 | |
jford38 | 21:edfeb289b21f | 253 | |
jford38 | 0:899c85cd266f | 254 | int main (void) { |
jford38 | 20:6a58052b0140 | 255 | |
jford38 | 20:6a58052b0140 | 256 | int* p1_buttons; |
jford38 | 20:6a58052b0140 | 257 | int* p2_buttons; |
macenzofan | 28:8dbb85f35be6 | 258 | int p1lives=2; |
macenzofan | 28:8dbb85f35be6 | 259 | int p2lives=2; |
macenzofan | 28:8dbb85f35be6 | 260 | int i=0; |
jford38 | 20:6a58052b0140 | 261 | |
jford38 | 20:6a58052b0140 | 262 | float ax1, ay1, az1; |
jford38 | 20:6a58052b0140 | 263 | float ax2, ay2, az2; |
macenzofan | 28:8dbb85f35be6 | 264 | ax1 = 0.0f; |
macenzofan | 28:8dbb85f35be6 | 265 | ay1 = 0.0f; |
macenzofan | 28:8dbb85f35be6 | 266 | ax2 = 0.0f; |
macenzofan | 28:8dbb85f35be6 | 267 | ay2 = 0.0f; |
jford38 | 8:e6dd05393290 | 268 | |
jford38 | 8:e6dd05393290 | 269 | game_init(); |
jford38 | 8:e6dd05393290 | 270 | |
jford38 | 22:3c68eea5a609 | 271 | // Create your tanks. |
macenzofan | 28:8dbb85f35be6 | 272 | Tank t1(4, 22, 16, 6, TANK_RED); // (min_x, min_y, width, height, color) |
macenzofan | 28:8dbb85f35be6 | 273 | Tank t2(108, 22, 16, 6, TANK_BLUE); // (min_x, min_y, width, height, color) |
jford38 | 22:3c68eea5a609 | 274 | |
jford38 | 22:3c68eea5a609 | 275 | // For each tank, create a bullet. |
jford38 | 12:088a8203a9bb | 276 | Bullet b1(&t1); |
jford38 | 12:088a8203a9bb | 277 | Bullet b2(&t2); |
macenzofan | 28:8dbb85f35be6 | 278 | //Bouncy b3(&t1); |
macenzofan | 28:8dbb85f35be6 | 279 | //Bouncy b4(&t2); |
jford38 | 12:088a8203a9bb | 280 | |
jford38 | 23:77049670cae6 | 281 | frame_timer.start(); |
macenzofan | 28:8dbb85f35be6 | 282 | |
jford38 | 23:77049670cae6 | 283 | // Your code starts here... |
jford38 | 20:6a58052b0140 | 284 | while(true) { |
macenzofan | 28:8dbb85f35be6 | 285 | t1y=t1.min_y(); |
macenzofan | 28:8dbb85f35be6 | 286 | t2y=t2.min_y(); |
macenzofan | 28:8dbb85f35be6 | 287 | t2minpixelcolor=sync.read_pixel(t2.min_x()-5,t2y); |
macenzofan | 28:8dbb85f35be6 | 288 | t1maxpixelcolor=sync.read_pixel(t1.max_x()+1,t1y); |
jford38 | 23:77049670cae6 | 289 | // Get a pointer to the buttons for both sides. |
jford38 | 23:77049670cae6 | 290 | // From now on, you can access the buttons for player x using |
jford38 | 23:77049670cae6 | 291 | // |
jford38 | 23:77049670cae6 | 292 | // px_buttons[U_BUTTON] |
jford38 | 23:77049670cae6 | 293 | // px_buttons[R_BUTTON] |
jford38 | 23:77049670cae6 | 294 | // px_buttons[D_BUTTON] |
jford38 | 23:77049670cae6 | 295 | // px_buttons[L_BUTTON] |
jford38 | 12:088a8203a9bb | 296 | |
jford38 | 20:6a58052b0140 | 297 | p1_buttons = sync.get_p1_buttons(); |
jford38 | 20:6a58052b0140 | 298 | p2_buttons = sync.get_p2_buttons(); |
jford38 | 23:77049670cae6 | 299 | |
jford38 | 23:77049670cae6 | 300 | led1 = p1_buttons[0] ^ p2_buttons[0]; |
jford38 | 23:77049670cae6 | 301 | led2 = p1_buttons[1] ^ p2_buttons[1]; |
jford38 | 23:77049670cae6 | 302 | led3 = p1_buttons[2] ^ p2_buttons[2]; |
jford38 | 23:77049670cae6 | 303 | led4 = p1_buttons[3] ^ p2_buttons[3]; |
jford38 | 20:6a58052b0140 | 304 | |
jford38 | 22:3c68eea5a609 | 305 | // Get the accelerometer values. |
jford38 | 22:3c68eea5a609 | 306 | sync.get_p1_accel_data(&ax1, &ay1, &az1); |
macenzofan | 28:8dbb85f35be6 | 307 | sync.get_p2_accel_data(&ax2, &ay2, &az2); |
macenzofan | 28:8dbb85f35be6 | 308 | //ay1 = joystick.xAxis()*2 -1; |
macenzofan | 28:8dbb85f35be6 | 309 | ax1 = -joystick.yAxis(); |
macenzofan | 28:8dbb85f35be6 | 310 | //ay2 = joystick.xAxis()*2 -1; |
macenzofan | 28:8dbb85f35be6 | 311 | ax2 = -joystick.yAxis(); |
macenzofan | 28:8dbb85f35be6 | 312 | |
macenzofan | 28:8dbb85f35be6 | 313 | int pix1=sync.read_pixel(64,30); |
macenzofan | 28:8dbb85f35be6 | 314 | int pix2=sync.read_pixel(64,39); |
macenzofan | 28:8dbb85f35be6 | 315 | int pix3=sync.read_pixel(64,48); |
macenzofan | 28:8dbb85f35be6 | 316 | int pix4=sync.read_pixel(64,57); |
macenzofan | 28:8dbb85f35be6 | 317 | int pix5=sync.read_pixel(64,66); |
macenzofan | 28:8dbb85f35be6 | 318 | int j; |
macenzofan | 28:8dbb85f35be6 | 319 | |
macenzofan | 28:8dbb85f35be6 | 320 | //Building collapse animation: |
macenzofan | 28:8dbb85f35be6 | 321 | if(sync.pixel_eq(pix5,SKY_COLOR)==1){ |
macenzofan | 28:8dbb85f35be6 | 322 | |
macenzofan | 28:8dbb85f35be6 | 323 | //If highest test point is hit, do the following for loop. |
macenzofan | 28:8dbb85f35be6 | 324 | for(j=0;j<10;j++){ |
macenzofan | 28:8dbb85f35be6 | 325 | |
macenzofan | 28:8dbb85f35be6 | 326 | sync.filled_rectangle(53, 78-j, 76, 80-j, SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 327 | sync.line(53, 77-j, 76, 79-j, BLACK); |
macenzofan | 28:8dbb85f35be6 | 328 | sync.filled_rectangle(54, 74-j, 75, 77-j, WHITE); |
macenzofan | 28:8dbb85f35be6 | 329 | sync.filled_circle(53, 64,6, LT_GREY); |
macenzofan | 28:8dbb85f35be6 | 330 | sync.filled_circle(76, 64,5, LT_GREY); |
macenzofan | 28:8dbb85f35be6 | 331 | sync.filled_circle(56, 64,4, MED_GREY); |
macenzofan | 28:8dbb85f35be6 | 332 | sync.filled_circle(73, 64,4, DRK_GREY); |
macenzofan | 28:8dbb85f35be6 | 333 | //playSound("/sd/wavfiles/colapse.wav"); |
macenzofan | 28:8dbb85f35be6 | 334 | haptics.play_waveform(1); |
macenzofan | 28:8dbb85f35be6 | 335 | sync.filled_circle(70, 64,5, MED_GREY); |
macenzofan | 28:8dbb85f35be6 | 336 | sync.filled_circle(64, 64,6, LT_GREY); |
macenzofan | 28:8dbb85f35be6 | 337 | |
macenzofan | 28:8dbb85f35be6 | 338 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 339 | sync.filled_circle(52,65,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 340 | sync.filled_circle(77,65,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 341 | sync.filled_circle(52,65,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 342 | sync.filled_circle(55,65,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 343 | sync.filled_circle(70,69,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 344 | sync.filled_circle(76,67,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 345 | sync.filled_circle(65,65,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 346 | sync.filled_circle(64,66,3,PIX); |
macenzofan | 28:8dbb85f35be6 | 347 | //playSound("/sd/wavfiles/colapse.wav"); |
macenzofan | 28:8dbb85f35be6 | 348 | haptics.play_waveform(1); |
macenzofan | 28:8dbb85f35be6 | 349 | } |
macenzofan | 28:8dbb85f35be6 | 350 | } |
macenzofan | 28:8dbb85f35be6 | 351 | if(sync.pixel_eq(pix4,SKY_COLOR)==1){ |
macenzofan | 28:8dbb85f35be6 | 352 | |
macenzofan | 28:8dbb85f35be6 | 353 | //If the second highest point is hit, do the following for loop. |
macenzofan | 28:8dbb85f35be6 | 354 | for(j=0;j<20;j++){ |
macenzofan | 28:8dbb85f35be6 | 355 | |
macenzofan | 28:8dbb85f35be6 | 356 | sync.filled_rectangle(53, 78-j, 76, 80-j, SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 357 | sync.line(53, 77-j, 76, 79-j, BLACK); |
macenzofan | 28:8dbb85f35be6 | 358 | sync.filled_rectangle(54, 74-j, 75, 77-j, WHITE); |
macenzofan | 28:8dbb85f35be6 | 359 | sync.filled_circle(53, 54,6, LT_GREY); |
macenzofan | 28:8dbb85f35be6 | 360 | sync.filled_circle(76, 54,5, LT_GREY); |
macenzofan | 28:8dbb85f35be6 | 361 | sync.filled_circle(56, 54,4, MED_GREY); |
macenzofan | 28:8dbb85f35be6 | 362 | sync.filled_circle(73, 54,4, DRK_GREY); |
macenzofan | 28:8dbb85f35be6 | 363 | //playSound("/sd/wavfiles/colapse.wav"); |
macenzofan | 28:8dbb85f35be6 | 364 | haptics.play_waveform(1); |
macenzofan | 28:8dbb85f35be6 | 365 | sync.filled_circle(70, 54,5, MED_GREY); |
macenzofan | 28:8dbb85f35be6 | 366 | sync.filled_circle(64, 54,6, LT_GREY); |
macenzofan | 28:8dbb85f35be6 | 367 | |
macenzofan | 28:8dbb85f35be6 | 368 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 369 | sync.filled_circle(52,55,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 370 | sync.filled_circle(77,55,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 371 | sync.filled_circle(52,55,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 372 | sync.filled_circle(60,55,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 373 | sync.filled_circle(70,59,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 374 | sync.filled_circle(76,57,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 375 | sync.filled_circle(65,55,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 376 | sync.filled_circle(64,56,3,PIX); |
macenzofan | 28:8dbb85f35be6 | 377 | sync.filled_circle(64,66,3,PIX); |
macenzofan | 28:8dbb85f35be6 | 378 | //playSound("/sd/wavfiles/colapse.wav"); |
macenzofan | 28:8dbb85f35be6 | 379 | haptics.play_waveform(1); |
macenzofan | 28:8dbb85f35be6 | 380 | } |
macenzofan | 28:8dbb85f35be6 | 381 | } |
macenzofan | 28:8dbb85f35be6 | 382 | if(sync.pixel_eq(pix3,SKY_COLOR)==1){ |
macenzofan | 28:8dbb85f35be6 | 383 | |
macenzofan | 28:8dbb85f35be6 | 384 | //If the third highest point is hit, do the following for loop. |
macenzofan | 28:8dbb85f35be6 | 385 | for(j=0;j<30;j++){ |
macenzofan | 28:8dbb85f35be6 | 386 | |
macenzofan | 28:8dbb85f35be6 | 387 | sync.filled_rectangle(53, 78-j, 76, 80-j, SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 388 | sync.line(53, 77-j, 76, 79-j, BLACK); |
macenzofan | 28:8dbb85f35be6 | 389 | sync.filled_rectangle(54, 74-j, 75, 77-j, WHITE); |
macenzofan | 28:8dbb85f35be6 | 390 | sync.filled_circle(53, 44,6, LT_GREY); |
macenzofan | 28:8dbb85f35be6 | 391 | sync.filled_circle(76, 44,5, LT_GREY); |
macenzofan | 28:8dbb85f35be6 | 392 | haptics.play_waveform(1); |
macenzofan | 28:8dbb85f35be6 | 393 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 394 | sync.filled_circle(56, 44,4, MED_GREY); |
macenzofan | 28:8dbb85f35be6 | 395 | sync.filled_circle(73, 44,4, DRK_GREY); |
macenzofan | 28:8dbb85f35be6 | 396 | //playSound("/sd/wavfiles/colapse.wav"); |
macenzofan | 28:8dbb85f35be6 | 397 | sync.filled_circle(70, 44,5, MED_GREY); |
macenzofan | 28:8dbb85f35be6 | 398 | sync.filled_circle(64, 44,6, LT_GREY); |
macenzofan | 28:8dbb85f35be6 | 399 | |
macenzofan | 28:8dbb85f35be6 | 400 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 401 | sync.filled_circle(52,45,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 402 | sync.filled_circle(77,45,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 403 | sync.filled_circle(52,45,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 404 | sync.filled_circle(60,45,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 405 | //playSound("/sd/wavfiles/colapse.wav"); |
macenzofan | 28:8dbb85f35be6 | 406 | sync.filled_circle(70,49,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 407 | sync.filled_circle(76,47,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 408 | sync.filled_circle(65,45,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 409 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 410 | haptics.play_waveform(1); |
macenzofan | 28:8dbb85f35be6 | 411 | sync.filled_circle(64,46,3,PIX); |
macenzofan | 28:8dbb85f35be6 | 412 | sync.filled_circle(64,56,3,PIX); |
macenzofan | 28:8dbb85f35be6 | 413 | sync.filled_circle(64,66,3,PIX); |
macenzofan | 28:8dbb85f35be6 | 414 | //wait(.5); |
macenzofan | 28:8dbb85f35be6 | 415 | haptics.play_waveform(1); |
macenzofan | 28:8dbb85f35be6 | 416 | } |
macenzofan | 28:8dbb85f35be6 | 417 | } |
macenzofan | 28:8dbb85f35be6 | 418 | if(sync.pixel_eq(pix2,SKY_COLOR)==1){ |
macenzofan | 28:8dbb85f35be6 | 419 | |
macenzofan | 28:8dbb85f35be6 | 420 | //If the lowest test point is hit, do the following for loop. |
macenzofan | 28:8dbb85f35be6 | 421 | for(j=0;j<40;j++){ |
macenzofan | 28:8dbb85f35be6 | 422 | |
macenzofan | 28:8dbb85f35be6 | 423 | sync.filled_rectangle(53, 78-j, 76, 80-j, SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 424 | sync.line(53, 77-j, 76, 79-j, BLACK); |
macenzofan | 28:8dbb85f35be6 | 425 | sync.filled_rectangle(54, 74-j, 75, 77-j, WHITE); |
macenzofan | 28:8dbb85f35be6 | 426 | sync.filled_circle(53, 34,6, LT_GREY); |
macenzofan | 28:8dbb85f35be6 | 427 | sync.filled_circle(76, 34,5, LT_GREY); |
macenzofan | 28:8dbb85f35be6 | 428 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 429 | sync.filled_circle(56, 34,4, MED_GREY); |
macenzofan | 28:8dbb85f35be6 | 430 | sync.filled_circle(73, 34,4, DRK_GREY); |
macenzofan | 28:8dbb85f35be6 | 431 | sync.filled_circle(70, 34,5, MED_GREY); |
macenzofan | 28:8dbb85f35be6 | 432 | sync.filled_circle(64, 34,6, LT_GREY); |
macenzofan | 28:8dbb85f35be6 | 433 | |
macenzofan | 28:8dbb85f35be6 | 434 | |
macenzofan | 28:8dbb85f35be6 | 435 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 436 | sync.filled_circle(52,35,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 437 | sync.filled_circle(77,35,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 438 | sync.filled_circle(52,35,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 439 | sync.filled_circle(60,35,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 440 | sync.filled_circle(70,39,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 441 | sync.filled_circle(76,37,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 442 | sync.filled_circle(65,35,6,SKY_COLOR); |
macenzofan | 28:8dbb85f35be6 | 443 | haptics.play_waveform(1); |
macenzofan | 28:8dbb85f35be6 | 444 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 445 | sync.filled_circle(64,36,3,PIX); |
macenzofan | 28:8dbb85f35be6 | 446 | sync.filled_circle(64,46,3,PIX); |
macenzofan | 28:8dbb85f35be6 | 447 | sync.filled_circle(64,56,3,PIX); |
macenzofan | 28:8dbb85f35be6 | 448 | sync.filled_circle(64,66,3,PIX); |
macenzofan | 28:8dbb85f35be6 | 449 | haptics.play_waveform(1); |
macenzofan | 28:8dbb85f35be6 | 450 | } |
macenzofan | 28:8dbb85f35be6 | 451 | } |
jford38 | 23:77049670cae6 | 452 | if(whose_turn == PLAYER1) { |
jford38 | 23:77049670cae6 | 453 | |
macenzofan | 28:8dbb85f35be6 | 454 | //Draw the initial lives for player one and light up tank color to indicate turn |
macenzofan | 28:8dbb85f35be6 | 455 | sync.rectangle(45, 2, 84, 7, BLACK); |
macenzofan | 28:8dbb85f35be6 | 456 | if(p1lives==0){ |
macenzofan | 28:8dbb85f35be6 | 457 | sync.filled_circle(4, 4, 2, TANK_RED); |
macenzofan | 28:8dbb85f35be6 | 458 | } |
macenzofan | 28:8dbb85f35be6 | 459 | if(p1lives==1){ |
macenzofan | 28:8dbb85f35be6 | 460 | sync.filled_circle(10, 4, 2, TANK_RED); |
macenzofan | 28:8dbb85f35be6 | 461 | sync.filled_circle(4, 4, 2, TANK_RED); |
macenzofan | 28:8dbb85f35be6 | 462 | } |
macenzofan | 28:8dbb85f35be6 | 463 | if(p1lives==2){ |
macenzofan | 28:8dbb85f35be6 | 464 | sync.filled_circle(10, 4, 2, TANK_RED); |
macenzofan | 28:8dbb85f35be6 | 465 | sync.filled_circle(4, 4, 2, TANK_RED); |
macenzofan | 28:8dbb85f35be6 | 466 | sync.filled_circle(16, 4, 2, TANK_RED); |
macenzofan | 28:8dbb85f35be6 | 467 | } |
macenzofan | 28:8dbb85f35be6 | 468 | //show light blue for player two showing its player ones turn. |
macenzofan | 28:8dbb85f35be6 | 469 | if(p2lives==0){ |
macenzofan | 28:8dbb85f35be6 | 470 | sync.filled_circle(124, 4, 2, LT_BLUE); |
macenzofan | 28:8dbb85f35be6 | 471 | } |
macenzofan | 28:8dbb85f35be6 | 472 | if(p2lives==1){ |
macenzofan | 28:8dbb85f35be6 | 473 | sync.filled_circle(118, 4, 2, LT_BLUE); |
macenzofan | 28:8dbb85f35be6 | 474 | sync.filled_circle(124, 4, 2, LT_BLUE); |
macenzofan | 28:8dbb85f35be6 | 475 | } |
macenzofan | 28:8dbb85f35be6 | 476 | if(p2lives==2){ |
macenzofan | 28:8dbb85f35be6 | 477 | sync.filled_circle(118, 4, 2, LT_BLUE); |
macenzofan | 28:8dbb85f35be6 | 478 | sync.filled_circle(112, 4, 2, LT_BLUE); |
macenzofan | 28:8dbb85f35be6 | 479 | sync.filled_circle(124, 4, 2, LT_BLUE); |
macenzofan | 28:8dbb85f35be6 | 480 | } |
macenzofan | 28:8dbb85f35be6 | 481 | |
macenzofan | 28:8dbb85f35be6 | 482 | |
jford38 | 23:77049670cae6 | 483 | // Accelerometer example |
macenzofan | 28:8dbb85f35be6 | 484 | if(ax1 < -ACC_THRESHOLD && sync.pixel_eq(t1maxpixelcolor,SKY_COLOR)==1) { |
macenzofan | 28:8dbb85f35be6 | 485 | // Move the tank to the right if the accelerometer is tipped far enough to the right. |
macenzofan | 28:8dbb85f35be6 | 486 | t1.reposition(+1, 0, 0); |
macenzofan | 28:8dbb85f35be6 | 487 | } |
macenzofan | 28:8dbb85f35be6 | 488 | if(ax1 < -1.5*ACC_THRESHOLD && sync.pixel_eq(t1maxpixelcolor,SKY_COLOR)==1) { |
jford38 | 23:77049670cae6 | 489 | // Move the tank to the right if the accelerometer is tipped far enough to the right. |
macenzofan | 28:8dbb85f35be6 | 490 | t1.reposition(+3, 0, 0); |
macenzofan | 28:8dbb85f35be6 | 491 | } |
macenzofan | 28:8dbb85f35be6 | 492 | if(ax1 > ACC_THRESHOLD && t1.min_x()>0) { |
macenzofan | 28:8dbb85f35be6 | 493 | // Move the tank to the left if the accelerometer is tipped far enough to the left. |
macenzofan | 28:8dbb85f35be6 | 494 | t1.reposition(-1, 0, 0); |
macenzofan | 28:8dbb85f35be6 | 495 | } |
macenzofan | 28:8dbb85f35be6 | 496 | if(ax1 > 1.5*ACC_THRESHOLD && t1.min_x()>0) { |
macenzofan | 28:8dbb85f35be6 | 497 | |
macenzofan | 28:8dbb85f35be6 | 498 | // Move the tank to the left if the accelerometer is tipped far enough to the left. |
macenzofan | 28:8dbb85f35be6 | 499 | t1.reposition(-3, 0, 0); |
macenzofan | 28:8dbb85f35be6 | 500 | } |
macenzofan | 28:8dbb85f35be6 | 501 | if(ay1 > ACC_THRESHOLD) { |
macenzofan | 28:8dbb85f35be6 | 502 | //Move the barrel up. |
macenzofan | 28:8dbb85f35be6 | 503 | t1.reposition(0, 0, PI/24); |
macenzofan | 28:8dbb85f35be6 | 504 | } |
macenzofan | 28:8dbb85f35be6 | 505 | if(ay1 < -ACC_THRESHOLD) { |
macenzofan | 28:8dbb85f35be6 | 506 | //Move the barrel down. |
macenzofan | 28:8dbb85f35be6 | 507 | t1.reposition(0, 0, -PI/24); |
jford38 | 23:77049670cae6 | 508 | } |
jford38 | 23:77049670cae6 | 509 | |
macenzofan | 28:8dbb85f35be6 | 510 | |
jford38 | 23:77049670cae6 | 511 | // Button example |
macenzofan | 28:8dbb85f35be6 | 512 | //if(p1_buttons[L_BUTTON]) { |
macenzofan | 28:8dbb85f35be6 | 513 | // b1.shoot(); |
macenzofan | 28:8dbb85f35be6 | 514 | //} |
macenzofan | 28:8dbb85f35be6 | 515 | // hold down left button to power up the power bar. |
macenzofan | 28:8dbb85f35be6 | 516 | if(p1_buttons[L_BUTTON]){ |
macenzofan | 28:8dbb85f35be6 | 517 | b1.speed+=3; |
macenzofan | 28:8dbb85f35be6 | 518 | i+=2; |
macenzofan | 28:8dbb85f35be6 | 519 | sync.filled_rectangle(46, 3, 45+i, 6, TANK_RED); |
macenzofan | 28:8dbb85f35be6 | 520 | haptics.play_waveform(1); |
macenzofan | 28:8dbb85f35be6 | 521 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 522 | if(i>=38){ |
macenzofan | 28:8dbb85f35be6 | 523 | i=37; |
macenzofan | 28:8dbb85f35be6 | 524 | b1.speed=56; |
macenzofan | 28:8dbb85f35be6 | 525 | } |
macenzofan | 28:8dbb85f35be6 | 526 | } |
macenzofan | 28:8dbb85f35be6 | 527 | else{ |
macenzofan | 28:8dbb85f35be6 | 528 | i=0; |
jford38 | 23:77049670cae6 | 529 | } |
macenzofan | 28:8dbb85f35be6 | 530 | //Press right button to fire. |
macenzofan | 28:8dbb85f35be6 | 531 | if(p1_buttons[R_BUTTON]){ |
macenzofan | 28:8dbb85f35be6 | 532 | b1.shoot(); |
macenzofan | 28:8dbb85f35be6 | 533 | playSound("/sd/wavfiles/Fire.wav"); |
macenzofan | 28:8dbb85f35be6 | 534 | haptics.play_waveform(1); |
macenzofan | 28:8dbb85f35be6 | 535 | b1.speed=0; |
macenzofan | 28:8dbb85f35be6 | 536 | sync.filled_rectangle(46, 3, 83, 6, GND_COLOR); |
macenzofan | 28:8dbb85f35be6 | 537 | sync.rectangle(45, 2, 84, 7, BLACK); |
jford38 | 23:77049670cae6 | 538 | |
macenzofan | 28:8dbb85f35be6 | 539 | |
macenzofan | 28:8dbb85f35be6 | 540 | } |
macenzofan | 28:8dbb85f35be6 | 541 | //if(p1_buttons[U_BUTTON]){ |
macenzofan | 28:8dbb85f35be6 | 542 | // sync.filled_circle(24,9,2,BLACK); |
macenzofan | 28:8dbb85f35be6 | 543 | // bouncy=1; |
macenzofan | 28:8dbb85f35be6 | 544 | |
macenzofan | 28:8dbb85f35be6 | 545 | |
macenzofan | 28:8dbb85f35be6 | 546 | //} |
jford38 | 23:77049670cae6 | 547 | float dt = frame_timer.read(); |
jford38 | 23:77049670cae6 | 548 | int intersection_code = b1.time_step(dt); |
jford38 | 23:77049670cae6 | 549 | |
jford38 | 23:77049670cae6 | 550 | if(intersection_code != BULLET_NO_COLLISION || intersection_code == BULLET_OFF_SCREEN) { |
jford38 | 23:77049670cae6 | 551 | pc.printf("Now it's P2's turn!\n"); |
jford38 | 23:77049670cae6 | 552 | whose_turn = PLAYER2; |
jford38 | 23:77049670cae6 | 553 | } |
jford38 | 23:77049670cae6 | 554 | |
macenzofan | 28:8dbb85f35be6 | 555 | // If you shot yourself, you lost a life. |
macenzofan | 28:8dbb85f35be6 | 556 | if(sync.pixel_eq(intersection_code, t1.tank_color)) { |
macenzofan | 28:8dbb85f35be6 | 557 | if(p1lives==0){ |
macenzofan | 28:8dbb85f35be6 | 558 | sync.filled_circle(4, 4, 2, GND_COLOR); |
macenzofan | 28:8dbb85f35be6 | 559 | sync.update(); // Is this necessary? |
macenzofan | 28:8dbb85f35be6 | 560 | winner = PLAYER2; |
macenzofan | 28:8dbb85f35be6 | 561 | //game_over(); |
macenzofan | 28:8dbb85f35be6 | 562 | break; |
macenzofan | 28:8dbb85f35be6 | 563 | } |
macenzofan | 28:8dbb85f35be6 | 564 | else if(p1lives!=0){ |
macenzofan | 28:8dbb85f35be6 | 565 | |
macenzofan | 28:8dbb85f35be6 | 566 | if(p1lives==1){ |
macenzofan | 28:8dbb85f35be6 | 567 | p1lives+=-1; |
macenzofan | 28:8dbb85f35be6 | 568 | sync.filled_circle(10, 4, 2, GND_COLOR); |
macenzofan | 28:8dbb85f35be6 | 569 | } |
macenzofan | 28:8dbb85f35be6 | 570 | if(p1lives==2){ |
macenzofan | 28:8dbb85f35be6 | 571 | p1lives+=-1; |
macenzofan | 28:8dbb85f35be6 | 572 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 573 | sync.filled_circle(16, 4, 2, GND_COLOR); |
macenzofan | 28:8dbb85f35be6 | 574 | |
macenzofan | 28:8dbb85f35be6 | 575 | } |
macenzofan | 28:8dbb85f35be6 | 576 | } |
macenzofan | 28:8dbb85f35be6 | 577 | } |
macenzofan | 28:8dbb85f35be6 | 578 | |
macenzofan | 28:8dbb85f35be6 | 579 | |
macenzofan | 28:8dbb85f35be6 | 580 | // If you shot the other guy, you took one life! |
macenzofan | 28:8dbb85f35be6 | 581 | if(sync.pixel_eq(intersection_code, t2.tank_color)) { |
macenzofan | 28:8dbb85f35be6 | 582 | if(p2lives==0){ |
macenzofan | 28:8dbb85f35be6 | 583 | sync.filled_circle(124, 4, 2, GND_COLOR); |
macenzofan | 28:8dbb85f35be6 | 584 | sync.update(); // Is this necessary? |
macenzofan | 28:8dbb85f35be6 | 585 | winner = PLAYER1; |
macenzofan | 28:8dbb85f35be6 | 586 | break; |
macenzofan | 28:8dbb85f35be6 | 587 | } |
macenzofan | 28:8dbb85f35be6 | 588 | else if(p2lives!=0){ |
macenzofan | 28:8dbb85f35be6 | 589 | |
macenzofan | 28:8dbb85f35be6 | 590 | if(p2lives==1){ |
macenzofan | 28:8dbb85f35be6 | 591 | p2lives+=-1; |
macenzofan | 28:8dbb85f35be6 | 592 | sync.filled_circle(118, 4, 2, GND_COLOR); |
macenzofan | 28:8dbb85f35be6 | 593 | } |
macenzofan | 28:8dbb85f35be6 | 594 | if(p2lives==2){ |
macenzofan | 28:8dbb85f35be6 | 595 | p2lives+=-1; |
macenzofan | 28:8dbb85f35be6 | 596 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 597 | sync.filled_circle(112, 4, 2, GND_COLOR); |
macenzofan | 28:8dbb85f35be6 | 598 | } |
macenzofan | 28:8dbb85f35be6 | 599 | |
macenzofan | 28:8dbb85f35be6 | 600 | } |
macenzofan | 28:8dbb85f35be6 | 601 | } |
macenzofan | 28:8dbb85f35be6 | 602 | } |
macenzofan | 28:8dbb85f35be6 | 603 | else if(whose_turn == PLAYER2) { |
macenzofan | 28:8dbb85f35be6 | 604 | |
macenzofan | 28:8dbb85f35be6 | 605 | // Show player two's lives in tank color and player ones in light red. |
macenzofan | 28:8dbb85f35be6 | 606 | if(p2lives==0){ |
macenzofan | 28:8dbb85f35be6 | 607 | sync.filled_circle(124, 4, 2, TANK_BLUE); |
macenzofan | 28:8dbb85f35be6 | 608 | } |
macenzofan | 28:8dbb85f35be6 | 609 | if(p2lives==1){ |
macenzofan | 28:8dbb85f35be6 | 610 | sync.filled_circle(118, 4, 2, TANK_BLUE); |
macenzofan | 28:8dbb85f35be6 | 611 | sync.filled_circle(124, 4, 2, TANK_BLUE); |
macenzofan | 28:8dbb85f35be6 | 612 | } |
macenzofan | 28:8dbb85f35be6 | 613 | if(p2lives==2){ |
macenzofan | 28:8dbb85f35be6 | 614 | sync.filled_circle(118, 4, 2, TANK_BLUE); |
macenzofan | 28:8dbb85f35be6 | 615 | sync.filled_circle(112, 4, 2, TANK_BLUE); |
macenzofan | 28:8dbb85f35be6 | 616 | sync.filled_circle(124, 4, 2, TANK_BLUE); |
macenzofan | 28:8dbb85f35be6 | 617 | } |
macenzofan | 28:8dbb85f35be6 | 618 | if(p1lives==0){ |
macenzofan | 28:8dbb85f35be6 | 619 | sync.filled_circle(4, 4, 2, LT_RED); |
macenzofan | 28:8dbb85f35be6 | 620 | } |
macenzofan | 28:8dbb85f35be6 | 621 | if(p1lives==1){ |
macenzofan | 28:8dbb85f35be6 | 622 | sync.filled_circle(10, 4, 2, LT_RED); |
macenzofan | 28:8dbb85f35be6 | 623 | sync.filled_circle(4, 4, 2, LT_RED); |
macenzofan | 28:8dbb85f35be6 | 624 | } |
macenzofan | 28:8dbb85f35be6 | 625 | if(p1lives==2){ |
macenzofan | 28:8dbb85f35be6 | 626 | sync.filled_circle(10, 4, 2, LT_RED); |
macenzofan | 28:8dbb85f35be6 | 627 | sync.filled_circle(4, 4, 2, LT_RED); |
macenzofan | 28:8dbb85f35be6 | 628 | sync.filled_circle(16, 4, 2, LT_RED); |
macenzofan | 28:8dbb85f35be6 | 629 | } |
macenzofan | 28:8dbb85f35be6 | 630 | |
macenzofan | 28:8dbb85f35be6 | 631 | // Accelerometer example |
macenzofan | 28:8dbb85f35be6 | 632 | if(sync.play_mode==SINGLE_PLAYER){ |
macenzofan | 28:8dbb85f35be6 | 633 | if(ax1 < -ACC_THRESHOLD && t2.max_x()<127) { |
macenzofan | 28:8dbb85f35be6 | 634 | // Move the tank to the right if the accelerometer is tipped far enough to the right. |
macenzofan | 28:8dbb85f35be6 | 635 | t2.reposition(+1, 0, 0); |
macenzofan | 28:8dbb85f35be6 | 636 | } |
macenzofan | 28:8dbb85f35be6 | 637 | if(ax1 < -1.5*ACC_THRESHOLD && t2.max_x()<127) { |
macenzofan | 28:8dbb85f35be6 | 638 | // Move the tank to the right if the accelerometer is tipped far enough to the right. |
macenzofan | 28:8dbb85f35be6 | 639 | t2.reposition(+3, 0, 0); |
macenzofan | 28:8dbb85f35be6 | 640 | } |
macenzofan | 28:8dbb85f35be6 | 641 | if(ax1 > ACC_THRESHOLD && sync.pixel_eq(t2minpixelcolor,SKY_COLOR)==1) { |
macenzofan | 28:8dbb85f35be6 | 642 | // Move the tank to the left if the accelerometer is tipped far enough to the left. |
macenzofan | 28:8dbb85f35be6 | 643 | t2.reposition(-1, 0, 0); |
macenzofan | 28:8dbb85f35be6 | 644 | } |
macenzofan | 28:8dbb85f35be6 | 645 | if(ax1 > 1.5*ACC_THRESHOLD && sync.pixel_eq(t2minpixelcolor,SKY_COLOR)==1) { |
macenzofan | 28:8dbb85f35be6 | 646 | // Move the tank to the left if the accelerometer is tipped far enough to the left. |
macenzofan | 28:8dbb85f35be6 | 647 | t2.reposition(-3, 0, 0); |
macenzofan | 28:8dbb85f35be6 | 648 | } |
macenzofan | 28:8dbb85f35be6 | 649 | if(ay1 > ACC_THRESHOLD) { |
macenzofan | 28:8dbb85f35be6 | 650 | //Move the barrel up. |
macenzofan | 28:8dbb85f35be6 | 651 | t2.reposition(0, 0, PI/24); |
macenzofan | 28:8dbb85f35be6 | 652 | } |
macenzofan | 28:8dbb85f35be6 | 653 | if(ay1 < -ACC_THRESHOLD) { |
macenzofan | 28:8dbb85f35be6 | 654 | //Move the barrel down. |
macenzofan | 28:8dbb85f35be6 | 655 | t2.reposition(0, 0, -PI/24); |
macenzofan | 28:8dbb85f35be6 | 656 | } |
jford38 | 23:77049670cae6 | 657 | } |
jford38 | 23:77049670cae6 | 658 | |
macenzofan | 28:8dbb85f35be6 | 659 | if(sync.play_mode==MULTI_PLAYER){ |
macenzofan | 28:8dbb85f35be6 | 660 | if(ax2 < -ACC_THRESHOLD && t2.max_x()<127) { |
macenzofan | 28:8dbb85f35be6 | 661 | // Move the tank to the right if the accelerometer is tipped far enough to the right. |
macenzofan | 28:8dbb85f35be6 | 662 | t2.reposition(+1, 0, 0); |
macenzofan | 28:8dbb85f35be6 | 663 | } |
macenzofan | 28:8dbb85f35be6 | 664 | if(ax2 < -1.5*ACC_THRESHOLD && t2.max_x()<127) { |
macenzofan | 28:8dbb85f35be6 | 665 | // Move the tank to the right if the accelerometer is tipped far enough to the right. |
macenzofan | 28:8dbb85f35be6 | 666 | t2.reposition(+3, 0, 0); |
macenzofan | 28:8dbb85f35be6 | 667 | } |
macenzofan | 28:8dbb85f35be6 | 668 | if(ax2 > ACC_THRESHOLD && sync.pixel_eq(t2minpixelcolor,SKY_COLOR)==1) { |
macenzofan | 28:8dbb85f35be6 | 669 | // Move the tank to the left if the accelerometer is tipped far enough to the left. |
macenzofan | 28:8dbb85f35be6 | 670 | t2.reposition(-1, 0, 0); |
macenzofan | 28:8dbb85f35be6 | 671 | } |
macenzofan | 28:8dbb85f35be6 | 672 | if(ax2 > 1.5*ACC_THRESHOLD && sync.pixel_eq(t2minpixelcolor,SKY_COLOR)==1) { |
macenzofan | 28:8dbb85f35be6 | 673 | // Move the tank to the left if the accelerometer is tipped far enough to the left. |
macenzofan | 28:8dbb85f35be6 | 674 | t2.reposition(-3, 0, 0); |
macenzofan | 28:8dbb85f35be6 | 675 | } |
macenzofan | 28:8dbb85f35be6 | 676 | if(ay2 > ACC_THRESHOLD) { |
macenzofan | 28:8dbb85f35be6 | 677 | //Move the barrel up. |
macenzofan | 28:8dbb85f35be6 | 678 | t2.reposition(0, 0, PI/24); |
macenzofan | 28:8dbb85f35be6 | 679 | } |
macenzofan | 28:8dbb85f35be6 | 680 | if(ay2 < -ACC_THRESHOLD) { |
macenzofan | 28:8dbb85f35be6 | 681 | //Move the barrel down. |
macenzofan | 28:8dbb85f35be6 | 682 | t2.reposition(0, 0, -PI/24); |
macenzofan | 28:8dbb85f35be6 | 683 | } |
macenzofan | 28:8dbb85f35be6 | 684 | } |
macenzofan | 28:8dbb85f35be6 | 685 | //If in single player mode charge the power bar. |
macenzofan | 28:8dbb85f35be6 | 686 | if(sync.play_mode==SINGLE_PLAYER){ |
macenzofan | 28:8dbb85f35be6 | 687 | if(p1_buttons[L_BUTTON]){ |
macenzofan | 28:8dbb85f35be6 | 688 | b2.speed+=3; |
macenzofan | 28:8dbb85f35be6 | 689 | i+=2; |
macenzofan | 28:8dbb85f35be6 | 690 | sync.filled_rectangle(83, 3, 85-i, 6, TANK_BLUE); |
macenzofan | 28:8dbb85f35be6 | 691 | haptics.play_waveform(1); |
macenzofan | 28:8dbb85f35be6 | 692 | if(i>=38){ |
macenzofan | 28:8dbb85f35be6 | 693 | i=37; |
macenzofan | 28:8dbb85f35be6 | 694 | b2.speed=56; |
macenzofan | 28:8dbb85f35be6 | 695 | } |
macenzofan | 28:8dbb85f35be6 | 696 | } |
macenzofan | 28:8dbb85f35be6 | 697 | else{ |
macenzofan | 28:8dbb85f35be6 | 698 | i=0; |
macenzofan | 28:8dbb85f35be6 | 699 | } |
macenzofan | 28:8dbb85f35be6 | 700 | // fire tank. |
macenzofan | 28:8dbb85f35be6 | 701 | if(p1_buttons[R_BUTTON]){ |
macenzofan | 28:8dbb85f35be6 | 702 | b2.shoot(); |
macenzofan | 28:8dbb85f35be6 | 703 | playSound("/sd/wavfiles/Fire.wav"); |
macenzofan | 28:8dbb85f35be6 | 704 | haptics.play_waveform(1); |
macenzofan | 28:8dbb85f35be6 | 705 | |
macenzofan | 28:8dbb85f35be6 | 706 | b2.speed=0; |
macenzofan | 28:8dbb85f35be6 | 707 | sync.filled_rectangle(46, 3, 83, 6, GND_COLOR); |
macenzofan | 28:8dbb85f35be6 | 708 | sync.rectangle(45, 2, 84, 7, BLACK); |
macenzofan | 28:8dbb85f35be6 | 709 | } |
macenzofan | 28:8dbb85f35be6 | 710 | } |
macenzofan | 28:8dbb85f35be6 | 711 | if(sync.play_mode==MULTI_PLAYER){ |
macenzofan | 28:8dbb85f35be6 | 712 | if(p2_buttons[L_BUTTON]){ |
macenzofan | 28:8dbb85f35be6 | 713 | b2.speed+=3; |
macenzofan | 28:8dbb85f35be6 | 714 | i+=2; |
macenzofan | 28:8dbb85f35be6 | 715 | sync.filled_rectangle(83, 3, 85-i, 6, TANK_BLUE); |
jford38 | 23:77049670cae6 | 716 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 717 | if(i>=38){ |
macenzofan | 28:8dbb85f35be6 | 718 | i=37; |
macenzofan | 28:8dbb85f35be6 | 719 | b2.speed=56; |
macenzofan | 28:8dbb85f35be6 | 720 | } |
macenzofan | 28:8dbb85f35be6 | 721 | } |
macenzofan | 28:8dbb85f35be6 | 722 | else{ |
macenzofan | 28:8dbb85f35be6 | 723 | i=0; |
jford38 | 23:77049670cae6 | 724 | } |
macenzofan | 28:8dbb85f35be6 | 725 | if(p2_buttons[R_BUTTON]){ |
macenzofan | 28:8dbb85f35be6 | 726 | b2.shoot(); |
macenzofan | 28:8dbb85f35be6 | 727 | playSound("/sd/wavfiles/Fire.wav"); |
macenzofan | 28:8dbb85f35be6 | 728 | haptics.play_waveform(1); |
macenzofan | 28:8dbb85f35be6 | 729 | b2.speed=0; |
macenzofan | 28:8dbb85f35be6 | 730 | sync.filled_rectangle(46, 3, 83, 6, GND_COLOR); |
macenzofan | 28:8dbb85f35be6 | 731 | sync.rectangle(45, 2, 84, 7, BLACK); |
macenzofan | 28:8dbb85f35be6 | 732 | |
macenzofan | 28:8dbb85f35be6 | 733 | } |
macenzofan | 28:8dbb85f35be6 | 734 | } |
jford38 | 23:77049670cae6 | 735 | |
macenzofan | 28:8dbb85f35be6 | 736 | float dt = frame_timer.read(); |
macenzofan | 28:8dbb85f35be6 | 737 | int intersection_code = b2.time_step(dt); |
macenzofan | 28:8dbb85f35be6 | 738 | |
macenzofan | 28:8dbb85f35be6 | 739 | if(intersection_code != BULLET_NO_COLLISION || intersection_code == BULLET_OFF_SCREEN) { |
macenzofan | 28:8dbb85f35be6 | 740 | pc.printf("Now it's P2's turn!\n"); |
macenzofan | 28:8dbb85f35be6 | 741 | whose_turn = PLAYER1; |
macenzofan | 28:8dbb85f35be6 | 742 | } |
macenzofan | 28:8dbb85f35be6 | 743 | |
macenzofan | 28:8dbb85f35be6 | 744 | // If you shot yourself, you lost a life. |
macenzofan | 28:8dbb85f35be6 | 745 | if(sync.pixel_eq(intersection_code, t1.tank_color)) { |
macenzofan | 28:8dbb85f35be6 | 746 | if(p1lives==0){ |
macenzofan | 28:8dbb85f35be6 | 747 | sync.update(); // Is this necessary? |
macenzofan | 28:8dbb85f35be6 | 748 | winner = PLAYER2; |
macenzofan | 28:8dbb85f35be6 | 749 | sync.filled_circle(4, 4, 2, GND_COLOR); |
macenzofan | 28:8dbb85f35be6 | 750 | break; |
macenzofan | 28:8dbb85f35be6 | 751 | } |
macenzofan | 28:8dbb85f35be6 | 752 | else if(p1lives!=0){ |
macenzofan | 28:8dbb85f35be6 | 753 | |
macenzofan | 28:8dbb85f35be6 | 754 | if(p1lives==1){ |
macenzofan | 28:8dbb85f35be6 | 755 | p1lives+=-1; |
macenzofan | 28:8dbb85f35be6 | 756 | sync.filled_circle(10, 4, 2, GND_COLOR); |
macenzofan | 28:8dbb85f35be6 | 757 | } |
macenzofan | 28:8dbb85f35be6 | 758 | if(p1lives==2){ |
macenzofan | 28:8dbb85f35be6 | 759 | p1lives+=-1; |
macenzofan | 28:8dbb85f35be6 | 760 | sync.filled_circle(16, 4, 2, GND_COLOR); |
macenzofan | 28:8dbb85f35be6 | 761 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 762 | } |
macenzofan | 28:8dbb85f35be6 | 763 | } |
macenzofan | 28:8dbb85f35be6 | 764 | |
macenzofan | 28:8dbb85f35be6 | 765 | // If you shot the other guy, took a life! |
macenzofan | 28:8dbb85f35be6 | 766 | if(sync.pixel_eq(intersection_code, t2.tank_color)) { |
macenzofan | 28:8dbb85f35be6 | 767 | if(p2lives==0){ |
macenzofan | 28:8dbb85f35be6 | 768 | sync.update(); // Is this necessary? |
macenzofan | 28:8dbb85f35be6 | 769 | winner = PLAYER1; |
macenzofan | 28:8dbb85f35be6 | 770 | sync.filled_circle(124, 4, 2, GND_COLOR); |
macenzofan | 28:8dbb85f35be6 | 771 | break; |
macenzofan | 28:8dbb85f35be6 | 772 | } |
macenzofan | 28:8dbb85f35be6 | 773 | else if(p2lives!=0){ |
macenzofan | 28:8dbb85f35be6 | 774 | |
macenzofan | 28:8dbb85f35be6 | 775 | if(p2lives==1){ |
macenzofan | 28:8dbb85f35be6 | 776 | p2lives+=-1; |
macenzofan | 28:8dbb85f35be6 | 777 | sync.filled_circle(118, 4, 2, GND_COLOR); |
macenzofan | 28:8dbb85f35be6 | 778 | } |
macenzofan | 28:8dbb85f35be6 | 779 | if(p2lives==2){ |
macenzofan | 28:8dbb85f35be6 | 780 | p2lives+=-1; |
macenzofan | 28:8dbb85f35be6 | 781 | sync.filled_circle(112, 4, 2, GND_COLOR); |
macenzofan | 28:8dbb85f35be6 | 782 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 783 | } |
macenzofan | 28:8dbb85f35be6 | 784 | } |
macenzofan | 28:8dbb85f35be6 | 785 | } |
macenzofan | 28:8dbb85f35be6 | 786 | } |
macenzofan | 28:8dbb85f35be6 | 787 | |
jford38 | 23:77049670cae6 | 788 | |
jford38 | 23:77049670cae6 | 789 | } |
jford38 | 23:77049670cae6 | 790 | |
jford38 | 23:77049670cae6 | 791 | frame_timer.reset(); |
jford38 | 23:77049670cae6 | 792 | sync.update(); |
jford38 | 3:3ddefff03cb2 | 793 | } |
jford38 | 15:4b27a3a95772 | 794 | |
jford38 | 22:3c68eea5a609 | 795 | game_over(); |
macenzofan | 28:8dbb85f35be6 | 796 | |
macenzofan | 28:8dbb85f35be6 | 797 | } |
macenzofan | 28:8dbb85f35be6 | 798 | void game_over() { |
jford38 | 21:edfeb289b21f | 799 | |
macenzofan | 28:8dbb85f35be6 | 800 | if(winner==PLAYER1){ |
macenzofan | 28:8dbb85f35be6 | 801 | |
macenzofan | 28:8dbb85f35be6 | 802 | char status[]= "GAME OVER!"; |
macenzofan | 28:8dbb85f35be6 | 803 | char player[]="Player 1 \n wins!"; |
macenzofan | 28:8dbb85f35be6 | 804 | char play[]="Play again? \n\n Yes No"; |
macenzofan | 28:8dbb85f35be6 | 805 | sync.rectangle(13,56,115,99,BLACK); |
macenzofan | 28:8dbb85f35be6 | 806 | sync.filled_rectangle(15,58,113,97,TANK_RED); |
macenzofan | 28:8dbb85f35be6 | 807 | sync.textbackground_color(TANK_RED); |
macenzofan | 28:8dbb85f35be6 | 808 | sync.locate(4,10); |
macenzofan | 28:8dbb85f35be6 | 809 | sync.puts(status, sizeof(status)); |
macenzofan | 28:8dbb85f35be6 | 810 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 811 | wait(2); |
macenzofan | 28:8dbb85f35be6 | 812 | sync.filled_rectangle(15,62,113,97,TANK_RED); |
macenzofan | 28:8dbb85f35be6 | 813 | sync.locate(5,10); |
macenzofan | 28:8dbb85f35be6 | 814 | sync.puts(player, sizeof(player)); |
macenzofan | 28:8dbb85f35be6 | 815 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 816 | playSound("/sd/wavfiles/cheering.wav"); |
macenzofan | 28:8dbb85f35be6 | 817 | sync.filled_rectangle(15,62,113,97,TANK_RED); |
macenzofan | 28:8dbb85f35be6 | 818 | sync.locate(4,10); |
macenzofan | 28:8dbb85f35be6 | 819 | sync.puts(play, sizeof(play)); |
macenzofan | 28:8dbb85f35be6 | 820 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 821 | while(2){ |
macenzofan | 28:8dbb85f35be6 | 822 | if(!pb_l){ |
macenzofan | 28:8dbb85f35be6 | 823 | sync.rectangle(33,63,60,76,WHITE); |
macenzofan | 28:8dbb85f35be6 | 824 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 825 | wait(2); |
macenzofan | 28:8dbb85f35be6 | 826 | sync.cls(); |
macenzofan | 28:8dbb85f35be6 | 827 | sync.textbackground_color(BLACK); |
macenzofan | 28:8dbb85f35be6 | 828 | sync.filled_rectangle(0,0,128,128,BLACK); |
macenzofan | 28:8dbb85f35be6 | 829 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 830 | game_init(); |
macenzofan | 28:8dbb85f35be6 | 831 | } |
macenzofan | 28:8dbb85f35be6 | 832 | else if(!pb_r){ |
macenzofan | 28:8dbb85f35be6 | 833 | sync.rectangle(68,63,95,76,WHITE); |
macenzofan | 28:8dbb85f35be6 | 834 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 835 | wait(2); |
macenzofan | 28:8dbb85f35be6 | 836 | sync.cls(); |
macenzofan | 28:8dbb85f35be6 | 837 | sync.textbackground_color(BLACK); |
macenzofan | 28:8dbb85f35be6 | 838 | sync.filled_rectangle(0,0,128,128,BLACK); |
macenzofan | 28:8dbb85f35be6 | 839 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 840 | main(); |
macenzofan | 28:8dbb85f35be6 | 841 | } |
macenzofan | 28:8dbb85f35be6 | 842 | } |
macenzofan | 28:8dbb85f35be6 | 843 | |
macenzofan | 28:8dbb85f35be6 | 844 | } |
macenzofan | 28:8dbb85f35be6 | 845 | |
macenzofan | 28:8dbb85f35be6 | 846 | if(winner==PLAYER2){ |
macenzofan | 28:8dbb85f35be6 | 847 | |
macenzofan | 28:8dbb85f35be6 | 848 | char status[]= "GAME OVER!"; |
macenzofan | 28:8dbb85f35be6 | 849 | char player[]="Player 2 \n wins!"; |
macenzofan | 28:8dbb85f35be6 | 850 | char play[]="Play again? \n\n Yes No"; |
macenzofan | 28:8dbb85f35be6 | 851 | sync.rectangle(13,56,115,99,BLACK); |
macenzofan | 28:8dbb85f35be6 | 852 | sync.filled_rectangle(15,58,113,97,TANK_BLUE); |
macenzofan | 28:8dbb85f35be6 | 853 | sync.textbackground_color(TANK_BLUE); |
macenzofan | 28:8dbb85f35be6 | 854 | sync.locate(4,10); |
macenzofan | 28:8dbb85f35be6 | 855 | sync.puts(status, sizeof(status)); |
macenzofan | 28:8dbb85f35be6 | 856 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 857 | wait(2); |
macenzofan | 28:8dbb85f35be6 | 858 | sync.filled_rectangle(15,62,113,97,TANK_BLUE); |
macenzofan | 28:8dbb85f35be6 | 859 | sync.locate(5,10); |
macenzofan | 28:8dbb85f35be6 | 860 | sync.puts(player, sizeof(player)); |
macenzofan | 28:8dbb85f35be6 | 861 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 862 | playSound("/sd/wavfiles/cheering.wav"); |
macenzofan | 28:8dbb85f35be6 | 863 | sync.filled_rectangle(15,62,113,97,TANK_BLUE); |
macenzofan | 28:8dbb85f35be6 | 864 | sync.locate(4,10); |
macenzofan | 28:8dbb85f35be6 | 865 | sync.puts(play, sizeof(play)); |
macenzofan | 28:8dbb85f35be6 | 866 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 867 | while(true){ |
macenzofan | 28:8dbb85f35be6 | 868 | if(!pb_l){ |
macenzofan | 28:8dbb85f35be6 | 869 | sync.rectangle(33,63,60,76,WHITE); |
macenzofan | 28:8dbb85f35be6 | 870 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 871 | wait(2); |
macenzofan | 28:8dbb85f35be6 | 872 | sync.cls(); |
macenzofan | 28:8dbb85f35be6 | 873 | sync.textbackground_color(BLACK); |
macenzofan | 28:8dbb85f35be6 | 874 | sync.filled_rectangle(0,0,128,128,BLACK); |
macenzofan | 28:8dbb85f35be6 | 875 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 876 | game_init(); |
macenzofan | 28:8dbb85f35be6 | 877 | } |
macenzofan | 28:8dbb85f35be6 | 878 | else if(!pb_r){ |
macenzofan | 28:8dbb85f35be6 | 879 | sync.rectangle(68,63,95,76,WHITE); |
macenzofan | 28:8dbb85f35be6 | 880 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 881 | wait(2); |
macenzofan | 28:8dbb85f35be6 | 882 | sync.cls(); |
macenzofan | 28:8dbb85f35be6 | 883 | sync.textbackground_color(BLACK); |
macenzofan | 28:8dbb85f35be6 | 884 | sync.filled_rectangle(0,0,128,128,BLACK); |
macenzofan | 28:8dbb85f35be6 | 885 | sync.update(); |
macenzofan | 28:8dbb85f35be6 | 886 | main(); |
macenzofan | 28:8dbb85f35be6 | 887 | } |
macenzofan | 28:8dbb85f35be6 | 888 | } |
macenzofan | 28:8dbb85f35be6 | 889 | } |
jford38 | 0:899c85cd266f | 890 | } |