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:
Sun Apr 18 16:04:18 2021 +0000
Revision:
11:cdea2c9ec531
Parent:
10:1eeb21ef7b2c
Child:
12:22aedb2598b1
Added LED lighting effects. The shot effect is hit or miss as it is written right now.

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