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:
Thu Apr 22 02:05:05 2021 +0000
Revision:
14:4e7608619043
Parent:
13:36cc024dcf6b
Child:
15:33582e9acd16
Added difficulty options. Incorporated initial features together. Working model.

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