Updated Space Invaders on the mbed. Improved upon Michael Son's "Mbed Space Invaders" at https://os.mbed.com/users/michaeljson/notebook/mbed-space-invaders/.

Dependencies:   mbed wave_player mbed-rtos 4DGL-uLCD-SE SparkfunAnalogJoystick SDFileSystem LSM9DS1_Library_cal_updated

Fork of Two-PlayerSpaceInvaders by William Minix

test

Committer:
wminix3
Date:
Fri Apr 23 02:32:00 2021 +0000
Revision:
16:e4e6515bdabb
Parent:
15:33582e9acd16
Child:
17:843a874eb4e3
Started to work on adding more detailed sprites for enemies. Got the analog joystick to work in place of the tactile switch.

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