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 William Minix

test

Committer:
wminix3
Date:
Fri Apr 16 20:22:47 2021 +0000
Revision:
10:1eeb21ef7b2c
Parent:
9:c22613b0007a
Child:
11:cdea2c9ec531
Master-Slave-based mbed communication threads. Don't forget to have only the appropriate thread uncommented. First player should only have master thread (and not slave). Second player should only have slave thread (and not master).

Who changed what in which revision?

UserRevisionLine numberNew 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);
michaeljson 0:3817adfaeb06 77 // Initialize all global enemy objects
michaeljson 0:3817adfaeb06 78 enemy_t enemy_1;
michaeljson 0:3817adfaeb06 79 enemy_t enemy_2;
michaeljson 0:3817adfaeb06 80 enemy_t enemy_3;
michaeljson 0:3817adfaeb06 81 enemy_t enemy_4;
michaeljson 0:3817adfaeb06 82 enemy_t enemy_5;
michaeljson 0:3817adfaeb06 83 enemy_t enemy_6;
michaeljson 0:3817adfaeb06 84 enemy_t enemy_7;
michaeljson 0:3817adfaeb06 85 enemy_t enemy_8;
michaeljson 0:3817adfaeb06 86 enemy_t enemy_9;
michaeljson 0:3817adfaeb06 87 enemy_t enemy_10;
michaeljson 0:3817adfaeb06 88 enemy_t enemy_11;
michaeljson 0:3817adfaeb06 89 enemy_t enemy_12;
michaeljson 0:3817adfaeb06 90 enemy_t enemy_13;
michaeljson 0:3817adfaeb06 91 enemy_t enemy_14;
michaeljson 0:3817adfaeb06 92 enemy_t enemy_15;
michaeljson 0:3817adfaeb06 93
michaeljson 0:3817adfaeb06 94 // Initialize variables
wminix3 2:4a3f97570578 95 // Brice added "volatile" here to protect global variables.
wminix3 2:4a3f97570578 96 volatile int numOfEnemies = 0;
wminix3 2:4a3f97570578 97 volatile int ENEMY_MOVE = 1;
wminix3 2:4a3f97570578 98 volatile int MOVE_DOWN = 0;
wminix3 2:4a3f97570578 99 volatile int DIRECTION = 1;
wminix3 2:4a3f97570578 100 volatile int firing_col = 0;
wminix3 2:4a3f97570578 101 volatile int hit_player = 0;
wminix3 2:4a3f97570578 102 volatile bool lose = false;
wminix3 2:4a3f97570578 103 volatile int lives = 3;
wminix3 2:4a3f97570578 104 volatile bool game_menu = false;
wminix3 2:4a3f97570578 105 volatile bool begin_game = false;
wminix3 2:4a3f97570578 106 volatile bool gameover = false;
wminix3 2:4a3f97570578 107 volatile int numPlayers = 1;
wminix3 4:739f6e0dd8af 108 volatile bool first_player_ready = false;
wminix3 4:739f6e0dd8af 109 volatile bool second_player_ready = false;
wminix3 5:b7934866b264 110 volatile bool begin_game2 = false;
wminix3 5:b7934866b264 111 volatile int numWins = 0;
wminix3 5:b7934866b264 112 volatile bool two_player_win = false;
wminix3 5:b7934866b264 113 volatile bool two_player_lose = false;
wminix3 8:27c4345be3db 114 Mutex mbedLock;
michaeljson 0:3817adfaeb06 115
michaeljson 0:3817adfaeb06 116 // Initialize global player object
michaeljson 0:3817adfaeb06 117 player_t player;
michaeljson 0:3817adfaeb06 118
michaeljson 0:3817adfaeb06 119 // Intialize global player and enemy missile
michaeljson 0:3817adfaeb06 120 missile_t missile; // player missile
michaeljson 0:3817adfaeb06 121 missile_t enemy_missile; // enemy missile
michaeljson 0:3817adfaeb06 122
michaeljson 0:3817adfaeb06 123 // Array of enemy objects
michaeljson 0:3817adfaeb06 124 enemy_t * enemyArray[15];
michaeljson 0:3817adfaeb06 125
michaeljson 0:3817adfaeb06 126 // Function Prototypes
michaeljson 0:3817adfaeb06 127 void move_enemy_down();
michaeljson 0:3817adfaeb06 128 void playstart(void const *args);
michaeljson 0:3817adfaeb06 129
michaeljson 0:3817adfaeb06 130 // Draws the enemies at the initial starting location
michaeljson 0:3817adfaeb06 131 void draw_enemies_level()
michaeljson 0:3817adfaeb06 132 {
michaeljson 0:3817adfaeb06 133 // Initialize local variables
michaeljson 0:3817adfaeb06 134 unsigned int start_x_pos = 12;
michaeljson 0:3817adfaeb06 135 unsigned int start_enemy_y_pos = 20;
michaeljson 0:3817adfaeb06 136 numOfEnemies = 15;
michaeljson 0:3817adfaeb06 137
michaeljson 0:3817adfaeb06 138 // First Row of Enemies
michaeljson 0:3817adfaeb06 139 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 140 enemy_show(&enemy_1); // displays the enemy on uLCD
michaeljson 0:3817adfaeb06 141
michaeljson 0:3817adfaeb06 142 enemy_init(&enemy_2,start_x_pos+15,start_enemy_y_pos,WHITE);
michaeljson 0:3817adfaeb06 143 enemy_show(&enemy_2);
michaeljson 0:3817adfaeb06 144
michaeljson 0:3817adfaeb06 145 enemy_init(&enemy_3,start_x_pos+30,start_enemy_y_pos,WHITE);
michaeljson 0:3817adfaeb06 146 enemy_show(&enemy_3);
michaeljson 0:3817adfaeb06 147
michaeljson 0:3817adfaeb06 148 enemy_init(&enemy_4,start_x_pos+45,start_enemy_y_pos,WHITE);
michaeljson 0:3817adfaeb06 149 enemy_show(&enemy_4);
michaeljson 0:3817adfaeb06 150
michaeljson 0:3817adfaeb06 151 enemy_init(&enemy_5,start_x_pos+60,start_enemy_y_pos,WHITE);
michaeljson 0:3817adfaeb06 152 enemy_show(&enemy_5);
michaeljson 0:3817adfaeb06 153
michaeljson 0:3817adfaeb06 154 // Second Row of Enemies
michaeljson 0:3817adfaeb06 155 enemy_init(&enemy_6,start_x_pos,start_enemy_y_pos+12,WHITE);
michaeljson 0:3817adfaeb06 156 enemy_show(&enemy_6);
michaeljson 0:3817adfaeb06 157
michaeljson 0:3817adfaeb06 158 enemy_init(&enemy_7,start_x_pos+15,start_enemy_y_pos+12,WHITE);
michaeljson 0:3817adfaeb06 159 enemy_show(&enemy_7);
michaeljson 0:3817adfaeb06 160
michaeljson 0:3817adfaeb06 161 enemy_init(&enemy_8,start_x_pos+30,start_enemy_y_pos+12,WHITE);
michaeljson 0:3817adfaeb06 162 enemy_show(&enemy_8);
michaeljson 0:3817adfaeb06 163
michaeljson 0:3817adfaeb06 164 enemy_init(&enemy_9,start_x_pos+45,start_enemy_y_pos+12,WHITE);
michaeljson 0:3817adfaeb06 165 enemy_show(&enemy_9);
michaeljson 0:3817adfaeb06 166
michaeljson 0:3817adfaeb06 167 enemy_init(&enemy_10,start_x_pos+60,start_enemy_y_pos+12,WHITE);
michaeljson 0:3817adfaeb06 168 enemy_show(&enemy_10);
michaeljson 0:3817adfaeb06 169
michaeljson 0:3817adfaeb06 170 // Third Row of Enemies
michaeljson 0:3817adfaeb06 171 enemy_init(&enemy_11,start_x_pos,start_enemy_y_pos+24,WHITE);
michaeljson 0:3817adfaeb06 172 enemy_show(&enemy_11);
michaeljson 0:3817adfaeb06 173
michaeljson 0:3817adfaeb06 174 enemy_init(&enemy_12,start_x_pos+15,start_enemy_y_pos+24,WHITE);
michaeljson 0:3817adfaeb06 175 enemy_show(&enemy_12);
michaeljson 0:3817adfaeb06 176
michaeljson 0:3817adfaeb06 177 enemy_init(&enemy_13,start_x_pos+30,start_enemy_y_pos+24,WHITE);
michaeljson 0:3817adfaeb06 178 enemy_show(&enemy_13);
michaeljson 0:3817adfaeb06 179
michaeljson 0:3817adfaeb06 180 enemy_init(&enemy_14,start_x_pos+45,start_enemy_y_pos+24,WHITE);
michaeljson 0:3817adfaeb06 181 enemy_show(&enemy_14);
michaeljson 0:3817adfaeb06 182
michaeljson 0:3817adfaeb06 183 enemy_init(&enemy_15,start_x_pos+60,start_enemy_y_pos+24,WHITE);
michaeljson 0:3817adfaeb06 184 enemy_show(&enemy_15);
michaeljson 0:3817adfaeb06 185
michaeljson 0:3817adfaeb06 186 // Put enemy objects into array
michaeljson 0:3817adfaeb06 187 enemyArray[0] = &enemy_1;
michaeljson 0:3817adfaeb06 188 enemyArray[1] = &enemy_2;
michaeljson 0:3817adfaeb06 189 enemyArray[2] = &enemy_3;
michaeljson 0:3817adfaeb06 190 enemyArray[3] = &enemy_4;
michaeljson 0:3817adfaeb06 191 enemyArray[4] = &enemy_5;
michaeljson 0:3817adfaeb06 192 enemyArray[5] = &enemy_6;
michaeljson 0:3817adfaeb06 193 enemyArray[6] = &enemy_7;
michaeljson 0:3817adfaeb06 194 enemyArray[7] = &enemy_8;
michaeljson 0:3817adfaeb06 195 enemyArray[8] = &enemy_9;
michaeljson 0:3817adfaeb06 196 enemyArray[9] = &enemy_10;
michaeljson 0:3817adfaeb06 197 enemyArray[10] = &enemy_11;
michaeljson 0:3817adfaeb06 198 enemyArray[11] = &enemy_12;
michaeljson 0:3817adfaeb06 199 enemyArray[12] = &enemy_13;
michaeljson 0:3817adfaeb06 200 enemyArray[13] = &enemy_14;
michaeljson 0:3817adfaeb06 201 enemyArray[14] = &enemy_15;
michaeljson 0:3817adfaeb06 202 }
michaeljson 0:3817adfaeb06 203
michaeljson 0:3817adfaeb06 204 // Draws the player at the initial starting location
michaeljson 0:3817adfaeb06 205 void draw_initial_player()
michaeljson 0:3817adfaeb06 206 {
michaeljson 0:3817adfaeb06 207 int start_x_pos = 59;
michaeljson 0:3817adfaeb06 208 int start_y_pos = 110;
michaeljson 0:3817adfaeb06 209
michaeljson 0:3817adfaeb06 210 player_init(&player,start_x_pos,start_y_pos,WHITE); // intialize x-pos and y-pos and color of player
michaeljson 0:3817adfaeb06 211 player_show(&player); // display player on uLCD
michaeljson 0:3817adfaeb06 212 }
michaeljson 0:3817adfaeb06 213
michaeljson 0:3817adfaeb06 214 // Checks all enemies in row 1
michaeljson 0:3817adfaeb06 215 void check_hit_enemy_row1()
michaeljson 0:3817adfaeb06 216 {
michaeljson 0:3817adfaeb06 217 int hit_enemy;
michaeljson 0:3817adfaeb06 218
michaeljson 0:3817adfaeb06 219 // First Row of Enemies
michaeljson 0:3817adfaeb06 220 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 221 numOfEnemies = numOfEnemies - hit_enemy; // updates the number of enemies left
michaeljson 0:3817adfaeb06 222 hit_enemy = check_enemy(&enemy_2, &missile);
michaeljson 0:3817adfaeb06 223 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 224 hit_enemy = check_enemy(&enemy_3, &missile);
michaeljson 0:3817adfaeb06 225 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 226 hit_enemy = check_enemy(&enemy_4, &missile);
michaeljson 0:3817adfaeb06 227 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 228 hit_enemy = check_enemy(&enemy_5, &missile);
michaeljson 0:3817adfaeb06 229 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 230 }
michaeljson 0:3817adfaeb06 231
michaeljson 0:3817adfaeb06 232 // Same as check_hit_enemy_row1, but checks enemies at row 2
michaeljson 0:3817adfaeb06 233 void check_hit_enemy_row2()
michaeljson 0:3817adfaeb06 234 {
michaeljson 0:3817adfaeb06 235 int hit_enemy;
michaeljson 0:3817adfaeb06 236
michaeljson 0:3817adfaeb06 237 // Second Row of Enemies
michaeljson 0:3817adfaeb06 238 hit_enemy = check_enemy(&enemy_6, &missile);
michaeljson 0:3817adfaeb06 239 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 240 hit_enemy = check_enemy(&enemy_7, &missile);
michaeljson 0:3817adfaeb06 241 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 242 hit_enemy = check_enemy(&enemy_8, &missile);
michaeljson 0:3817adfaeb06 243 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 244 hit_enemy = check_enemy(&enemy_9, &missile);
michaeljson 0:3817adfaeb06 245 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 246 hit_enemy = check_enemy(&enemy_10, &missile);
michaeljson 0:3817adfaeb06 247 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 248 }
michaeljson 0:3817adfaeb06 249
michaeljson 0:3817adfaeb06 250 // Same as check_hit_enemy_row1, but checks enemies at row 3
michaeljson 0:3817adfaeb06 251 void check_hit_enemy_row3()
michaeljson 0:3817adfaeb06 252 {
michaeljson 0:3817adfaeb06 253 int hit_enemy;
michaeljson 0:3817adfaeb06 254
michaeljson 0:3817adfaeb06 255 // Third Row of Enemies
michaeljson 0:3817adfaeb06 256 hit_enemy = check_enemy(&enemy_11, &missile);
michaeljson 0:3817adfaeb06 257 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 258 hit_enemy = check_enemy(&enemy_12, &missile);
michaeljson 0:3817adfaeb06 259 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 260 hit_enemy = check_enemy(&enemy_13, &missile);
michaeljson 0:3817adfaeb06 261 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 262 hit_enemy = check_enemy(&enemy_14, &missile);
michaeljson 0:3817adfaeb06 263 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 264 hit_enemy = check_enemy(&enemy_15, &missile);
michaeljson 0:3817adfaeb06 265 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 266 }
michaeljson 0:3817adfaeb06 267
michaeljson 0:3817adfaeb06 268 // Checks if player is hit
michaeljson 0:3817adfaeb06 269 void check_player_hit()
michaeljson 0:3817adfaeb06 270 {
michaeljson 0:3817adfaeb06 271 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 272 }
michaeljson 0:3817adfaeb06 273
michaeljson 0:3817adfaeb06 274 // Randomly selects an enemy to fire and updates the position of where the missile will fire from
michaeljson 0:3817adfaeb06 275 void random_attack_gen()
michaeljson 0:3817adfaeb06 276 {
michaeljson 0:3817adfaeb06 277 firing_col = rand() % 5; // selects a random column
michaeljson 0:3817adfaeb06 278
michaeljson 0:3817adfaeb06 279 // first checks if the 3rd row closest to the player is alive
michaeljson 0:3817adfaeb06 280 if (enemyArray[firing_col+10]->status == ENEMY_ALIVE)
michaeljson 0:3817adfaeb06 281 {
michaeljson 0:3817adfaeb06 282 // If alive, the enemy missile position is updated to the center of the enemy
michaeljson 0:3817adfaeb06 283 enemy_missile.missile_blk_x = enemyArray[firing_col+10]->enemy_blk_x + (enemyArray[firing_col+10]->enemy_width/2);
michaeljson 0:3817adfaeb06 284 enemy_missile.missile_blk_y = enemyArray[firing_col+10]->enemy_blk_y + enemyArray[firing_col+10]->enemy_height + 1;
michaeljson 0:3817adfaeb06 285 enemy_missile.status = ENEMY_MISSILE_ACTIVE; // sets the enemy missile as active
michaeljson 0:3817adfaeb06 286 }
michaeljson 0:3817adfaeb06 287 // if enemy at 3rd row is dead, checks the enemy in the 2nd row
michaeljson 0:3817adfaeb06 288 else if (enemyArray[firing_col+5]->status == ENEMY_ALIVE)
michaeljson 0:3817adfaeb06 289 {
michaeljson 0:3817adfaeb06 290 // If alive, the enemy missile position is updated to the center of the enemy
michaeljson 0:3817adfaeb06 291 enemy_missile.missile_blk_x = enemyArray[firing_col+5]->enemy_blk_x + (enemyArray[firing_col+5]->enemy_width/2);
michaeljson 0:3817adfaeb06 292 enemy_missile.missile_blk_y = enemyArray[firing_col+5]->enemy_blk_y + enemyArray[firing_col+5]->enemy_height + 1;
michaeljson 0:3817adfaeb06 293 enemy_missile.status = ENEMY_MISSILE_ACTIVE;
michaeljson 0:3817adfaeb06 294 }
michaeljson 0:3817adfaeb06 295 // if enemy at 2nd and 3rd row is dead, checks the enemy in the 1st row
michaeljson 0:3817adfaeb06 296 else if (enemyArray[firing_col]->status == ENEMY_ALIVE)
michaeljson 0:3817adfaeb06 297 {
michaeljson 0:3817adfaeb06 298 // If alive, the enemy missile position is updated to the center of the enemy
michaeljson 0:3817adfaeb06 299 enemy_missile.missile_blk_x = enemyArray[firing_col]->enemy_blk_x + (enemyArray[firing_col]->enemy_width/2);
michaeljson 0:3817adfaeb06 300 enemy_missile.missile_blk_y = enemyArray[firing_col]->enemy_blk_y + enemyArray[firing_col]->enemy_height + 1;
michaeljson 0:3817adfaeb06 301 enemy_missile.status = ENEMY_MISSILE_ACTIVE;
michaeljson 0:3817adfaeb06 302 }
michaeljson 0:3817adfaeb06 303 }
michaeljson 0:3817adfaeb06 304
michaeljson 0:3817adfaeb06 305 // moves the enemy
michaeljson 0:3817adfaeb06 306 void enemy_motion()
michaeljson 0:3817adfaeb06 307 {
michaeljson 0:3817adfaeb06 308 // will move the enemy every 6 loops
michaeljson 0:3817adfaeb06 309 if (ENEMY_MOVE % 6 == 0)
michaeljson 0:3817adfaeb06 310 {
michaeljson 0:3817adfaeb06 311 // FIrst Row of Enemies
michaeljson 0:3817adfaeb06 312 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 313 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 314 MOVE_DOWN = move_enemy(&enemy_3, MOVE_DOWN, DIRECTION); // MOVE_DOWN will be 2 if enemies reach the player, otherwise 0
michaeljson 0:3817adfaeb06 315 MOVE_DOWN = move_enemy(&enemy_4, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 316 MOVE_DOWN = move_enemy(&enemy_5, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 317
michaeljson 0:3817adfaeb06 318 // Second Row of Enemies
michaeljson 0:3817adfaeb06 319 MOVE_DOWN = move_enemy(&enemy_6, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 320 MOVE_DOWN = move_enemy(&enemy_7, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 321 MOVE_DOWN = move_enemy(&enemy_8, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 322 MOVE_DOWN = move_enemy(&enemy_9, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 323 MOVE_DOWN = move_enemy(&enemy_10, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 324
michaeljson 0:3817adfaeb06 325 // Third Row of Enemies
michaeljson 0:3817adfaeb06 326 MOVE_DOWN = move_enemy(&enemy_11, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 327 MOVE_DOWN = move_enemy(&enemy_12, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 328 MOVE_DOWN = move_enemy(&enemy_13, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 329 MOVE_DOWN = move_enemy(&enemy_14, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 330 MOVE_DOWN = move_enemy(&enemy_15, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 331
michaeljson 0:3817adfaeb06 332 // if MOVE_DOWN is 2, then the enemies have reached the player
michaeljson 0:3817adfaeb06 333 if (MOVE_DOWN == 2)
michaeljson 0:3817adfaeb06 334 {
michaeljson 0:3817adfaeb06 335 lose = true; // set global lose to true to go to gameover screen
michaeljson 0:3817adfaeb06 336 }
michaeljson 0:3817adfaeb06 337
michaeljson 0:3817adfaeb06 338 // if MOVE_DOWN is 1, update the y-pos of the enemies
michaeljson 0:3817adfaeb06 339 if (MOVE_DOWN == 1)
michaeljson 0:3817adfaeb06 340 {
michaeljson 0:3817adfaeb06 341 move_enemy_down(); // updates the y-pos of the enemies
michaeljson 0:3817adfaeb06 342
michaeljson 0:3817adfaeb06 343 // Flips the direction when the enemy moves down
michaeljson 0:3817adfaeb06 344 if (DIRECTION == 1)
michaeljson 0:3817adfaeb06 345 {
michaeljson 0:3817adfaeb06 346 DIRECTION = 2;
michaeljson 0:3817adfaeb06 347 }
michaeljson 0:3817adfaeb06 348 else
michaeljson 0:3817adfaeb06 349 {
michaeljson 0:3817adfaeb06 350 DIRECTION = 1;
michaeljson 0:3817adfaeb06 351 }
michaeljson 0:3817adfaeb06 352 MOVE_DOWN = 0; // sets MOVE_DOWN back to 0 to stop down movement until
michaeljson 0:3817adfaeb06 353 }
michaeljson 0:3817adfaeb06 354
michaeljson 0:3817adfaeb06 355 ENEMY_MOVE += 1;
michaeljson 0:3817adfaeb06 356 }
michaeljson 0:3817adfaeb06 357 else
michaeljson 0:3817adfaeb06 358 {
michaeljson 0:3817adfaeb06 359 ENEMY_MOVE += 1;
michaeljson 0:3817adfaeb06 360 }
michaeljson 0:3817adfaeb06 361 }
michaeljson 0:3817adfaeb06 362
michaeljson 0:3817adfaeb06 363 // moves all the enemies down in the y-direction
michaeljson 0:3817adfaeb06 364 void move_enemy_down()
michaeljson 0:3817adfaeb06 365 {
michaeljson 0:3817adfaeb06 366 // First Row of Enemies
michaeljson 0:3817adfaeb06 367 enemy_erase(&enemy_1);
michaeljson 0:3817adfaeb06 368 enemy_erase(&enemy_2);
michaeljson 0:3817adfaeb06 369 enemy_erase(&enemy_3);
michaeljson 0:3817adfaeb06 370 enemy_erase(&enemy_4);
michaeljson 0:3817adfaeb06 371 enemy_erase(&enemy_5);
michaeljson 0:3817adfaeb06 372
michaeljson 0:3817adfaeb06 373 enemy_1.enemy_blk_y += enemy_1.enemy_height+4;
michaeljson 0:3817adfaeb06 374 enemy_2.enemy_blk_y += enemy_2.enemy_height+4;
michaeljson 0:3817adfaeb06 375 enemy_3.enemy_blk_y += enemy_3.enemy_height+4;
michaeljson 0:3817adfaeb06 376 enemy_4.enemy_blk_y += enemy_4.enemy_height+4;
michaeljson 0:3817adfaeb06 377 enemy_5.enemy_blk_y += enemy_5.enemy_height+4;
michaeljson 0:3817adfaeb06 378
michaeljson 0:3817adfaeb06 379 // Second Row of Enemies
michaeljson 0:3817adfaeb06 380 enemy_erase(&enemy_6);
michaeljson 0:3817adfaeb06 381 enemy_erase(&enemy_7);
michaeljson 0:3817adfaeb06 382 enemy_erase(&enemy_8);
michaeljson 0:3817adfaeb06 383 enemy_erase(&enemy_9);
michaeljson 0:3817adfaeb06 384 enemy_erase(&enemy_10);
michaeljson 0:3817adfaeb06 385
michaeljson 0:3817adfaeb06 386 enemy_6.enemy_blk_y += enemy_6.enemy_height+4;
michaeljson 0:3817adfaeb06 387 enemy_7.enemy_blk_y += enemy_7.enemy_height+4;
michaeljson 0:3817adfaeb06 388 enemy_8.enemy_blk_y += enemy_8.enemy_height+4;
michaeljson 0:3817adfaeb06 389 enemy_9.enemy_blk_y += enemy_9.enemy_height+4;
michaeljson 0:3817adfaeb06 390 enemy_10.enemy_blk_y += enemy_10.enemy_height+4;
michaeljson 0:3817adfaeb06 391
michaeljson 0:3817adfaeb06 392 // Third Row of Enemies
michaeljson 0:3817adfaeb06 393 enemy_erase(&enemy_11);
michaeljson 0:3817adfaeb06 394 enemy_erase(&enemy_12);
michaeljson 0:3817adfaeb06 395 enemy_erase(&enemy_13);
michaeljson 0:3817adfaeb06 396 enemy_erase(&enemy_14);
michaeljson 0:3817adfaeb06 397 enemy_erase(&enemy_15);
michaeljson 0:3817adfaeb06 398
michaeljson 0:3817adfaeb06 399 enemy_11.enemy_blk_y += enemy_11.enemy_height+4;
michaeljson 0:3817adfaeb06 400 enemy_12.enemy_blk_y += enemy_12.enemy_height+4;
michaeljson 0:3817adfaeb06 401 enemy_13.enemy_blk_y += enemy_13.enemy_height+4;
michaeljson 0:3817adfaeb06 402 enemy_14.enemy_blk_y += enemy_14.enemy_height+4;
michaeljson 0:3817adfaeb06 403 enemy_15.enemy_blk_y += enemy_15.enemy_height+4;
michaeljson 0:3817adfaeb06 404 }
michaeljson 0:3817adfaeb06 405
michaeljson 0:3817adfaeb06 406 // thread that plays sounds during game
michaeljson 0:3817adfaeb06 407 void playstart(void const *args)//Th
michaeljson 0:3817adfaeb06 408 { //Depending on the state of the game,
michaeljson 0:3817adfaeb06 409 //queue either screen music or play sound effect upon fire
michaeljson 0:3817adfaeb06 410 while(true) {
michaeljson 0:3817adfaeb06 411 FILE *wave_file;
michaeljson 0:3817adfaeb06 412
michaeljson 0:3817adfaeb06 413 // plays intro music during menu screen
michaeljson 0:3817adfaeb06 414 while(game_menu)
michaeljson 0:3817adfaeb06 415 {
wminix3 2:4a3f97570578 416 wave_file=fopen("/sd/wavfiles/futureEdit2.wav","r");
wminix3 2:4a3f97570578 417 if(wave_file==NULL) pc.printf("file open error!\n\n\r"); // added this for open error check
michaeljson 0:3817adfaeb06 418 waver.play(wave_file);
michaeljson 0:3817adfaeb06 419 fclose(wave_file);
michaeljson 0:3817adfaeb06 420 }
michaeljson 0:3817adfaeb06 421
michaeljson 0:3817adfaeb06 422 // Checks in game sound conditions
wminix3 5:b7934866b264 423 while(begin_game || begin_game2) // added OR begin_game2 so that music/sounds play during one-player or two-player
michaeljson 0:3817adfaeb06 424 {
michaeljson 0:3817adfaeb06 425 // play firing sound when the player fires
michaeljson 0:3817adfaeb06 426 if(!pb && missile.status == PLAYER_MISSILE_INACTIVE) {
michaeljson 0:3817adfaeb06 427
wminix3 2:4a3f97570578 428 wave_file=fopen("/sd/wavfiles/laserEdit.wav","r");
wminix3 2:4a3f97570578 429 if(wave_file==NULL) pc.printf("laser file open error!\n\n\r"); // added this for open error check
michaeljson 0:3817adfaeb06 430
michaeljson 0:3817adfaeb06 431 waver.play(wave_file);
michaeljson 0:3817adfaeb06 432 fclose(wave_file);
michaeljson 0:3817adfaeb06 433 }
michaeljson 0:3817adfaeb06 434
michaeljson 0:3817adfaeb06 435 // if player hit, play hit sound
michaeljson 0:3817adfaeb06 436 if (hit_player)
michaeljson 0:3817adfaeb06 437 {
wminix3 2:4a3f97570578 438 wave_file=fopen("/sd/wavfiles/bigExplosionEdit2.wav","r");
wminix3 2:4a3f97570578 439 if(wave_file==NULL) pc.printf("explosion file open error!\n\n\r"); // added this for open error check
michaeljson 0:3817adfaeb06 440 waver.play(wave_file);
michaeljson 0:3817adfaeb06 441 fclose(wave_file);
michaeljson 0:3817adfaeb06 442 }
michaeljson 0:3817adfaeb06 443 }
michaeljson 0:3817adfaeb06 444
michaeljson 0:3817adfaeb06 445 // players gameover voice if player loses
michaeljson 0:3817adfaeb06 446 while(gameover)
michaeljson 0:3817adfaeb06 447 {
wminix3 2:4a3f97570578 448 wave_file=fopen("/sd/wavfiles/comicalgameoverEdit.wav","r");
wminix3 2:4a3f97570578 449 if(wave_file==NULL) pc.printf("gameover 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);
michaeljson 0:3817adfaeb06 452 }
michaeljson 0:3817adfaeb06 453 }
michaeljson 0:3817adfaeb06 454 }
wminix3 10:1eeb21ef7b2c 455 /*
wminix3 5:b7934866b264 456 // Thread added for mbed communication, which allows two-player -- Brice
wminix3 6:c44055f94cc3 457 void mbedSend(void const *args) {
wminix3 4:739f6e0dd8af 458 while(1) {
wminix3 5:b7934866b264 459 while(numPlayers == 1) Thread::yield(); // with one player, thread is unneeded and should yield.
wminix3 5:b7934866b264 460 while(!first_player_ready || !second_player_ready) { // while either player isn't ready, send a start code, and become ready
wminix3 4:739f6e0dd8af 461 //if (!first_player_ready || !second_player_ready) {
wminix3 8:27c4345be3db 462 mbedLock.lock();
wminix3 4:739f6e0dd8af 463 secondMbed.putc('S');
wminix3 8:27c4345be3db 464 mbedLock.unlock();
wminix3 4:739f6e0dd8af 465 first_player_ready = true;
wminix3 10:1eeb21ef7b2c 466 pc.printf("first player ready");
wminix3 7:9a826617be92 467 Thread::wait(1000);
wminix3 4:739f6e0dd8af 468 //}
wminix3 6:c44055f94cc3 469 // 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 470 // if (secondMbed.getc() == 'S') {
wminix3 6:c44055f94cc3 471 // second_player_ready = true;
wminix3 6:c44055f94cc3 472 // pc.printf("second play ready");
wminix3 6:c44055f94cc3 473 // }
wminix3 6:c44055f94cc3 474 // }
wminix3 6:c44055f94cc3 475 // Thread::wait(1000); // run once a second
wminix3 5:b7934866b264 476 }
wminix3 5:b7934866b264 477 if (two_player_win) { // if this player wins, notify the other mbed/player that they lost.
wminix3 8:27c4345be3db 478 mbedLock.lock();
wminix3 5:b7934866b264 479 secondMbed.putc('W');
wminix3 8:27c4345be3db 480 mbedLock.unlock();
wminix3 4:739f6e0dd8af 481 }
wminix3 6:c44055f94cc3 482 //if (secondMbed.readable()) {
wminix3 6:c44055f94cc3 483 // if (secondMbed.getc() == 'W') {
wminix3 6:c44055f94cc3 484 // two_player_lose = true;
wminix3 6:c44055f94cc3 485 // }
wminix3 6:c44055f94cc3 486 //}
wminix3 7:9a826617be92 487 Thread::wait(2000); // check twice a second for a win
wminix3 6:c44055f94cc3 488 }
wminix3 6:c44055f94cc3 489 }
wminix3 10:1eeb21ef7b2c 490 */
wminix3 10:1eeb21ef7b2c 491 /*
wminix3 6:c44055f94cc3 492 void mbedReceive(void const *args) {
wminix3 7:9a826617be92 493 char rx;
wminix3 6:c44055f94cc3 494 while(1) {
wminix3 10:1eeb21ef7b2c 495 rx = '0';
wminix3 8:27c4345be3db 496 mbedLock.lock();
wminix3 7:9a826617be92 497 while(numPlayers == 1 || !secondMbed.readable()) {
wminix3 9:c22613b0007a 498 mbedLock.unlock();
wminix3 10:1eeb21ef7b2c 499 pc.printf("yielding");
wminix3 7:9a826617be92 500 Thread::yield(); // with one player, thread is unneeded and should yield.
wminix3 7:9a826617be92 501 }
wminix3 7:9a826617be92 502 rx = secondMbed.getc();
wminix3 10:1eeb21ef7b2c 503 pc.printf("%c", rx);
wminix3 8:27c4345be3db 504 mbedLock.unlock();
wminix3 7:9a826617be92 505 if (rx == 'S') {
wminix3 7:9a826617be92 506 second_player_ready = true;
wminix3 10:1eeb21ef7b2c 507 pc.printf("second play ready");
wminix3 6:c44055f94cc3 508 }
wminix3 6:c44055f94cc3 509 // Thread::wait(1000); // run once a second
wminix3 6:c44055f94cc3 510 //}
wminix3 7:9a826617be92 511 if (rx == 'W') {
wminix3 6:c44055f94cc3 512 two_player_lose = true;
wminix3 5:b7934866b264 513 }
wminix3 10:1eeb21ef7b2c 514 Thread::wait(3000); // check twice a second for a win
wminix3 10:1eeb21ef7b2c 515 }
wminix3 10:1eeb21ef7b2c 516 }
wminix3 10:1eeb21ef7b2c 517 */
wminix3 10:1eeb21ef7b2c 518
wminix3 10:1eeb21ef7b2c 519 // UNCOMMENT THIS THREAD IF SECOND PLAYER AND COMMENT MASTER THREAD
wminix3 10:1eeb21ef7b2c 520 /*
wminix3 10:1eeb21ef7b2c 521 // The slave mbed device (second player) should uncomment this thread -- Brice
wminix3 10:1eeb21ef7b2c 522 void mbedSlave(void const *args) {
wminix3 10:1eeb21ef7b2c 523 char rx;
wminix3 10:1eeb21ef7b2c 524 while(1) {
wminix3 10:1eeb21ef7b2c 525 while(numPlayers == 1) Thread::yield();
wminix3 10:1eeb21ef7b2c 526 rx = '0';
wminix3 10:1eeb21ef7b2c 527 if (secondMbed.readable()) {
wminix3 10:1eeb21ef7b2c 528 rx = secondMbed.getc();
wminix3 10:1eeb21ef7b2c 529 if (!begin_game2 && rx == 'S') {
wminix3 10:1eeb21ef7b2c 530 secondMbed.putc(rx);
wminix3 10:1eeb21ef7b2c 531 first_player_ready = true;
wminix3 10:1eeb21ef7b2c 532 second_player_ready = true;
wminix3 10:1eeb21ef7b2c 533 } else if (begin_game2 && rx == 'W') {
wminix3 10:1eeb21ef7b2c 534 secondMbed.putc(rx);
wminix3 10:1eeb21ef7b2c 535 first_player_ready = false;
wminix3 10:1eeb21ef7b2c 536 second_player_ready = false;
wminix3 10:1eeb21ef7b2c 537 two_player_lose = true;
wminix3 10:1eeb21ef7b2c 538 }
wminix3 10:1eeb21ef7b2c 539 }
wminix3 10:1eeb21ef7b2c 540 if (begin_game2 && two_player_win) {
wminix3 10:1eeb21ef7b2c 541 secondMbed.putc('W');
wminix3 10:1eeb21ef7b2c 542 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 543 rx = secondMbed.getc();
wminix3 10:1eeb21ef7b2c 544 if (rx == 'W') {
wminix3 10:1eeb21ef7b2c 545 begin_game2 = false;
wminix3 10:1eeb21ef7b2c 546 first_player_ready = false;
wminix3 10:1eeb21ef7b2c 547 second_player_ready = false;
wminix3 10:1eeb21ef7b2c 548 }
wminix3 10:1eeb21ef7b2c 549 }
wminix3 10:1eeb21ef7b2c 550 Thread::wait(1000);
wminix3 10:1eeb21ef7b2c 551 }
wminix3 10:1eeb21ef7b2c 552 }
wminix3 10:1eeb21ef7b2c 553 */
wminix3 10:1eeb21ef7b2c 554
wminix3 10:1eeb21ef7b2c 555 // UNCOMMENT THIS THREAD IF FIRST PLAYER AND COMMENT SLAVE THREAD
wminix3 10:1eeb21ef7b2c 556 // The master mbed device (second player) should uncomment this thread -- Brice
wminix3 10:1eeb21ef7b2c 557 void mbedMaster(void const *args) {
wminix3 10:1eeb21ef7b2c 558 char rx;
wminix3 10:1eeb21ef7b2c 559 while(1) {
wminix3 10:1eeb21ef7b2c 560 while(numPlayers == 1) Thread::yield();
wminix3 10:1eeb21ef7b2c 561 rx = '0';
wminix3 10:1eeb21ef7b2c 562 if (!begin_game2) {
wminix3 10:1eeb21ef7b2c 563 secondMbed.putc('S');
wminix3 10:1eeb21ef7b2c 564 while(!secondMbed.readable()) wait(0.5); // okay to lock up until can confirm game is ready. --Brice
wminix3 10:1eeb21ef7b2c 565 rx = secondMbed.getc();
wminix3 10:1eeb21ef7b2c 566 if (rx == 'S') {
wminix3 10:1eeb21ef7b2c 567 first_player_ready = true;
wminix3 10:1eeb21ef7b2c 568 second_player_ready = true;
wminix3 10:1eeb21ef7b2c 569 }
wminix3 10:1eeb21ef7b2c 570 } else {
wminix3 10:1eeb21ef7b2c 571 if (secondMbed.readable()) {
wminix3 10:1eeb21ef7b2c 572 rx = secondMbed.getc();
wminix3 10:1eeb21ef7b2c 573 if (rx == 'W') {
wminix3 10:1eeb21ef7b2c 574 secondMbed.putc(rx);
wminix3 10:1eeb21ef7b2c 575 first_player_ready = false;
wminix3 10:1eeb21ef7b2c 576 second_player_ready = false;
wminix3 10:1eeb21ef7b2c 577 two_player_lose = true;
wminix3 10:1eeb21ef7b2c 578 }
wminix3 10:1eeb21ef7b2c 579 }
wminix3 10:1eeb21ef7b2c 580 if (two_player_win) {
wminix3 10:1eeb21ef7b2c 581 secondMbed.putc('W');
wminix3 10:1eeb21ef7b2c 582 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 583 rx = secondMbed.getc();
wminix3 10:1eeb21ef7b2c 584 if (rx == 'W') {
wminix3 10:1eeb21ef7b2c 585 begin_game2 = false;
wminix3 10:1eeb21ef7b2c 586 first_player_ready = false;
wminix3 10:1eeb21ef7b2c 587 second_player_ready = false;
wminix3 10:1eeb21ef7b2c 588 }
wminix3 10:1eeb21ef7b2c 589 }
wminix3 10:1eeb21ef7b2c 590 }
wminix3 10:1eeb21ef7b2c 591 Thread::wait(1000);
wminix3 4:739f6e0dd8af 592 }
wminix3 4:739f6e0dd8af 593 }
wminix3 4:739f6e0dd8af 594
michaeljson 0:3817adfaeb06 595 int main() {
michaeljson 0:3817adfaeb06 596
michaeljson 0:3817adfaeb06 597 // Initialization of Setup variables
michaeljson 0:3817adfaeb06 598 int blk_x, blk_y;
michaeljson 0:3817adfaeb06 599 pb.mode(PullUp);
michaeljson 0:3817adfaeb06 600
michaeljson 0:3817adfaeb06 601 Thread thread(playstart); // intializes the thread to play sound
wminix3 10:1eeb21ef7b2c 602 // Should only have the Slave thread uncommented if second player.
wminix3 10:1eeb21ef7b2c 603 // Should only have the Master thread uncommented if first player.
wminix3 10:1eeb21ef7b2c 604 //Thread thread2(mbedSlave); // uncommented if second player -- Brice
wminix3 10:1eeb21ef7b2c 605 Thread thread3(mbedMaster); // uncommented if first player -- Brice
michaeljson 0:3817adfaeb06 606 uLCD.baudrate(500000); // set to 500000 to increase smooth gameplay
michaeljson 0:3817adfaeb06 607
michaeljson 0:3817adfaeb06 608 // Initialization of Game Menu variables
michaeljson 0:3817adfaeb06 609 const int title_x_pos = 2; // initial x-pos of title
michaeljson 0:3817adfaeb06 610 const int title_y_pos = 3; // initial y-pos of title
michaeljson 0:3817adfaeb06 611 int start_label_x_pos = 7; // start label x-pos
michaeljson 0:3817adfaeb06 612 int start_label_y_pos = 7; // start label y-pos
michaeljson 0:3817adfaeb06 613 int level_cursor_x_pos = 5; // level cursor x-position
wminix3 2:4a3f97570578 614 int level_cursor_y_pos_start = 7; // level cursor y-position
wminix3 2:4a3f97570578 615 //int level_cursor_y_pos = 7; // initial level_cursor_y_pos @ start -- Added by Brice for more menu options
wminix3 2:4a3f97570578 616 int level_cursor_y_pos_end = 8; // BOTTOM CURSOR POS -- Added by Brice for more menu options
michaeljson 0:3817adfaeb06 617 int gameover_x_pos = 5; // gameover label x-position
michaeljson 0:3817adfaeb06 618 int gameover_y_pos = 5; // gameover label y-position
michaeljson 0:3817adfaeb06 619 int win_x_pos = 2; // congratulations label x-position
michaeljson 0:3817adfaeb06 620 int win_y_pos = 5; // congratulations label y-position
michaeljson 0:3817adfaeb06 621 int startover_x_pos = 3; // startover label x-position
michaeljson 0:3817adfaeb06 622 int startover_y_pos = 8; // startover label y-position
michaeljson 0:3817adfaeb06 623
michaeljson 0:3817adfaeb06 624 // intialize temporary score and current score
michaeljson 0:3817adfaeb06 625 int temp = 0;
michaeljson 0:3817adfaeb06 626 int score = 0;
michaeljson 0:3817adfaeb06 627
wminix3 2:4a3f97570578 628 // Additional globals added for two-player and one-player capabilities (by Brice)
wminix3 2:4a3f97570578 629
michaeljson 0:3817adfaeb06 630 // Begin game loop
michaeljson 0:3817adfaeb06 631 while(1)
michaeljson 0:3817adfaeb06 632 {
michaeljson 0:3817adfaeb06 633 // initialize all starting conditions for the beginning of the game
michaeljson 0:3817adfaeb06 634 game_menu = true; // defaults to display menu screen
michaeljson 0:3817adfaeb06 635 ENEMY_MOVE = true; // defaults to move enemy
michaeljson 0:3817adfaeb06 636 DIRECTION = 1; // default to move right
michaeljson 0:3817adfaeb06 637 hit_player = 0; // default to not player hit
michaeljson 0:3817adfaeb06 638 MOVE_DOWN = 0; // default to not move down
michaeljson 0:3817adfaeb06 639 lose = false; // default to not lose
michaeljson 0:3817adfaeb06 640 lives = 3; // defaults to 3 lives
michaeljson 0:3817adfaeb06 641 score = 0; // default to score of 0
wminix3 2:4a3f97570578 642 int level_cursor_y_pos = 7; // initial level_cursor_y_pos @ start -- Added by Brice for more menu options
michaeljson 0:3817adfaeb06 643 uLCD.cls();
wminix3 2:4a3f97570578 644 // Brice moved this out of the loop since it shouldn't change
wminix3 2:4a3f97570578 645 //uLCD.locate(title_x_pos,title_y_pos); // "SPACE INVADERS" title position
wminix3 2:4a3f97570578 646 //uLCD.printf("SPACE INVADERS"); // Title
michaeljson 0:3817adfaeb06 647 // Implementation of Game Menu
michaeljson 0:3817adfaeb06 648 while(game_menu)
michaeljson 0:3817adfaeb06 649 {
wminix3 2:4a3f97570578 650 // Brice added this in order to move the cursor through the menu options
wminix3 2:4a3f97570578 651 uLCD.locate(level_cursor_x_pos, level_cursor_y_pos);
wminix3 2:4a3f97570578 652 uLCD.printf(" ");
wminix3 2:4a3f97570578 653 if (myNav.down() && level_cursor_y_pos < level_cursor_y_pos_end) {
wminix3 2:4a3f97570578 654 level_cursor_y_pos += 1;
wminix3 2:4a3f97570578 655 } else if (myNav.up() && level_cursor_y_pos > level_cursor_y_pos_start) {
wminix3 2:4a3f97570578 656 level_cursor_y_pos -= 1;
wminix3 2:4a3f97570578 657 }
wminix3 2:4a3f97570578 658 // end of movable cursor
michaeljson 0:3817adfaeb06 659 uLCD.locate(level_cursor_x_pos,level_cursor_y_pos); // draws cursor next to "START" label
michaeljson 0:3817adfaeb06 660 uLCD.printf("->");
michaeljson 0:3817adfaeb06 661
michaeljson 0:3817adfaeb06 662 uLCD.locate(title_x_pos,title_y_pos); // "SPACE INVADERS" title position
michaeljson 0:3817adfaeb06 663 uLCD.printf("SPACE INVADERS"); // Title
michaeljson 0:3817adfaeb06 664
michaeljson 0:3817adfaeb06 665 uLCD.locate(start_label_x_pos,start_label_y_pos); // "START" label position
wminix3 2:4a3f97570578 666 uLCD.printf("ONE-PLAYER");
wminix3 2:4a3f97570578 667
wminix3 2:4a3f97570578 668 uLCD.locate(start_label_x_pos,start_label_y_pos + 1);
wminix3 2:4a3f97570578 669 uLCD.printf("TWO-PLAYER");
michaeljson 0:3817adfaeb06 670 // if pushbutton is pressed, game menu is exited and game begins
michaeljson 0:3817adfaeb06 671 if(!pb)
michaeljson 0:3817adfaeb06 672 {
michaeljson 0:3817adfaeb06 673 game_menu = false;
wminix3 2:4a3f97570578 674 if (level_cursor_y_pos == start_label_y_pos) {
wminix3 2:4a3f97570578 675 numPlayers = 1;
wminix3 2:4a3f97570578 676 } else if (level_cursor_y_pos == start_label_y_pos + 1) {
wminix3 2:4a3f97570578 677 numPlayers = 2;
wminix3 6:c44055f94cc3 678 pc.printf("num players: 2");
wminix3 2:4a3f97570578 679 }
wminix3 3:e6c081c7f6f1 680 Thread::wait(500); // changed this to Thread::wait ... originally wait(0.5);
michaeljson 0:3817adfaeb06 681 }
michaeljson 0:3817adfaeb06 682 }
wminix3 4:739f6e0dd8af 683 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 684 if (numPlayers == 2 && first_player_ready && second_player_ready) {
wminix3 5:b7934866b264 685 begin_game2 = true;
wminix3 5:b7934866b264 686 numWins = 0;
wminix3 5:b7934866b264 687 } else {
wminix3 5:b7934866b264 688 begin_game = true; // defaults begin_game to true
wminix3 5:b7934866b264 689 }
michaeljson 0:3817adfaeb06 690
michaeljson 0:3817adfaeb06 691 uLCD.cls();
michaeljson 0:3817adfaeb06 692
michaeljson 0:3817adfaeb06 693 // Draw the enemies
michaeljson 0:3817adfaeb06 694 draw_enemies_level();
michaeljson 0:3817adfaeb06 695
michaeljson 0:3817adfaeb06 696 // Draw the player
michaeljson 0:3817adfaeb06 697 draw_initial_player();
michaeljson 0:3817adfaeb06 698
michaeljson 0:3817adfaeb06 699 // sets the initial position of player missile and enemy missile (later updated)
michaeljson 0:3817adfaeb06 700 blk_x = player.player_blk_x+(player.player_width/2);
michaeljson 0:3817adfaeb06 701 blk_y = player.player_blk_y;
michaeljson 0:3817adfaeb06 702 missile_init(&missile, blk_x, blk_y, WHITE);
michaeljson 1:618aa2c4ca6a 703 int e_blk_x = 0;
michaeljson 1:618aa2c4ca6a 704 int e_blk_y = 2;
michaeljson 1:618aa2c4ca6a 705 enemy_missile_init(&enemy_missile, e_blk_x, e_blk_y, WHITE);
michaeljson 0:3817adfaeb06 706
michaeljson 0:3817adfaeb06 707 // prints lives
michaeljson 0:3817adfaeb06 708 uLCD.locate(0,0);
michaeljson 0:3817adfaeb06 709 uLCD.printf("Lives:%i", lives);
michaeljson 0:3817adfaeb06 710
michaeljson 0:3817adfaeb06 711 // prints score
michaeljson 0:3817adfaeb06 712 uLCD.locate(9,0);
michaeljson 0:3817adfaeb06 713 uLCD.printf("Score:%i", score);
michaeljson 0:3817adfaeb06 714
michaeljson 0:3817adfaeb06 715 // game play loop
michaeljson 0:3817adfaeb06 716 while(begin_game)
michaeljson 0:3817adfaeb06 717 {
michaeljson 0:3817adfaeb06 718 // updates score
michaeljson 0:3817adfaeb06 719 temp = score;
michaeljson 0:3817adfaeb06 720 score = (15-numOfEnemies)*15;
michaeljson 0:3817adfaeb06 721
michaeljson 0:3817adfaeb06 722 // prints score if score changes
michaeljson 0:3817adfaeb06 723 if (score != temp)
michaeljson 0:3817adfaeb06 724 {
michaeljson 0:3817adfaeb06 725 uLCD.locate(9,0);
michaeljson 0:3817adfaeb06 726 uLCD.printf("Score:%i", score);
michaeljson 0:3817adfaeb06 727 }
michaeljson 0:3817adfaeb06 728
michaeljson 0:3817adfaeb06 729 // move enemy
michaeljson 0:3817adfaeb06 730 enemy_motion();
michaeljson 0:3817adfaeb06 731
michaeljson 0:3817adfaeb06 732 // checks if player missile passes y-pos of row1
michaeljson 0:3817adfaeb06 733 if (missile.missile_blk_y+1-missile.missile_height <= enemy_1.enemy_blk_y
michaeljson 0:3817adfaeb06 734 && missile.missile_blk_y+1-missile.missile_height >= enemy_1.enemy_blk_y-enemy_1.enemy_height)
michaeljson 0:3817adfaeb06 735 {
michaeljson 0:3817adfaeb06 736 check_hit_enemy_row1();
michaeljson 0:3817adfaeb06 737 }
michaeljson 0:3817adfaeb06 738
michaeljson 0:3817adfaeb06 739 // checks if player missile passes y-pos of row2
michaeljson 0:3817adfaeb06 740 if (missile.missile_blk_y+1-missile.missile_height <= enemy_6.enemy_blk_y
michaeljson 0:3817adfaeb06 741 && missile.missile_blk_y+1-missile.missile_height >= enemy_6.enemy_blk_y-enemy_6.enemy_height)
michaeljson 0:3817adfaeb06 742 {
michaeljson 0:3817adfaeb06 743 check_hit_enemy_row2();
michaeljson 0:3817adfaeb06 744 }
michaeljson 0:3817adfaeb06 745
michaeljson 0:3817adfaeb06 746 // checks if player missile passes y-pos of row3
michaeljson 0:3817adfaeb06 747 if (missile.missile_blk_y+1-missile.missile_height <= enemy_11.enemy_blk_y
michaeljson 0:3817adfaeb06 748 && missile.missile_blk_y+1-missile.missile_height >= enemy_11.enemy_blk_y-enemy_11.enemy_height)
michaeljson 0:3817adfaeb06 749 {
michaeljson 0:3817adfaeb06 750 check_hit_enemy_row3();
michaeljson 0:3817adfaeb06 751 }
michaeljson 0:3817adfaeb06 752
michaeljson 0:3817adfaeb06 753 // Random Enemy Fire
michaeljson 0:3817adfaeb06 754 if (enemy_missile.status == ENEMY_MISSILE_INACTIVE)
michaeljson 0:3817adfaeb06 755 {
michaeljson 0:3817adfaeb06 756 random_attack_gen();
michaeljson 0:3817adfaeb06 757 }
michaeljson 0:3817adfaeb06 758
michaeljson 0:3817adfaeb06 759 // checks if enemy missile passes y-pos of player
michaeljson 0:3817adfaeb06 760 if (enemy_missile.missile_blk_y >= player.player_blk_y
michaeljson 0:3817adfaeb06 761 && enemy_missile.missile_blk_y <= player.player_blk_y+player.player_height)
michaeljson 0:3817adfaeb06 762 {
michaeljson 0:3817adfaeb06 763 check_player_hit();
michaeljson 0:3817adfaeb06 764 }
michaeljson 0:3817adfaeb06 765
michaeljson 0:3817adfaeb06 766 update_missile_pos(&missile); // updates player missile position
michaeljson 0:3817adfaeb06 767 update_enemy_missile_pos(&enemy_missile); // updates enemy missile position
michaeljson 0:3817adfaeb06 768
michaeljson 0:3817adfaeb06 769 // Player Movement checked with navigation switch
michaeljson 0:3817adfaeb06 770 if (myNav.left() && ((player.player_blk_x-3) > 0))
michaeljson 0:3817adfaeb06 771 {
michaeljson 0:3817adfaeb06 772 player_erase(&player);
michaeljson 0:3817adfaeb06 773 player.player_blk_x -= 3;
michaeljson 0:3817adfaeb06 774 player_show(&player);
michaeljson 0:3817adfaeb06 775 }
michaeljson 0:3817adfaeb06 776 else if (myNav.right() && ((player.player_blk_x+3) < (128-player.player_width)))
michaeljson 0:3817adfaeb06 777 {
michaeljson 0:3817adfaeb06 778 player_erase(&player);
michaeljson 0:3817adfaeb06 779 player.player_blk_x += 3;
michaeljson 0:3817adfaeb06 780 player_show(&player);
michaeljson 0:3817adfaeb06 781 }
michaeljson 0:3817adfaeb06 782
michaeljson 0:3817adfaeb06 783 // Player Fire
michaeljson 0:3817adfaeb06 784 if (pb == 0 && missile.status == PLAYER_MISSILE_INACTIVE)
michaeljson 0:3817adfaeb06 785 {
michaeljson 0:3817adfaeb06 786 missile.missile_blk_x = player.player_blk_x+(player.player_width/2);
michaeljson 0:3817adfaeb06 787 missile.missile_blk_y = player.player_blk_y;
michaeljson 0:3817adfaeb06 788 missile.status = PLAYER_MISSILE_ACTIVE;
michaeljson 0:3817adfaeb06 789 }
michaeljson 0:3817adfaeb06 790
michaeljson 0:3817adfaeb06 791 // checks if player destroyed all enemies
michaeljson 0:3817adfaeb06 792 if (numOfEnemies == 0)
michaeljson 0:3817adfaeb06 793 {
michaeljson 0:3817adfaeb06 794 uLCD.cls();
michaeljson 0:3817adfaeb06 795
michaeljson 0:3817adfaeb06 796 bool win = true; // sets win to true, for win screen
michaeljson 0:3817adfaeb06 797 begin_game = false;
michaeljson 0:3817adfaeb06 798
michaeljson 0:3817adfaeb06 799 // displays video clip
michaeljson 0:3817adfaeb06 800 uLCD.cls();
michaeljson 0:3817adfaeb06 801 uLCD.media_init();
michaeljson 0:3817adfaeb06 802 uLCD.set_sector_address(0x00, 0x00);
michaeljson 0:3817adfaeb06 803 uLCD.display_video(0,0);
wminix3 5:b7934866b264 804 Thread::wait(1000); // changed from wait(1) to Thread::wait(1000) since we're using threads -- Brice
michaeljson 0:3817adfaeb06 805
michaeljson 0:3817adfaeb06 806 uLCD.cls();
michaeljson 0:3817adfaeb06 807
michaeljson 0:3817adfaeb06 808 // prints "Congratulations" on uLCD
michaeljson 0:3817adfaeb06 809 uLCD.locate(win_x_pos,win_y_pos);
michaeljson 0:3817adfaeb06 810 uLCD.printf("CONGRATULATIONS!");
michaeljson 0:3817adfaeb06 811
michaeljson 0:3817adfaeb06 812 // prints "Play Again?" and "Press pb..."
michaeljson 0:3817adfaeb06 813 uLCD.locate(startover_x_pos, startover_y_pos);
michaeljson 0:3817adfaeb06 814 uLCD.printf("Play Again?");
michaeljson 0:3817adfaeb06 815 uLCD.locate(startover_x_pos, startover_y_pos+1);
michaeljson 0:3817adfaeb06 816 uLCD.printf("Press pb...");
michaeljson 0:3817adfaeb06 817
michaeljson 0:3817adfaeb06 818 // waits at win screen until pushbutton is pressed
michaeljson 0:3817adfaeb06 819 while (win)
michaeljson 0:3817adfaeb06 820 {
michaeljson 0:3817adfaeb06 821 // if pb is pressed, reset game to start menu
michaeljson 0:3817adfaeb06 822 if (!pb)
michaeljson 0:3817adfaeb06 823 {
michaeljson 0:3817adfaeb06 824 win = false;
wminix3 5:b7934866b264 825 Thread::wait(500); // changed from wait(0.5) to Thread::wait(500) since we're using threads
michaeljson 0:3817adfaeb06 826 }
michaeljson 0:3817adfaeb06 827 }
michaeljson 0:3817adfaeb06 828
michaeljson 0:3817adfaeb06 829 }
michaeljson 0:3817adfaeb06 830
michaeljson 0:3817adfaeb06 831 // checks if player was hit
michaeljson 0:3817adfaeb06 832 if (hit_player)
michaeljson 0:3817adfaeb06 833 {
michaeljson 0:3817adfaeb06 834 // updates lives
michaeljson 0:3817adfaeb06 835 lives -= 1;
wminix3 5:b7934866b264 836 Thread::wait(500); // changed from wait(0.5) to Thread::wait since we're using threads -- Brice
michaeljson 0:3817adfaeb06 837 hit_player = 0;
michaeljson 0:3817adfaeb06 838 player_show(&player);
michaeljson 0:3817adfaeb06 839 player.status = PLAYER_ALIVE;
michaeljson 0:3817adfaeb06 840
michaeljson 0:3817adfaeb06 841 // prints updated lives number
michaeljson 0:3817adfaeb06 842 uLCD.locate(0,0);
michaeljson 0:3817adfaeb06 843 uLCD.printf("Lives:%i", lives);
michaeljson 0:3817adfaeb06 844 }
michaeljson 0:3817adfaeb06 845
michaeljson 0:3817adfaeb06 846 // if player loses all lives or enemy reaches the player
michaeljson 0:3817adfaeb06 847 if (lose || lives == 0)
michaeljson 0:3817adfaeb06 848 {
michaeljson 0:3817adfaeb06 849 begin_game = false; // set to false to end game
michaeljson 0:3817adfaeb06 850 uLCD.cls();
michaeljson 0:3817adfaeb06 851
michaeljson 0:3817adfaeb06 852 gameover = true; // set to go to display gameover screen
michaeljson 0:3817adfaeb06 853
michaeljson 0:3817adfaeb06 854 // prints "GAMEOVER" to uLCD
michaeljson 0:3817adfaeb06 855 uLCD.locate(gameover_x_pos, gameover_y_pos);
michaeljson 0:3817adfaeb06 856 uLCD.printf("GAMEOVER");
wminix3 5:b7934866b264 857 Thread::wait(1000); // changed from wait(1) to thread::wait since we're using threads -- Brice
michaeljson 0:3817adfaeb06 858
michaeljson 0:3817adfaeb06 859 // prints "Play Again?" and "Press pb..."
michaeljson 0:3817adfaeb06 860 uLCD.locate(startover_x_pos, startover_y_pos);
michaeljson 0:3817adfaeb06 861 uLCD.printf("Play Again?");
michaeljson 0:3817adfaeb06 862 uLCD.locate(startover_x_pos, startover_y_pos+1);
michaeljson 0:3817adfaeb06 863 uLCD.printf("Press pb...");
michaeljson 0:3817adfaeb06 864
michaeljson 0:3817adfaeb06 865 // stays in gameover screen until pb is pressed
michaeljson 0:3817adfaeb06 866 while (gameover)
michaeljson 0:3817adfaeb06 867 {
michaeljson 0:3817adfaeb06 868 // if pb is pressed, game is reset to the game menu screen
michaeljson 0:3817adfaeb06 869 if (!pb)
michaeljson 0:3817adfaeb06 870 {
michaeljson 0:3817adfaeb06 871 gameover = false;
michaeljson 0:3817adfaeb06 872 game_menu = true;
wminix3 5:b7934866b264 873 Thread::wait(500); // changed wait(0.5) to Thread::wait since we're using threads -- Brice
wminix3 5:b7934866b264 874 }
wminix3 5:b7934866b264 875 }
wminix3 5:b7934866b264 876 }
wminix3 5:b7934866b264 877
wminix3 5:b7934866b264 878 }
wminix3 5:b7934866b264 879 // game play loop
wminix3 5:b7934866b264 880 while(begin_game2)
wminix3 5:b7934866b264 881 {
wminix3 5:b7934866b264 882 // updates score
wminix3 5:b7934866b264 883 temp = score;
wminix3 5:b7934866b264 884 score = (15-numOfEnemies)*15;
wminix3 5:b7934866b264 885
wminix3 5:b7934866b264 886 // prints score if score changes
wminix3 5:b7934866b264 887 if (score != temp)
wminix3 5:b7934866b264 888 {
wminix3 5:b7934866b264 889 uLCD.locate(9,0);
wminix3 5:b7934866b264 890 uLCD.printf("Score:%i", score);
wminix3 5:b7934866b264 891 }
wminix3 5:b7934866b264 892
wminix3 5:b7934866b264 893 // move enemy
wminix3 5:b7934866b264 894 enemy_motion();
wminix3 5:b7934866b264 895
wminix3 5:b7934866b264 896 // checks if player missile passes y-pos of row1
wminix3 5:b7934866b264 897 if (missile.missile_blk_y+1-missile.missile_height <= enemy_1.enemy_blk_y
wminix3 5:b7934866b264 898 && missile.missile_blk_y+1-missile.missile_height >= enemy_1.enemy_blk_y-enemy_1.enemy_height)
wminix3 5:b7934866b264 899 {
wminix3 5:b7934866b264 900 check_hit_enemy_row1();
wminix3 5:b7934866b264 901 }
wminix3 5:b7934866b264 902
wminix3 5:b7934866b264 903 // checks if player missile passes y-pos of row2
wminix3 5:b7934866b264 904 if (missile.missile_blk_y+1-missile.missile_height <= enemy_6.enemy_blk_y
wminix3 5:b7934866b264 905 && missile.missile_blk_y+1-missile.missile_height >= enemy_6.enemy_blk_y-enemy_6.enemy_height)
wminix3 5:b7934866b264 906 {
wminix3 5:b7934866b264 907 check_hit_enemy_row2();
wminix3 5:b7934866b264 908 }
wminix3 5:b7934866b264 909
wminix3 5:b7934866b264 910 // checks if player missile passes y-pos of row3
wminix3 5:b7934866b264 911 if (missile.missile_blk_y+1-missile.missile_height <= enemy_11.enemy_blk_y
wminix3 5:b7934866b264 912 && missile.missile_blk_y+1-missile.missile_height >= enemy_11.enemy_blk_y-enemy_11.enemy_height)
wminix3 5:b7934866b264 913 {
wminix3 5:b7934866b264 914 check_hit_enemy_row3();
wminix3 5:b7934866b264 915 }
wminix3 5:b7934866b264 916
wminix3 5:b7934866b264 917 // Random Enemy Fire
wminix3 5:b7934866b264 918 if (enemy_missile.status == ENEMY_MISSILE_INACTIVE)
wminix3 5:b7934866b264 919 {
wminix3 5:b7934866b264 920 random_attack_gen();
wminix3 5:b7934866b264 921 }
wminix3 5:b7934866b264 922
wminix3 5:b7934866b264 923 // checks if enemy missile passes y-pos of player
wminix3 5:b7934866b264 924 if (enemy_missile.missile_blk_y >= player.player_blk_y
wminix3 5:b7934866b264 925 && enemy_missile.missile_blk_y <= player.player_blk_y+player.player_height)
wminix3 5:b7934866b264 926 {
wminix3 5:b7934866b264 927 check_player_hit();
wminix3 5:b7934866b264 928 }
wminix3 5:b7934866b264 929
wminix3 5:b7934866b264 930 update_missile_pos(&missile); // updates player missile position
wminix3 5:b7934866b264 931 update_enemy_missile_pos(&enemy_missile); // updates enemy missile position
wminix3 5:b7934866b264 932
wminix3 5:b7934866b264 933 // Player Movement checked with navigation switch
wminix3 5:b7934866b264 934 if (myNav.left() && ((player.player_blk_x-3) > 0))
wminix3 5:b7934866b264 935 {
wminix3 5:b7934866b264 936 player_erase(&player);
wminix3 5:b7934866b264 937 player.player_blk_x -= 3;
wminix3 5:b7934866b264 938 player_show(&player);
wminix3 5:b7934866b264 939 }
wminix3 5:b7934866b264 940 else if (myNav.right() && ((player.player_blk_x+3) < (128-player.player_width)))
wminix3 5:b7934866b264 941 {
wminix3 5:b7934866b264 942 player_erase(&player);
wminix3 5:b7934866b264 943 player.player_blk_x += 3;
wminix3 5:b7934866b264 944 player_show(&player);
wminix3 5:b7934866b264 945 }
wminix3 5:b7934866b264 946
wminix3 5:b7934866b264 947 // Player Fire
wminix3 5:b7934866b264 948 if (pb == 0 && missile.status == PLAYER_MISSILE_INACTIVE)
wminix3 5:b7934866b264 949 {
wminix3 5:b7934866b264 950 missile.missile_blk_x = player.player_blk_x+(player.player_width/2);
wminix3 5:b7934866b264 951 missile.missile_blk_y = player.player_blk_y;
wminix3 5:b7934866b264 952 missile.status = PLAYER_MISSILE_ACTIVE;
wminix3 5:b7934866b264 953 }
wminix3 5:b7934866b264 954
wminix3 5:b7934866b264 955 // checks if player destroyed all enemies
wminix3 5:b7934866b264 956 if (numOfEnemies == 0)
wminix3 5:b7934866b264 957 {
wminix3 5:b7934866b264 958 uLCD.cls();
wminix3 5:b7934866b264 959
wminix3 5:b7934866b264 960 bool win = true; // sets win to true, for win screen
wminix3 5:b7934866b264 961 numWins += 1;
wminix3 5:b7934866b264 962 if (numWins == 3) {
wminix3 10:1eeb21ef7b2c 963 //begin_game2 = false; // Allow the mbed communication thread to change the begin_game2 bool to false after letting other mbed know.
wminix3 5:b7934866b264 964 two_player_win = true;
wminix3 5:b7934866b264 965 }
wminix3 5:b7934866b264 966
wminix3 5:b7934866b264 967 // displays video clip
wminix3 5:b7934866b264 968 uLCD.cls();
wminix3 5:b7934866b264 969 uLCD.media_init();
wminix3 5:b7934866b264 970 uLCD.set_sector_address(0x00, 0x00);
wminix3 5:b7934866b264 971 uLCD.display_video(0,0);
wminix3 5:b7934866b264 972 Thread::wait(1000); // changed from wait(1)
wminix3 5:b7934866b264 973
wminix3 5:b7934866b264 974 uLCD.cls();
wminix3 5:b7934866b264 975 if (!two_player_win) {
wminix3 5:b7934866b264 976 // prints "Number of Wins" on uLCD -- Brice. A step towards victory, not a complete victory.
wminix3 5:b7934866b264 977 uLCD.locate(win_x_pos,win_y_pos);
wminix3 5:b7934866b264 978 uLCD.printf("YOU HAVE %d WINS!", numWins);
wminix3 5:b7934866b264 979
wminix3 5:b7934866b264 980 // prints "Continue?" and "Press pb..." Keep trying to get points --Brice.
wminix3 5:b7934866b264 981 uLCD.locate(startover_x_pos, startover_y_pos);
wminix3 5:b7934866b264 982 uLCD.printf("Continue?");
wminix3 5:b7934866b264 983 uLCD.locate(startover_x_pos, startover_y_pos+1);
wminix3 5:b7934866b264 984 uLCD.printf("Press pb...");
wminix3 5:b7934866b264 985
wminix3 5:b7934866b264 986 // waits at win screen until pushbutton is pressed
wminix3 5:b7934866b264 987 while (win)
wminix3 5:b7934866b264 988 {
wminix3 5:b7934866b264 989 // if pb is pressed, reset game to start menu
wminix3 5:b7934866b264 990 if (!pb)
wminix3 5:b7934866b264 991 {
wminix3 5:b7934866b264 992 win = false;
wminix3 5:b7934866b264 993 Thread::wait(500); // changed from wait(0.5) since we have threads -- Brice
wminix3 5:b7934866b264 994 }
wminix3 5:b7934866b264 995 }
wminix3 5:b7934866b264 996 } else {
wminix3 5:b7934866b264 997 // prints CONGRATULATIONS! since player has won. --Brice
wminix3 5:b7934866b264 998 uLCD.locate(win_x_pos,win_y_pos);
wminix3 5:b7934866b264 999 uLCD.printf("CONGRATULATIONS!");
wminix3 5:b7934866b264 1000
wminix3 5:b7934866b264 1001 // prints "Return to menu?" and "Press pb..." --Brice.
wminix3 5:b7934866b264 1002 uLCD.locate(startover_x_pos, startover_y_pos);
wminix3 5:b7934866b264 1003 uLCD.printf("Return to menu?");
wminix3 5:b7934866b264 1004 uLCD.locate(startover_x_pos, startover_y_pos+1);
wminix3 5:b7934866b264 1005 uLCD.printf("Press pb...");
wminix3 5:b7934866b264 1006
wminix3 5:b7934866b264 1007 // waits at win screen until pushbutton is pressed
wminix3 5:b7934866b264 1008 while (two_player_win)
wminix3 5:b7934866b264 1009 {
wminix3 5:b7934866b264 1010 // if pb is pressed, reset game to start menu
wminix3 5:b7934866b264 1011 if (!pb)
wminix3 5:b7934866b264 1012 {
wminix3 5:b7934866b264 1013 two_player_win = false; // close win -- Brice
wminix3 5:b7934866b264 1014 game_menu = true; // go back to menu -- Brice
wminix3 5:b7934866b264 1015 Thread::wait(500); // changed from wait(0.5) since we have threads -- Brice
wminix3 5:b7934866b264 1016 }
wminix3 5:b7934866b264 1017 }
wminix3 5:b7934866b264 1018 }
wminix3 5:b7934866b264 1019 }
wminix3 5:b7934866b264 1020
wminix3 5:b7934866b264 1021 // checks if player was hit
wminix3 5:b7934866b264 1022 if (hit_player)
wminix3 5:b7934866b264 1023 {
wminix3 5:b7934866b264 1024 // updates lives
wminix3 5:b7934866b264 1025 lives -= 1;
wminix3 5:b7934866b264 1026 Thread::wait(500); // changed from wait(0.5) since we're using threads --Brice
wminix3 5:b7934866b264 1027 hit_player = 0;
wminix3 5:b7934866b264 1028 player_show(&player);
wminix3 5:b7934866b264 1029 player.status = PLAYER_ALIVE;
wminix3 5:b7934866b264 1030
wminix3 5:b7934866b264 1031 // prints updated lives number
wminix3 5:b7934866b264 1032 uLCD.locate(0,0);
wminix3 5:b7934866b264 1033 uLCD.printf("Lives:%i", lives);
wminix3 5:b7934866b264 1034 }
wminix3 5:b7934866b264 1035
wminix3 5:b7934866b264 1036 // if player loses all lives or enemy reaches the player
wminix3 5:b7934866b264 1037 if (lose || lives == 0)
wminix3 5:b7934866b264 1038 {
wminix3 5:b7934866b264 1039 //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 1040 uLCD.cls();
wminix3 5:b7934866b264 1041
wminix3 5:b7934866b264 1042 gameover = true; // set to go to display gameover screen
wminix3 5:b7934866b264 1043
wminix3 5:b7934866b264 1044 // prints "GAMEOVER" to uLCD
wminix3 5:b7934866b264 1045 uLCD.locate(gameover_x_pos, gameover_y_pos);
wminix3 5:b7934866b264 1046 uLCD.printf("YOU DIED");
wminix3 5:b7934866b264 1047 Thread::wait(1000); // changed from wait(1) since we have multiple threads -- Brice
wminix3 5:b7934866b264 1048
wminix3 5:b7934866b264 1049 // prints "Play Again?" and "Press pb..."
wminix3 5:b7934866b264 1050 uLCD.locate(startover_x_pos, startover_y_pos);
wminix3 5:b7934866b264 1051 uLCD.printf("Keep trying!");
wminix3 5:b7934866b264 1052 uLCD.locate(startover_x_pos, startover_y_pos+1);
wminix3 5:b7934866b264 1053 uLCD.printf("Press pb...");
wminix3 5:b7934866b264 1054
wminix3 5:b7934866b264 1055 // stays in gameover screen until pb is pressed
wminix3 5:b7934866b264 1056 while (gameover)
wminix3 5:b7934866b264 1057 {
wminix3 5:b7934866b264 1058 // if pb is pressed, game is reset to the game menu screen
wminix3 5:b7934866b264 1059 if (!pb)
wminix3 5:b7934866b264 1060 {
wminix3 5:b7934866b264 1061 gameover = false;
wminix3 5:b7934866b264 1062 //game_menu = true; // removed since other player must win three times for game to be over.
wminix3 5:b7934866b264 1063 Thread::wait(500); // changed from wait(0.5) since we have threads.
wminix3 5:b7934866b264 1064 }
wminix3 5:b7934866b264 1065 }
wminix3 5:b7934866b264 1066 }
wminix3 5:b7934866b264 1067 if (two_player_lose)
wminix3 5:b7934866b264 1068 {
wminix3 5:b7934866b264 1069 begin_game2 = false; // set to false to end game. End game since other player won.
wminix3 5:b7934866b264 1070 uLCD.cls();
wminix3 5:b7934866b264 1071
wminix3 5:b7934866b264 1072 gameover = true; // set to go to display gameover screen
wminix3 5:b7934866b264 1073
wminix3 5:b7934866b264 1074 // prints "GAMEOVER" to uLCD
wminix3 5:b7934866b264 1075 uLCD.locate(gameover_x_pos, gameover_y_pos);
wminix3 5:b7934866b264 1076 uLCD.printf("GAMEOVER");
wminix3 5:b7934866b264 1077 Thread::wait(1000); // thread wait since we have multiple threads -- Brice
wminix3 5:b7934866b264 1078
wminix3 5:b7934866b264 1079 // prints "Return to menu?" and "Press pb..."
wminix3 5:b7934866b264 1080 uLCD.locate(startover_x_pos, startover_y_pos);
wminix3 5:b7934866b264 1081 uLCD.printf("Return to menu?");
wminix3 5:b7934866b264 1082 uLCD.locate(startover_x_pos, startover_y_pos+1);
wminix3 5:b7934866b264 1083 uLCD.printf("Press pb...");
wminix3 5:b7934866b264 1084
wminix3 5:b7934866b264 1085 // stays in gameover screen until pb is pressed
wminix3 5:b7934866b264 1086 while (gameover)
wminix3 5:b7934866b264 1087 {
wminix3 5:b7934866b264 1088 // if pb is pressed, game is reset to the game menu screen
wminix3 5:b7934866b264 1089 if (!pb)
wminix3 5:b7934866b264 1090 {
wminix3 5:b7934866b264 1091 gameover = false;
wminix3 5:b7934866b264 1092 two_player_lose = false; // end lose.
wminix3 5:b7934866b264 1093 game_menu = true; // removed since other player must win three times for game to be over.
wminix3 5:b7934866b264 1094 Thread::wait(500); // changed from wait(0.5) since we have threads.
michaeljson 0:3817adfaeb06 1095 }
michaeljson 0:3817adfaeb06 1096 }
michaeljson 0:3817adfaeb06 1097 }
michaeljson 0:3817adfaeb06 1098
michaeljson 0:3817adfaeb06 1099 }
michaeljson 0:3817adfaeb06 1100 }
michaeljson 0:3817adfaeb06 1101 }