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 04:34:14 2021 +0000
Revision:
17:843a874eb4e3
Parent:
16:e4e6515bdabb
Child:
18:16a2d4343ae4
Added both analog joystick and IMU to be used to control the menu and the ship. All features seem to work simultaneously at this point.

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