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