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:
Mon Apr 19 19:35:11 2021 +0000
Revision:
12:22aedb2598b1
Parent:
11:cdea2c9ec531
Child:
13:36cc024dcf6b
Updated slave thread a bit.

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 }
wminix3 12:22aedb2598b1 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 12:22aedb2598b1 458 */
wminix3 11:cdea2c9ec531 459
wminix3 11:cdea2c9ec531 460 // thread that adds RGB LED Lighting Effects that coincide with the game -- Brice
wminix3 11:cdea2c9ec531 461 void ledEffects(void const *args)//Th
wminix3 11:cdea2c9ec531 462 { //Depending on the state of the game,
wminix3 11:cdea2c9ec531 463 //generate different patterns/colors of lighting
wminix3 11:cdea2c9ec531 464 while(1) {
wminix3 11:cdea2c9ec531 465 // gradually increases and decreases each color independently. (A chill build up effect?)
wminix3 11:cdea2c9ec531 466 while(game_menu)
wminix3 11:cdea2c9ec531 467 {
wminix3 11:cdea2c9ec531 468 red = 0.0;
wminix3 11:cdea2c9ec531 469 green = 0.0;
wminix3 11:cdea2c9ec531 470 blue = 0.0;
wminix3 11:cdea2c9ec531 471 for (float i = 0.0; i < 0.5; i = i + 0.05) {
wminix3 11:cdea2c9ec531 472 red = i;
wminix3 11:cdea2c9ec531 473 Thread::wait(10);
wminix3 11:cdea2c9ec531 474 }
wminix3 11:cdea2c9ec531 475 red = 0.5;
wminix3 11:cdea2c9ec531 476 Thread::wait(300);
wminix3 11:cdea2c9ec531 477 for (float i = 0.5; i > 0.0; i = i - 0.05) {
wminix3 11:cdea2c9ec531 478 red = i;
wminix3 11:cdea2c9ec531 479 Thread::wait(10);
wminix3 11:cdea2c9ec531 480 }
wminix3 11:cdea2c9ec531 481 red = 0.0;
wminix3 11:cdea2c9ec531 482 for (float i = 0.0; i < 0.25; i = i + 0.05) {
wminix3 11:cdea2c9ec531 483 green = i;
wminix3 11:cdea2c9ec531 484 Thread::wait(10);
wminix3 11:cdea2c9ec531 485 }
wminix3 11:cdea2c9ec531 486 green = 0.25;
wminix3 11:cdea2c9ec531 487 Thread::wait(300);
wminix3 11:cdea2c9ec531 488 for (float i = 0.25; i > 0.0; i = i - 0.05) {
wminix3 11:cdea2c9ec531 489 green = i;
wminix3 11:cdea2c9ec531 490 Thread::wait(10);
wminix3 11:cdea2c9ec531 491 }
wminix3 11:cdea2c9ec531 492 green = 0.0;
wminix3 11:cdea2c9ec531 493 for (float i = 0.0; i < 0.5; i = i + 0.05) {
wminix3 11:cdea2c9ec531 494 blue = i;
wminix3 11:cdea2c9ec531 495 Thread::wait(10);
wminix3 11:cdea2c9ec531 496 }
wminix3 11:cdea2c9ec531 497 blue = 0.5;
wminix3 11:cdea2c9ec531 498 Thread::wait(300);
wminix3 11:cdea2c9ec531 499 for (float i = 0.5; i > 0.0; i = i - 0.05) {
wminix3 11:cdea2c9ec531 500 blue = i;
wminix3 11:cdea2c9ec531 501 Thread::wait(10);
wminix3 11:cdea2c9ec531 502 }
wminix3 11:cdea2c9ec531 503 blue = 0.0;
wminix3 11:cdea2c9ec531 504 }
wminix3 11:cdea2c9ec531 505 // Checks in game sound conditions
wminix3 11:cdea2c9ec531 506 while(begin_game || begin_game2) // added OR begin_game2 so that music/sounds play during one-player or two-player
wminix3 11:cdea2c9ec531 507 {
wminix3 11:cdea2c9ec531 508 // play firing sound when the player fires
wminix3 11:cdea2c9ec531 509 if(!pb && missile.status == PLAYER_MISSILE_INACTIVE) {
wminix3 11:cdea2c9ec531 510 red = 0.0;
wminix3 11:cdea2c9ec531 511 green = 0.0;
wminix3 11:cdea2c9ec531 512 blue = 0.0;
wminix3 11:cdea2c9ec531 513 red = 0.5;
wminix3 11:cdea2c9ec531 514 Thread::wait(200);
wminix3 11:cdea2c9ec531 515 green = 0.15;
wminix3 11:cdea2c9ec531 516 Thread::wait(200);
wminix3 11:cdea2c9ec531 517 red = 0.0;
wminix3 11:cdea2c9ec531 518 green = 0.0;
wminix3 11:cdea2c9ec531 519 }
wminix3 11:cdea2c9ec531 520
wminix3 11:cdea2c9ec531 521 // if player hit, play hit sound
wminix3 11:cdea2c9ec531 522 if (hit_player)
wminix3 11:cdea2c9ec531 523 {
wminix3 11:cdea2c9ec531 524 red = 0.0;
wminix3 11:cdea2c9ec531 525 green = 0.0;
wminix3 11:cdea2c9ec531 526 blue = 0.0;
wminix3 11:cdea2c9ec531 527 red = 0.5;
wminix3 11:cdea2c9ec531 528 Thread::wait(60);
wminix3 11:cdea2c9ec531 529 red = 0.0;
wminix3 11:cdea2c9ec531 530 Thread::wait(60);
wminix3 11:cdea2c9ec531 531 red = 0.5;
wminix3 11:cdea2c9ec531 532 Thread::wait(60);
wminix3 11:cdea2c9ec531 533 red = 0.0;
wminix3 11:cdea2c9ec531 534 Thread::wait(60);
wminix3 11:cdea2c9ec531 535 red = 0.5;
wminix3 11:cdea2c9ec531 536 Thread::wait(60);
wminix3 11:cdea2c9ec531 537 red = 0.0;
wminix3 11:cdea2c9ec531 538 }
wminix3 11:cdea2c9ec531 539 Thread::wait(500);
wminix3 11:cdea2c9ec531 540 }
wminix3 11:cdea2c9ec531 541
wminix3 11:cdea2c9ec531 542 // players gameover voice if player loses
wminix3 11:cdea2c9ec531 543 while(gameover)
wminix3 11:cdea2c9ec531 544 {
wminix3 11:cdea2c9ec531 545 for (float i = 0.0; i < 0.25; i = i + 0.05) {
wminix3 11:cdea2c9ec531 546 red = i;
wminix3 11:cdea2c9ec531 547 Thread::wait(10);
wminix3 11:cdea2c9ec531 548 }
wminix3 11:cdea2c9ec531 549 red = 0.25;
wminix3 11:cdea2c9ec531 550 Thread::wait(300);
wminix3 11:cdea2c9ec531 551 for (float i = 0.25; i > 0.0; i = i - 0.05) {
wminix3 11:cdea2c9ec531 552 red = i;
wminix3 11:cdea2c9ec531 553 Thread::wait(10);
wminix3 11:cdea2c9ec531 554 }
wminix3 11:cdea2c9ec531 555 red = 0.0;
wminix3 11:cdea2c9ec531 556 Thread::wait(500);
wminix3 11:cdea2c9ec531 557 }
wminix3 11:cdea2c9ec531 558 }
wminix3 11:cdea2c9ec531 559 }
wminix3 10:1eeb21ef7b2c 560 /*
wminix3 5:b7934866b264 561 // Thread added for mbed communication, which allows two-player -- Brice
wminix3 6:c44055f94cc3 562 void mbedSend(void const *args) {
wminix3 4:739f6e0dd8af 563 while(1) {
wminix3 5:b7934866b264 564 while(numPlayers == 1) Thread::yield(); // with one player, thread is unneeded and should yield.
wminix3 5:b7934866b264 565 while(!first_player_ready || !second_player_ready) { // while either player isn't ready, send a start code, and become ready
wminix3 4:739f6e0dd8af 566 //if (!first_player_ready || !second_player_ready) {
wminix3 8:27c4345be3db 567 mbedLock.lock();
wminix3 4:739f6e0dd8af 568 secondMbed.putc('S');
wminix3 8:27c4345be3db 569 mbedLock.unlock();
wminix3 4:739f6e0dd8af 570 first_player_ready = true;
wminix3 10:1eeb21ef7b2c 571 pc.printf("first player ready");
wminix3 7:9a826617be92 572 Thread::wait(1000);
wminix3 4:739f6e0dd8af 573 //}
wminix3 6:c44055f94cc3 574 // 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 575 // if (secondMbed.getc() == 'S') {
wminix3 6:c44055f94cc3 576 // second_player_ready = true;
wminix3 6:c44055f94cc3 577 // pc.printf("second play ready");
wminix3 6:c44055f94cc3 578 // }
wminix3 6:c44055f94cc3 579 // }
wminix3 6:c44055f94cc3 580 // Thread::wait(1000); // run once a second
wminix3 5:b7934866b264 581 }
wminix3 5:b7934866b264 582 if (two_player_win) { // if this player wins, notify the other mbed/player that they lost.
wminix3 8:27c4345be3db 583 mbedLock.lock();
wminix3 5:b7934866b264 584 secondMbed.putc('W');
wminix3 8:27c4345be3db 585 mbedLock.unlock();
wminix3 4:739f6e0dd8af 586 }
wminix3 6:c44055f94cc3 587 //if (secondMbed.readable()) {
wminix3 6:c44055f94cc3 588 // if (secondMbed.getc() == 'W') {
wminix3 6:c44055f94cc3 589 // two_player_lose = true;
wminix3 6:c44055f94cc3 590 // }
wminix3 6:c44055f94cc3 591 //}
wminix3 7:9a826617be92 592 Thread::wait(2000); // check twice a second for a win
wminix3 6:c44055f94cc3 593 }
wminix3 6:c44055f94cc3 594 }
wminix3 10:1eeb21ef7b2c 595 */
wminix3 10:1eeb21ef7b2c 596 /*
wminix3 6:c44055f94cc3 597 void mbedReceive(void const *args) {
wminix3 7:9a826617be92 598 char rx;
wminix3 6:c44055f94cc3 599 while(1) {
wminix3 10:1eeb21ef7b2c 600 rx = '0';
wminix3 8:27c4345be3db 601 mbedLock.lock();
wminix3 7:9a826617be92 602 while(numPlayers == 1 || !secondMbed.readable()) {
wminix3 9:c22613b0007a 603 mbedLock.unlock();
wminix3 10:1eeb21ef7b2c 604 pc.printf("yielding");
wminix3 7:9a826617be92 605 Thread::yield(); // with one player, thread is unneeded and should yield.
wminix3 7:9a826617be92 606 }
wminix3 7:9a826617be92 607 rx = secondMbed.getc();
wminix3 10:1eeb21ef7b2c 608 pc.printf("%c", rx);
wminix3 8:27c4345be3db 609 mbedLock.unlock();
wminix3 7:9a826617be92 610 if (rx == 'S') {
wminix3 7:9a826617be92 611 second_player_ready = true;
wminix3 10:1eeb21ef7b2c 612 pc.printf("second play ready");
wminix3 6:c44055f94cc3 613 }
wminix3 6:c44055f94cc3 614 // Thread::wait(1000); // run once a second
wminix3 6:c44055f94cc3 615 //}
wminix3 7:9a826617be92 616 if (rx == 'W') {
wminix3 6:c44055f94cc3 617 two_player_lose = true;
wminix3 5:b7934866b264 618 }
wminix3 10:1eeb21ef7b2c 619 Thread::wait(3000); // check twice a second for a win
wminix3 10:1eeb21ef7b2c 620 }
wminix3 10:1eeb21ef7b2c 621 }
wminix3 10:1eeb21ef7b2c 622 */
wminix3 10:1eeb21ef7b2c 623
wminix3 10:1eeb21ef7b2c 624 // UNCOMMENT THIS THREAD IF SECOND PLAYER AND COMMENT MASTER THREAD
wminix3 10:1eeb21ef7b2c 625 /*
wminix3 10:1eeb21ef7b2c 626 // The slave mbed device (second player) should uncomment this thread -- Brice
wminix3 10:1eeb21ef7b2c 627 void mbedSlave(void const *args) {
wminix3 10:1eeb21ef7b2c 628 char rx;
wminix3 10:1eeb21ef7b2c 629 while(1) {
wminix3 12:22aedb2598b1 630 //while(numPlayers == 1) Thread::yield();
wminix3 10:1eeb21ef7b2c 631 rx = '0';
wminix3 10:1eeb21ef7b2c 632 if (secondMbed.readable()) {
wminix3 10:1eeb21ef7b2c 633 rx = secondMbed.getc();
wminix3 10:1eeb21ef7b2c 634 if (!begin_game2 && rx == 'S') {
wminix3 12:22aedb2598b1 635 while (!secondMbed.writeable()) wait(0.5);
wminix3 10:1eeb21ef7b2c 636 secondMbed.putc(rx);
wminix3 10:1eeb21ef7b2c 637 first_player_ready = true;
wminix3 10:1eeb21ef7b2c 638 second_player_ready = true;
wminix3 12:22aedb2598b1 639 numPlayers = 2;
wminix3 10:1eeb21ef7b2c 640 } else if (begin_game2 && rx == 'W') {
wminix3 10:1eeb21ef7b2c 641 secondMbed.putc(rx);
wminix3 10:1eeb21ef7b2c 642 first_player_ready = false;
wminix3 10:1eeb21ef7b2c 643 second_player_ready = false;
wminix3 10:1eeb21ef7b2c 644 two_player_lose = true;
wminix3 10:1eeb21ef7b2c 645 }
wminix3 10:1eeb21ef7b2c 646 }
wminix3 10:1eeb21ef7b2c 647 if (begin_game2 && two_player_win) {
wminix3 10:1eeb21ef7b2c 648 secondMbed.putc('W');
wminix3 10:1eeb21ef7b2c 649 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 650 rx = secondMbed.getc();
wminix3 10:1eeb21ef7b2c 651 if (rx == 'W') {
wminix3 10:1eeb21ef7b2c 652 begin_game2 = false;
wminix3 10:1eeb21ef7b2c 653 first_player_ready = false;
wminix3 10:1eeb21ef7b2c 654 second_player_ready = false;
wminix3 10:1eeb21ef7b2c 655 }
wminix3 10:1eeb21ef7b2c 656 }
wminix3 10:1eeb21ef7b2c 657 Thread::wait(1000);
wminix3 10:1eeb21ef7b2c 658 }
wminix3 10:1eeb21ef7b2c 659 }
wminix3 10:1eeb21ef7b2c 660 */
wminix3 10:1eeb21ef7b2c 661
wminix3 10:1eeb21ef7b2c 662 // UNCOMMENT THIS THREAD IF FIRST PLAYER AND COMMENT SLAVE THREAD
wminix3 10:1eeb21ef7b2c 663 // The master mbed device (second player) should uncomment this thread -- Brice
wminix3 10:1eeb21ef7b2c 664 void mbedMaster(void const *args) {
wminix3 10:1eeb21ef7b2c 665 char rx;
wminix3 10:1eeb21ef7b2c 666 while(1) {
wminix3 10:1eeb21ef7b2c 667 while(numPlayers == 1) Thread::yield();
wminix3 10:1eeb21ef7b2c 668 rx = '0';
wminix3 10:1eeb21ef7b2c 669 if (!begin_game2) {
wminix3 12:22aedb2598b1 670 while(!secondMbed.writeable()) {
wminix3 12:22aedb2598b1 671 pc.printf("not writeable");
wminix3 12:22aedb2598b1 672 wait(0.5);
wminix3 12:22aedb2598b1 673 }
wminix3 10:1eeb21ef7b2c 674 secondMbed.putc('S');
wminix3 12:22aedb2598b1 675 while(!secondMbed.readable()) {
wminix3 12:22aedb2598b1 676 pc.printf("no read\n\r");
wminix3 12:22aedb2598b1 677 wait(0.5); // okay to lock up until can confirm game is ready. --Brice
wminix3 12:22aedb2598b1 678 }
wminix3 10:1eeb21ef7b2c 679 rx = secondMbed.getc();
wminix3 12:22aedb2598b1 680 pc.printf("rx = %c", rx);
wminix3 10:1eeb21ef7b2c 681 if (rx == 'S') {
wminix3 10:1eeb21ef7b2c 682 first_player_ready = true;
wminix3 10:1eeb21ef7b2c 683 second_player_ready = true;
wminix3 12:22aedb2598b1 684 pc.printf("both players ready");
wminix3 10:1eeb21ef7b2c 685 }
wminix3 12:22aedb2598b1 686 }
wminix3 12:22aedb2598b1 687 //} else {
wminix3 12:22aedb2598b1 688 while (first_player_ready && second_player_ready) {
wminix3 12:22aedb2598b1 689 rx = '0';
wminix3 10:1eeb21ef7b2c 690 if (secondMbed.readable()) {
wminix3 10:1eeb21ef7b2c 691 rx = secondMbed.getc();
wminix3 10:1eeb21ef7b2c 692 if (rx == 'W') {
wminix3 10:1eeb21ef7b2c 693 secondMbed.putc(rx);
wminix3 10:1eeb21ef7b2c 694 first_player_ready = false;
wminix3 10:1eeb21ef7b2c 695 second_player_ready = false;
wminix3 10:1eeb21ef7b2c 696 two_player_lose = true;
wminix3 10:1eeb21ef7b2c 697 }
wminix3 10:1eeb21ef7b2c 698 }
wminix3 10:1eeb21ef7b2c 699 if (two_player_win) {
wminix3 10:1eeb21ef7b2c 700 secondMbed.putc('W');
wminix3 10:1eeb21ef7b2c 701 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 702 rx = secondMbed.getc();
wminix3 10:1eeb21ef7b2c 703 if (rx == 'W') {
wminix3 10:1eeb21ef7b2c 704 begin_game2 = false;
wminix3 10:1eeb21ef7b2c 705 first_player_ready = false;
wminix3 10:1eeb21ef7b2c 706 second_player_ready = false;
wminix3 10:1eeb21ef7b2c 707 }
wminix3 10:1eeb21ef7b2c 708 }
wminix3 12:22aedb2598b1 709 Thread::wait(1000);
wminix3 10:1eeb21ef7b2c 710 }
wminix3 12:22aedb2598b1 711 //Thread::wait(1000);
wminix3 4:739f6e0dd8af 712 }
wminix3 4:739f6e0dd8af 713 }
wminix3 4:739f6e0dd8af 714
michaeljson 0:3817adfaeb06 715 int main() {
michaeljson 0:3817adfaeb06 716
michaeljson 0:3817adfaeb06 717 // Initialization of Setup variables
michaeljson 0:3817adfaeb06 718 int blk_x, blk_y;
michaeljson 0:3817adfaeb06 719 pb.mode(PullUp);
michaeljson 0:3817adfaeb06 720
michaeljson 0:3817adfaeb06 721 Thread thread(playstart); // intializes the thread to play sound
wminix3 10:1eeb21ef7b2c 722 // Should only have the Slave thread uncommented if second player.
wminix3 10:1eeb21ef7b2c 723 // Should only have the Master thread uncommented if first player.
wminix3 10:1eeb21ef7b2c 724 //Thread thread2(mbedSlave); // uncommented if second player -- Brice
wminix3 10:1eeb21ef7b2c 725 Thread thread3(mbedMaster); // uncommented if first player -- Brice
wminix3 11:cdea2c9ec531 726 Thread thread4(ledEffects); // thread added for LED lighting effects -- Brice
wminix3 12:22aedb2598b1 727 secondMbed.baud(9600);
michaeljson 0:3817adfaeb06 728 uLCD.baudrate(500000); // set to 500000 to increase smooth gameplay
michaeljson 0:3817adfaeb06 729
michaeljson 0:3817adfaeb06 730 // Initialization of Game Menu variables
michaeljson 0:3817adfaeb06 731 const int title_x_pos = 2; // initial x-pos of title
michaeljson 0:3817adfaeb06 732 const int title_y_pos = 3; // initial y-pos of title
michaeljson 0:3817adfaeb06 733 int start_label_x_pos = 7; // start label x-pos
michaeljson 0:3817adfaeb06 734 int start_label_y_pos = 7; // start label y-pos
michaeljson 0:3817adfaeb06 735 int level_cursor_x_pos = 5; // level cursor x-position
wminix3 2:4a3f97570578 736 int level_cursor_y_pos_start = 7; // level cursor y-position
wminix3 2:4a3f97570578 737 //int level_cursor_y_pos = 7; // initial level_cursor_y_pos @ start -- Added by Brice for more menu options
wminix3 2:4a3f97570578 738 int level_cursor_y_pos_end = 8; // BOTTOM CURSOR POS -- Added by Brice for more menu options
michaeljson 0:3817adfaeb06 739 int gameover_x_pos = 5; // gameover label x-position
michaeljson 0:3817adfaeb06 740 int gameover_y_pos = 5; // gameover label y-position
michaeljson 0:3817adfaeb06 741 int win_x_pos = 2; // congratulations label x-position
michaeljson 0:3817adfaeb06 742 int win_y_pos = 5; // congratulations label y-position
michaeljson 0:3817adfaeb06 743 int startover_x_pos = 3; // startover label x-position
michaeljson 0:3817adfaeb06 744 int startover_y_pos = 8; // startover label y-position
michaeljson 0:3817adfaeb06 745
michaeljson 0:3817adfaeb06 746 // intialize temporary score and current score
michaeljson 0:3817adfaeb06 747 int temp = 0;
michaeljson 0:3817adfaeb06 748 int score = 0;
michaeljson 0:3817adfaeb06 749
wminix3 2:4a3f97570578 750 // Additional globals added for two-player and one-player capabilities (by Brice)
wminix3 2:4a3f97570578 751
michaeljson 0:3817adfaeb06 752 // Begin game loop
michaeljson 0:3817adfaeb06 753 while(1)
michaeljson 0:3817adfaeb06 754 {
michaeljson 0:3817adfaeb06 755 // initialize all starting conditions for the beginning of the game
michaeljson 0:3817adfaeb06 756 game_menu = true; // defaults to display menu screen
michaeljson 0:3817adfaeb06 757 ENEMY_MOVE = true; // defaults to move enemy
michaeljson 0:3817adfaeb06 758 DIRECTION = 1; // default to move right
michaeljson 0:3817adfaeb06 759 hit_player = 0; // default to not player hit
michaeljson 0:3817adfaeb06 760 MOVE_DOWN = 0; // default to not move down
michaeljson 0:3817adfaeb06 761 lose = false; // default to not lose
michaeljson 0:3817adfaeb06 762 lives = 3; // defaults to 3 lives
michaeljson 0:3817adfaeb06 763 score = 0; // default to score of 0
wminix3 2:4a3f97570578 764 int level_cursor_y_pos = 7; // initial level_cursor_y_pos @ start -- Added by Brice for more menu options
michaeljson 0:3817adfaeb06 765 uLCD.cls();
wminix3 2:4a3f97570578 766 // Brice moved this out of the loop since it shouldn't change
wminix3 2:4a3f97570578 767 //uLCD.locate(title_x_pos,title_y_pos); // "SPACE INVADERS" title position
wminix3 2:4a3f97570578 768 //uLCD.printf("SPACE INVADERS"); // Title
michaeljson 0:3817adfaeb06 769 // Implementation of Game Menu
michaeljson 0:3817adfaeb06 770 while(game_menu)
michaeljson 0:3817adfaeb06 771 {
wminix3 2:4a3f97570578 772 // Brice added this in order to move the cursor through the menu options
wminix3 2:4a3f97570578 773 uLCD.locate(level_cursor_x_pos, level_cursor_y_pos);
wminix3 2:4a3f97570578 774 uLCD.printf(" ");
wminix3 2:4a3f97570578 775 if (myNav.down() && level_cursor_y_pos < level_cursor_y_pos_end) {
wminix3 2:4a3f97570578 776 level_cursor_y_pos += 1;
wminix3 2:4a3f97570578 777 } else if (myNav.up() && level_cursor_y_pos > level_cursor_y_pos_start) {
wminix3 2:4a3f97570578 778 level_cursor_y_pos -= 1;
wminix3 2:4a3f97570578 779 }
wminix3 2:4a3f97570578 780 // end of movable cursor
michaeljson 0:3817adfaeb06 781 uLCD.locate(level_cursor_x_pos,level_cursor_y_pos); // draws cursor next to "START" label
michaeljson 0:3817adfaeb06 782 uLCD.printf("->");
michaeljson 0:3817adfaeb06 783
michaeljson 0:3817adfaeb06 784 uLCD.locate(title_x_pos,title_y_pos); // "SPACE INVADERS" title position
michaeljson 0:3817adfaeb06 785 uLCD.printf("SPACE INVADERS"); // Title
michaeljson 0:3817adfaeb06 786
michaeljson 0:3817adfaeb06 787 uLCD.locate(start_label_x_pos,start_label_y_pos); // "START" label position
wminix3 2:4a3f97570578 788 uLCD.printf("ONE-PLAYER");
wminix3 2:4a3f97570578 789
wminix3 2:4a3f97570578 790 uLCD.locate(start_label_x_pos,start_label_y_pos + 1);
wminix3 2:4a3f97570578 791 uLCD.printf("TWO-PLAYER");
michaeljson 0:3817adfaeb06 792 // if pushbutton is pressed, game menu is exited and game begins
michaeljson 0:3817adfaeb06 793 if(!pb)
michaeljson 0:3817adfaeb06 794 {
michaeljson 0:3817adfaeb06 795 game_menu = false;
wminix3 2:4a3f97570578 796 if (level_cursor_y_pos == start_label_y_pos) {
wminix3 2:4a3f97570578 797 numPlayers = 1;
wminix3 2:4a3f97570578 798 } else if (level_cursor_y_pos == start_label_y_pos + 1) {
wminix3 2:4a3f97570578 799 numPlayers = 2;
wminix3 6:c44055f94cc3 800 pc.printf("num players: 2");
wminix3 2:4a3f97570578 801 }
wminix3 3:e6c081c7f6f1 802 Thread::wait(500); // changed this to Thread::wait ... originally wait(0.5);
michaeljson 0:3817adfaeb06 803 }
michaeljson 0:3817adfaeb06 804 }
wminix3 4:739f6e0dd8af 805 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 806 if (numPlayers == 2 && first_player_ready && second_player_ready) {
wminix3 5:b7934866b264 807 begin_game2 = true;
wminix3 5:b7934866b264 808 numWins = 0;
wminix3 5:b7934866b264 809 } else {
wminix3 5:b7934866b264 810 begin_game = true; // defaults begin_game to true
wminix3 5:b7934866b264 811 }
michaeljson 0:3817adfaeb06 812
michaeljson 0:3817adfaeb06 813 uLCD.cls();
michaeljson 0:3817adfaeb06 814
michaeljson 0:3817adfaeb06 815 // Draw the enemies
michaeljson 0:3817adfaeb06 816 draw_enemies_level();
michaeljson 0:3817adfaeb06 817
michaeljson 0:3817adfaeb06 818 // Draw the player
michaeljson 0:3817adfaeb06 819 draw_initial_player();
michaeljson 0:3817adfaeb06 820
michaeljson 0:3817adfaeb06 821 // sets the initial position of player missile and enemy missile (later updated)
michaeljson 0:3817adfaeb06 822 blk_x = player.player_blk_x+(player.player_width/2);
michaeljson 0:3817adfaeb06 823 blk_y = player.player_blk_y;
michaeljson 0:3817adfaeb06 824 missile_init(&missile, blk_x, blk_y, WHITE);
michaeljson 1:618aa2c4ca6a 825 int e_blk_x = 0;
michaeljson 1:618aa2c4ca6a 826 int e_blk_y = 2;
michaeljson 1:618aa2c4ca6a 827 enemy_missile_init(&enemy_missile, e_blk_x, e_blk_y, WHITE);
michaeljson 0:3817adfaeb06 828
michaeljson 0:3817adfaeb06 829 // prints lives
michaeljson 0:3817adfaeb06 830 uLCD.locate(0,0);
michaeljson 0:3817adfaeb06 831 uLCD.printf("Lives:%i", lives);
michaeljson 0:3817adfaeb06 832
michaeljson 0:3817adfaeb06 833 // prints score
michaeljson 0:3817adfaeb06 834 uLCD.locate(9,0);
michaeljson 0:3817adfaeb06 835 uLCD.printf("Score:%i", score);
michaeljson 0:3817adfaeb06 836
michaeljson 0:3817adfaeb06 837 // game play loop
michaeljson 0:3817adfaeb06 838 while(begin_game)
michaeljson 0:3817adfaeb06 839 {
michaeljson 0:3817adfaeb06 840 // updates score
michaeljson 0:3817adfaeb06 841 temp = score;
michaeljson 0:3817adfaeb06 842 score = (15-numOfEnemies)*15;
michaeljson 0:3817adfaeb06 843
michaeljson 0:3817adfaeb06 844 // prints score if score changes
michaeljson 0:3817adfaeb06 845 if (score != temp)
michaeljson 0:3817adfaeb06 846 {
michaeljson 0:3817adfaeb06 847 uLCD.locate(9,0);
michaeljson 0:3817adfaeb06 848 uLCD.printf("Score:%i", score);
michaeljson 0:3817adfaeb06 849 }
michaeljson 0:3817adfaeb06 850
michaeljson 0:3817adfaeb06 851 // move enemy
michaeljson 0:3817adfaeb06 852 enemy_motion();
michaeljson 0:3817adfaeb06 853
michaeljson 0:3817adfaeb06 854 // checks if player missile passes y-pos of row1
michaeljson 0:3817adfaeb06 855 if (missile.missile_blk_y+1-missile.missile_height <= enemy_1.enemy_blk_y
michaeljson 0:3817adfaeb06 856 && missile.missile_blk_y+1-missile.missile_height >= enemy_1.enemy_blk_y-enemy_1.enemy_height)
michaeljson 0:3817adfaeb06 857 {
michaeljson 0:3817adfaeb06 858 check_hit_enemy_row1();
michaeljson 0:3817adfaeb06 859 }
michaeljson 0:3817adfaeb06 860
michaeljson 0:3817adfaeb06 861 // checks if player missile passes y-pos of row2
michaeljson 0:3817adfaeb06 862 if (missile.missile_blk_y+1-missile.missile_height <= enemy_6.enemy_blk_y
michaeljson 0:3817adfaeb06 863 && missile.missile_blk_y+1-missile.missile_height >= enemy_6.enemy_blk_y-enemy_6.enemy_height)
michaeljson 0:3817adfaeb06 864 {
michaeljson 0:3817adfaeb06 865 check_hit_enemy_row2();
michaeljson 0:3817adfaeb06 866 }
michaeljson 0:3817adfaeb06 867
michaeljson 0:3817adfaeb06 868 // checks if player missile passes y-pos of row3
michaeljson 0:3817adfaeb06 869 if (missile.missile_blk_y+1-missile.missile_height <= enemy_11.enemy_blk_y
michaeljson 0:3817adfaeb06 870 && missile.missile_blk_y+1-missile.missile_height >= enemy_11.enemy_blk_y-enemy_11.enemy_height)
michaeljson 0:3817adfaeb06 871 {
michaeljson 0:3817adfaeb06 872 check_hit_enemy_row3();
michaeljson 0:3817adfaeb06 873 }
michaeljson 0:3817adfaeb06 874
michaeljson 0:3817adfaeb06 875 // Random Enemy Fire
michaeljson 0:3817adfaeb06 876 if (enemy_missile.status == ENEMY_MISSILE_INACTIVE)
michaeljson 0:3817adfaeb06 877 {
michaeljson 0:3817adfaeb06 878 random_attack_gen();
michaeljson 0:3817adfaeb06 879 }
michaeljson 0:3817adfaeb06 880
michaeljson 0:3817adfaeb06 881 // checks if enemy missile passes y-pos of player
michaeljson 0:3817adfaeb06 882 if (enemy_missile.missile_blk_y >= player.player_blk_y
michaeljson 0:3817adfaeb06 883 && enemy_missile.missile_blk_y <= player.player_blk_y+player.player_height)
michaeljson 0:3817adfaeb06 884 {
michaeljson 0:3817adfaeb06 885 check_player_hit();
michaeljson 0:3817adfaeb06 886 }
michaeljson 0:3817adfaeb06 887
michaeljson 0:3817adfaeb06 888 update_missile_pos(&missile); // updates player missile position
michaeljson 0:3817adfaeb06 889 update_enemy_missile_pos(&enemy_missile); // updates enemy missile position
michaeljson 0:3817adfaeb06 890
michaeljson 0:3817adfaeb06 891 // Player Movement checked with navigation switch
michaeljson 0:3817adfaeb06 892 if (myNav.left() && ((player.player_blk_x-3) > 0))
michaeljson 0:3817adfaeb06 893 {
michaeljson 0:3817adfaeb06 894 player_erase(&player);
michaeljson 0:3817adfaeb06 895 player.player_blk_x -= 3;
michaeljson 0:3817adfaeb06 896 player_show(&player);
michaeljson 0:3817adfaeb06 897 }
michaeljson 0:3817adfaeb06 898 else if (myNav.right() && ((player.player_blk_x+3) < (128-player.player_width)))
michaeljson 0:3817adfaeb06 899 {
michaeljson 0:3817adfaeb06 900 player_erase(&player);
michaeljson 0:3817adfaeb06 901 player.player_blk_x += 3;
michaeljson 0:3817adfaeb06 902 player_show(&player);
michaeljson 0:3817adfaeb06 903 }
michaeljson 0:3817adfaeb06 904
michaeljson 0:3817adfaeb06 905 // Player Fire
michaeljson 0:3817adfaeb06 906 if (pb == 0 && missile.status == PLAYER_MISSILE_INACTIVE)
michaeljson 0:3817adfaeb06 907 {
michaeljson 0:3817adfaeb06 908 missile.missile_blk_x = player.player_blk_x+(player.player_width/2);
michaeljson 0:3817adfaeb06 909 missile.missile_blk_y = player.player_blk_y;
michaeljson 0:3817adfaeb06 910 missile.status = PLAYER_MISSILE_ACTIVE;
michaeljson 0:3817adfaeb06 911 }
michaeljson 0:3817adfaeb06 912
michaeljson 0:3817adfaeb06 913 // checks if player destroyed all enemies
michaeljson 0:3817adfaeb06 914 if (numOfEnemies == 0)
michaeljson 0:3817adfaeb06 915 {
michaeljson 0:3817adfaeb06 916 uLCD.cls();
michaeljson 0:3817adfaeb06 917
michaeljson 0:3817adfaeb06 918 bool win = true; // sets win to true, for win screen
michaeljson 0:3817adfaeb06 919 begin_game = false;
michaeljson 0:3817adfaeb06 920
michaeljson 0:3817adfaeb06 921 // displays video clip
michaeljson 0:3817adfaeb06 922 uLCD.cls();
michaeljson 0:3817adfaeb06 923 uLCD.media_init();
michaeljson 0:3817adfaeb06 924 uLCD.set_sector_address(0x00, 0x00);
michaeljson 0:3817adfaeb06 925 uLCD.display_video(0,0);
wminix3 5:b7934866b264 926 Thread::wait(1000); // changed from wait(1) to Thread::wait(1000) since we're using threads -- Brice
michaeljson 0:3817adfaeb06 927
michaeljson 0:3817adfaeb06 928 uLCD.cls();
michaeljson 0:3817adfaeb06 929
michaeljson 0:3817adfaeb06 930 // prints "Congratulations" on uLCD
michaeljson 0:3817adfaeb06 931 uLCD.locate(win_x_pos,win_y_pos);
michaeljson 0:3817adfaeb06 932 uLCD.printf("CONGRATULATIONS!");
michaeljson 0:3817adfaeb06 933
michaeljson 0:3817adfaeb06 934 // prints "Play Again?" and "Press pb..."
michaeljson 0:3817adfaeb06 935 uLCD.locate(startover_x_pos, startover_y_pos);
michaeljson 0:3817adfaeb06 936 uLCD.printf("Play Again?");
michaeljson 0:3817adfaeb06 937 uLCD.locate(startover_x_pos, startover_y_pos+1);
michaeljson 0:3817adfaeb06 938 uLCD.printf("Press pb...");
michaeljson 0:3817adfaeb06 939
michaeljson 0:3817adfaeb06 940 // waits at win screen until pushbutton is pressed
michaeljson 0:3817adfaeb06 941 while (win)
michaeljson 0:3817adfaeb06 942 {
michaeljson 0:3817adfaeb06 943 // if pb is pressed, reset game to start menu
michaeljson 0:3817adfaeb06 944 if (!pb)
michaeljson 0:3817adfaeb06 945 {
michaeljson 0:3817adfaeb06 946 win = false;
wminix3 5:b7934866b264 947 Thread::wait(500); // changed from wait(0.5) to Thread::wait(500) since we're using threads
michaeljson 0:3817adfaeb06 948 }
michaeljson 0:3817adfaeb06 949 }
michaeljson 0:3817adfaeb06 950
michaeljson 0:3817adfaeb06 951 }
michaeljson 0:3817adfaeb06 952
michaeljson 0:3817adfaeb06 953 // checks if player was hit
michaeljson 0:3817adfaeb06 954 if (hit_player)
michaeljson 0:3817adfaeb06 955 {
michaeljson 0:3817adfaeb06 956 // updates lives
michaeljson 0:3817adfaeb06 957 lives -= 1;
wminix3 5:b7934866b264 958 Thread::wait(500); // changed from wait(0.5) to Thread::wait since we're using threads -- Brice
michaeljson 0:3817adfaeb06 959 hit_player = 0;
michaeljson 0:3817adfaeb06 960 player_show(&player);
michaeljson 0:3817adfaeb06 961 player.status = PLAYER_ALIVE;
michaeljson 0:3817adfaeb06 962
michaeljson 0:3817adfaeb06 963 // prints updated lives number
michaeljson 0:3817adfaeb06 964 uLCD.locate(0,0);
michaeljson 0:3817adfaeb06 965 uLCD.printf("Lives:%i", lives);
michaeljson 0:3817adfaeb06 966 }
michaeljson 0:3817adfaeb06 967
michaeljson 0:3817adfaeb06 968 // if player loses all lives or enemy reaches the player
michaeljson 0:3817adfaeb06 969 if (lose || lives == 0)
michaeljson 0:3817adfaeb06 970 {
michaeljson 0:3817adfaeb06 971 begin_game = false; // set to false to end game
michaeljson 0:3817adfaeb06 972 uLCD.cls();
michaeljson 0:3817adfaeb06 973
michaeljson 0:3817adfaeb06 974 gameover = true; // set to go to display gameover screen
michaeljson 0:3817adfaeb06 975
michaeljson 0:3817adfaeb06 976 // prints "GAMEOVER" to uLCD
michaeljson 0:3817adfaeb06 977 uLCD.locate(gameover_x_pos, gameover_y_pos);
michaeljson 0:3817adfaeb06 978 uLCD.printf("GAMEOVER");
wminix3 5:b7934866b264 979 Thread::wait(1000); // changed from wait(1) to thread::wait since we're using threads -- Brice
michaeljson 0:3817adfaeb06 980
michaeljson 0:3817adfaeb06 981 // prints "Play Again?" and "Press pb..."
michaeljson 0:3817adfaeb06 982 uLCD.locate(startover_x_pos, startover_y_pos);
michaeljson 0:3817adfaeb06 983 uLCD.printf("Play Again?");
michaeljson 0:3817adfaeb06 984 uLCD.locate(startover_x_pos, startover_y_pos+1);
michaeljson 0:3817adfaeb06 985 uLCD.printf("Press pb...");
michaeljson 0:3817adfaeb06 986
michaeljson 0:3817adfaeb06 987 // stays in gameover screen until pb is pressed
michaeljson 0:3817adfaeb06 988 while (gameover)
michaeljson 0:3817adfaeb06 989 {
michaeljson 0:3817adfaeb06 990 // if pb is pressed, game is reset to the game menu screen
michaeljson 0:3817adfaeb06 991 if (!pb)
michaeljson 0:3817adfaeb06 992 {
michaeljson 0:3817adfaeb06 993 gameover = false;
michaeljson 0:3817adfaeb06 994 game_menu = true;
wminix3 5:b7934866b264 995 Thread::wait(500); // changed wait(0.5) to Thread::wait since we're using threads -- Brice
wminix3 5:b7934866b264 996 }
wminix3 5:b7934866b264 997 }
wminix3 5:b7934866b264 998 }
wminix3 5:b7934866b264 999
wminix3 5:b7934866b264 1000 }
wminix3 5:b7934866b264 1001 // game play loop
wminix3 5:b7934866b264 1002 while(begin_game2)
wminix3 5:b7934866b264 1003 {
wminix3 5:b7934866b264 1004 // updates score
wminix3 5:b7934866b264 1005 temp = score;
wminix3 5:b7934866b264 1006 score = (15-numOfEnemies)*15;
wminix3 5:b7934866b264 1007
wminix3 5:b7934866b264 1008 // prints score if score changes
wminix3 5:b7934866b264 1009 if (score != temp)
wminix3 5:b7934866b264 1010 {
wminix3 5:b7934866b264 1011 uLCD.locate(9,0);
wminix3 5:b7934866b264 1012 uLCD.printf("Score:%i", score);
wminix3 5:b7934866b264 1013 }
wminix3 5:b7934866b264 1014
wminix3 5:b7934866b264 1015 // move enemy
wminix3 5:b7934866b264 1016 enemy_motion();
wminix3 5:b7934866b264 1017
wminix3 5:b7934866b264 1018 // checks if player missile passes y-pos of row1
wminix3 5:b7934866b264 1019 if (missile.missile_blk_y+1-missile.missile_height <= enemy_1.enemy_blk_y
wminix3 5:b7934866b264 1020 && missile.missile_blk_y+1-missile.missile_height >= enemy_1.enemy_blk_y-enemy_1.enemy_height)
wminix3 5:b7934866b264 1021 {
wminix3 5:b7934866b264 1022 check_hit_enemy_row1();
wminix3 5:b7934866b264 1023 }
wminix3 5:b7934866b264 1024
wminix3 5:b7934866b264 1025 // checks if player missile passes y-pos of row2
wminix3 5:b7934866b264 1026 if (missile.missile_blk_y+1-missile.missile_height <= enemy_6.enemy_blk_y
wminix3 5:b7934866b264 1027 && missile.missile_blk_y+1-missile.missile_height >= enemy_6.enemy_blk_y-enemy_6.enemy_height)
wminix3 5:b7934866b264 1028 {
wminix3 5:b7934866b264 1029 check_hit_enemy_row2();
wminix3 5:b7934866b264 1030 }
wminix3 5:b7934866b264 1031
wminix3 5:b7934866b264 1032 // checks if player missile passes y-pos of row3
wminix3 5:b7934866b264 1033 if (missile.missile_blk_y+1-missile.missile_height <= enemy_11.enemy_blk_y
wminix3 5:b7934866b264 1034 && missile.missile_blk_y+1-missile.missile_height >= enemy_11.enemy_blk_y-enemy_11.enemy_height)
wminix3 5:b7934866b264 1035 {
wminix3 5:b7934866b264 1036 check_hit_enemy_row3();
wminix3 5:b7934866b264 1037 }
wminix3 5:b7934866b264 1038
wminix3 5:b7934866b264 1039 // Random Enemy Fire
wminix3 5:b7934866b264 1040 if (enemy_missile.status == ENEMY_MISSILE_INACTIVE)
wminix3 5:b7934866b264 1041 {
wminix3 5:b7934866b264 1042 random_attack_gen();
wminix3 5:b7934866b264 1043 }
wminix3 5:b7934866b264 1044
wminix3 5:b7934866b264 1045 // checks if enemy missile passes y-pos of player
wminix3 5:b7934866b264 1046 if (enemy_missile.missile_blk_y >= player.player_blk_y
wminix3 5:b7934866b264 1047 && enemy_missile.missile_blk_y <= player.player_blk_y+player.player_height)
wminix3 5:b7934866b264 1048 {
wminix3 5:b7934866b264 1049 check_player_hit();
wminix3 5:b7934866b264 1050 }
wminix3 5:b7934866b264 1051
wminix3 5:b7934866b264 1052 update_missile_pos(&missile); // updates player missile position
wminix3 5:b7934866b264 1053 update_enemy_missile_pos(&enemy_missile); // updates enemy missile position
wminix3 5:b7934866b264 1054
wminix3 5:b7934866b264 1055 // Player Movement checked with navigation switch
wminix3 5:b7934866b264 1056 if (myNav.left() && ((player.player_blk_x-3) > 0))
wminix3 5:b7934866b264 1057 {
wminix3 5:b7934866b264 1058 player_erase(&player);
wminix3 5:b7934866b264 1059 player.player_blk_x -= 3;
wminix3 5:b7934866b264 1060 player_show(&player);
wminix3 5:b7934866b264 1061 }
wminix3 5:b7934866b264 1062 else if (myNav.right() && ((player.player_blk_x+3) < (128-player.player_width)))
wminix3 5:b7934866b264 1063 {
wminix3 5:b7934866b264 1064 player_erase(&player);
wminix3 5:b7934866b264 1065 player.player_blk_x += 3;
wminix3 5:b7934866b264 1066 player_show(&player);
wminix3 5:b7934866b264 1067 }
wminix3 5:b7934866b264 1068
wminix3 5:b7934866b264 1069 // Player Fire
wminix3 5:b7934866b264 1070 if (pb == 0 && missile.status == PLAYER_MISSILE_INACTIVE)
wminix3 5:b7934866b264 1071 {
wminix3 5:b7934866b264 1072 missile.missile_blk_x = player.player_blk_x+(player.player_width/2);
wminix3 5:b7934866b264 1073 missile.missile_blk_y = player.player_blk_y;
wminix3 5:b7934866b264 1074 missile.status = PLAYER_MISSILE_ACTIVE;
wminix3 5:b7934866b264 1075 }
wminix3 5:b7934866b264 1076
wminix3 5:b7934866b264 1077 // checks if player destroyed all enemies
wminix3 5:b7934866b264 1078 if (numOfEnemies == 0)
wminix3 5:b7934866b264 1079 {
wminix3 5:b7934866b264 1080 uLCD.cls();
wminix3 5:b7934866b264 1081
wminix3 5:b7934866b264 1082 bool win = true; // sets win to true, for win screen
wminix3 5:b7934866b264 1083 numWins += 1;
wminix3 5:b7934866b264 1084 if (numWins == 3) {
wminix3 10:1eeb21ef7b2c 1085 //begin_game2 = false; // Allow the mbed communication thread to change the begin_game2 bool to false after letting other mbed know.
wminix3 5:b7934866b264 1086 two_player_win = true;
wminix3 5:b7934866b264 1087 }
wminix3 5:b7934866b264 1088
wminix3 5:b7934866b264 1089 // displays video clip
wminix3 5:b7934866b264 1090 uLCD.cls();
wminix3 5:b7934866b264 1091 uLCD.media_init();
wminix3 5:b7934866b264 1092 uLCD.set_sector_address(0x00, 0x00);
wminix3 5:b7934866b264 1093 uLCD.display_video(0,0);
wminix3 5:b7934866b264 1094 Thread::wait(1000); // changed from wait(1)
wminix3 5:b7934866b264 1095
wminix3 5:b7934866b264 1096 uLCD.cls();
wminix3 5:b7934866b264 1097 if (!two_player_win) {
wminix3 5:b7934866b264 1098 // prints "Number of Wins" on uLCD -- Brice. A step towards victory, not a complete victory.
wminix3 5:b7934866b264 1099 uLCD.locate(win_x_pos,win_y_pos);
wminix3 5:b7934866b264 1100 uLCD.printf("YOU HAVE %d WINS!", numWins);
wminix3 5:b7934866b264 1101
wminix3 5:b7934866b264 1102 // prints "Continue?" and "Press pb..." Keep trying to get points --Brice.
wminix3 5:b7934866b264 1103 uLCD.locate(startover_x_pos, startover_y_pos);
wminix3 5:b7934866b264 1104 uLCD.printf("Continue?");
wminix3 5:b7934866b264 1105 uLCD.locate(startover_x_pos, startover_y_pos+1);
wminix3 5:b7934866b264 1106 uLCD.printf("Press pb...");
wminix3 5:b7934866b264 1107
wminix3 5:b7934866b264 1108 // waits at win screen until pushbutton is pressed
wminix3 5:b7934866b264 1109 while (win)
wminix3 5:b7934866b264 1110 {
wminix3 5:b7934866b264 1111 // if pb is pressed, reset game to start menu
wminix3 5:b7934866b264 1112 if (!pb)
wminix3 5:b7934866b264 1113 {
wminix3 5:b7934866b264 1114 win = false;
wminix3 5:b7934866b264 1115 Thread::wait(500); // changed from wait(0.5) since we have threads -- Brice
wminix3 5:b7934866b264 1116 }
wminix3 5:b7934866b264 1117 }
wminix3 5:b7934866b264 1118 } else {
wminix3 5:b7934866b264 1119 // prints CONGRATULATIONS! since player has won. --Brice
wminix3 5:b7934866b264 1120 uLCD.locate(win_x_pos,win_y_pos);
wminix3 5:b7934866b264 1121 uLCD.printf("CONGRATULATIONS!");
wminix3 5:b7934866b264 1122
wminix3 5:b7934866b264 1123 // prints "Return to menu?" and "Press pb..." --Brice.
wminix3 5:b7934866b264 1124 uLCD.locate(startover_x_pos, startover_y_pos);
wminix3 5:b7934866b264 1125 uLCD.printf("Return to menu?");
wminix3 5:b7934866b264 1126 uLCD.locate(startover_x_pos, startover_y_pos+1);
wminix3 5:b7934866b264 1127 uLCD.printf("Press pb...");
wminix3 5:b7934866b264 1128
wminix3 5:b7934866b264 1129 // waits at win screen until pushbutton is pressed
wminix3 5:b7934866b264 1130 while (two_player_win)
wminix3 5:b7934866b264 1131 {
wminix3 5:b7934866b264 1132 // if pb is pressed, reset game to start menu
wminix3 5:b7934866b264 1133 if (!pb)
wminix3 5:b7934866b264 1134 {
wminix3 5:b7934866b264 1135 two_player_win = false; // close win -- Brice
wminix3 5:b7934866b264 1136 game_menu = true; // go back to menu -- Brice
wminix3 5:b7934866b264 1137 Thread::wait(500); // changed from wait(0.5) since we have threads -- Brice
wminix3 5:b7934866b264 1138 }
wminix3 5:b7934866b264 1139 }
wminix3 5:b7934866b264 1140 }
wminix3 5:b7934866b264 1141 }
wminix3 5:b7934866b264 1142
wminix3 5:b7934866b264 1143 // checks if player was hit
wminix3 5:b7934866b264 1144 if (hit_player)
wminix3 5:b7934866b264 1145 {
wminix3 5:b7934866b264 1146 // updates lives
wminix3 5:b7934866b264 1147 lives -= 1;
wminix3 5:b7934866b264 1148 Thread::wait(500); // changed from wait(0.5) since we're using threads --Brice
wminix3 5:b7934866b264 1149 hit_player = 0;
wminix3 5:b7934866b264 1150 player_show(&player);
wminix3 5:b7934866b264 1151 player.status = PLAYER_ALIVE;
wminix3 5:b7934866b264 1152
wminix3 5:b7934866b264 1153 // prints updated lives number
wminix3 5:b7934866b264 1154 uLCD.locate(0,0);
wminix3 5:b7934866b264 1155 uLCD.printf("Lives:%i", lives);
wminix3 5:b7934866b264 1156 }
wminix3 5:b7934866b264 1157
wminix3 5:b7934866b264 1158 // if player loses all lives or enemy reaches the player
wminix3 5:b7934866b264 1159 if (lose || lives == 0)
wminix3 5:b7934866b264 1160 {
wminix3 5:b7934866b264 1161 //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 1162 uLCD.cls();
wminix3 5:b7934866b264 1163
wminix3 5:b7934866b264 1164 gameover = true; // set to go to display gameover screen
wminix3 5:b7934866b264 1165
wminix3 5:b7934866b264 1166 // prints "GAMEOVER" to uLCD
wminix3 5:b7934866b264 1167 uLCD.locate(gameover_x_pos, gameover_y_pos);
wminix3 5:b7934866b264 1168 uLCD.printf("YOU DIED");
wminix3 5:b7934866b264 1169 Thread::wait(1000); // changed from wait(1) since we have multiple threads -- Brice
wminix3 5:b7934866b264 1170
wminix3 5:b7934866b264 1171 // prints "Play Again?" and "Press pb..."
wminix3 5:b7934866b264 1172 uLCD.locate(startover_x_pos, startover_y_pos);
wminix3 5:b7934866b264 1173 uLCD.printf("Keep trying!");
wminix3 5:b7934866b264 1174 uLCD.locate(startover_x_pos, startover_y_pos+1);
wminix3 5:b7934866b264 1175 uLCD.printf("Press pb...");
wminix3 5:b7934866b264 1176
wminix3 5:b7934866b264 1177 // stays in gameover screen until pb is pressed
wminix3 5:b7934866b264 1178 while (gameover)
wminix3 5:b7934866b264 1179 {
wminix3 5:b7934866b264 1180 // if pb is pressed, game is reset to the game menu screen
wminix3 5:b7934866b264 1181 if (!pb)
wminix3 5:b7934866b264 1182 {
wminix3 5:b7934866b264 1183 gameover = false;
wminix3 5:b7934866b264 1184 //game_menu = true; // removed since other player must win three times for game to be over.
wminix3 5:b7934866b264 1185 Thread::wait(500); // changed from wait(0.5) since we have threads.
wminix3 5:b7934866b264 1186 }
wminix3 5:b7934866b264 1187 }
wminix3 5:b7934866b264 1188 }
wminix3 5:b7934866b264 1189 if (two_player_lose)
wminix3 5:b7934866b264 1190 {
wminix3 5:b7934866b264 1191 begin_game2 = false; // set to false to end game. End game since other player won.
wminix3 5:b7934866b264 1192 uLCD.cls();
wminix3 5:b7934866b264 1193
wminix3 5:b7934866b264 1194 gameover = true; // set to go to display gameover screen
wminix3 5:b7934866b264 1195
wminix3 5:b7934866b264 1196 // prints "GAMEOVER" to uLCD
wminix3 5:b7934866b264 1197 uLCD.locate(gameover_x_pos, gameover_y_pos);
wminix3 5:b7934866b264 1198 uLCD.printf("GAMEOVER");
wminix3 5:b7934866b264 1199 Thread::wait(1000); // thread wait since we have multiple threads -- Brice
wminix3 5:b7934866b264 1200
wminix3 5:b7934866b264 1201 // prints "Return to menu?" and "Press pb..."
wminix3 5:b7934866b264 1202 uLCD.locate(startover_x_pos, startover_y_pos);
wminix3 5:b7934866b264 1203 uLCD.printf("Return to menu?");
wminix3 5:b7934866b264 1204 uLCD.locate(startover_x_pos, startover_y_pos+1);
wminix3 5:b7934866b264 1205 uLCD.printf("Press pb...");
wminix3 5:b7934866b264 1206
wminix3 5:b7934866b264 1207 // stays in gameover screen until pb is pressed
wminix3 5:b7934866b264 1208 while (gameover)
wminix3 5:b7934866b264 1209 {
wminix3 5:b7934866b264 1210 // if pb is pressed, game is reset to the game menu screen
wminix3 5:b7934866b264 1211 if (!pb)
wminix3 5:b7934866b264 1212 {
wminix3 5:b7934866b264 1213 gameover = false;
wminix3 5:b7934866b264 1214 two_player_lose = false; // end lose.
wminix3 5:b7934866b264 1215 game_menu = true; // removed since other player must win three times for game to be over.
wminix3 5:b7934866b264 1216 Thread::wait(500); // changed from wait(0.5) since we have threads.
michaeljson 0:3817adfaeb06 1217 }
michaeljson 0:3817adfaeb06 1218 }
michaeljson 0:3817adfaeb06 1219 }
michaeljson 0:3817adfaeb06 1220
michaeljson 0:3817adfaeb06 1221 }
michaeljson 0:3817adfaeb06 1222 }
michaeljson 0:3817adfaeb06 1223 }