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