Updated Space Invaders on the mbed. Improved upon Michael Son's "Mbed Space Invaders" at https://os.mbed.com/users/michaeljson/notebook/mbed-space-invaders/.
Dependencies: mbed wave_player mbed-rtos 4DGL-uLCD-SE SparkfunAnalogJoystick SDFileSystem LSM9DS1_Library_cal_updated
Fork of Two-PlayerSpaceInvaders by
test
main.cpp@25:17f2ec000357, 2021-04-28 (annotated)
- Committer:
- wminix3
- Date:
- Wed Apr 28 01:17:30 2021 +0000
- Revision:
- 25:17f2ec000357
- Parent:
- 24:ea19c7e8a479
- Child:
- 26:3270c6edd7d9
Fewer barriers with higher difficulties works.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaeljson | 0:3817adfaeb06 | 1 | #include "mbed.h" |
michaeljson | 0:3817adfaeb06 | 2 | #include "SDFileSystem.h" |
michaeljson | 0:3817adfaeb06 | 3 | #include "wave_player.h" |
michaeljson | 0:3817adfaeb06 | 4 | #include "enemy.h" |
michaeljson | 0:3817adfaeb06 | 5 | #include "player.h" |
wminix3 | 23:56f6a12aaebd | 6 | #include "barrier.h" |
michaeljson | 0:3817adfaeb06 | 7 | #include "missile.h" |
michaeljson | 0:3817adfaeb06 | 8 | #include "globals.h" |
michaeljson | 0:3817adfaeb06 | 9 | #include "rtos.h" |
wminix3 | 16:e4e6515bdabb | 10 | #include "SparkfunAnalogJoystick.h" |
wminix3 | 17:843a874eb4e3 | 11 | #include "LSM9DS1.h" |
michaeljson | 0:3817adfaeb06 | 12 | #include <string> |
michaeljson | 0:3817adfaeb06 | 13 | |
michaeljson | 0:3817adfaeb06 | 14 | /* ==== Navigation Switch ===== */ |
wminix3 | 16:e4e6515bdabb | 15 | /* |
michaeljson | 0:3817adfaeb06 | 16 | class Nav_Switch |
michaeljson | 0:3817adfaeb06 | 17 | { |
michaeljson | 0:3817adfaeb06 | 18 | public: |
michaeljson | 0:3817adfaeb06 | 19 | Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire); |
michaeljson | 0:3817adfaeb06 | 20 | int read(); |
michaeljson | 0:3817adfaeb06 | 21 | //boolean functions to test each switch |
michaeljson | 0:3817adfaeb06 | 22 | bool up(); |
michaeljson | 0:3817adfaeb06 | 23 | bool down(); |
michaeljson | 0:3817adfaeb06 | 24 | bool left(); |
michaeljson | 0:3817adfaeb06 | 25 | bool right(); |
michaeljson | 0:3817adfaeb06 | 26 | bool fire(); |
michaeljson | 0:3817adfaeb06 | 27 | //automatic read on RHS |
michaeljson | 0:3817adfaeb06 | 28 | operator int (); |
michaeljson | 0:3817adfaeb06 | 29 | //index to any switch array style |
michaeljson | 0:3817adfaeb06 | 30 | bool operator[](int index) { |
michaeljson | 0:3817adfaeb06 | 31 | return _pins[index]; |
michaeljson | 0:3817adfaeb06 | 32 | }; |
michaeljson | 0:3817adfaeb06 | 33 | private: |
michaeljson | 0:3817adfaeb06 | 34 | BusIn _pins; |
michaeljson | 0:3817adfaeb06 | 35 | |
michaeljson | 0:3817adfaeb06 | 36 | }; |
michaeljson | 0:3817adfaeb06 | 37 | Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire): |
michaeljson | 0:3817adfaeb06 | 38 | _pins(up, down, left, right, fire) |
michaeljson | 0:3817adfaeb06 | 39 | { |
michaeljson | 0:3817adfaeb06 | 40 | _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise |
michaeljson | 0:3817adfaeb06 | 41 | wait(0.001); //delays just a bit for pullups to pull inputs high |
michaeljson | 0:3817adfaeb06 | 42 | } |
michaeljson | 0:3817adfaeb06 | 43 | inline bool Nav_Switch::up() |
michaeljson | 0:3817adfaeb06 | 44 | { |
michaeljson | 0:3817adfaeb06 | 45 | return !(_pins[0]); |
michaeljson | 0:3817adfaeb06 | 46 | } |
michaeljson | 0:3817adfaeb06 | 47 | inline bool Nav_Switch::down() |
michaeljson | 0:3817adfaeb06 | 48 | { |
michaeljson | 0:3817adfaeb06 | 49 | return !(_pins[1]); |
michaeljson | 0:3817adfaeb06 | 50 | } |
michaeljson | 0:3817adfaeb06 | 51 | inline bool Nav_Switch::left() |
michaeljson | 0:3817adfaeb06 | 52 | { |
michaeljson | 0:3817adfaeb06 | 53 | return !(_pins[2]); |
michaeljson | 0:3817adfaeb06 | 54 | } |
michaeljson | 0:3817adfaeb06 | 55 | inline bool Nav_Switch::right() |
michaeljson | 0:3817adfaeb06 | 56 | { |
michaeljson | 0:3817adfaeb06 | 57 | return !(_pins[3]); |
michaeljson | 0:3817adfaeb06 | 58 | } |
michaeljson | 0:3817adfaeb06 | 59 | inline bool Nav_Switch::fire() |
michaeljson | 0:3817adfaeb06 | 60 | { |
michaeljson | 0:3817adfaeb06 | 61 | return !(_pins[4]); |
michaeljson | 0:3817adfaeb06 | 62 | } |
michaeljson | 0:3817adfaeb06 | 63 | inline int Nav_Switch::read() |
michaeljson | 0:3817adfaeb06 | 64 | { |
michaeljson | 0:3817adfaeb06 | 65 | return _pins.read(); |
michaeljson | 0:3817adfaeb06 | 66 | } |
michaeljson | 0:3817adfaeb06 | 67 | inline Nav_Switch::operator int () |
michaeljson | 0:3817adfaeb06 | 68 | { |
michaeljson | 0:3817adfaeb06 | 69 | return _pins.read(); |
michaeljson | 0:3817adfaeb06 | 70 | } |
wminix3 | 16:e4e6515bdabb | 71 | */ |
michaeljson | 0:3817adfaeb06 | 72 | |
michaeljson | 0:3817adfaeb06 | 73 | // Platform initialization |
wminix3 | 16:e4e6515bdabb | 74 | uLCD_4DGL uLCD(p28,p27,p29); // LCD (serial tx, serial rx, reset pin;) |
michaeljson | 0:3817adfaeb06 | 75 | AnalogOut DACout(p18); // speaker |
michaeljson | 0:3817adfaeb06 | 76 | wave_player waver(&DACout); // wav player |
michaeljson | 0:3817adfaeb06 | 77 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD card and filesystem (mosi, miso, sck, cs) |
wminix3 | 16:e4e6515bdabb | 78 | //Nav_Switch myNav(p16, p10, p11, p9, p12); //pin order on Sparkfun breakout. move U from p13 to p16 so we can use another Serial to communicate with mbed |
wminix3 | 16:e4e6515bdabb | 79 | DigitalIn pb(p20); // push button for player misisle fire |
wminix3 | 2:4a3f97570578 | 80 | Serial pc(USBTX, USBRX); |
wminix3 | 24:ea19c7e8a479 | 81 | RawSerial secondMbed(p13, p14); |
wminix3 | 11:cdea2c9ec531 | 82 | PwmOut red(p21); // added to dim and brighten red LED -- Brice |
wminix3 | 11:cdea2c9ec531 | 83 | PwmOut green(p22); // added to dim and brighten green LED -- Brice |
wminix3 | 11:cdea2c9ec531 | 84 | PwmOut blue(p23); // added to dim and brighten blue LED -- Brice |
wminix3 | 16:e4e6515bdabb | 85 | SparkfunAnalogJoystick stick(p15, p16, p17); // Sparkfun analog joystick to replace the tactile switch (menu control and more fluid control of ship) |
wminix3 | 17:843a874eb4e3 | 86 | LSM9DS1 IMU(p9, p10, 0xD6, 0x3C); |
michaeljson | 0:3817adfaeb06 | 87 | // Initialize all global enemy objects |
michaeljson | 0:3817adfaeb06 | 88 | enemy_t enemy_1; |
michaeljson | 0:3817adfaeb06 | 89 | enemy_t enemy_2; |
michaeljson | 0:3817adfaeb06 | 90 | enemy_t enemy_3; |
michaeljson | 0:3817adfaeb06 | 91 | enemy_t enemy_4; |
michaeljson | 0:3817adfaeb06 | 92 | enemy_t enemy_5; |
michaeljson | 0:3817adfaeb06 | 93 | enemy_t enemy_6; |
michaeljson | 0:3817adfaeb06 | 94 | enemy_t enemy_7; |
michaeljson | 0:3817adfaeb06 | 95 | enemy_t enemy_8; |
michaeljson | 0:3817adfaeb06 | 96 | enemy_t enemy_9; |
michaeljson | 0:3817adfaeb06 | 97 | enemy_t enemy_10; |
michaeljson | 0:3817adfaeb06 | 98 | enemy_t enemy_11; |
michaeljson | 0:3817adfaeb06 | 99 | enemy_t enemy_12; |
michaeljson | 0:3817adfaeb06 | 100 | enemy_t enemy_13; |
michaeljson | 0:3817adfaeb06 | 101 | enemy_t enemy_14; |
michaeljson | 0:3817adfaeb06 | 102 | enemy_t enemy_15; |
michaeljson | 0:3817adfaeb06 | 103 | |
wminix3 | 23:56f6a12aaebd | 104 | barrier_t barrier_1; |
wminix3 | 23:56f6a12aaebd | 105 | barrier_t barrier_2; |
wminix3 | 23:56f6a12aaebd | 106 | barrier_t barrier_3; |
wminix3 | 23:56f6a12aaebd | 107 | barrier_t barrier_4; |
wminix3 | 23:56f6a12aaebd | 108 | |
michaeljson | 0:3817adfaeb06 | 109 | // Initialize variables |
wminix3 | 2:4a3f97570578 | 110 | // Brice added "volatile" here to protect global variables. |
wminix3 | 2:4a3f97570578 | 111 | volatile int numOfEnemies = 0; |
wminix3 | 2:4a3f97570578 | 112 | volatile int ENEMY_MOVE = 1; |
wminix3 | 2:4a3f97570578 | 113 | volatile int MOVE_DOWN = 0; |
wminix3 | 2:4a3f97570578 | 114 | volatile int DIRECTION = 1; |
wminix3 | 2:4a3f97570578 | 115 | volatile int firing_col = 0; |
wminix3 | 2:4a3f97570578 | 116 | volatile int hit_player = 0; |
wminix3 | 2:4a3f97570578 | 117 | volatile bool lose = false; |
wminix3 | 24:ea19c7e8a479 | 118 | volatile int lives1 = 3; // number of lives in level 1 |
wminix3 | 24:ea19c7e8a479 | 119 | volatile int lives2 = 2; // number of lives in level 2 |
wminix3 | 24:ea19c7e8a479 | 120 | volatile int lives3 = 1; // number of lives in level 3 |
wminix3 | 24:ea19c7e8a479 | 121 | volatile int level = 1; // the level, number of lives/difficulty |
wminix3 | 2:4a3f97570578 | 122 | volatile bool game_menu = false; |
wminix3 | 2:4a3f97570578 | 123 | volatile bool begin_game = false; |
wminix3 | 2:4a3f97570578 | 124 | volatile bool gameover = false; |
wminix3 | 2:4a3f97570578 | 125 | volatile int numPlayers = 1; |
wminix3 | 14:4e7608619043 | 126 | //volatile bool first_player_ready = false; |
wminix3 | 14:4e7608619043 | 127 | //volatile bool second_player_ready = false; |
wminix3 | 5:b7934866b264 | 128 | volatile bool begin_game2 = false; |
wminix3 | 5:b7934866b264 | 129 | volatile int numWins = 0; |
wminix3 | 20:ede4fa57d082 | 130 | volatile bool two_player_win = false; // global for a two_player_win |
wminix3 | 20:ede4fa57d082 | 131 | volatile bool two_player_lose = false; // global for a two_player_lose |
wminix3 | 20:ede4fa57d082 | 132 | volatile bool win = false; // add win to global variables so that we can have LED effects and sound effects for a win |
wminix3 | 13:36cc024dcf6b | 133 | Timer bestTimer; |
wminix3 | 13:36cc024dcf6b | 134 | Mutex SDLock; |
wminix3 | 8:27c4345be3db | 135 | Mutex mbedLock; |
michaeljson | 0:3817adfaeb06 | 136 | |
michaeljson | 0:3817adfaeb06 | 137 | // Initialize global player object |
michaeljson | 0:3817adfaeb06 | 138 | player_t player; |
michaeljson | 0:3817adfaeb06 | 139 | |
michaeljson | 0:3817adfaeb06 | 140 | // Intialize global player and enemy missile |
michaeljson | 0:3817adfaeb06 | 141 | missile_t missile; // player missile |
michaeljson | 0:3817adfaeb06 | 142 | missile_t enemy_missile; // enemy missile |
wminix3 | 25:17f2ec000357 | 143 | //missile_t enemy_missile2; // first extra enemy missile for level 2 (medium difficulty) |
michaeljson | 0:3817adfaeb06 | 144 | |
michaeljson | 0:3817adfaeb06 | 145 | // Array of enemy objects |
michaeljson | 0:3817adfaeb06 | 146 | enemy_t * enemyArray[15]; |
michaeljson | 0:3817adfaeb06 | 147 | |
michaeljson | 0:3817adfaeb06 | 148 | // Function Prototypes |
michaeljson | 0:3817adfaeb06 | 149 | void move_enemy_down(); |
wminix3 | 14:4e7608619043 | 150 | void playstart(void const *args); // PUT BACK IN |
michaeljson | 0:3817adfaeb06 | 151 | |
michaeljson | 0:3817adfaeb06 | 152 | // Draws the enemies at the initial starting location |
michaeljson | 0:3817adfaeb06 | 153 | void draw_enemies_level() |
michaeljson | 0:3817adfaeb06 | 154 | { |
wminix3 | 23:56f6a12aaebd | 155 | // Initialize local variables |
michaeljson | 0:3817adfaeb06 | 156 | unsigned int start_x_pos = 12; |
michaeljson | 0:3817adfaeb06 | 157 | unsigned int start_enemy_y_pos = 20; |
michaeljson | 0:3817adfaeb06 | 158 | numOfEnemies = 15; |
michaeljson | 0:3817adfaeb06 | 159 | |
michaeljson | 0:3817adfaeb06 | 160 | // First Row of Enemies |
michaeljson | 0:3817adfaeb06 | 161 | enemy_init(&enemy_1,start_x_pos,start_enemy_y_pos,WHITE); // initialize x-pos and y-pos and color of enemy |
michaeljson | 0:3817adfaeb06 | 162 | enemy_show(&enemy_1); // displays the enemy on uLCD |
michaeljson | 0:3817adfaeb06 | 163 | |
michaeljson | 0:3817adfaeb06 | 164 | enemy_init(&enemy_2,start_x_pos+15,start_enemy_y_pos,WHITE); |
michaeljson | 0:3817adfaeb06 | 165 | enemy_show(&enemy_2); |
michaeljson | 0:3817adfaeb06 | 166 | |
michaeljson | 0:3817adfaeb06 | 167 | enemy_init(&enemy_3,start_x_pos+30,start_enemy_y_pos,WHITE); |
michaeljson | 0:3817adfaeb06 | 168 | enemy_show(&enemy_3); |
michaeljson | 0:3817adfaeb06 | 169 | |
michaeljson | 0:3817adfaeb06 | 170 | enemy_init(&enemy_4,start_x_pos+45,start_enemy_y_pos,WHITE); |
michaeljson | 0:3817adfaeb06 | 171 | enemy_show(&enemy_4); |
michaeljson | 0:3817adfaeb06 | 172 | |
michaeljson | 0:3817adfaeb06 | 173 | enemy_init(&enemy_5,start_x_pos+60,start_enemy_y_pos,WHITE); |
michaeljson | 0:3817adfaeb06 | 174 | enemy_show(&enemy_5); |
michaeljson | 0:3817adfaeb06 | 175 | |
michaeljson | 0:3817adfaeb06 | 176 | // Second Row of Enemies |
michaeljson | 0:3817adfaeb06 | 177 | enemy_init(&enemy_6,start_x_pos,start_enemy_y_pos+12,WHITE); |
michaeljson | 0:3817adfaeb06 | 178 | enemy_show(&enemy_6); |
michaeljson | 0:3817adfaeb06 | 179 | |
michaeljson | 0:3817adfaeb06 | 180 | enemy_init(&enemy_7,start_x_pos+15,start_enemy_y_pos+12,WHITE); |
michaeljson | 0:3817adfaeb06 | 181 | enemy_show(&enemy_7); |
michaeljson | 0:3817adfaeb06 | 182 | |
michaeljson | 0:3817adfaeb06 | 183 | enemy_init(&enemy_8,start_x_pos+30,start_enemy_y_pos+12,WHITE); |
michaeljson | 0:3817adfaeb06 | 184 | enemy_show(&enemy_8); |
michaeljson | 0:3817adfaeb06 | 185 | |
michaeljson | 0:3817adfaeb06 | 186 | enemy_init(&enemy_9,start_x_pos+45,start_enemy_y_pos+12,WHITE); |
michaeljson | 0:3817adfaeb06 | 187 | enemy_show(&enemy_9); |
michaeljson | 0:3817adfaeb06 | 188 | |
michaeljson | 0:3817adfaeb06 | 189 | enemy_init(&enemy_10,start_x_pos+60,start_enemy_y_pos+12,WHITE); |
michaeljson | 0:3817adfaeb06 | 190 | enemy_show(&enemy_10); |
michaeljson | 0:3817adfaeb06 | 191 | |
michaeljson | 0:3817adfaeb06 | 192 | // Third Row of Enemies |
michaeljson | 0:3817adfaeb06 | 193 | enemy_init(&enemy_11,start_x_pos,start_enemy_y_pos+24,WHITE); |
michaeljson | 0:3817adfaeb06 | 194 | enemy_show(&enemy_11); |
michaeljson | 0:3817adfaeb06 | 195 | |
michaeljson | 0:3817adfaeb06 | 196 | enemy_init(&enemy_12,start_x_pos+15,start_enemy_y_pos+24,WHITE); |
michaeljson | 0:3817adfaeb06 | 197 | enemy_show(&enemy_12); |
michaeljson | 0:3817adfaeb06 | 198 | |
michaeljson | 0:3817adfaeb06 | 199 | enemy_init(&enemy_13,start_x_pos+30,start_enemy_y_pos+24,WHITE); |
michaeljson | 0:3817adfaeb06 | 200 | enemy_show(&enemy_13); |
michaeljson | 0:3817adfaeb06 | 201 | |
michaeljson | 0:3817adfaeb06 | 202 | enemy_init(&enemy_14,start_x_pos+45,start_enemy_y_pos+24,WHITE); |
michaeljson | 0:3817adfaeb06 | 203 | enemy_show(&enemy_14); |
michaeljson | 0:3817adfaeb06 | 204 | |
michaeljson | 0:3817adfaeb06 | 205 | enemy_init(&enemy_15,start_x_pos+60,start_enemy_y_pos+24,WHITE); |
michaeljson | 0:3817adfaeb06 | 206 | enemy_show(&enemy_15); |
michaeljson | 0:3817adfaeb06 | 207 | |
michaeljson | 0:3817adfaeb06 | 208 | // Put enemy objects into array |
michaeljson | 0:3817adfaeb06 | 209 | enemyArray[0] = &enemy_1; |
michaeljson | 0:3817adfaeb06 | 210 | enemyArray[1] = &enemy_2; |
michaeljson | 0:3817adfaeb06 | 211 | enemyArray[2] = &enemy_3; |
michaeljson | 0:3817adfaeb06 | 212 | enemyArray[3] = &enemy_4; |
michaeljson | 0:3817adfaeb06 | 213 | enemyArray[4] = &enemy_5; |
michaeljson | 0:3817adfaeb06 | 214 | enemyArray[5] = &enemy_6; |
michaeljson | 0:3817adfaeb06 | 215 | enemyArray[6] = &enemy_7; |
michaeljson | 0:3817adfaeb06 | 216 | enemyArray[7] = &enemy_8; |
michaeljson | 0:3817adfaeb06 | 217 | enemyArray[8] = &enemy_9; |
michaeljson | 0:3817adfaeb06 | 218 | enemyArray[9] = &enemy_10; |
michaeljson | 0:3817adfaeb06 | 219 | enemyArray[10] = &enemy_11; |
michaeljson | 0:3817adfaeb06 | 220 | enemyArray[11] = &enemy_12; |
michaeljson | 0:3817adfaeb06 | 221 | enemyArray[12] = &enemy_13; |
michaeljson | 0:3817adfaeb06 | 222 | enemyArray[13] = &enemy_14; |
michaeljson | 0:3817adfaeb06 | 223 | enemyArray[14] = &enemy_15; |
michaeljson | 0:3817adfaeb06 | 224 | } |
michaeljson | 0:3817adfaeb06 | 225 | |
wminix3 | 23:56f6a12aaebd | 226 | |
wminix3 | 23:56f6a12aaebd | 227 | void draw_barriers_level() { |
wminix3 | 23:56f6a12aaebd | 228 | // Initialize local variables |
wminix3 | 23:56f6a12aaebd | 229 | unsigned int start_x_pos = 5; |
wminix3 | 23:56f6a12aaebd | 230 | unsigned int start_barrier_y_pos = 95; |
wminix3 | 23:56f6a12aaebd | 231 | |
wminix3 | 23:56f6a12aaebd | 232 | // First Row of Enemies |
wminix3 | 24:ea19c7e8a479 | 233 | if (level == 1) { |
wminix3 | 24:ea19c7e8a479 | 234 | barrier_init(&barrier_1,start_x_pos,start_barrier_y_pos,GREEN); // initialize x-pos and y-pos and color of enemy |
wminix3 | 24:ea19c7e8a479 | 235 | barrier_show(&barrier_1); // displays the enemy on uLCD |
wminix3 | 24:ea19c7e8a479 | 236 | } |
wminix3 | 23:56f6a12aaebd | 237 | |
wminix3 | 23:56f6a12aaebd | 238 | barrier_init(&barrier_2,start_x_pos+32,start_barrier_y_pos,GREEN); |
wminix3 | 23:56f6a12aaebd | 239 | barrier_show(&barrier_2); |
wminix3 | 23:56f6a12aaebd | 240 | |
wminix3 | 24:ea19c7e8a479 | 241 | if (level == 1 || level == 2) { |
wminix3 | 24:ea19c7e8a479 | 242 | barrier_init(&barrier_3,start_x_pos+64,start_barrier_y_pos,GREEN); |
wminix3 | 24:ea19c7e8a479 | 243 | barrier_show(&barrier_3); |
wminix3 | 24:ea19c7e8a479 | 244 | } |
wminix3 | 23:56f6a12aaebd | 245 | |
wminix3 | 24:ea19c7e8a479 | 246 | if (level == 1) { |
wminix3 | 24:ea19c7e8a479 | 247 | barrier_init(&barrier_4,start_x_pos+96,start_barrier_y_pos,GREEN); |
wminix3 | 24:ea19c7e8a479 | 248 | barrier_show(&barrier_4); |
wminix3 | 24:ea19c7e8a479 | 249 | } |
wminix3 | 23:56f6a12aaebd | 250 | } |
wminix3 | 23:56f6a12aaebd | 251 | |
michaeljson | 0:3817adfaeb06 | 252 | // Draws the player at the initial starting location |
michaeljson | 0:3817adfaeb06 | 253 | void draw_initial_player() |
michaeljson | 0:3817adfaeb06 | 254 | { |
michaeljson | 0:3817adfaeb06 | 255 | int start_x_pos = 59; |
wminix3 | 23:56f6a12aaebd | 256 | int start_y_pos = 120; |
michaeljson | 0:3817adfaeb06 | 257 | |
michaeljson | 0:3817adfaeb06 | 258 | player_init(&player,start_x_pos,start_y_pos,WHITE); // intialize x-pos and y-pos and color of player |
michaeljson | 0:3817adfaeb06 | 259 | player_show(&player); // display player on uLCD |
michaeljson | 0:3817adfaeb06 | 260 | } |
michaeljson | 0:3817adfaeb06 | 261 | |
michaeljson | 0:3817adfaeb06 | 262 | // Checks all enemies in row 1 |
michaeljson | 0:3817adfaeb06 | 263 | void check_hit_enemy_row1() |
michaeljson | 0:3817adfaeb06 | 264 | { |
michaeljson | 0:3817adfaeb06 | 265 | int hit_enemy; |
michaeljson | 0:3817adfaeb06 | 266 | |
michaeljson | 0:3817adfaeb06 | 267 | // First Row of Enemies |
michaeljson | 0:3817adfaeb06 | 268 | hit_enemy = check_enemy(&enemy_1, &missile); // checks if the missile hits the enemy and returns 1 if hit, 0 if not hit |
michaeljson | 0:3817adfaeb06 | 269 | numOfEnemies = numOfEnemies - hit_enemy; // updates the number of enemies left |
michaeljson | 0:3817adfaeb06 | 270 | hit_enemy = check_enemy(&enemy_2, &missile); |
michaeljson | 0:3817adfaeb06 | 271 | numOfEnemies = numOfEnemies - hit_enemy; |
michaeljson | 0:3817adfaeb06 | 272 | hit_enemy = check_enemy(&enemy_3, &missile); |
michaeljson | 0:3817adfaeb06 | 273 | numOfEnemies = numOfEnemies - hit_enemy; |
michaeljson | 0:3817adfaeb06 | 274 | hit_enemy = check_enemy(&enemy_4, &missile); |
michaeljson | 0:3817adfaeb06 | 275 | numOfEnemies = numOfEnemies - hit_enemy; |
michaeljson | 0:3817adfaeb06 | 276 | hit_enemy = check_enemy(&enemy_5, &missile); |
michaeljson | 0:3817adfaeb06 | 277 | numOfEnemies = numOfEnemies - hit_enemy; |
michaeljson | 0:3817adfaeb06 | 278 | } |
michaeljson | 0:3817adfaeb06 | 279 | |
michaeljson | 0:3817adfaeb06 | 280 | // Same as check_hit_enemy_row1, but checks enemies at row 2 |
michaeljson | 0:3817adfaeb06 | 281 | void check_hit_enemy_row2() |
michaeljson | 0:3817adfaeb06 | 282 | { |
michaeljson | 0:3817adfaeb06 | 283 | int hit_enemy; |
michaeljson | 0:3817adfaeb06 | 284 | |
michaeljson | 0:3817adfaeb06 | 285 | // Second Row of Enemies |
michaeljson | 0:3817adfaeb06 | 286 | hit_enemy = check_enemy(&enemy_6, &missile); |
michaeljson | 0:3817adfaeb06 | 287 | numOfEnemies = numOfEnemies - hit_enemy; |
michaeljson | 0:3817adfaeb06 | 288 | hit_enemy = check_enemy(&enemy_7, &missile); |
michaeljson | 0:3817adfaeb06 | 289 | numOfEnemies = numOfEnemies - hit_enemy; |
michaeljson | 0:3817adfaeb06 | 290 | hit_enemy = check_enemy(&enemy_8, &missile); |
michaeljson | 0:3817adfaeb06 | 291 | numOfEnemies = numOfEnemies - hit_enemy; |
michaeljson | 0:3817adfaeb06 | 292 | hit_enemy = check_enemy(&enemy_9, &missile); |
michaeljson | 0:3817adfaeb06 | 293 | numOfEnemies = numOfEnemies - hit_enemy; |
michaeljson | 0:3817adfaeb06 | 294 | hit_enemy = check_enemy(&enemy_10, &missile); |
michaeljson | 0:3817adfaeb06 | 295 | numOfEnemies = numOfEnemies - hit_enemy; |
michaeljson | 0:3817adfaeb06 | 296 | } |
michaeljson | 0:3817adfaeb06 | 297 | |
michaeljson | 0:3817adfaeb06 | 298 | // Same as check_hit_enemy_row1, but checks enemies at row 3 |
michaeljson | 0:3817adfaeb06 | 299 | void check_hit_enemy_row3() |
michaeljson | 0:3817adfaeb06 | 300 | { |
michaeljson | 0:3817adfaeb06 | 301 | int hit_enemy; |
michaeljson | 0:3817adfaeb06 | 302 | |
michaeljson | 0:3817adfaeb06 | 303 | // Third Row of Enemies |
michaeljson | 0:3817adfaeb06 | 304 | hit_enemy = check_enemy(&enemy_11, &missile); |
michaeljson | 0:3817adfaeb06 | 305 | numOfEnemies = numOfEnemies - hit_enemy; |
michaeljson | 0:3817adfaeb06 | 306 | hit_enemy = check_enemy(&enemy_12, &missile); |
michaeljson | 0:3817adfaeb06 | 307 | numOfEnemies = numOfEnemies - hit_enemy; |
michaeljson | 0:3817adfaeb06 | 308 | hit_enemy = check_enemy(&enemy_13, &missile); |
michaeljson | 0:3817adfaeb06 | 309 | numOfEnemies = numOfEnemies - hit_enemy; |
michaeljson | 0:3817adfaeb06 | 310 | hit_enemy = check_enemy(&enemy_14, &missile); |
michaeljson | 0:3817adfaeb06 | 311 | numOfEnemies = numOfEnemies - hit_enemy; |
michaeljson | 0:3817adfaeb06 | 312 | hit_enemy = check_enemy(&enemy_15, &missile); |
michaeljson | 0:3817adfaeb06 | 313 | numOfEnemies = numOfEnemies - hit_enemy; |
michaeljson | 0:3817adfaeb06 | 314 | } |
michaeljson | 0:3817adfaeb06 | 315 | |
michaeljson | 0:3817adfaeb06 | 316 | // Checks if player is hit |
michaeljson | 0:3817adfaeb06 | 317 | void check_player_hit() |
wminix3 | 25:17f2ec000357 | 318 | { |
wminix3 | 25:17f2ec000357 | 319 | hit_player = check_player(&player, &enemy_missile); // checks if the missile hits the player and returns 1 if hit, 0 if not hit |
michaeljson | 0:3817adfaeb06 | 320 | } |
michaeljson | 0:3817adfaeb06 | 321 | |
michaeljson | 0:3817adfaeb06 | 322 | // Randomly selects an enemy to fire and updates the position of where the missile will fire from |
michaeljson | 0:3817adfaeb06 | 323 | void random_attack_gen() |
michaeljson | 0:3817adfaeb06 | 324 | { |
michaeljson | 0:3817adfaeb06 | 325 | firing_col = rand() % 5; // selects a random column |
michaeljson | 0:3817adfaeb06 | 326 | |
michaeljson | 0:3817adfaeb06 | 327 | // first checks if the 3rd row closest to the player is alive |
michaeljson | 0:3817adfaeb06 | 328 | if (enemyArray[firing_col+10]->status == ENEMY_ALIVE) |
michaeljson | 0:3817adfaeb06 | 329 | { |
michaeljson | 0:3817adfaeb06 | 330 | // If alive, the enemy missile position is updated to the center of the enemy |
michaeljson | 0:3817adfaeb06 | 331 | enemy_missile.missile_blk_x = enemyArray[firing_col+10]->enemy_blk_x + (enemyArray[firing_col+10]->enemy_width/2); |
michaeljson | 0:3817adfaeb06 | 332 | enemy_missile.missile_blk_y = enemyArray[firing_col+10]->enemy_blk_y + enemyArray[firing_col+10]->enemy_height + 1; |
michaeljson | 0:3817adfaeb06 | 333 | enemy_missile.status = ENEMY_MISSILE_ACTIVE; // sets the enemy missile as active |
michaeljson | 0:3817adfaeb06 | 334 | } |
michaeljson | 0:3817adfaeb06 | 335 | // if enemy at 3rd row is dead, checks the enemy in the 2nd row |
michaeljson | 0:3817adfaeb06 | 336 | else if (enemyArray[firing_col+5]->status == ENEMY_ALIVE) |
michaeljson | 0:3817adfaeb06 | 337 | { |
michaeljson | 0:3817adfaeb06 | 338 | // If alive, the enemy missile position is updated to the center of the enemy |
michaeljson | 0:3817adfaeb06 | 339 | enemy_missile.missile_blk_x = enemyArray[firing_col+5]->enemy_blk_x + (enemyArray[firing_col+5]->enemy_width/2); |
michaeljson | 0:3817adfaeb06 | 340 | enemy_missile.missile_blk_y = enemyArray[firing_col+5]->enemy_blk_y + enemyArray[firing_col+5]->enemy_height + 1; |
michaeljson | 0:3817adfaeb06 | 341 | enemy_missile.status = ENEMY_MISSILE_ACTIVE; |
michaeljson | 0:3817adfaeb06 | 342 | } |
michaeljson | 0:3817adfaeb06 | 343 | // if enemy at 2nd and 3rd row is dead, checks the enemy in the 1st row |
michaeljson | 0:3817adfaeb06 | 344 | else if (enemyArray[firing_col]->status == ENEMY_ALIVE) |
michaeljson | 0:3817adfaeb06 | 345 | { |
michaeljson | 0:3817adfaeb06 | 346 | // If alive, the enemy missile position is updated to the center of the enemy |
michaeljson | 0:3817adfaeb06 | 347 | enemy_missile.missile_blk_x = enemyArray[firing_col]->enemy_blk_x + (enemyArray[firing_col]->enemy_width/2); |
michaeljson | 0:3817adfaeb06 | 348 | enemy_missile.missile_blk_y = enemyArray[firing_col]->enemy_blk_y + enemyArray[firing_col]->enemy_height + 1; |
michaeljson | 0:3817adfaeb06 | 349 | enemy_missile.status = ENEMY_MISSILE_ACTIVE; |
michaeljson | 0:3817adfaeb06 | 350 | } |
michaeljson | 0:3817adfaeb06 | 351 | } |
michaeljson | 0:3817adfaeb06 | 352 | |
michaeljson | 0:3817adfaeb06 | 353 | // moves the enemy |
michaeljson | 0:3817adfaeb06 | 354 | void enemy_motion() |
michaeljson | 0:3817adfaeb06 | 355 | { |
michaeljson | 0:3817adfaeb06 | 356 | // will move the enemy every 6 loops |
michaeljson | 0:3817adfaeb06 | 357 | if (ENEMY_MOVE % 6 == 0) |
michaeljson | 0:3817adfaeb06 | 358 | { |
michaeljson | 0:3817adfaeb06 | 359 | // FIrst Row of Enemies |
michaeljson | 0:3817adfaeb06 | 360 | MOVE_DOWN = move_enemy(&enemy_1, MOVE_DOWN, DIRECTION); // moves the enemy based on the DIRECTION passed in and also sends global MOVE_DOWN to update in enemy.cpp |
michaeljson | 0:3817adfaeb06 | 361 | MOVE_DOWN = move_enemy(&enemy_2, MOVE_DOWN, DIRECTION); // MOVE_DOWN will be 1 enemies reach the left or right edge of the screen |
michaeljson | 0:3817adfaeb06 | 362 | MOVE_DOWN = move_enemy(&enemy_3, MOVE_DOWN, DIRECTION); // MOVE_DOWN will be 2 if enemies reach the player, otherwise 0 |
michaeljson | 0:3817adfaeb06 | 363 | MOVE_DOWN = move_enemy(&enemy_4, MOVE_DOWN, DIRECTION); |
michaeljson | 0:3817adfaeb06 | 364 | MOVE_DOWN = move_enemy(&enemy_5, MOVE_DOWN, DIRECTION); |
michaeljson | 0:3817adfaeb06 | 365 | |
michaeljson | 0:3817adfaeb06 | 366 | // Second Row of Enemies |
michaeljson | 0:3817adfaeb06 | 367 | MOVE_DOWN = move_enemy(&enemy_6, MOVE_DOWN, DIRECTION); |
michaeljson | 0:3817adfaeb06 | 368 | MOVE_DOWN = move_enemy(&enemy_7, MOVE_DOWN, DIRECTION); |
michaeljson | 0:3817adfaeb06 | 369 | MOVE_DOWN = move_enemy(&enemy_8, MOVE_DOWN, DIRECTION); |
michaeljson | 0:3817adfaeb06 | 370 | MOVE_DOWN = move_enemy(&enemy_9, MOVE_DOWN, DIRECTION); |
michaeljson | 0:3817adfaeb06 | 371 | MOVE_DOWN = move_enemy(&enemy_10, MOVE_DOWN, DIRECTION); |
michaeljson | 0:3817adfaeb06 | 372 | |
michaeljson | 0:3817adfaeb06 | 373 | // Third Row of Enemies |
michaeljson | 0:3817adfaeb06 | 374 | MOVE_DOWN = move_enemy(&enemy_11, MOVE_DOWN, DIRECTION); |
michaeljson | 0:3817adfaeb06 | 375 | MOVE_DOWN = move_enemy(&enemy_12, MOVE_DOWN, DIRECTION); |
michaeljson | 0:3817adfaeb06 | 376 | MOVE_DOWN = move_enemy(&enemy_13, MOVE_DOWN, DIRECTION); |
michaeljson | 0:3817adfaeb06 | 377 | MOVE_DOWN = move_enemy(&enemy_14, MOVE_DOWN, DIRECTION); |
michaeljson | 0:3817adfaeb06 | 378 | MOVE_DOWN = move_enemy(&enemy_15, MOVE_DOWN, DIRECTION); |
michaeljson | 0:3817adfaeb06 | 379 | |
michaeljson | 0:3817adfaeb06 | 380 | // if MOVE_DOWN is 2, then the enemies have reached the player |
michaeljson | 0:3817adfaeb06 | 381 | if (MOVE_DOWN == 2) |
michaeljson | 0:3817adfaeb06 | 382 | { |
michaeljson | 0:3817adfaeb06 | 383 | lose = true; // set global lose to true to go to gameover screen |
michaeljson | 0:3817adfaeb06 | 384 | } |
michaeljson | 0:3817adfaeb06 | 385 | |
michaeljson | 0:3817adfaeb06 | 386 | // if MOVE_DOWN is 1, update the y-pos of the enemies |
michaeljson | 0:3817adfaeb06 | 387 | if (MOVE_DOWN == 1) |
michaeljson | 0:3817adfaeb06 | 388 | { |
michaeljson | 0:3817adfaeb06 | 389 | move_enemy_down(); // updates the y-pos of the enemies |
michaeljson | 0:3817adfaeb06 | 390 | |
michaeljson | 0:3817adfaeb06 | 391 | // Flips the direction when the enemy moves down |
michaeljson | 0:3817adfaeb06 | 392 | if (DIRECTION == 1) |
michaeljson | 0:3817adfaeb06 | 393 | { |
michaeljson | 0:3817adfaeb06 | 394 | DIRECTION = 2; |
michaeljson | 0:3817adfaeb06 | 395 | } |
michaeljson | 0:3817adfaeb06 | 396 | else |
michaeljson | 0:3817adfaeb06 | 397 | { |
michaeljson | 0:3817adfaeb06 | 398 | DIRECTION = 1; |
michaeljson | 0:3817adfaeb06 | 399 | } |
michaeljson | 0:3817adfaeb06 | 400 | MOVE_DOWN = 0; // sets MOVE_DOWN back to 0 to stop down movement until |
michaeljson | 0:3817adfaeb06 | 401 | } |
michaeljson | 0:3817adfaeb06 | 402 | |
michaeljson | 0:3817adfaeb06 | 403 | ENEMY_MOVE += 1; |
michaeljson | 0:3817adfaeb06 | 404 | } |
michaeljson | 0:3817adfaeb06 | 405 | else |
michaeljson | 0:3817adfaeb06 | 406 | { |
michaeljson | 0:3817adfaeb06 | 407 | ENEMY_MOVE += 1; |
michaeljson | 0:3817adfaeb06 | 408 | } |
michaeljson | 0:3817adfaeb06 | 409 | } |
michaeljson | 0:3817adfaeb06 | 410 | |
michaeljson | 0:3817adfaeb06 | 411 | // moves all the enemies down in the y-direction |
michaeljson | 0:3817adfaeb06 | 412 | void move_enemy_down() |
michaeljson | 0:3817adfaeb06 | 413 | { |
michaeljson | 0:3817adfaeb06 | 414 | // First Row of Enemies |
michaeljson | 0:3817adfaeb06 | 415 | enemy_erase(&enemy_1); |
michaeljson | 0:3817adfaeb06 | 416 | enemy_erase(&enemy_2); |
michaeljson | 0:3817adfaeb06 | 417 | enemy_erase(&enemy_3); |
michaeljson | 0:3817adfaeb06 | 418 | enemy_erase(&enemy_4); |
michaeljson | 0:3817adfaeb06 | 419 | enemy_erase(&enemy_5); |
michaeljson | 0:3817adfaeb06 | 420 | |
michaeljson | 0:3817adfaeb06 | 421 | enemy_1.enemy_blk_y += enemy_1.enemy_height+4; |
michaeljson | 0:3817adfaeb06 | 422 | enemy_2.enemy_blk_y += enemy_2.enemy_height+4; |
michaeljson | 0:3817adfaeb06 | 423 | enemy_3.enemy_blk_y += enemy_3.enemy_height+4; |
michaeljson | 0:3817adfaeb06 | 424 | enemy_4.enemy_blk_y += enemy_4.enemy_height+4; |
michaeljson | 0:3817adfaeb06 | 425 | enemy_5.enemy_blk_y += enemy_5.enemy_height+4; |
michaeljson | 0:3817adfaeb06 | 426 | |
michaeljson | 0:3817adfaeb06 | 427 | // Second Row of Enemies |
michaeljson | 0:3817adfaeb06 | 428 | enemy_erase(&enemy_6); |
michaeljson | 0:3817adfaeb06 | 429 | enemy_erase(&enemy_7); |
michaeljson | 0:3817adfaeb06 | 430 | enemy_erase(&enemy_8); |
michaeljson | 0:3817adfaeb06 | 431 | enemy_erase(&enemy_9); |
michaeljson | 0:3817adfaeb06 | 432 | enemy_erase(&enemy_10); |
michaeljson | 0:3817adfaeb06 | 433 | |
michaeljson | 0:3817adfaeb06 | 434 | enemy_6.enemy_blk_y += enemy_6.enemy_height+4; |
michaeljson | 0:3817adfaeb06 | 435 | enemy_7.enemy_blk_y += enemy_7.enemy_height+4; |
michaeljson | 0:3817adfaeb06 | 436 | enemy_8.enemy_blk_y += enemy_8.enemy_height+4; |
michaeljson | 0:3817adfaeb06 | 437 | enemy_9.enemy_blk_y += enemy_9.enemy_height+4; |
michaeljson | 0:3817adfaeb06 | 438 | enemy_10.enemy_blk_y += enemy_10.enemy_height+4; |
michaeljson | 0:3817adfaeb06 | 439 | |
michaeljson | 0:3817adfaeb06 | 440 | // Third Row of Enemies |
michaeljson | 0:3817adfaeb06 | 441 | enemy_erase(&enemy_11); |
michaeljson | 0:3817adfaeb06 | 442 | enemy_erase(&enemy_12); |
michaeljson | 0:3817adfaeb06 | 443 | enemy_erase(&enemy_13); |
michaeljson | 0:3817adfaeb06 | 444 | enemy_erase(&enemy_14); |
michaeljson | 0:3817adfaeb06 | 445 | enemy_erase(&enemy_15); |
michaeljson | 0:3817adfaeb06 | 446 | |
michaeljson | 0:3817adfaeb06 | 447 | enemy_11.enemy_blk_y += enemy_11.enemy_height+4; |
michaeljson | 0:3817adfaeb06 | 448 | enemy_12.enemy_blk_y += enemy_12.enemy_height+4; |
michaeljson | 0:3817adfaeb06 | 449 | enemy_13.enemy_blk_y += enemy_13.enemy_height+4; |
michaeljson | 0:3817adfaeb06 | 450 | enemy_14.enemy_blk_y += enemy_14.enemy_height+4; |
michaeljson | 0:3817adfaeb06 | 451 | enemy_15.enemy_blk_y += enemy_15.enemy_height+4; |
michaeljson | 0:3817adfaeb06 | 452 | } |
wminix3 | 14:4e7608619043 | 453 | |
michaeljson | 0:3817adfaeb06 | 454 | // thread that plays sounds during game |
michaeljson | 0:3817adfaeb06 | 455 | void playstart(void const *args)//Th |
michaeljson | 0:3817adfaeb06 | 456 | { //Depending on the state of the game, |
michaeljson | 0:3817adfaeb06 | 457 | //queue either screen music or play sound effect upon fire |
michaeljson | 0:3817adfaeb06 | 458 | while(true) { |
michaeljson | 0:3817adfaeb06 | 459 | FILE *wave_file; |
michaeljson | 0:3817adfaeb06 | 460 | |
michaeljson | 0:3817adfaeb06 | 461 | // plays intro music during menu screen |
michaeljson | 0:3817adfaeb06 | 462 | while(game_menu) |
michaeljson | 0:3817adfaeb06 | 463 | { |
wminix3 | 13:36cc024dcf6b | 464 | SDLock.lock(); |
wminix3 | 2:4a3f97570578 | 465 | wave_file=fopen("/sd/wavfiles/futureEdit2.wav","r"); |
wminix3 | 2:4a3f97570578 | 466 | if(wave_file==NULL) pc.printf("file open error!\n\n\r"); // added this for open error check |
michaeljson | 0:3817adfaeb06 | 467 | waver.play(wave_file); |
michaeljson | 0:3817adfaeb06 | 468 | fclose(wave_file); |
wminix3 | 13:36cc024dcf6b | 469 | SDLock.unlock(); |
michaeljson | 0:3817adfaeb06 | 470 | } |
michaeljson | 0:3817adfaeb06 | 471 | |
michaeljson | 0:3817adfaeb06 | 472 | // Checks in game sound conditions |
wminix3 | 5:b7934866b264 | 473 | while(begin_game || begin_game2) // added OR begin_game2 so that music/sounds play during one-player or two-player |
michaeljson | 0:3817adfaeb06 | 474 | { |
michaeljson | 0:3817adfaeb06 | 475 | // play firing sound when the player fires |
michaeljson | 0:3817adfaeb06 | 476 | if(!pb && missile.status == PLAYER_MISSILE_INACTIVE) { |
michaeljson | 0:3817adfaeb06 | 477 | |
wminix3 | 13:36cc024dcf6b | 478 | SDLock.lock(); |
wminix3 | 2:4a3f97570578 | 479 | wave_file=fopen("/sd/wavfiles/laserEdit.wav","r"); |
wminix3 | 2:4a3f97570578 | 480 | if(wave_file==NULL) pc.printf("laser file open error!\n\n\r"); // added this for open error check |
michaeljson | 0:3817adfaeb06 | 481 | |
michaeljson | 0:3817adfaeb06 | 482 | waver.play(wave_file); |
michaeljson | 0:3817adfaeb06 | 483 | fclose(wave_file); |
wminix3 | 13:36cc024dcf6b | 484 | SDLock.unlock(); |
michaeljson | 0:3817adfaeb06 | 485 | } |
michaeljson | 0:3817adfaeb06 | 486 | |
michaeljson | 0:3817adfaeb06 | 487 | // if player hit, play hit sound |
michaeljson | 0:3817adfaeb06 | 488 | if (hit_player) |
michaeljson | 0:3817adfaeb06 | 489 | { |
wminix3 | 13:36cc024dcf6b | 490 | SDLock.lock(); |
wminix3 | 2:4a3f97570578 | 491 | wave_file=fopen("/sd/wavfiles/bigExplosionEdit2.wav","r"); |
wminix3 | 2:4a3f97570578 | 492 | if(wave_file==NULL) pc.printf("explosion file open error!\n\n\r"); // added this for open error check |
michaeljson | 0:3817adfaeb06 | 493 | waver.play(wave_file); |
michaeljson | 0:3817adfaeb06 | 494 | fclose(wave_file); |
wminix3 | 13:36cc024dcf6b | 495 | SDLock.unlock(); |
michaeljson | 0:3817adfaeb06 | 496 | } |
michaeljson | 0:3817adfaeb06 | 497 | } |
michaeljson | 0:3817adfaeb06 | 498 | |
wminix3 | 20:ede4fa57d082 | 499 | // victory music |
wminix3 | 20:ede4fa57d082 | 500 | while(win) { |
wminix3 | 20:ede4fa57d082 | 501 | SDLock.lock(); |
wminix3 | 20:ede4fa57d082 | 502 | wave_file=fopen("/sd/wavfiles/victoryEdit2.wav", "r"); |
wminix3 | 20:ede4fa57d082 | 503 | if(wave_file==NULL) pc.printf("explosion file open error!\n\n\r"); // added this for open error check |
wminix3 | 20:ede4fa57d082 | 504 | waver.play(wave_file); |
wminix3 | 20:ede4fa57d082 | 505 | fclose(wave_file); |
wminix3 | 20:ede4fa57d082 | 506 | SDLock.unlock(); |
wminix3 | 20:ede4fa57d082 | 507 | } |
wminix3 | 20:ede4fa57d082 | 508 | |
wminix3 | 20:ede4fa57d082 | 509 | // gameover music |
michaeljson | 0:3817adfaeb06 | 510 | while(gameover) |
michaeljson | 0:3817adfaeb06 | 511 | { |
wminix3 | 13:36cc024dcf6b | 512 | SDLock.lock(); |
wminix3 | 2:4a3f97570578 | 513 | wave_file=fopen("/sd/wavfiles/comicalgameoverEdit.wav","r"); |
wminix3 | 2:4a3f97570578 | 514 | if(wave_file==NULL) pc.printf("gameover file open error!\n\n\r"); // added this for open error check |
michaeljson | 0:3817adfaeb06 | 515 | waver.play(wave_file); |
michaeljson | 0:3817adfaeb06 | 516 | fclose(wave_file); |
wminix3 | 13:36cc024dcf6b | 517 | SDLock.unlock(); |
michaeljson | 0:3817adfaeb06 | 518 | } |
michaeljson | 0:3817adfaeb06 | 519 | } |
michaeljson | 0:3817adfaeb06 | 520 | } |
wminix3 | 14:4e7608619043 | 521 | |
wminix3 | 11:cdea2c9ec531 | 522 | |
wminix3 | 11:cdea2c9ec531 | 523 | // thread that adds RGB LED Lighting Effects that coincide with the game -- Brice |
wminix3 | 11:cdea2c9ec531 | 524 | void ledEffects(void const *args)//Th |
wminix3 | 11:cdea2c9ec531 | 525 | { //Depending on the state of the game, |
wminix3 | 11:cdea2c9ec531 | 526 | //generate different patterns/colors of lighting |
wminix3 | 11:cdea2c9ec531 | 527 | while(1) { |
wminix3 | 11:cdea2c9ec531 | 528 | // gradually increases and decreases each color independently. (A chill build up effect?) |
wminix3 | 11:cdea2c9ec531 | 529 | while(game_menu) |
wminix3 | 11:cdea2c9ec531 | 530 | { |
wminix3 | 11:cdea2c9ec531 | 531 | red = 0.0; |
wminix3 | 11:cdea2c9ec531 | 532 | green = 0.0; |
wminix3 | 11:cdea2c9ec531 | 533 | blue = 0.0; |
wminix3 | 11:cdea2c9ec531 | 534 | for (float i = 0.0; i < 0.5; i = i + 0.05) { |
wminix3 | 11:cdea2c9ec531 | 535 | red = i; |
wminix3 | 11:cdea2c9ec531 | 536 | Thread::wait(10); |
wminix3 | 11:cdea2c9ec531 | 537 | } |
wminix3 | 11:cdea2c9ec531 | 538 | red = 0.5; |
wminix3 | 11:cdea2c9ec531 | 539 | Thread::wait(300); |
wminix3 | 11:cdea2c9ec531 | 540 | for (float i = 0.5; i > 0.0; i = i - 0.05) { |
wminix3 | 11:cdea2c9ec531 | 541 | red = i; |
wminix3 | 11:cdea2c9ec531 | 542 | Thread::wait(10); |
wminix3 | 11:cdea2c9ec531 | 543 | } |
wminix3 | 11:cdea2c9ec531 | 544 | red = 0.0; |
wminix3 | 11:cdea2c9ec531 | 545 | for (float i = 0.0; i < 0.25; i = i + 0.05) { |
wminix3 | 11:cdea2c9ec531 | 546 | green = i; |
wminix3 | 11:cdea2c9ec531 | 547 | Thread::wait(10); |
wminix3 | 11:cdea2c9ec531 | 548 | } |
wminix3 | 11:cdea2c9ec531 | 549 | green = 0.25; |
wminix3 | 11:cdea2c9ec531 | 550 | Thread::wait(300); |
wminix3 | 11:cdea2c9ec531 | 551 | for (float i = 0.25; i > 0.0; i = i - 0.05) { |
wminix3 | 11:cdea2c9ec531 | 552 | green = i; |
wminix3 | 11:cdea2c9ec531 | 553 | Thread::wait(10); |
wminix3 | 11:cdea2c9ec531 | 554 | } |
wminix3 | 11:cdea2c9ec531 | 555 | green = 0.0; |
wminix3 | 11:cdea2c9ec531 | 556 | for (float i = 0.0; i < 0.5; i = i + 0.05) { |
wminix3 | 11:cdea2c9ec531 | 557 | blue = i; |
wminix3 | 11:cdea2c9ec531 | 558 | Thread::wait(10); |
wminix3 | 11:cdea2c9ec531 | 559 | } |
wminix3 | 11:cdea2c9ec531 | 560 | blue = 0.5; |
wminix3 | 11:cdea2c9ec531 | 561 | Thread::wait(300); |
wminix3 | 11:cdea2c9ec531 | 562 | for (float i = 0.5; i > 0.0; i = i - 0.05) { |
wminix3 | 11:cdea2c9ec531 | 563 | blue = i; |
wminix3 | 11:cdea2c9ec531 | 564 | Thread::wait(10); |
wminix3 | 11:cdea2c9ec531 | 565 | } |
wminix3 | 11:cdea2c9ec531 | 566 | blue = 0.0; |
wminix3 | 11:cdea2c9ec531 | 567 | } |
wminix3 | 11:cdea2c9ec531 | 568 | // Checks in game sound conditions |
wminix3 | 11:cdea2c9ec531 | 569 | while(begin_game || begin_game2) // added OR begin_game2 so that music/sounds play during one-player or two-player |
wminix3 | 11:cdea2c9ec531 | 570 | { |
wminix3 | 11:cdea2c9ec531 | 571 | // play firing sound when the player fires |
wminix3 | 11:cdea2c9ec531 | 572 | if(!pb && missile.status == PLAYER_MISSILE_INACTIVE) { |
wminix3 | 11:cdea2c9ec531 | 573 | red = 0.0; |
wminix3 | 11:cdea2c9ec531 | 574 | green = 0.0; |
wminix3 | 11:cdea2c9ec531 | 575 | blue = 0.0; |
wminix3 | 20:ede4fa57d082 | 576 | //red = 0.5; |
wminix3 | 20:ede4fa57d082 | 577 | //Thread::wait(200); |
wminix3 | 11:cdea2c9ec531 | 578 | red = 0.5; |
wminix3 | 11:cdea2c9ec531 | 579 | green = 0.15; |
wminix3 | 20:ede4fa57d082 | 580 | Thread::wait(300); |
wminix3 | 11:cdea2c9ec531 | 581 | red = 0.0; |
wminix3 | 11:cdea2c9ec531 | 582 | green = 0.0; |
wminix3 | 11:cdea2c9ec531 | 583 | } |
wminix3 | 11:cdea2c9ec531 | 584 | |
wminix3 | 11:cdea2c9ec531 | 585 | // if player hit, play hit sound |
wminix3 | 11:cdea2c9ec531 | 586 | if (hit_player) |
wminix3 | 11:cdea2c9ec531 | 587 | { |
wminix3 | 11:cdea2c9ec531 | 588 | red = 0.0; |
wminix3 | 11:cdea2c9ec531 | 589 | green = 0.0; |
wminix3 | 11:cdea2c9ec531 | 590 | blue = 0.0; |
wminix3 | 11:cdea2c9ec531 | 591 | red = 0.5; |
wminix3 | 11:cdea2c9ec531 | 592 | Thread::wait(60); |
wminix3 | 11:cdea2c9ec531 | 593 | red = 0.0; |
wminix3 | 11:cdea2c9ec531 | 594 | Thread::wait(60); |
wminix3 | 11:cdea2c9ec531 | 595 | red = 0.5; |
wminix3 | 11:cdea2c9ec531 | 596 | Thread::wait(60); |
wminix3 | 11:cdea2c9ec531 | 597 | red = 0.0; |
wminix3 | 11:cdea2c9ec531 | 598 | Thread::wait(60); |
wminix3 | 11:cdea2c9ec531 | 599 | red = 0.5; |
wminix3 | 11:cdea2c9ec531 | 600 | Thread::wait(60); |
wminix3 | 11:cdea2c9ec531 | 601 | red = 0.0; |
wminix3 | 11:cdea2c9ec531 | 602 | } |
wminix3 | 11:cdea2c9ec531 | 603 | Thread::wait(500); |
wminix3 | 11:cdea2c9ec531 | 604 | } |
wminix3 | 11:cdea2c9ec531 | 605 | |
wminix3 | 20:ede4fa57d082 | 606 | while(win) { |
wminix3 | 20:ede4fa57d082 | 607 | for (float i = 0.0; i < 0.25; i = i + 0.05) { |
wminix3 | 20:ede4fa57d082 | 608 | green = i; |
wminix3 | 20:ede4fa57d082 | 609 | Thread::wait(10); |
wminix3 | 20:ede4fa57d082 | 610 | } |
wminix3 | 20:ede4fa57d082 | 611 | green = 0.25; |
wminix3 | 20:ede4fa57d082 | 612 | Thread::wait(300); |
wminix3 | 20:ede4fa57d082 | 613 | for (float i = 0.25; i > 0.0; i = i - 0.05) { |
wminix3 | 20:ede4fa57d082 | 614 | green = i; |
wminix3 | 20:ede4fa57d082 | 615 | Thread::wait(10); |
wminix3 | 20:ede4fa57d082 | 616 | } |
wminix3 | 20:ede4fa57d082 | 617 | green = 0.0; |
wminix3 | 20:ede4fa57d082 | 618 | Thread::wait(500); |
wminix3 | 20:ede4fa57d082 | 619 | } |
wminix3 | 20:ede4fa57d082 | 620 | |
wminix3 | 11:cdea2c9ec531 | 621 | // players gameover voice if player loses |
wminix3 | 11:cdea2c9ec531 | 622 | while(gameover) |
wminix3 | 11:cdea2c9ec531 | 623 | { |
wminix3 | 11:cdea2c9ec531 | 624 | for (float i = 0.0; i < 0.25; i = i + 0.05) { |
wminix3 | 11:cdea2c9ec531 | 625 | red = i; |
wminix3 | 11:cdea2c9ec531 | 626 | Thread::wait(10); |
wminix3 | 11:cdea2c9ec531 | 627 | } |
wminix3 | 11:cdea2c9ec531 | 628 | red = 0.25; |
wminix3 | 11:cdea2c9ec531 | 629 | Thread::wait(300); |
wminix3 | 11:cdea2c9ec531 | 630 | for (float i = 0.25; i > 0.0; i = i - 0.05) { |
wminix3 | 11:cdea2c9ec531 | 631 | red = i; |
wminix3 | 11:cdea2c9ec531 | 632 | Thread::wait(10); |
wminix3 | 11:cdea2c9ec531 | 633 | } |
wminix3 | 11:cdea2c9ec531 | 634 | red = 0.0; |
wminix3 | 11:cdea2c9ec531 | 635 | Thread::wait(500); |
wminix3 | 11:cdea2c9ec531 | 636 | } |
wminix3 | 11:cdea2c9ec531 | 637 | } |
wminix3 | 11:cdea2c9ec531 | 638 | } |
wminix3 | 10:1eeb21ef7b2c | 639 | /* |
wminix3 | 5:b7934866b264 | 640 | // Thread added for mbed communication, which allows two-player -- Brice |
wminix3 | 6:c44055f94cc3 | 641 | void mbedSend(void const *args) { |
wminix3 | 4:739f6e0dd8af | 642 | while(1) { |
wminix3 | 5:b7934866b264 | 643 | while(numPlayers == 1) Thread::yield(); // with one player, thread is unneeded and should yield. |
wminix3 | 5:b7934866b264 | 644 | while(!first_player_ready || !second_player_ready) { // while either player isn't ready, send a start code, and become ready |
wminix3 | 4:739f6e0dd8af | 645 | //if (!first_player_ready || !second_player_ready) { |
wminix3 | 8:27c4345be3db | 646 | mbedLock.lock(); |
wminix3 | 4:739f6e0dd8af | 647 | secondMbed.putc('S'); |
wminix3 | 8:27c4345be3db | 648 | mbedLock.unlock(); |
wminix3 | 4:739f6e0dd8af | 649 | first_player_ready = true; |
wminix3 | 10:1eeb21ef7b2c | 650 | pc.printf("first player ready"); |
wminix3 | 7:9a826617be92 | 651 | Thread::wait(1000); |
wminix3 | 4:739f6e0dd8af | 652 | //} |
wminix3 | 6:c44055f94cc3 | 653 | // if (secondMbed.readable()) { // read in a start code to know that the second player is ready (if the second player sends a start code) |
wminix3 | 6:c44055f94cc3 | 654 | // if (secondMbed.getc() == 'S') { |
wminix3 | 6:c44055f94cc3 | 655 | // second_player_ready = true; |
wminix3 | 6:c44055f94cc3 | 656 | // pc.printf("second play ready"); |
wminix3 | 6:c44055f94cc3 | 657 | // } |
wminix3 | 6:c44055f94cc3 | 658 | // } |
wminix3 | 6:c44055f94cc3 | 659 | // Thread::wait(1000); // run once a second |
wminix3 | 5:b7934866b264 | 660 | } |
wminix3 | 5:b7934866b264 | 661 | if (two_player_win) { // if this player wins, notify the other mbed/player that they lost. |
wminix3 | 8:27c4345be3db | 662 | mbedLock.lock(); |
wminix3 | 5:b7934866b264 | 663 | secondMbed.putc('W'); |
wminix3 | 8:27c4345be3db | 664 | mbedLock.unlock(); |
wminix3 | 4:739f6e0dd8af | 665 | } |
wminix3 | 6:c44055f94cc3 | 666 | //if (secondMbed.readable()) { |
wminix3 | 6:c44055f94cc3 | 667 | // if (secondMbed.getc() == 'W') { |
wminix3 | 6:c44055f94cc3 | 668 | // two_player_lose = true; |
wminix3 | 6:c44055f94cc3 | 669 | // } |
wminix3 | 6:c44055f94cc3 | 670 | //} |
wminix3 | 7:9a826617be92 | 671 | Thread::wait(2000); // check twice a second for a win |
wminix3 | 6:c44055f94cc3 | 672 | } |
wminix3 | 6:c44055f94cc3 | 673 | } |
wminix3 | 10:1eeb21ef7b2c | 674 | */ |
wminix3 | 10:1eeb21ef7b2c | 675 | /* |
wminix3 | 6:c44055f94cc3 | 676 | void mbedReceive(void const *args) { |
wminix3 | 7:9a826617be92 | 677 | char rx; |
wminix3 | 6:c44055f94cc3 | 678 | while(1) { |
wminix3 | 10:1eeb21ef7b2c | 679 | rx = '0'; |
wminix3 | 8:27c4345be3db | 680 | mbedLock.lock(); |
wminix3 | 7:9a826617be92 | 681 | while(numPlayers == 1 || !secondMbed.readable()) { |
wminix3 | 9:c22613b0007a | 682 | mbedLock.unlock(); |
wminix3 | 10:1eeb21ef7b2c | 683 | pc.printf("yielding"); |
wminix3 | 7:9a826617be92 | 684 | Thread::yield(); // with one player, thread is unneeded and should yield. |
wminix3 | 7:9a826617be92 | 685 | } |
wminix3 | 7:9a826617be92 | 686 | rx = secondMbed.getc(); |
wminix3 | 10:1eeb21ef7b2c | 687 | pc.printf("%c", rx); |
wminix3 | 8:27c4345be3db | 688 | mbedLock.unlock(); |
wminix3 | 7:9a826617be92 | 689 | if (rx == 'S') { |
wminix3 | 7:9a826617be92 | 690 | second_player_ready = true; |
wminix3 | 10:1eeb21ef7b2c | 691 | pc.printf("second play ready"); |
wminix3 | 6:c44055f94cc3 | 692 | } |
wminix3 | 6:c44055f94cc3 | 693 | // Thread::wait(1000); // run once a second |
wminix3 | 6:c44055f94cc3 | 694 | //} |
wminix3 | 7:9a826617be92 | 695 | if (rx == 'W') { |
wminix3 | 6:c44055f94cc3 | 696 | two_player_lose = true; |
wminix3 | 5:b7934866b264 | 697 | } |
wminix3 | 10:1eeb21ef7b2c | 698 | Thread::wait(3000); // check twice a second for a win |
wminix3 | 10:1eeb21ef7b2c | 699 | } |
wminix3 | 10:1eeb21ef7b2c | 700 | } |
wminix3 | 10:1eeb21ef7b2c | 701 | */ |
wminix3 | 10:1eeb21ef7b2c | 702 | |
wminix3 | 10:1eeb21ef7b2c | 703 | // UNCOMMENT THIS THREAD IF SECOND PLAYER AND COMMENT MASTER THREAD |
wminix3 | 14:4e7608619043 | 704 | |
wminix3 | 14:4e7608619043 | 705 | // The slave mbed device (second player) should uncomment this thread -- Brice |
wminix3 | 10:1eeb21ef7b2c | 706 | /* |
wminix3 | 10:1eeb21ef7b2c | 707 | void mbedSlave(void const *args) { |
wminix3 | 10:1eeb21ef7b2c | 708 | char rx; |
wminix3 | 10:1eeb21ef7b2c | 709 | while(1) { |
wminix3 | 12:22aedb2598b1 | 710 | //while(numPlayers == 1) Thread::yield(); |
wminix3 | 10:1eeb21ef7b2c | 711 | rx = '0'; |
wminix3 | 24:ea19c7e8a479 | 712 | pc.printf("Slave thread running.\n\r"); |
wminix3 | 10:1eeb21ef7b2c | 713 | if (secondMbed.readable()) { |
wminix3 | 10:1eeb21ef7b2c | 714 | rx = secondMbed.getc(); |
wminix3 | 14:4e7608619043 | 715 | pc.printf("rx = %c\n\r", rx); |
wminix3 | 14:4e7608619043 | 716 | //if (!begin_game2 && rx == 'S') { |
wminix3 | 14:4e7608619043 | 717 | while (!secondMbed.writeable()) wait(0.5); |
wminix3 | 14:4e7608619043 | 718 | secondMbed.putc(rx); |
wminix3 | 10:1eeb21ef7b2c | 719 | } |
wminix3 | 24:ea19c7e8a479 | 720 | Thread::wait(1000); |
wminix3 | 14:4e7608619043 | 721 | } |
wminix3 | 14:4e7608619043 | 722 | } |
wminix3 | 24:ea19c7e8a479 | 723 | */ |
wminix3 | 24:ea19c7e8a479 | 724 | |
wminix3 | 14:4e7608619043 | 725 | //first_player_ready = true; |
wminix3 | 14:4e7608619043 | 726 | //second_player_ready = true; |
wminix3 | 14:4e7608619043 | 727 | // begin_game2 = true; |
wminix3 | 14:4e7608619043 | 728 | // numPlayers = 2; |
wminix3 | 14:4e7608619043 | 729 | //} else if (begin_game2 && rx == 'W') { |
wminix3 | 14:4e7608619043 | 730 | // secondMbed.putc(rx); |
wminix3 | 14:4e7608619043 | 731 | //first_player_ready = false; |
wminix3 | 14:4e7608619043 | 732 | //second_player_ready = false; |
wminix3 | 14:4e7608619043 | 733 | // begin_game2 = false; |
wminix3 | 14:4e7608619043 | 734 | // two_player_lose = true; |
wminix3 | 14:4e7608619043 | 735 | //} |
wminix3 | 14:4e7608619043 | 736 | //}*/ |
wminix3 | 14:4e7608619043 | 737 | /* |
wminix3 | 10:1eeb21ef7b2c | 738 | if (begin_game2 && two_player_win) { |
wminix3 | 14:4e7608619043 | 739 | while(!secondMbed.writeable()) wait(0.5); |
wminix3 | 10:1eeb21ef7b2c | 740 | secondMbed.putc('W'); |
wminix3 | 10:1eeb21ef7b2c | 741 | while(!secondMbed.readable()) wait(0.5); // ok to lock up with wait because we don't want to confirm when before anything else. --Brice |
wminix3 | 10:1eeb21ef7b2c | 742 | rx = secondMbed.getc(); |
wminix3 | 10:1eeb21ef7b2c | 743 | if (rx == 'W') { |
wminix3 | 10:1eeb21ef7b2c | 744 | begin_game2 = false; |
wminix3 | 14:4e7608619043 | 745 | //first_player_ready = false; |
wminix3 | 14:4e7608619043 | 746 | //second_player_ready = false; |
wminix3 | 10:1eeb21ef7b2c | 747 | } |
wminix3 | 10:1eeb21ef7b2c | 748 | } |
wminix3 | 14:4e7608619043 | 749 | */ |
wminix3 | 14:4e7608619043 | 750 | // Thread::wait(1000); |
wminix3 | 14:4e7608619043 | 751 | // } |
wminix3 | 14:4e7608619043 | 752 | //} |
wminix3 | 14:4e7608619043 | 753 | |
wminix3 | 14:4e7608619043 | 754 | |
wminix3 | 10:1eeb21ef7b2c | 755 | |
wminix3 | 10:1eeb21ef7b2c | 756 | // UNCOMMENT THIS THREAD IF FIRST PLAYER AND COMMENT SLAVE THREAD |
wminix3 | 10:1eeb21ef7b2c | 757 | // The master mbed device (second player) should uncomment this thread -- Brice |
wminix3 | 24:ea19c7e8a479 | 758 | |
wminix3 | 10:1eeb21ef7b2c | 759 | void mbedMaster(void const *args) { |
wminix3 | 10:1eeb21ef7b2c | 760 | char rx; |
wminix3 | 10:1eeb21ef7b2c | 761 | while(1) { |
wminix3 | 24:ea19c7e8a479 | 762 | pc.printf("Master called\n\r"); |
wminix3 | 10:1eeb21ef7b2c | 763 | while(numPlayers == 1) Thread::yield(); |
wminix3 | 24:ea19c7e8a479 | 764 | pc.printf("Num players is 2.\n\r"); |
wminix3 | 10:1eeb21ef7b2c | 765 | rx = '0'; |
wminix3 | 10:1eeb21ef7b2c | 766 | if (!begin_game2) { |
wminix3 | 12:22aedb2598b1 | 767 | while(!secondMbed.writeable()) { |
wminix3 | 14:4e7608619043 | 768 | // pc.printf("not writeable"); |
wminix3 | 12:22aedb2598b1 | 769 | wait(0.5); |
wminix3 | 12:22aedb2598b1 | 770 | } |
wminix3 | 10:1eeb21ef7b2c | 771 | secondMbed.putc('S'); |
wminix3 | 12:22aedb2598b1 | 772 | while(!secondMbed.readable()) { |
wminix3 | 14:4e7608619043 | 773 | // pc.printf("no read\n\r"); |
wminix3 | 12:22aedb2598b1 | 774 | wait(0.5); // okay to lock up until can confirm game is ready. --Brice |
wminix3 | 12:22aedb2598b1 | 775 | } |
wminix3 | 10:1eeb21ef7b2c | 776 | rx = secondMbed.getc(); |
wminix3 | 14:4e7608619043 | 777 | pc.printf("rx = %c\n\r", rx); |
wminix3 | 24:ea19c7e8a479 | 778 | if (rx == 'S') { |
wminix3 | 24:ea19c7e8a479 | 779 | begin_game2 = true; |
wminix3 | 24:ea19c7e8a479 | 780 | pc.printf("both players ready\n\r"); |
wminix3 | 24:ea19c7e8a479 | 781 | } |
wminix3 | 24:ea19c7e8a479 | 782 | } else { |
wminix3 | 24:ea19c7e8a479 | 783 | while (begin_game2) { |
wminix3 | 24:ea19c7e8a479 | 784 | rx = '0'; |
wminix3 | 24:ea19c7e8a479 | 785 | if (secondMbed.readable()) { |
wminix3 | 24:ea19c7e8a479 | 786 | rx = secondMbed.getc(); |
wminix3 | 24:ea19c7e8a479 | 787 | if (rx == 'W') { |
wminix3 | 24:ea19c7e8a479 | 788 | secondMbed.putc(rx); |
wminix3 | 24:ea19c7e8a479 | 789 | begin_game2 = false; |
wminix3 | 24:ea19c7e8a479 | 790 | two_player_lose = true; |
wminix3 | 24:ea19c7e8a479 | 791 | } |
wminix3 | 24:ea19c7e8a479 | 792 | } |
wminix3 | 24:ea19c7e8a479 | 793 | if (two_player_win) { |
wminix3 | 24:ea19c7e8a479 | 794 | secondMbed.putc('W'); |
wminix3 | 24:ea19c7e8a479 | 795 | while(!secondMbed.readable()) wait(0.5); // ok to lock up with wait because we don't want to confirm when before anything else. --Brice |
wminix3 | 24:ea19c7e8a479 | 796 | rx = secondMbed.getc(); |
wminix3 | 24:ea19c7e8a479 | 797 | if (rx == 'W') { |
wminix3 | 24:ea19c7e8a479 | 798 | begin_game2 = false; |
wminix3 | 24:ea19c7e8a479 | 799 | } |
wminix3 | 24:ea19c7e8a479 | 800 | } |
wminix3 | 24:ea19c7e8a479 | 801 | Thread::wait(1000); |
wminix3 | 24:ea19c7e8a479 | 802 | } |
wminix3 | 12:22aedb2598b1 | 803 | } |
wminix3 | 24:ea19c7e8a479 | 804 | Thread::wait(1000); |
wminix3 | 4:739f6e0dd8af | 805 | } |
wminix3 | 4:739f6e0dd8af | 806 | } |
wminix3 | 24:ea19c7e8a479 | 807 | |
wminix3 | 24:ea19c7e8a479 | 808 | |
wminix3 | 4:739f6e0dd8af | 809 | |
michaeljson | 0:3817adfaeb06 | 810 | int main() { |
wminix3 | 24:ea19c7e8a479 | 811 | //pc.printf("in main"); |
wminix3 | 17:843a874eb4e3 | 812 | IMU.begin(); |
wminix3 | 17:843a874eb4e3 | 813 | if (!IMU.begin()) { |
wminix3 | 17:843a874eb4e3 | 814 | pc.printf("Failed to communicate with IMU\n\r"); |
wminix3 | 17:843a874eb4e3 | 815 | } |
wminix3 | 17:843a874eb4e3 | 816 | IMU.calibrate(1); |
michaeljson | 0:3817adfaeb06 | 817 | // Initialization of Setup variables |
michaeljson | 0:3817adfaeb06 | 818 | int blk_x, blk_y; |
michaeljson | 0:3817adfaeb06 | 819 | pb.mode(PullUp); |
michaeljson | 0:3817adfaeb06 | 820 | |
wminix3 | 14:4e7608619043 | 821 | Thread thread(playstart); // intializes the thread to play sound |
wminix3 | 10:1eeb21ef7b2c | 822 | // Should only have the Slave thread uncommented if second player. |
wminix3 | 10:1eeb21ef7b2c | 823 | // Should only have the Master thread uncommented if first player. |
wminix3 | 10:1eeb21ef7b2c | 824 | //Thread thread2(mbedSlave); // uncommented if second player -- Brice |
wminix3 | 24:ea19c7e8a479 | 825 | Thread thread3(mbedMaster); // uncommented if first player -- Brice |
wminix3 | 11:cdea2c9ec531 | 826 | Thread thread4(ledEffects); // thread added for LED lighting effects -- Brice |
wminix3 | 12:22aedb2598b1 | 827 | secondMbed.baud(9600); |
wminix3 | 15:33582e9acd16 | 828 | uLCD.baudrate(3000000); // set to 3000000 to increase smooth gameplay |
michaeljson | 0:3817adfaeb06 | 829 | |
michaeljson | 0:3817adfaeb06 | 830 | // Initialization of Game Menu variables |
michaeljson | 0:3817adfaeb06 | 831 | const int title_x_pos = 2; // initial x-pos of title |
michaeljson | 0:3817adfaeb06 | 832 | const int title_y_pos = 3; // initial y-pos of title |
michaeljson | 0:3817adfaeb06 | 833 | int start_label_x_pos = 7; // start label x-pos |
michaeljson | 0:3817adfaeb06 | 834 | int start_label_y_pos = 7; // start label y-pos |
michaeljson | 0:3817adfaeb06 | 835 | int level_cursor_x_pos = 5; // level cursor x-position |
wminix3 | 2:4a3f97570578 | 836 | int level_cursor_y_pos_start = 7; // level cursor y-position |
wminix3 | 2:4a3f97570578 | 837 | //int level_cursor_y_pos = 7; // initial level_cursor_y_pos @ start -- Added by Brice for more menu options |
wminix3 | 24:ea19c7e8a479 | 838 | int level_cursor_y_pos_end = 13; // BOTTOM CURSOR POS -- Added by Brice for more menu options |
michaeljson | 0:3817adfaeb06 | 839 | int gameover_x_pos = 5; // gameover label x-position |
michaeljson | 0:3817adfaeb06 | 840 | int gameover_y_pos = 5; // gameover label y-position |
michaeljson | 0:3817adfaeb06 | 841 | int win_x_pos = 2; // congratulations label x-position |
wminix3 | 13:36cc024dcf6b | 842 | int win_y_pos = 4; // congratulations label y-position |
michaeljson | 0:3817adfaeb06 | 843 | int startover_x_pos = 3; // startover label x-position |
wminix3 | 13:36cc024dcf6b | 844 | int startover_y_pos = 7; // startover label y-position |
wminix3 | 20:ede4fa57d082 | 845 | int newAnalogClick = 0; // to prevent the analog click from changing multiple skins with one click |
wminix3 | 20:ede4fa57d082 | 846 | int prevAnalogClick = 0; // to prevent the analog click from changing multiple skins with one click |
michaeljson | 0:3817adfaeb06 | 847 | |
michaeljson | 0:3817adfaeb06 | 848 | // intialize temporary score and current score |
michaeljson | 0:3817adfaeb06 | 849 | int temp = 0; |
michaeljson | 0:3817adfaeb06 | 850 | int score = 0; |
michaeljson | 0:3817adfaeb06 | 851 | |
wminix3 | 2:4a3f97570578 | 852 | // Additional globals added for two-player and one-player capabilities (by Brice) |
wminix3 | 24:ea19c7e8a479 | 853 | char buffer[3] = {0}; |
wminix3 | 24:ea19c7e8a479 | 854 | char c = {0}; |
wminix3 | 24:ea19c7e8a479 | 855 | char *token; |
wminix3 | 24:ea19c7e8a479 | 856 | int i = 0; |
wminix3 | 24:ea19c7e8a479 | 857 | int storedTime = 999; |
wminix3 | 24:ea19c7e8a479 | 858 | SDLock.lock(); |
wminix3 | 24:ea19c7e8a479 | 859 | FILE *sdtime; |
wminix3 | 24:ea19c7e8a479 | 860 | sdtime=fopen("/sd/besttime.txt","r"); |
wminix3 | 24:ea19c7e8a479 | 861 | if(sdtime==NULL) pc.printf("file open error!\n\n\r"); // added this for open error check |
wminix3 | 24:ea19c7e8a479 | 862 | while ((c != '\n') && (i < 3)) { |
wminix3 | 24:ea19c7e8a479 | 863 | c = fgetc(sdtime); |
wminix3 | 24:ea19c7e8a479 | 864 | buffer[i] = c; |
wminix3 | 24:ea19c7e8a479 | 865 | i++; |
wminix3 | 24:ea19c7e8a479 | 866 | } |
wminix3 | 24:ea19c7e8a479 | 867 | fclose(sdtime); |
wminix3 | 24:ea19c7e8a479 | 868 | SDLock.unlock(); |
wminix3 | 24:ea19c7e8a479 | 869 | token = strtok(buffer, "\n"); |
wminix3 | 24:ea19c7e8a479 | 870 | storedTime = atoi(token); // convert string from file to integer |
michaeljson | 0:3817adfaeb06 | 871 | // Begin game loop |
michaeljson | 0:3817adfaeb06 | 872 | while(1) |
michaeljson | 0:3817adfaeb06 | 873 | { |
michaeljson | 0:3817adfaeb06 | 874 | // initialize all starting conditions for the beginning of the game |
michaeljson | 0:3817adfaeb06 | 875 | game_menu = true; // defaults to display menu screen |
michaeljson | 0:3817adfaeb06 | 876 | ENEMY_MOVE = true; // defaults to move enemy |
michaeljson | 0:3817adfaeb06 | 877 | DIRECTION = 1; // default to move right |
michaeljson | 0:3817adfaeb06 | 878 | hit_player = 0; // default to not player hit |
michaeljson | 0:3817adfaeb06 | 879 | MOVE_DOWN = 0; // default to not move down |
michaeljson | 0:3817adfaeb06 | 880 | lose = false; // default to not lose |
wminix3 | 14:4e7608619043 | 881 | lives1 = 3; // defaults to 3 lives |
wminix3 | 14:4e7608619043 | 882 | lives2 = 2; // 2 lives for medium |
wminix3 | 14:4e7608619043 | 883 | lives3 = 1; // 1 life for hard |
michaeljson | 0:3817adfaeb06 | 884 | score = 0; // default to score of 0 |
wminix3 | 2:4a3f97570578 | 885 | int level_cursor_y_pos = 7; // initial level_cursor_y_pos @ start -- Added by Brice for more menu options |
michaeljson | 0:3817adfaeb06 | 886 | uLCD.cls(); |
wminix3 | 2:4a3f97570578 | 887 | // Brice moved this out of the loop since it shouldn't change |
wminix3 | 2:4a3f97570578 | 888 | //uLCD.locate(title_x_pos,title_y_pos); // "SPACE INVADERS" title position |
wminix3 | 2:4a3f97570578 | 889 | //uLCD.printf("SPACE INVADERS"); // Title |
michaeljson | 0:3817adfaeb06 | 890 | // Implementation of Game Menu |
wminix3 | 24:ea19c7e8a479 | 891 | |
wminix3 | 24:ea19c7e8a479 | 892 | //char buffer[3] = {0}; |
wminix3 | 24:ea19c7e8a479 | 893 | // char c = {0}; |
wminix3 | 24:ea19c7e8a479 | 894 | // char *token; |
wminix3 | 24:ea19c7e8a479 | 895 | // int i = 0; |
wminix3 | 24:ea19c7e8a479 | 896 | // int storedTime = 999; |
wminix3 | 24:ea19c7e8a479 | 897 | // SDLock.lock(); |
wminix3 | 24:ea19c7e8a479 | 898 | // FILE *sdtime; |
wminix3 | 24:ea19c7e8a479 | 899 | // sdtime=fopen("/sd/besttime.txt","r"); |
wminix3 | 24:ea19c7e8a479 | 900 | // if(sdtime==NULL) pc.printf("file open error!\n\n\r"); // added this for open error check |
wminix3 | 24:ea19c7e8a479 | 901 | // while ((c != '\n') && (i < 3)) { |
wminix3 | 24:ea19c7e8a479 | 902 | // c = fgetc(sdtime); |
wminix3 | 24:ea19c7e8a479 | 903 | // buffer[i] = c; |
wminix3 | 24:ea19c7e8a479 | 904 | // i++; |
wminix3 | 24:ea19c7e8a479 | 905 | // } |
wminix3 | 24:ea19c7e8a479 | 906 | // fclose(sdtime); |
wminix3 | 24:ea19c7e8a479 | 907 | // SDLock.unlock(); |
wminix3 | 24:ea19c7e8a479 | 908 | // token = strtok(buffer, "\n"); |
wminix3 | 24:ea19c7e8a479 | 909 | // storedTime = atoi(token); // convert string from file to integer |
wminix3 | 24:ea19c7e8a479 | 910 | // Implementation of game_menu |
michaeljson | 0:3817adfaeb06 | 911 | while(game_menu) |
michaeljson | 0:3817adfaeb06 | 912 | { |
wminix3 | 17:843a874eb4e3 | 913 | float accelY = 0.0; // y acceleration |
wminix3 | 17:843a874eb4e3 | 914 | //float accelZ = 0.0; // z acceleration |
wminix3 | 2:4a3f97570578 | 915 | // Brice added this in order to move the cursor through the menu options |
wminix3 | 2:4a3f97570578 | 916 | uLCD.locate(level_cursor_x_pos, level_cursor_y_pos); |
wminix3 | 2:4a3f97570578 | 917 | uLCD.printf(" "); |
wminix3 | 16:e4e6515bdabb | 918 | //if (myNav.down() && level_cursor_y_pos < level_cursor_y_pos_end) { |
wminix3 | 18:16a2d4343ae4 | 919 | // Control menu with Analog Joystick |
wminix3 | 16:e4e6515bdabb | 920 | if ((stick.angle() <= 280 && stick.angle() >= 260) && level_cursor_y_pos < level_cursor_y_pos_end) { |
wminix3 | 14:4e7608619043 | 921 | level_cursor_y_pos += 2; |
wminix3 | 16:e4e6515bdabb | 922 | //} else if (myNav.up() && level_cursor_y_pos > level_cursor_y_pos_start) { |
wminix3 | 16:e4e6515bdabb | 923 | } else if ((stick.angle() <= 100 && stick.angle() >= 80) && level_cursor_y_pos > level_cursor_y_pos_start) { |
wminix3 | 14:4e7608619043 | 924 | level_cursor_y_pos -= 2; |
wminix3 | 2:4a3f97570578 | 925 | } |
wminix3 | 18:16a2d4343ae4 | 926 | // Control Menu with IMU |
wminix3 | 17:843a874eb4e3 | 927 | if (IMU.accelAvailable()) { |
wminix3 | 17:843a874eb4e3 | 928 | IMU.readAccel(); |
wminix3 | 17:843a874eb4e3 | 929 | accelY = IMU.calcAccel(IMU.ay); |
wminix3 | 17:843a874eb4e3 | 930 | //pc.printf("Calc Accel: %f", accelY); |
wminix3 | 17:843a874eb4e3 | 931 | } |
wminix3 | 17:843a874eb4e3 | 932 | if ((accelY >= 0.25) && level_cursor_y_pos < level_cursor_y_pos_end) { |
wminix3 | 17:843a874eb4e3 | 933 | level_cursor_y_pos += 2; |
wminix3 | 17:843a874eb4e3 | 934 | //} else if (myNav.up() && level_cursor_y_pos > level_cursor_y_pos_start) { |
wminix3 | 17:843a874eb4e3 | 935 | } else if ((accelY <= -0.25) && level_cursor_y_pos > level_cursor_y_pos_start) { |
wminix3 | 17:843a874eb4e3 | 936 | level_cursor_y_pos -= 2; |
wminix3 | 17:843a874eb4e3 | 937 | } |
wminix3 | 2:4a3f97570578 | 938 | // end of movable cursor |
michaeljson | 0:3817adfaeb06 | 939 | uLCD.locate(level_cursor_x_pos,level_cursor_y_pos); // draws cursor next to "START" label |
michaeljson | 0:3817adfaeb06 | 940 | uLCD.printf("->"); |
michaeljson | 0:3817adfaeb06 | 941 | |
michaeljson | 0:3817adfaeb06 | 942 | uLCD.locate(title_x_pos,title_y_pos); // "SPACE INVADERS" title position |
michaeljson | 0:3817adfaeb06 | 943 | uLCD.printf("SPACE INVADERS"); // Title |
michaeljson | 0:3817adfaeb06 | 944 | |
wminix3 | 14:4e7608619043 | 945 | //uLCD.locate(start_label_x_pos,start_label_y_pos); // "START" label position |
wminix3 | 14:4e7608619043 | 946 | //uLCD.printf("ONE-PLAYER"); |
wminix3 | 14:4e7608619043 | 947 | |
wminix3 | 14:4e7608619043 | 948 | //uLCD.locate(start_label_x_pos,start_label_y_pos + 1); |
wminix3 | 14:4e7608619043 | 949 | //uLCD.printf("TWO-PLAYER"); |
wminix3 | 2:4a3f97570578 | 950 | |
wminix3 | 14:4e7608619043 | 951 | uLCD.locate(start_label_x_pos,start_label_y_pos); // "START" label position |
wminix3 | 14:4e7608619043 | 952 | uLCD.printf("LEVEL 1"); |
wminix3 | 14:4e7608619043 | 953 | |
wminix3 | 14:4e7608619043 | 954 | uLCD.locate(start_label_x_pos,start_label_y_pos + 2); |
wminix3 | 14:4e7608619043 | 955 | uLCD.printf("LEVEL 2"); |
wminix3 | 14:4e7608619043 | 956 | |
wminix3 | 14:4e7608619043 | 957 | uLCD.locate(start_label_x_pos,start_label_y_pos + 4); |
wminix3 | 14:4e7608619043 | 958 | uLCD.printf("LEVEL 3"); |
wminix3 | 24:ea19c7e8a479 | 959 | |
wminix3 | 24:ea19c7e8a479 | 960 | uLCD.locate(start_label_x_pos, start_label_y_pos + 6); |
wminix3 | 24:ea19c7e8a479 | 961 | uLCD.printf("TWO-PLAYER"); |
wminix3 | 24:ea19c7e8a479 | 962 | |
wminix3 | 24:ea19c7e8a479 | 963 | uLCD.locate(2,15); |
wminix3 | 24:ea19c7e8a479 | 964 | uLCD.printf("Best time: %d s", storedTime); |
michaeljson | 0:3817adfaeb06 | 965 | // if pushbutton is pressed, game menu is exited and game begins |
michaeljson | 0:3817adfaeb06 | 966 | if(!pb) |
michaeljson | 0:3817adfaeb06 | 967 | { |
michaeljson | 0:3817adfaeb06 | 968 | game_menu = false; |
wminix3 | 2:4a3f97570578 | 969 | if (level_cursor_y_pos == start_label_y_pos) { |
wminix3 | 14:4e7608619043 | 970 | //numPlayers = 1; |
wminix3 | 14:4e7608619043 | 971 | level = 1; |
wminix3 | 14:4e7608619043 | 972 | } else if (level_cursor_y_pos == start_label_y_pos + 2) { |
wminix3 | 14:4e7608619043 | 973 | //numPlayers = 2; |
wminix3 | 14:4e7608619043 | 974 | //pc.printf("num players: 2"); |
wminix3 | 14:4e7608619043 | 975 | level = 2; |
wminix3 | 14:4e7608619043 | 976 | } else if (level_cursor_y_pos == start_label_y_pos + 4) { |
wminix3 | 14:4e7608619043 | 977 | level = 3; |
wminix3 | 24:ea19c7e8a479 | 978 | } else if (level_cursor_y_pos == start_label_y_pos + 6) { |
wminix3 | 24:ea19c7e8a479 | 979 | numPlayers = 2; |
wminix3 | 24:ea19c7e8a479 | 980 | level = 1; |
wminix3 | 2:4a3f97570578 | 981 | } |
wminix3 | 3:e6c081c7f6f1 | 982 | Thread::wait(500); // changed this to Thread::wait ... originally wait(0.5); |
michaeljson | 0:3817adfaeb06 | 983 | } |
michaeljson | 0:3817adfaeb06 | 984 | } |
wminix3 | 14:4e7608619043 | 985 | while(numPlayers != 1 && !begin_game2) Thread::yield(); // added to force wait with two-player and one player not ready. -- added by Brice |
wminix3 | 14:4e7608619043 | 986 | if (numPlayers == 2 && begin_game2) { |
wminix3 | 5:b7934866b264 | 987 | numWins = 0; |
wminix3 | 24:ea19c7e8a479 | 988 | bestTimer.start(); |
wminix3 | 5:b7934866b264 | 989 | } else { |
wminix3 | 5:b7934866b264 | 990 | begin_game = true; // defaults begin_game to true |
wminix3 | 13:36cc024dcf6b | 991 | |
wminix3 | 13:36cc024dcf6b | 992 | // Start timer to check for best time in single player. |
wminix3 | 13:36cc024dcf6b | 993 | bestTimer.start(); |
wminix3 | 5:b7934866b264 | 994 | } |
michaeljson | 0:3817adfaeb06 | 995 | |
michaeljson | 0:3817adfaeb06 | 996 | uLCD.cls(); |
michaeljson | 0:3817adfaeb06 | 997 | |
michaeljson | 0:3817adfaeb06 | 998 | // Draw the enemies |
michaeljson | 0:3817adfaeb06 | 999 | draw_enemies_level(); |
michaeljson | 0:3817adfaeb06 | 1000 | |
michaeljson | 0:3817adfaeb06 | 1001 | // Draw the player |
michaeljson | 0:3817adfaeb06 | 1002 | draw_initial_player(); |
michaeljson | 0:3817adfaeb06 | 1003 | |
wminix3 | 23:56f6a12aaebd | 1004 | // Draw the barriers |
wminix3 | 23:56f6a12aaebd | 1005 | draw_barriers_level(); |
wminix3 | 23:56f6a12aaebd | 1006 | |
michaeljson | 0:3817adfaeb06 | 1007 | // sets the initial position of player missile and enemy missile (later updated) |
michaeljson | 0:3817adfaeb06 | 1008 | blk_x = player.player_blk_x+(player.player_width/2); |
michaeljson | 0:3817adfaeb06 | 1009 | blk_y = player.player_blk_y; |
michaeljson | 0:3817adfaeb06 | 1010 | missile_init(&missile, blk_x, blk_y, WHITE); |
michaeljson | 1:618aa2c4ca6a | 1011 | int e_blk_x = 0; |
michaeljson | 1:618aa2c4ca6a | 1012 | int e_blk_y = 2; |
michaeljson | 1:618aa2c4ca6a | 1013 | enemy_missile_init(&enemy_missile, e_blk_x, e_blk_y, WHITE); |
wminix3 | 25:17f2ec000357 | 1014 | |
michaeljson | 0:3817adfaeb06 | 1015 | // prints lives |
wminix3 | 14:4e7608619043 | 1016 | if (level == 1) { |
wminix3 | 14:4e7608619043 | 1017 | uLCD.locate(0,0); |
wminix3 | 14:4e7608619043 | 1018 | uLCD.printf("Lives:%i", 3); |
wminix3 | 14:4e7608619043 | 1019 | } else if (level == 2) { |
wminix3 | 14:4e7608619043 | 1020 | uLCD.locate(0,0); |
wminix3 | 14:4e7608619043 | 1021 | uLCD.printf("Lives:%i", 2); |
wminix3 | 14:4e7608619043 | 1022 | } else if (level == 3) { |
wminix3 | 14:4e7608619043 | 1023 | uLCD.locate(0,0); |
wminix3 | 14:4e7608619043 | 1024 | uLCD.printf("Lives:%i", 1); |
wminix3 | 14:4e7608619043 | 1025 | } |
wminix3 | 14:4e7608619043 | 1026 | //uLCD.locate(0,0); |
wminix3 | 14:4e7608619043 | 1027 | //uLCD.printf("Lives:%i", lives); |
michaeljson | 0:3817adfaeb06 | 1028 | |
michaeljson | 0:3817adfaeb06 | 1029 | // prints score |
michaeljson | 0:3817adfaeb06 | 1030 | uLCD.locate(9,0); |
michaeljson | 0:3817adfaeb06 | 1031 | uLCD.printf("Score:%i", score); |
michaeljson | 0:3817adfaeb06 | 1032 | |
michaeljson | 0:3817adfaeb06 | 1033 | // game play loop |
michaeljson | 0:3817adfaeb06 | 1034 | while(begin_game) |
michaeljson | 0:3817adfaeb06 | 1035 | { |
michaeljson | 0:3817adfaeb06 | 1036 | // updates score |
michaeljson | 0:3817adfaeb06 | 1037 | temp = score; |
michaeljson | 0:3817adfaeb06 | 1038 | score = (15-numOfEnemies)*15; |
michaeljson | 0:3817adfaeb06 | 1039 | |
michaeljson | 0:3817adfaeb06 | 1040 | // prints score if score changes |
michaeljson | 0:3817adfaeb06 | 1041 | if (score != temp) |
michaeljson | 0:3817adfaeb06 | 1042 | { |
michaeljson | 0:3817adfaeb06 | 1043 | uLCD.locate(9,0); |
michaeljson | 0:3817adfaeb06 | 1044 | uLCD.printf("Score:%i", score); |
michaeljson | 0:3817adfaeb06 | 1045 | } |
michaeljson | 0:3817adfaeb06 | 1046 | |
michaeljson | 0:3817adfaeb06 | 1047 | // move enemy |
michaeljson | 0:3817adfaeb06 | 1048 | enemy_motion(); |
wminix3 | 23:56f6a12aaebd | 1049 | // check barriers for player missile hit |
wminix3 | 23:56f6a12aaebd | 1050 | if (missile.missile_blk_y+1-missile.missile_height >= barrier_1.barrier_blk_y |
wminix3 | 23:56f6a12aaebd | 1051 | && missile.missile_blk_y+1-missile.missile_height <= barrier_1.barrier_blk_y+barrier_1.barrier_height |
wminix3 | 23:56f6a12aaebd | 1052 | && missile.status == PLAYER_MISSILE_ACTIVE) |
wminix3 | 23:56f6a12aaebd | 1053 | { |
wminix3 | 24:ea19c7e8a479 | 1054 | if (level == 1) { |
wminix3 | 24:ea19c7e8a479 | 1055 | check_barrier(&barrier_1, &missile); |
wminix3 | 24:ea19c7e8a479 | 1056 | } |
wminix3 | 23:56f6a12aaebd | 1057 | check_barrier(&barrier_2, &missile); |
wminix3 | 24:ea19c7e8a479 | 1058 | if (level == 1 || level == 2) { |
wminix3 | 24:ea19c7e8a479 | 1059 | check_barrier(&barrier_3, &missile); |
wminix3 | 24:ea19c7e8a479 | 1060 | } |
wminix3 | 24:ea19c7e8a479 | 1061 | if (level == 1) { |
wminix3 | 24:ea19c7e8a479 | 1062 | check_barrier(&barrier_4, &missile); |
wminix3 | 24:ea19c7e8a479 | 1063 | } |
wminix3 | 23:56f6a12aaebd | 1064 | } |
wminix3 | 23:56f6a12aaebd | 1065 | |
michaeljson | 0:3817adfaeb06 | 1066 | // checks if player missile passes y-pos of row1 |
michaeljson | 0:3817adfaeb06 | 1067 | if (missile.missile_blk_y+1-missile.missile_height <= enemy_1.enemy_blk_y |
michaeljson | 0:3817adfaeb06 | 1068 | && missile.missile_blk_y+1-missile.missile_height >= enemy_1.enemy_blk_y-enemy_1.enemy_height) |
michaeljson | 0:3817adfaeb06 | 1069 | { |
michaeljson | 0:3817adfaeb06 | 1070 | check_hit_enemy_row1(); |
michaeljson | 0:3817adfaeb06 | 1071 | } |
michaeljson | 0:3817adfaeb06 | 1072 | |
michaeljson | 0:3817adfaeb06 | 1073 | // checks if player missile passes y-pos of row2 |
michaeljson | 0:3817adfaeb06 | 1074 | if (missile.missile_blk_y+1-missile.missile_height <= enemy_6.enemy_blk_y |
michaeljson | 0:3817adfaeb06 | 1075 | && missile.missile_blk_y+1-missile.missile_height >= enemy_6.enemy_blk_y-enemy_6.enemy_height) |
michaeljson | 0:3817adfaeb06 | 1076 | { |
michaeljson | 0:3817adfaeb06 | 1077 | check_hit_enemy_row2(); |
michaeljson | 0:3817adfaeb06 | 1078 | } |
michaeljson | 0:3817adfaeb06 | 1079 | |
michaeljson | 0:3817adfaeb06 | 1080 | // checks if player missile passes y-pos of row3 |
michaeljson | 0:3817adfaeb06 | 1081 | if (missile.missile_blk_y+1-missile.missile_height <= enemy_11.enemy_blk_y |
michaeljson | 0:3817adfaeb06 | 1082 | && missile.missile_blk_y+1-missile.missile_height >= enemy_11.enemy_blk_y-enemy_11.enemy_height) |
michaeljson | 0:3817adfaeb06 | 1083 | { |
michaeljson | 0:3817adfaeb06 | 1084 | check_hit_enemy_row3(); |
michaeljson | 0:3817adfaeb06 | 1085 | } |
michaeljson | 0:3817adfaeb06 | 1086 | |
michaeljson | 0:3817adfaeb06 | 1087 | // Random Enemy Fire |
wminix3 | 25:17f2ec000357 | 1088 | if (enemy_missile.status == ENEMY_MISSILE_INACTIVE) |
michaeljson | 0:3817adfaeb06 | 1089 | { |
michaeljson | 0:3817adfaeb06 | 1090 | random_attack_gen(); |
wminix3 | 25:17f2ec000357 | 1091 | /* if (level == 2 || level == 3) { |
wminix3 | 24:ea19c7e8a479 | 1092 | random_attack_gen(); |
wminix3 | 24:ea19c7e8a479 | 1093 | } |
wminix3 | 24:ea19c7e8a479 | 1094 | if (level == 3) { |
wminix3 | 24:ea19c7e8a479 | 1095 | random_attack_gen(); |
wminix3 | 24:ea19c7e8a479 | 1096 | } |
wminix3 | 24:ea19c7e8a479 | 1097 | */ |
michaeljson | 0:3817adfaeb06 | 1098 | } |
michaeljson | 0:3817adfaeb06 | 1099 | |
wminix3 | 23:56f6a12aaebd | 1100 | // check if enemy missile passes the y-pos of the barrier and updates the barriers if they are hit. |
wminix3 | 23:56f6a12aaebd | 1101 | if (enemy_missile.missile_blk_y >= barrier_1.barrier_blk_y |
wminix3 | 23:56f6a12aaebd | 1102 | && enemy_missile.missile_blk_y <= barrier_1.barrier_blk_y+barrier_1.barrier_height) |
wminix3 | 23:56f6a12aaebd | 1103 | { |
wminix3 | 24:ea19c7e8a479 | 1104 | if (level == 1) { |
wminix3 | 24:ea19c7e8a479 | 1105 | check_barrier(&barrier_1, &enemy_missile); |
wminix3 | 24:ea19c7e8a479 | 1106 | } |
wminix3 | 23:56f6a12aaebd | 1107 | check_barrier(&barrier_2, &enemy_missile); |
wminix3 | 24:ea19c7e8a479 | 1108 | if (level == 1 || level == 2) { |
wminix3 | 24:ea19c7e8a479 | 1109 | check_barrier(&barrier_3, &enemy_missile); |
wminix3 | 24:ea19c7e8a479 | 1110 | } |
wminix3 | 24:ea19c7e8a479 | 1111 | if (level == 1) { |
wminix3 | 24:ea19c7e8a479 | 1112 | check_barrier(&barrier_4, &enemy_missile); |
wminix3 | 24:ea19c7e8a479 | 1113 | } |
wminix3 | 24:ea19c7e8a479 | 1114 | } |
wminix3 | 24:ea19c7e8a479 | 1115 | |
michaeljson | 0:3817adfaeb06 | 1116 | // checks if enemy missile passes y-pos of player |
michaeljson | 0:3817adfaeb06 | 1117 | if (enemy_missile.missile_blk_y >= player.player_blk_y |
michaeljson | 0:3817adfaeb06 | 1118 | && enemy_missile.missile_blk_y <= player.player_blk_y+player.player_height) |
michaeljson | 0:3817adfaeb06 | 1119 | { |
michaeljson | 0:3817adfaeb06 | 1120 | check_player_hit(); |
michaeljson | 0:3817adfaeb06 | 1121 | } |
wminix3 | 23:56f6a12aaebd | 1122 | |
wminix3 | 25:17f2ec000357 | 1123 | |
michaeljson | 0:3817adfaeb06 | 1124 | update_missile_pos(&missile); // updates player missile position |
michaeljson | 0:3817adfaeb06 | 1125 | update_enemy_missile_pos(&enemy_missile); // updates enemy missile position |
wminix3 | 25:17f2ec000357 | 1126 | |
michaeljson | 0:3817adfaeb06 | 1127 | // Player Movement checked with navigation switch |
wminix3 | 16:e4e6515bdabb | 1128 | //if (myNav.left() && ((player.player_blk_x-3) > 0)) |
wminix3 | 18:16a2d4343ae4 | 1129 | // With joystick click, change color of player from GREEN -> BLUE -> PINK -> PURPLE -> YELLOW (and loop). |
wminix3 | 20:ede4fa57d082 | 1130 | prevAnalogClick = newAnalogClick; |
wminix3 | 20:ede4fa57d082 | 1131 | newAnalogClick = stick.button(); |
wminix3 | 20:ede4fa57d082 | 1132 | if (newAnalogClick && !prevAnalogClick) { |
wminix3 | 18:16a2d4343ae4 | 1133 | if (player.player_color == 0x00FF00) { // if GREEN (start) |
wminix3 | 18:16a2d4343ae4 | 1134 | player.player_color = 0x0000FF; // BLUE |
wminix3 | 18:16a2d4343ae4 | 1135 | } else if (player.player_color == 0x0000FF) { // if BLUE |
wminix3 | 18:16a2d4343ae4 | 1136 | player.player_color = 0xFFC0CB; // pink. hot pink: 0xFF69B4 |
wminix3 | 18:16a2d4343ae4 | 1137 | } else if (player.player_color == 0xFFC0CB) { // if pink |
wminix3 | 18:16a2d4343ae4 | 1138 | player.player_color = 0x800080; // Purple: 0x800080. periwinkle purple: 0xCCCCFF |
wminix3 | 18:16a2d4343ae4 | 1139 | } else if (player.player_color == 0x800080) { // if purple |
wminix3 | 18:16a2d4343ae4 | 1140 | player.player_color = 0xFFFF00; // yellow. metallic gold: 0xD4AF37 |
wminix3 | 18:16a2d4343ae4 | 1141 | } else if (player.player_color == 0xFFFF00) { // if yellow |
wminix3 | 18:16a2d4343ae4 | 1142 | player.player_color = 0x00FF00; // set back to GREEN |
wminix3 | 18:16a2d4343ae4 | 1143 | } |
wminix3 | 18:16a2d4343ae4 | 1144 | } |
wminix3 | 18:16a2d4343ae4 | 1145 | // Control Player with Analog Joystick -- Brice |
wminix3 | 16:e4e6515bdabb | 1146 | float stickDist = stick.xAxis(); |
wminix3 | 21:a6b4c5598083 | 1147 | if ((stickDist < 0.0) && (player.player_blk_x + stickDist*3 > 0.0)){ |
michaeljson | 0:3817adfaeb06 | 1148 | player_erase(&player); |
wminix3 | 16:e4e6515bdabb | 1149 | //player.player_blk_x -= 3; |
wminix3 | 16:e4e6515bdabb | 1150 | player.player_blk_x += (int)(stickDist*3); |
michaeljson | 0:3817adfaeb06 | 1151 | player_show(&player); |
michaeljson | 0:3817adfaeb06 | 1152 | } |
wminix3 | 16:e4e6515bdabb | 1153 | //else if (myNav.right() && ((player.player_blk_x+3) < (128-player.player_width))) |
wminix3 | 17:843a874eb4e3 | 1154 | else if ((stickDist > 0.0) && ((player.player_blk_x + stickDist*3) < (128 - player.player_width))) |
michaeljson | 0:3817adfaeb06 | 1155 | { |
michaeljson | 0:3817adfaeb06 | 1156 | player_erase(&player); |
wminix3 | 16:e4e6515bdabb | 1157 | player.player_blk_x += (int)(stickDist*3); |
michaeljson | 0:3817adfaeb06 | 1158 | player_show(&player); |
michaeljson | 0:3817adfaeb06 | 1159 | } |
wminix3 | 18:16a2d4343ae4 | 1160 | // Control Player with IMU -- Brice |
wminix3 | 17:843a874eb4e3 | 1161 | float accelX = 0.0; // x acceleration |
wminix3 | 17:843a874eb4e3 | 1162 | if (IMU.accelAvailable()) { |
wminix3 | 17:843a874eb4e3 | 1163 | IMU.readAccel(); |
wminix3 | 17:843a874eb4e3 | 1164 | accelX = IMU.calcAccel(IMU.ax); |
wminix3 | 17:843a874eb4e3 | 1165 | //pc.printf("Calc Accel: %f", accelY); |
wminix3 | 17:843a874eb4e3 | 1166 | } |
wminix3 | 17:843a874eb4e3 | 1167 | if ((accelX <= -0.25) && (player.player_blk_x + accelX*5) > 0.0) { |
wminix3 | 17:843a874eb4e3 | 1168 | player_erase(&player); |
wminix3 | 17:843a874eb4e3 | 1169 | //player.player_blk_x -= 3; |
wminix3 | 24:ea19c7e8a479 | 1170 | player.player_blk_x += (int)(accelX*8); |
wminix3 | 17:843a874eb4e3 | 1171 | player_show(&player); |
wminix3 | 17:843a874eb4e3 | 1172 | //} else if (myNav.up() && level_cursor_y_pos > level_cursor_y_pos_start) { |
wminix3 | 17:843a874eb4e3 | 1173 | } else if ((accelX >= 0.25) && ((player.player_blk_x + accelX*5) < (128 - player.player_width))) { |
wminix3 | 17:843a874eb4e3 | 1174 | player_erase(&player); |
wminix3 | 17:843a874eb4e3 | 1175 | //player.player_blk_x -= 3; |
wminix3 | 24:ea19c7e8a479 | 1176 | player.player_blk_x += (int)(accelX*8); |
wminix3 | 17:843a874eb4e3 | 1177 | player_show(&player); |
wminix3 | 17:843a874eb4e3 | 1178 | } |
michaeljson | 0:3817adfaeb06 | 1179 | // Player Fire |
michaeljson | 0:3817adfaeb06 | 1180 | if (pb == 0 && missile.status == PLAYER_MISSILE_INACTIVE) |
michaeljson | 0:3817adfaeb06 | 1181 | { |
michaeljson | 0:3817adfaeb06 | 1182 | missile.missile_blk_x = player.player_blk_x+(player.player_width/2); |
michaeljson | 0:3817adfaeb06 | 1183 | missile.missile_blk_y = player.player_blk_y; |
michaeljson | 0:3817adfaeb06 | 1184 | missile.status = PLAYER_MISSILE_ACTIVE; |
michaeljson | 0:3817adfaeb06 | 1185 | } |
michaeljson | 0:3817adfaeb06 | 1186 | |
michaeljson | 0:3817adfaeb06 | 1187 | // checks if player destroyed all enemies |
michaeljson | 0:3817adfaeb06 | 1188 | if (numOfEnemies == 0) |
michaeljson | 0:3817adfaeb06 | 1189 | { |
wminix3 | 13:36cc024dcf6b | 1190 | // idea for best-time reading and updating: https://os.mbed.com/questions/75718/i-want-to-assign-int-value-saved-in-sd-t/ |
wminix3 | 13:36cc024dcf6b | 1191 | int time = bestTimer.read(); |
wminix3 | 13:36cc024dcf6b | 1192 | bestTimer.stop(); |
wminix3 | 13:36cc024dcf6b | 1193 | bestTimer.reset(); |
michaeljson | 0:3817adfaeb06 | 1194 | uLCD.cls(); |
wminix3 | 13:36cc024dcf6b | 1195 | char buffer[3] = {0}; |
wminix3 | 13:36cc024dcf6b | 1196 | char c = {0}; |
wminix3 | 13:36cc024dcf6b | 1197 | char *token; |
wminix3 | 13:36cc024dcf6b | 1198 | int i = 0; |
wminix3 | 13:36cc024dcf6b | 1199 | int storedTime = 999; |
wminix3 | 13:36cc024dcf6b | 1200 | SDLock.lock(); |
wminix3 | 13:36cc024dcf6b | 1201 | FILE *sdtime; |
wminix3 | 13:36cc024dcf6b | 1202 | sdtime=fopen("/sd/besttime.txt","r"); |
wminix3 | 13:36cc024dcf6b | 1203 | if(sdtime==NULL) pc.printf("file open error!\n\n\r"); // added this for open error check |
wminix3 | 13:36cc024dcf6b | 1204 | while ((c != '\n') && (i < 3)) { |
wminix3 | 13:36cc024dcf6b | 1205 | c = fgetc(sdtime); |
wminix3 | 13:36cc024dcf6b | 1206 | buffer[i] = c; |
wminix3 | 13:36cc024dcf6b | 1207 | i++; |
wminix3 | 13:36cc024dcf6b | 1208 | } |
wminix3 | 13:36cc024dcf6b | 1209 | token = strtok(buffer, "\n"); |
wminix3 | 13:36cc024dcf6b | 1210 | storedTime = atoi(token); // convert string from file to integer |
wminix3 | 13:36cc024dcf6b | 1211 | fclose(sdtime); |
wminix3 | 13:36cc024dcf6b | 1212 | if (time < storedTime) { |
wminix3 | 13:36cc024dcf6b | 1213 | uLCD.locate(2,10); |
wminix3 | 13:36cc024dcf6b | 1214 | uLCD.printf("NEW BEST TIME!"); |
wminix3 | 13:36cc024dcf6b | 1215 | uLCD.locate(2,11); |
wminix3 | 13:36cc024dcf6b | 1216 | uLCD.printf("%d seconds!", time); |
wminix3 | 13:36cc024dcf6b | 1217 | sdtime = fopen("/sd/besttime.txt", "w"); |
wminix3 | 13:36cc024dcf6b | 1218 | if (sdtime != NULL) { |
wminix3 | 13:36cc024dcf6b | 1219 | fprintf(sdtime, "%d\r\n", time); |
wminix3 | 13:36cc024dcf6b | 1220 | fclose(sdtime); |
wminix3 | 13:36cc024dcf6b | 1221 | } else { |
wminix3 | 13:36cc024dcf6b | 1222 | pc.printf("write: failed!\r\n"); |
wminix3 | 13:36cc024dcf6b | 1223 | } |
wminix3 | 13:36cc024dcf6b | 1224 | } |
wminix3 | 13:36cc024dcf6b | 1225 | SDLock.unlock(); |
wminix3 | 13:36cc024dcf6b | 1226 | |
michaeljson | 0:3817adfaeb06 | 1227 | |
wminix3 | 20:ede4fa57d082 | 1228 | win = true; // sets win to true, for win screen |
michaeljson | 0:3817adfaeb06 | 1229 | begin_game = false; |
michaeljson | 0:3817adfaeb06 | 1230 | |
wminix3 | 24:ea19c7e8a479 | 1231 | // displays video clip ????. Original intention here is a little obscure. |
wminix3 | 13:36cc024dcf6b | 1232 | /* |
michaeljson | 0:3817adfaeb06 | 1233 | uLCD.cls(); |
michaeljson | 0:3817adfaeb06 | 1234 | uLCD.media_init(); |
michaeljson | 0:3817adfaeb06 | 1235 | uLCD.set_sector_address(0x00, 0x00); |
michaeljson | 0:3817adfaeb06 | 1236 | uLCD.display_video(0,0); |
wminix3 | 5:b7934866b264 | 1237 | Thread::wait(1000); // changed from wait(1) to Thread::wait(1000) since we're using threads -- Brice |
wminix3 | 13:36cc024dcf6b | 1238 | */ |
michaeljson | 0:3817adfaeb06 | 1239 | |
wminix3 | 13:36cc024dcf6b | 1240 | //uLCD.cls(); |
michaeljson | 0:3817adfaeb06 | 1241 | |
michaeljson | 0:3817adfaeb06 | 1242 | // prints "Congratulations" on uLCD |
michaeljson | 0:3817adfaeb06 | 1243 | uLCD.locate(win_x_pos,win_y_pos); |
michaeljson | 0:3817adfaeb06 | 1244 | uLCD.printf("CONGRATULATIONS!"); |
michaeljson | 0:3817adfaeb06 | 1245 | |
michaeljson | 0:3817adfaeb06 | 1246 | // prints "Play Again?" and "Press pb..." |
michaeljson | 0:3817adfaeb06 | 1247 | uLCD.locate(startover_x_pos, startover_y_pos); |
michaeljson | 0:3817adfaeb06 | 1248 | uLCD.printf("Play Again?"); |
michaeljson | 0:3817adfaeb06 | 1249 | uLCD.locate(startover_x_pos, startover_y_pos+1); |
michaeljson | 0:3817adfaeb06 | 1250 | uLCD.printf("Press pb..."); |
michaeljson | 0:3817adfaeb06 | 1251 | |
michaeljson | 0:3817adfaeb06 | 1252 | // waits at win screen until pushbutton is pressed |
michaeljson | 0:3817adfaeb06 | 1253 | while (win) |
michaeljson | 0:3817adfaeb06 | 1254 | { |
michaeljson | 0:3817adfaeb06 | 1255 | // if pb is pressed, reset game to start menu |
michaeljson | 0:3817adfaeb06 | 1256 | if (!pb) |
michaeljson | 0:3817adfaeb06 | 1257 | { |
michaeljson | 0:3817adfaeb06 | 1258 | win = false; |
wminix3 | 5:b7934866b264 | 1259 | Thread::wait(500); // changed from wait(0.5) to Thread::wait(500) since we're using threads |
michaeljson | 0:3817adfaeb06 | 1260 | } |
michaeljson | 0:3817adfaeb06 | 1261 | } |
michaeljson | 0:3817adfaeb06 | 1262 | |
michaeljson | 0:3817adfaeb06 | 1263 | } |
wminix3 | 22:a907eeb128a4 | 1264 | int prevColor; |
michaeljson | 0:3817adfaeb06 | 1265 | // checks if player was hit |
michaeljson | 0:3817adfaeb06 | 1266 | if (hit_player) |
michaeljson | 0:3817adfaeb06 | 1267 | { |
michaeljson | 0:3817adfaeb06 | 1268 | // updates lives |
wminix3 | 14:4e7608619043 | 1269 | if (level == 1) { |
wminix3 | 14:4e7608619043 | 1270 | lives1 -= 1; |
wminix3 | 22:a907eeb128a4 | 1271 | prevColor = player.player_color; |
wminix3 | 22:a907eeb128a4 | 1272 | player_erase(&player); |
wminix3 | 22:a907eeb128a4 | 1273 | player.player_color = 0xFF0000; // briefly flash the player red. |
wminix3 | 22:a907eeb128a4 | 1274 | player_show(&player); |
wminix3 | 14:4e7608619043 | 1275 | Thread::wait(500); // changed from wait(0.5) to Thread::wait since we're using threads -- Brice |
wminix3 | 22:a907eeb128a4 | 1276 | player_erase(&player); |
wminix3 | 22:a907eeb128a4 | 1277 | player.player_color = prevColor; // briefly flash the player red. |
wminix3 | 22:a907eeb128a4 | 1278 | player_show(&player); |
wminix3 | 22:a907eeb128a4 | 1279 | hit_player = 0; |
wminix3 | 14:4e7608619043 | 1280 | player_show(&player); |
wminix3 | 14:4e7608619043 | 1281 | player.status = PLAYER_ALIVE; |
michaeljson | 0:3817adfaeb06 | 1282 | |
michaeljson | 0:3817adfaeb06 | 1283 | // prints updated lives number |
wminix3 | 14:4e7608619043 | 1284 | uLCD.locate(0,0); |
wminix3 | 14:4e7608619043 | 1285 | uLCD.printf("Lives:%i", lives1); |
wminix3 | 14:4e7608619043 | 1286 | } else if (level == 2) { |
wminix3 | 14:4e7608619043 | 1287 | lives2 -= 1; |
wminix3 | 22:a907eeb128a4 | 1288 | prevColor = player.player_color; |
wminix3 | 22:a907eeb128a4 | 1289 | player_erase(&player); |
wminix3 | 22:a907eeb128a4 | 1290 | player.player_color = 0xFF0000; // briefly flash the player red. |
wminix3 | 22:a907eeb128a4 | 1291 | player_show(&player); |
wminix3 | 14:4e7608619043 | 1292 | Thread::wait(500); // changed from wait(0.5) to Thread::wait since we're using threads -- Brice |
wminix3 | 22:a907eeb128a4 | 1293 | player_erase(&player); |
wminix3 | 22:a907eeb128a4 | 1294 | player.player_color = prevColor; // briefly flash the player red. |
wminix3 | 22:a907eeb128a4 | 1295 | player_show(&player); |
wminix3 | 22:a907eeb128a4 | 1296 | //Thread::wait(500); // changed from wait(0.5) to Thread::wait since we're using threads -- Brice |
wminix3 | 14:4e7608619043 | 1297 | hit_player = 0; |
wminix3 | 14:4e7608619043 | 1298 | player_show(&player); |
wminix3 | 14:4e7608619043 | 1299 | player.status = PLAYER_ALIVE; |
wminix3 | 14:4e7608619043 | 1300 | |
wminix3 | 14:4e7608619043 | 1301 | // prints updated lives number |
wminix3 | 14:4e7608619043 | 1302 | uLCD.locate(0,0); |
wminix3 | 14:4e7608619043 | 1303 | uLCD.printf("Lives:%i", lives2); |
wminix3 | 14:4e7608619043 | 1304 | } else if (level == 3) { |
wminix3 | 14:4e7608619043 | 1305 | lives3 -= 1; |
wminix3 | 22:a907eeb128a4 | 1306 | prevColor = player.player_color; |
wminix3 | 22:a907eeb128a4 | 1307 | player_erase(&player); |
wminix3 | 22:a907eeb128a4 | 1308 | player.player_color = 0xFF0000; // briefly flash the player red. |
wminix3 | 22:a907eeb128a4 | 1309 | player_show(&player); |
wminix3 | 14:4e7608619043 | 1310 | Thread::wait(500); // changed from wait(0.5) to Thread::wait since we're using threads -- Brice |
wminix3 | 22:a907eeb128a4 | 1311 | player_erase(&player); |
wminix3 | 22:a907eeb128a4 | 1312 | player.player_color = prevColor; // briefly flash the player red. |
wminix3 | 22:a907eeb128a4 | 1313 | player_show(&player); |
wminix3 | 22:a907eeb128a4 | 1314 | //Thread::wait(500); // changed from wait(0.5) to Thread::wait since we're using threads -- Brice |
wminix3 | 14:4e7608619043 | 1315 | hit_player = 0; |
wminix3 | 14:4e7608619043 | 1316 | player_show(&player); |
wminix3 | 14:4e7608619043 | 1317 | player.status = PLAYER_ALIVE; |
wminix3 | 14:4e7608619043 | 1318 | |
wminix3 | 14:4e7608619043 | 1319 | // prints updated lives number |
wminix3 | 14:4e7608619043 | 1320 | uLCD.locate(0,0); |
wminix3 | 14:4e7608619043 | 1321 | uLCD.printf("Lives:%i", lives3); |
wminix3 | 14:4e7608619043 | 1322 | } |
michaeljson | 0:3817adfaeb06 | 1323 | } |
michaeljson | 0:3817adfaeb06 | 1324 | |
michaeljson | 0:3817adfaeb06 | 1325 | // if player loses all lives or enemy reaches the player |
wminix3 | 14:4e7608619043 | 1326 | if (lose || lives1 == 0 || lives2 == 0 || lives3 == 0) |
michaeljson | 0:3817adfaeb06 | 1327 | { |
michaeljson | 0:3817adfaeb06 | 1328 | begin_game = false; // set to false to end game |
michaeljson | 0:3817adfaeb06 | 1329 | uLCD.cls(); |
michaeljson | 0:3817adfaeb06 | 1330 | |
michaeljson | 0:3817adfaeb06 | 1331 | gameover = true; // set to go to display gameover screen |
michaeljson | 0:3817adfaeb06 | 1332 | |
michaeljson | 0:3817adfaeb06 | 1333 | // prints "GAMEOVER" to uLCD |
michaeljson | 0:3817adfaeb06 | 1334 | uLCD.locate(gameover_x_pos, gameover_y_pos); |
michaeljson | 0:3817adfaeb06 | 1335 | uLCD.printf("GAMEOVER"); |
wminix3 | 5:b7934866b264 | 1336 | Thread::wait(1000); // changed from wait(1) to thread::wait since we're using threads -- Brice |
michaeljson | 0:3817adfaeb06 | 1337 | |
michaeljson | 0:3817adfaeb06 | 1338 | // prints "Play Again?" and "Press pb..." |
michaeljson | 0:3817adfaeb06 | 1339 | uLCD.locate(startover_x_pos, startover_y_pos); |
michaeljson | 0:3817adfaeb06 | 1340 | uLCD.printf("Play Again?"); |
michaeljson | 0:3817adfaeb06 | 1341 | uLCD.locate(startover_x_pos, startover_y_pos+1); |
michaeljson | 0:3817adfaeb06 | 1342 | uLCD.printf("Press pb..."); |
michaeljson | 0:3817adfaeb06 | 1343 | |
michaeljson | 0:3817adfaeb06 | 1344 | // stays in gameover screen until pb is pressed |
michaeljson | 0:3817adfaeb06 | 1345 | while (gameover) |
michaeljson | 0:3817adfaeb06 | 1346 | { |
michaeljson | 0:3817adfaeb06 | 1347 | // if pb is pressed, game is reset to the game menu screen |
michaeljson | 0:3817adfaeb06 | 1348 | if (!pb) |
michaeljson | 0:3817adfaeb06 | 1349 | { |
michaeljson | 0:3817adfaeb06 | 1350 | gameover = false; |
michaeljson | 0:3817adfaeb06 | 1351 | game_menu = true; |
wminix3 | 5:b7934866b264 | 1352 | Thread::wait(500); // changed wait(0.5) to Thread::wait since we're using threads -- Brice |
wminix3 | 5:b7934866b264 | 1353 | } |
wminix3 | 5:b7934866b264 | 1354 | } |
wminix3 | 5:b7934866b264 | 1355 | } |
wminix3 | 5:b7934866b264 | 1356 | |
wminix3 | 5:b7934866b264 | 1357 | } |
wminix3 | 5:b7934866b264 | 1358 | // game play loop |
wminix3 | 5:b7934866b264 | 1359 | while(begin_game2) |
wminix3 | 5:b7934866b264 | 1360 | { |
wminix3 | 5:b7934866b264 | 1361 | temp = score; |
wminix3 | 5:b7934866b264 | 1362 | score = (15-numOfEnemies)*15; |
wminix3 | 5:b7934866b264 | 1363 | |
wminix3 | 5:b7934866b264 | 1364 | // prints score if score changes |
wminix3 | 5:b7934866b264 | 1365 | if (score != temp) |
wminix3 | 5:b7934866b264 | 1366 | { |
wminix3 | 5:b7934866b264 | 1367 | uLCD.locate(9,0); |
wminix3 | 5:b7934866b264 | 1368 | uLCD.printf("Score:%i", score); |
wminix3 | 5:b7934866b264 | 1369 | } |
wminix3 | 5:b7934866b264 | 1370 | |
wminix3 | 5:b7934866b264 | 1371 | // move enemy |
wminix3 | 5:b7934866b264 | 1372 | enemy_motion(); |
wminix3 | 24:ea19c7e8a479 | 1373 | // check barriers for player missile hit |
wminix3 | 24:ea19c7e8a479 | 1374 | if (missile.missile_blk_y+1-missile.missile_height >= barrier_1.barrier_blk_y |
wminix3 | 24:ea19c7e8a479 | 1375 | && missile.missile_blk_y+1-missile.missile_height <= barrier_1.barrier_blk_y+barrier_1.barrier_height |
wminix3 | 24:ea19c7e8a479 | 1376 | && missile.status == PLAYER_MISSILE_ACTIVE) |
wminix3 | 24:ea19c7e8a479 | 1377 | { |
wminix3 | 24:ea19c7e8a479 | 1378 | check_barrier(&barrier_1, &missile); |
wminix3 | 24:ea19c7e8a479 | 1379 | check_barrier(&barrier_2, &missile); |
wminix3 | 24:ea19c7e8a479 | 1380 | check_barrier(&barrier_3, &missile); |
wminix3 | 24:ea19c7e8a479 | 1381 | check_barrier(&barrier_4, &missile); |
wminix3 | 24:ea19c7e8a479 | 1382 | } |
wminix3 | 24:ea19c7e8a479 | 1383 | |
wminix3 | 5:b7934866b264 | 1384 | // checks if player missile passes y-pos of row1 |
wminix3 | 5:b7934866b264 | 1385 | if (missile.missile_blk_y+1-missile.missile_height <= enemy_1.enemy_blk_y |
wminix3 | 5:b7934866b264 | 1386 | && missile.missile_blk_y+1-missile.missile_height >= enemy_1.enemy_blk_y-enemy_1.enemy_height) |
wminix3 | 5:b7934866b264 | 1387 | { |
wminix3 | 5:b7934866b264 | 1388 | check_hit_enemy_row1(); |
wminix3 | 5:b7934866b264 | 1389 | } |
wminix3 | 5:b7934866b264 | 1390 | |
wminix3 | 5:b7934866b264 | 1391 | // checks if player missile passes y-pos of row2 |
wminix3 | 5:b7934866b264 | 1392 | if (missile.missile_blk_y+1-missile.missile_height <= enemy_6.enemy_blk_y |
wminix3 | 5:b7934866b264 | 1393 | && missile.missile_blk_y+1-missile.missile_height >= enemy_6.enemy_blk_y-enemy_6.enemy_height) |
wminix3 | 5:b7934866b264 | 1394 | { |
wminix3 | 5:b7934866b264 | 1395 | check_hit_enemy_row2(); |
wminix3 | 5:b7934866b264 | 1396 | } |
wminix3 | 5:b7934866b264 | 1397 | |
wminix3 | 5:b7934866b264 | 1398 | // checks if player missile passes y-pos of row3 |
wminix3 | 5:b7934866b264 | 1399 | if (missile.missile_blk_y+1-missile.missile_height <= enemy_11.enemy_blk_y |
wminix3 | 5:b7934866b264 | 1400 | && missile.missile_blk_y+1-missile.missile_height >= enemy_11.enemy_blk_y-enemy_11.enemy_height) |
wminix3 | 5:b7934866b264 | 1401 | { |
wminix3 | 5:b7934866b264 | 1402 | check_hit_enemy_row3(); |
wminix3 | 5:b7934866b264 | 1403 | } |
wminix3 | 5:b7934866b264 | 1404 | |
wminix3 | 5:b7934866b264 | 1405 | // Random Enemy Fire |
wminix3 | 5:b7934866b264 | 1406 | if (enemy_missile.status == ENEMY_MISSILE_INACTIVE) |
wminix3 | 5:b7934866b264 | 1407 | { |
wminix3 | 5:b7934866b264 | 1408 | random_attack_gen(); |
wminix3 | 5:b7934866b264 | 1409 | } |
wminix3 | 5:b7934866b264 | 1410 | |
wminix3 | 24:ea19c7e8a479 | 1411 | // check if enemy missile passes the y-pos of the barrier and updates the barriers if they are hit. |
wminix3 | 24:ea19c7e8a479 | 1412 | if (enemy_missile.missile_blk_y >= barrier_1.barrier_blk_y |
wminix3 | 24:ea19c7e8a479 | 1413 | && enemy_missile.missile_blk_y <= barrier_1.barrier_blk_y+barrier_1.barrier_height) |
wminix3 | 24:ea19c7e8a479 | 1414 | { |
wminix3 | 24:ea19c7e8a479 | 1415 | check_barrier(&barrier_1, &enemy_missile); |
wminix3 | 24:ea19c7e8a479 | 1416 | check_barrier(&barrier_2, &enemy_missile); |
wminix3 | 24:ea19c7e8a479 | 1417 | check_barrier(&barrier_3, &enemy_missile); |
wminix3 | 24:ea19c7e8a479 | 1418 | check_barrier(&barrier_4, &enemy_missile); |
wminix3 | 24:ea19c7e8a479 | 1419 | } |
wminix3 | 24:ea19c7e8a479 | 1420 | |
wminix3 | 5:b7934866b264 | 1421 | // checks if enemy missile passes y-pos of player |
wminix3 | 5:b7934866b264 | 1422 | if (enemy_missile.missile_blk_y >= player.player_blk_y |
wminix3 | 5:b7934866b264 | 1423 | && enemy_missile.missile_blk_y <= player.player_blk_y+player.player_height) |
wminix3 | 5:b7934866b264 | 1424 | { |
wminix3 | 5:b7934866b264 | 1425 | check_player_hit(); |
wminix3 | 5:b7934866b264 | 1426 | } |
wminix3 | 24:ea19c7e8a479 | 1427 | |
wminix3 | 5:b7934866b264 | 1428 | |
wminix3 | 5:b7934866b264 | 1429 | update_missile_pos(&missile); // updates player missile position |
wminix3 | 5:b7934866b264 | 1430 | update_enemy_missile_pos(&enemy_missile); // updates enemy missile position |
wminix3 | 5:b7934866b264 | 1431 | |
wminix3 | 5:b7934866b264 | 1432 | // Player Movement checked with navigation switch |
wminix3 | 16:e4e6515bdabb | 1433 | //if (myNav.left() && ((player.player_blk_x-3) > 0)) |
wminix3 | 24:ea19c7e8a479 | 1434 | // With joystick click, change color of player from GREEN -> BLUE -> PINK -> PURPLE -> YELLOW (and loop). |
wminix3 | 24:ea19c7e8a479 | 1435 | prevAnalogClick = newAnalogClick; |
wminix3 | 24:ea19c7e8a479 | 1436 | newAnalogClick = stick.button(); |
wminix3 | 24:ea19c7e8a479 | 1437 | if (newAnalogClick && !prevAnalogClick) { |
wminix3 | 24:ea19c7e8a479 | 1438 | if (player.player_color == 0x00FF00) { // if GREEN (start) |
wminix3 | 24:ea19c7e8a479 | 1439 | player.player_color = 0x0000FF; // BLUE |
wminix3 | 24:ea19c7e8a479 | 1440 | } else if (player.player_color == 0x0000FF) { // if BLUE |
wminix3 | 24:ea19c7e8a479 | 1441 | player.player_color = 0xFFC0CB; // pink. hot pink: 0xFF69B4 |
wminix3 | 24:ea19c7e8a479 | 1442 | } else if (player.player_color == 0xFFC0CB) { // if pink |
wminix3 | 24:ea19c7e8a479 | 1443 | player.player_color = 0x800080; // Purple: 0x800080. periwinkle purple: 0xCCCCFF |
wminix3 | 24:ea19c7e8a479 | 1444 | } else if (player.player_color == 0x800080) { // if purple |
wminix3 | 24:ea19c7e8a479 | 1445 | player.player_color = 0xFFFF00; // yellow. metallic gold: 0xD4AF37 |
wminix3 | 24:ea19c7e8a479 | 1446 | } else if (player.player_color == 0xFFFF00) { // if yellow |
wminix3 | 24:ea19c7e8a479 | 1447 | player.player_color = 0x00FF00; // set back to GREEN |
wminix3 | 24:ea19c7e8a479 | 1448 | } |
wminix3 | 24:ea19c7e8a479 | 1449 | } |
wminix3 | 24:ea19c7e8a479 | 1450 | // Control Player with Analog Joystick -- Brice |
wminix3 | 16:e4e6515bdabb | 1451 | float stickDist = stick.xAxis(); |
wminix3 | 24:ea19c7e8a479 | 1452 | if ((stickDist < 0.0) && (player.player_blk_x + stickDist*3 > 0.0)){ |
wminix3 | 5:b7934866b264 | 1453 | player_erase(&player); |
wminix3 | 16:e4e6515bdabb | 1454 | //player.player_blk_x -= 3; |
wminix3 | 24:ea19c7e8a479 | 1455 | player.player_blk_x += (int)(stickDist*3); |
wminix3 | 5:b7934866b264 | 1456 | player_show(&player); |
wminix3 | 5:b7934866b264 | 1457 | } |
wminix3 | 16:e4e6515bdabb | 1458 | //else if (myNav.right() && ((player.player_blk_x+3) < (128-player.player_width))) |
wminix3 | 24:ea19c7e8a479 | 1459 | else if ((stickDist > 0.0) && ((player.player_blk_x + stickDist*3) < (128 - player.player_width))) |
wminix3 | 5:b7934866b264 | 1460 | { |
wminix3 | 5:b7934866b264 | 1461 | player_erase(&player); |
wminix3 | 24:ea19c7e8a479 | 1462 | player.player_blk_x += (int)(stickDist*3); |
wminix3 | 5:b7934866b264 | 1463 | player_show(&player); |
wminix3 | 5:b7934866b264 | 1464 | } |
wminix3 | 24:ea19c7e8a479 | 1465 | // Control Player with IMU -- Brice |
wminix3 | 24:ea19c7e8a479 | 1466 | float accelX = 0.0; // x acceleration |
wminix3 | 24:ea19c7e8a479 | 1467 | if (IMU.accelAvailable()) { |
wminix3 | 24:ea19c7e8a479 | 1468 | IMU.readAccel(); |
wminix3 | 24:ea19c7e8a479 | 1469 | accelX = IMU.calcAccel(IMU.ax); |
wminix3 | 24:ea19c7e8a479 | 1470 | //pc.printf("Calc Accel: %f", accelY); |
wminix3 | 24:ea19c7e8a479 | 1471 | } |
wminix3 | 24:ea19c7e8a479 | 1472 | if ((accelX <= -0.25) && (player.player_blk_x + accelX*5) > 0.0) { |
wminix3 | 24:ea19c7e8a479 | 1473 | player_erase(&player); |
wminix3 | 24:ea19c7e8a479 | 1474 | //player.player_blk_x -= 3; |
wminix3 | 24:ea19c7e8a479 | 1475 | player.player_blk_x += (int)(accelX*8); |
wminix3 | 24:ea19c7e8a479 | 1476 | player_show(&player); |
wminix3 | 24:ea19c7e8a479 | 1477 | //} else if (myNav.up() && level_cursor_y_pos > level_cursor_y_pos_start) { |
wminix3 | 24:ea19c7e8a479 | 1478 | } else if ((accelX >= 0.25) && ((player.player_blk_x + accelX*5) < (128 - player.player_width))) { |
wminix3 | 24:ea19c7e8a479 | 1479 | player_erase(&player); |
wminix3 | 24:ea19c7e8a479 | 1480 | //player.player_blk_x -= 3; |
wminix3 | 24:ea19c7e8a479 | 1481 | player.player_blk_x += (int)(accelX*8); |
wminix3 | 24:ea19c7e8a479 | 1482 | player_show(&player); |
wminix3 | 24:ea19c7e8a479 | 1483 | } |
wminix3 | 5:b7934866b264 | 1484 | // Player Fire |
wminix3 | 5:b7934866b264 | 1485 | if (pb == 0 && missile.status == PLAYER_MISSILE_INACTIVE) |
wminix3 | 5:b7934866b264 | 1486 | { |
wminix3 | 5:b7934866b264 | 1487 | missile.missile_blk_x = player.player_blk_x+(player.player_width/2); |
wminix3 | 5:b7934866b264 | 1488 | missile.missile_blk_y = player.player_blk_y; |
wminix3 | 5:b7934866b264 | 1489 | missile.status = PLAYER_MISSILE_ACTIVE; |
wminix3 | 5:b7934866b264 | 1490 | } |
wminix3 | 5:b7934866b264 | 1491 | |
wminix3 | 5:b7934866b264 | 1492 | // checks if player destroyed all enemies |
wminix3 | 5:b7934866b264 | 1493 | if (numOfEnemies == 0) |
wminix3 | 5:b7934866b264 | 1494 | { |
wminix3 | 24:ea19c7e8a479 | 1495 | // idea for best-time reading and updating: https://os.mbed.com/questions/75718/i-want-to-assign-int-value-saved-in-sd-t/ |
wminix3 | 24:ea19c7e8a479 | 1496 | int time = bestTimer.read(); |
wminix3 | 24:ea19c7e8a479 | 1497 | bestTimer.stop(); |
wminix3 | 24:ea19c7e8a479 | 1498 | bestTimer.reset(); |
wminix3 | 5:b7934866b264 | 1499 | uLCD.cls(); |
wminix3 | 24:ea19c7e8a479 | 1500 | char buffer[3] = {0}; |
wminix3 | 24:ea19c7e8a479 | 1501 | char c = {0}; |
wminix3 | 24:ea19c7e8a479 | 1502 | char *token; |
wminix3 | 24:ea19c7e8a479 | 1503 | int i = 0; |
wminix3 | 24:ea19c7e8a479 | 1504 | int storedTime = 999; |
wminix3 | 24:ea19c7e8a479 | 1505 | SDLock.lock(); |
wminix3 | 24:ea19c7e8a479 | 1506 | FILE *sdtime; |
wminix3 | 24:ea19c7e8a479 | 1507 | sdtime=fopen("/sd/besttime.txt","r"); |
wminix3 | 24:ea19c7e8a479 | 1508 | if(sdtime==NULL) pc.printf("file open error!\n\n\r"); // added this for open error check |
wminix3 | 24:ea19c7e8a479 | 1509 | while ((c != '\n') && (i < 3)) { |
wminix3 | 24:ea19c7e8a479 | 1510 | c = fgetc(sdtime); |
wminix3 | 24:ea19c7e8a479 | 1511 | buffer[i] = c; |
wminix3 | 24:ea19c7e8a479 | 1512 | i++; |
wminix3 | 24:ea19c7e8a479 | 1513 | } |
wminix3 | 24:ea19c7e8a479 | 1514 | token = strtok(buffer, "\n"); |
wminix3 | 24:ea19c7e8a479 | 1515 | storedTime = atoi(token); // convert string from file to integer |
wminix3 | 24:ea19c7e8a479 | 1516 | fclose(sdtime); |
wminix3 | 24:ea19c7e8a479 | 1517 | if (time < storedTime) { |
wminix3 | 24:ea19c7e8a479 | 1518 | uLCD.locate(2,10); |
wminix3 | 24:ea19c7e8a479 | 1519 | uLCD.printf("NEW BEST TIME!"); |
wminix3 | 24:ea19c7e8a479 | 1520 | uLCD.locate(2,11); |
wminix3 | 24:ea19c7e8a479 | 1521 | uLCD.printf("%d seconds!", time); |
wminix3 | 24:ea19c7e8a479 | 1522 | sdtime = fopen("/sd/besttime.txt", "w"); |
wminix3 | 24:ea19c7e8a479 | 1523 | if (sdtime != NULL) { |
wminix3 | 24:ea19c7e8a479 | 1524 | fprintf(sdtime, "%d\r\n", time); |
wminix3 | 24:ea19c7e8a479 | 1525 | fclose(sdtime); |
wminix3 | 24:ea19c7e8a479 | 1526 | } else { |
wminix3 | 24:ea19c7e8a479 | 1527 | pc.printf("write: failed!\r\n"); |
wminix3 | 24:ea19c7e8a479 | 1528 | } |
wminix3 | 24:ea19c7e8a479 | 1529 | } |
wminix3 | 24:ea19c7e8a479 | 1530 | SDLock.unlock(); |
wminix3 | 5:b7934866b264 | 1531 | |
wminix3 | 24:ea19c7e8a479 | 1532 | two_player_win = true; // sets win to true, for win screen |
wminix3 | 24:ea19c7e8a479 | 1533 | win = true; |
wminix3 | 24:ea19c7e8a479 | 1534 | //begin_game = false; |
wminix3 | 5:b7934866b264 | 1535 | |
wminix3 | 24:ea19c7e8a479 | 1536 | // displays video clip ???? |
wminix3 | 13:36cc024dcf6b | 1537 | /* |
wminix3 | 5:b7934866b264 | 1538 | uLCD.cls(); |
wminix3 | 5:b7934866b264 | 1539 | uLCD.media_init(); |
wminix3 | 5:b7934866b264 | 1540 | uLCD.set_sector_address(0x00, 0x00); |
wminix3 | 5:b7934866b264 | 1541 | uLCD.display_video(0,0); |
wminix3 | 24:ea19c7e8a479 | 1542 | Thread::wait(1000); // changed from wait(1) to Thread::wait(1000) since we're using threads -- Brice |
wminix3 | 24:ea19c7e8a479 | 1543 | */ |
wminix3 | 5:b7934866b264 | 1544 | |
wminix3 | 24:ea19c7e8a479 | 1545 | //uLCD.cls(); |
wminix3 | 24:ea19c7e8a479 | 1546 | |
wminix3 | 24:ea19c7e8a479 | 1547 | // prints "Congratulations" on uLCD |
wminix3 | 24:ea19c7e8a479 | 1548 | uLCD.locate(win_x_pos,win_y_pos); |
wminix3 | 24:ea19c7e8a479 | 1549 | uLCD.printf("CONGRATULATIONS!"); |
wminix3 | 5:b7934866b264 | 1550 | |
wminix3 | 24:ea19c7e8a479 | 1551 | // prints "Play Again?" and "Press pb..." |
wminix3 | 24:ea19c7e8a479 | 1552 | uLCD.locate(startover_x_pos, startover_y_pos); |
wminix3 | 24:ea19c7e8a479 | 1553 | uLCD.printf("Play again?"); |
wminix3 | 24:ea19c7e8a479 | 1554 | uLCD.locate(startover_x_pos, startover_y_pos+1); |
wminix3 | 24:ea19c7e8a479 | 1555 | uLCD.printf("Press pb..."); |
wminix3 | 5:b7934866b264 | 1556 | |
wminix3 | 24:ea19c7e8a479 | 1557 | // waits at win screen until pushbutton is pressed |
wminix3 | 24:ea19c7e8a479 | 1558 | while (two_player_win) |
wminix3 | 24:ea19c7e8a479 | 1559 | { |
wminix3 | 24:ea19c7e8a479 | 1560 | // if pb is pressed, reset game to start menu |
wminix3 | 24:ea19c7e8a479 | 1561 | if (!pb) |
wminix3 | 5:b7934866b264 | 1562 | { |
wminix3 | 24:ea19c7e8a479 | 1563 | win = false; |
wminix3 | 24:ea19c7e8a479 | 1564 | two_player_win = false; |
wminix3 | 24:ea19c7e8a479 | 1565 | begin_game2 = false; |
wminix3 | 24:ea19c7e8a479 | 1566 | numPlayers = 1; |
wminix3 | 24:ea19c7e8a479 | 1567 | game_menu = true; |
wminix3 | 24:ea19c7e8a479 | 1568 | Thread::wait(500); // changed from wait(0.5) to Thread::wait(500) since we're using threads |
wminix3 | 5:b7934866b264 | 1569 | } |
wminix3 | 24:ea19c7e8a479 | 1570 | }/* |
wminix3 | 24:ea19c7e8a479 | 1571 | REMOVED THIS AND MADE THE WINNER THE FIRST TO 1 WIN |
wminix3 | 24:ea19c7e8a479 | 1572 | if (numWins == 3) { |
wminix3 | 24:ea19c7e8a479 | 1573 | two_player_win = true; |
wminix3 | 24:ea19c7e8a479 | 1574 | while(two_player_win) { |
wminix3 | 24:ea19c7e8a479 | 1575 | // if pb is pressed, reset game to start menu |
wminix3 | 24:ea19c7e8a479 | 1576 | Thread::wait(2000); |
wminix3 | 24:ea19c7e8a479 | 1577 | uLCD.locate(win_x_pos,win_y_pos); |
wminix3 | 24:ea19c7e8a479 | 1578 | uLCD.printf("CONGRATULATIONS!"); |
wminix3 | 5:b7934866b264 | 1579 | |
wminix3 | 24:ea19c7e8a479 | 1580 | // prints "Play Again?" and "Press pb..." |
wminix3 | 24:ea19c7e8a479 | 1581 | uLCD.locate(startover_x_pos, startover_y_pos); |
wminix3 | 24:ea19c7e8a479 | 1582 | uLCD.printf("Play Again?"); |
wminix3 | 24:ea19c7e8a479 | 1583 | uLCD.locate(startover_x_pos, startover_y_pos+1); |
wminix3 | 24:ea19c7e8a479 | 1584 | uLCD.printf("Press pb..."); |
wminix3 | 24:ea19c7e8a479 | 1585 | if (!pb) |
wminix3 | 24:ea19c7e8a479 | 1586 | { |
wminix3 | 24:ea19c7e8a479 | 1587 | two_player_win = false; |
wminix3 | 24:ea19c7e8a479 | 1588 | begin_game2 = false; |
wminix3 | 24:ea19c7e8a479 | 1589 | game_menu = true; |
wminix3 | 24:ea19c7e8a479 | 1590 | Thread::wait(500); // changed from wait(0.5) to Thread::wait(500) since we're using threads |
wminix3 | 24:ea19c7e8a479 | 1591 | } |
wminix3 | 24:ea19c7e8a479 | 1592 | } |
wminix3 | 5:b7934866b264 | 1593 | } |
wminix3 | 24:ea19c7e8a479 | 1594 | */ |
wminix3 | 24:ea19c7e8a479 | 1595 | |
wminix3 | 5:b7934866b264 | 1596 | } |
wminix3 | 24:ea19c7e8a479 | 1597 | int prevColor; |
wminix3 | 5:b7934866b264 | 1598 | // checks if player was hit |
wminix3 | 5:b7934866b264 | 1599 | if (hit_player) |
wminix3 | 5:b7934866b264 | 1600 | { |
wminix3 | 5:b7934866b264 | 1601 | // updates lives |
wminix3 | 24:ea19c7e8a479 | 1602 | if (level == 1) { |
wminix3 | 24:ea19c7e8a479 | 1603 | lives1 -= 1; |
wminix3 | 24:ea19c7e8a479 | 1604 | prevColor = player.player_color; |
wminix3 | 24:ea19c7e8a479 | 1605 | player_erase(&player); |
wminix3 | 24:ea19c7e8a479 | 1606 | player.player_color = 0xFF0000; // briefly flash the player red. |
wminix3 | 24:ea19c7e8a479 | 1607 | player_show(&player); |
wminix3 | 24:ea19c7e8a479 | 1608 | Thread::wait(500); // changed from wait(0.5) to Thread::wait since we're using threads -- Brice |
wminix3 | 24:ea19c7e8a479 | 1609 | player_erase(&player); |
wminix3 | 24:ea19c7e8a479 | 1610 | player.player_color = prevColor; // briefly flash the player red. |
wminix3 | 24:ea19c7e8a479 | 1611 | player_show(&player); |
wminix3 | 24:ea19c7e8a479 | 1612 | hit_player = 0; |
wminix3 | 24:ea19c7e8a479 | 1613 | player_show(&player); |
wminix3 | 24:ea19c7e8a479 | 1614 | player.status = PLAYER_ALIVE; |
wminix3 | 5:b7934866b264 | 1615 | |
wminix3 | 5:b7934866b264 | 1616 | // prints updated lives number |
wminix3 | 24:ea19c7e8a479 | 1617 | uLCD.locate(0,0); |
wminix3 | 24:ea19c7e8a479 | 1618 | uLCD.printf("Lives:%i", lives1); |
wminix3 | 24:ea19c7e8a479 | 1619 | } else if (level == 2) { |
wminix3 | 24:ea19c7e8a479 | 1620 | lives2 -= 1; |
wminix3 | 24:ea19c7e8a479 | 1621 | prevColor = player.player_color; |
wminix3 | 24:ea19c7e8a479 | 1622 | player_erase(&player); |
wminix3 | 24:ea19c7e8a479 | 1623 | player.player_color = 0xFF0000; // briefly flash the player red. |
wminix3 | 24:ea19c7e8a479 | 1624 | player_show(&player); |
wminix3 | 24:ea19c7e8a479 | 1625 | Thread::wait(500); // changed from wait(0.5) to Thread::wait since we're using threads -- Brice |
wminix3 | 24:ea19c7e8a479 | 1626 | player_erase(&player); |
wminix3 | 24:ea19c7e8a479 | 1627 | player.player_color = prevColor; // briefly flash the player red. |
wminix3 | 24:ea19c7e8a479 | 1628 | player_show(&player); |
wminix3 | 24:ea19c7e8a479 | 1629 | //Thread::wait(500); // changed from wait(0.5) to Thread::wait since we're using threads -- Brice |
wminix3 | 24:ea19c7e8a479 | 1630 | hit_player = 0; |
wminix3 | 24:ea19c7e8a479 | 1631 | player_show(&player); |
wminix3 | 24:ea19c7e8a479 | 1632 | player.status = PLAYER_ALIVE; |
wminix3 | 24:ea19c7e8a479 | 1633 | |
wminix3 | 24:ea19c7e8a479 | 1634 | // prints updated lives number |
wminix3 | 24:ea19c7e8a479 | 1635 | uLCD.locate(0,0); |
wminix3 | 24:ea19c7e8a479 | 1636 | uLCD.printf("Lives:%i", lives2); |
wminix3 | 24:ea19c7e8a479 | 1637 | } else if (level == 3) { |
wminix3 | 24:ea19c7e8a479 | 1638 | lives3 -= 1; |
wminix3 | 24:ea19c7e8a479 | 1639 | prevColor = player.player_color; |
wminix3 | 24:ea19c7e8a479 | 1640 | player_erase(&player); |
wminix3 | 24:ea19c7e8a479 | 1641 | player.player_color = 0xFF0000; // briefly flash the player red. |
wminix3 | 24:ea19c7e8a479 | 1642 | player_show(&player); |
wminix3 | 24:ea19c7e8a479 | 1643 | Thread::wait(500); // changed from wait(0.5) to Thread::wait since we're using threads -- Brice |
wminix3 | 24:ea19c7e8a479 | 1644 | player_erase(&player); |
wminix3 | 24:ea19c7e8a479 | 1645 | player.player_color = prevColor; // briefly flash the player red. |
wminix3 | 24:ea19c7e8a479 | 1646 | player_show(&player); |
wminix3 | 24:ea19c7e8a479 | 1647 | //Thread::wait(500); // changed from wait(0.5) to Thread::wait since we're using threads -- Brice |
wminix3 | 24:ea19c7e8a479 | 1648 | hit_player = 0; |
wminix3 | 24:ea19c7e8a479 | 1649 | player_show(&player); |
wminix3 | 24:ea19c7e8a479 | 1650 | player.status = PLAYER_ALIVE; |
wminix3 | 24:ea19c7e8a479 | 1651 | |
wminix3 | 24:ea19c7e8a479 | 1652 | // prints updated lives number |
wminix3 | 24:ea19c7e8a479 | 1653 | uLCD.locate(0,0); |
wminix3 | 24:ea19c7e8a479 | 1654 | uLCD.printf("Lives:%i", lives3); |
wminix3 | 24:ea19c7e8a479 | 1655 | } |
wminix3 | 5:b7934866b264 | 1656 | } |
wminix3 | 5:b7934866b264 | 1657 | |
wminix3 | 5:b7934866b264 | 1658 | // if player loses all lives or enemy reaches the player |
wminix3 | 24:ea19c7e8a479 | 1659 | if (lose || lives1 == 0 || lives2 == 0 || lives3 == 0) |
wminix3 | 5:b7934866b264 | 1660 | { |
wminix3 | 24:ea19c7e8a479 | 1661 | //begin_game = false; // set to false to end game |
wminix3 | 24:ea19c7e8a479 | 1662 | lives1 = 3; |
wminix3 | 5:b7934866b264 | 1663 | uLCD.cls(); |
wminix3 | 5:b7934866b264 | 1664 | |
wminix3 | 24:ea19c7e8a479 | 1665 | two_player_lose = true; |
wminix3 | 5:b7934866b264 | 1666 | gameover = true; // set to go to display gameover screen |
wminix3 | 24:ea19c7e8a479 | 1667 | numPlayers = 1; |
wminix3 | 5:b7934866b264 | 1668 | |
wminix3 | 5:b7934866b264 | 1669 | // prints "GAMEOVER" to uLCD |
wminix3 | 5:b7934866b264 | 1670 | uLCD.locate(gameover_x_pos, gameover_y_pos); |
wminix3 | 24:ea19c7e8a479 | 1671 | uLCD.printf("GAMEOVER"); |
wminix3 | 24:ea19c7e8a479 | 1672 | Thread::wait(1000); // changed from wait(1) to thread::wait since we're using threads -- Brice |
wminix3 | 5:b7934866b264 | 1673 | |
wminix3 | 5:b7934866b264 | 1674 | // prints "Play Again?" and "Press pb..." |
wminix3 | 5:b7934866b264 | 1675 | uLCD.locate(startover_x_pos, startover_y_pos); |
wminix3 | 24:ea19c7e8a479 | 1676 | uLCD.printf("Play again?"); |
wminix3 | 5:b7934866b264 | 1677 | uLCD.locate(startover_x_pos, startover_y_pos+1); |
wminix3 | 5:b7934866b264 | 1678 | uLCD.printf("Press pb..."); |
wminix3 | 5:b7934866b264 | 1679 | |
wminix3 | 5:b7934866b264 | 1680 | // stays in gameover screen until pb is pressed |
wminix3 | 5:b7934866b264 | 1681 | while (gameover) |
wminix3 | 5:b7934866b264 | 1682 | { |
wminix3 | 5:b7934866b264 | 1683 | // if pb is pressed, game is reset to the game menu screen |
wminix3 | 5:b7934866b264 | 1684 | if (!pb) |
wminix3 | 5:b7934866b264 | 1685 | { |
wminix3 | 5:b7934866b264 | 1686 | gameover = false; |
wminix3 | 24:ea19c7e8a479 | 1687 | two_player_lose = false; |
wminix3 | 24:ea19c7e8a479 | 1688 | game_menu = true; |
wminix3 | 24:ea19c7e8a479 | 1689 | begin_game2 = false; |
wminix3 | 24:ea19c7e8a479 | 1690 | numPlayers = 1; |
wminix3 | 24:ea19c7e8a479 | 1691 | //game_menu = true; |
wminix3 | 24:ea19c7e8a479 | 1692 | Thread::wait(500); // changed wait(0.5) to Thread::wait since we're using threads -- Brice |
wminix3 | 5:b7934866b264 | 1693 | } |
wminix3 | 5:b7934866b264 | 1694 | } |
wminix3 | 5:b7934866b264 | 1695 | } |
wminix3 | 24:ea19c7e8a479 | 1696 | if (two_player_lose) { |
wminix3 | 5:b7934866b264 | 1697 | uLCD.cls(); |
wminix3 | 24:ea19c7e8a479 | 1698 | numPlayers = 1; |
wminix3 | 24:ea19c7e8a479 | 1699 | Thread::wait(2000); |
wminix3 | 24:ea19c7e8a479 | 1700 | uLCD.locate(gameover_x_pos, gameover_y_pos); |
wminix3 | 24:ea19c7e8a479 | 1701 | uLCD.printf("You lost!"); |
wminix3 | 24:ea19c7e8a479 | 1702 | Thread::wait(1000); // changed from wait(1) to thread::wait since we're using threads -- Brice |
wminix3 | 5:b7934866b264 | 1703 | |
wminix3 | 24:ea19c7e8a479 | 1704 | // prints "Play Again?" and "Press pb..." |
wminix3 | 5:b7934866b264 | 1705 | uLCD.locate(startover_x_pos, startover_y_pos); |
wminix3 | 24:ea19c7e8a479 | 1706 | uLCD.printf("Continue?"); |
wminix3 | 5:b7934866b264 | 1707 | uLCD.locate(startover_x_pos, startover_y_pos+1); |
wminix3 | 5:b7934866b264 | 1708 | uLCD.printf("Press pb..."); |
wminix3 | 24:ea19c7e8a479 | 1709 | } |
wminix3 | 24:ea19c7e8a479 | 1710 | while(two_player_lose) { |
wminix3 | 24:ea19c7e8a479 | 1711 | //uLCD.cls(); |
wminix3 | 5:b7934866b264 | 1712 | |
wminix3 | 24:ea19c7e8a479 | 1713 | if (!pb) |
wminix3 | 5:b7934866b264 | 1714 | { |
wminix3 | 24:ea19c7e8a479 | 1715 | two_player_lose = false; |
wminix3 | 24:ea19c7e8a479 | 1716 | game_menu = true; |
wminix3 | 24:ea19c7e8a479 | 1717 | begin_game2 = false; |
wminix3 | 24:ea19c7e8a479 | 1718 | numPlayers = 1; |
wminix3 | 24:ea19c7e8a479 | 1719 | Thread::wait(500); // changed wait(0.5) to Thread::wait since we're using threads -- Brice |
michaeljson | 0:3817adfaeb06 | 1720 | } |
michaeljson | 0:3817adfaeb06 | 1721 | } |
michaeljson | 0:3817adfaeb06 | 1722 | } |
michaeljson | 0:3817adfaeb06 | 1723 | } |
michaeljson | 0:3817adfaeb06 | 1724 | } |