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 05:08:49 2021 +0000
Revision:
18:16a2d4343ae4
Parent:
17:843a874eb4e3
Child:
20:ede4fa57d082
Added changing player color/skin by clicking the analog joystick.

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 18:16a2d4343ae4 812 // Control menu with Analog Joystick
wminix3 16:e4e6515bdabb 813 if ((stick.angle() <= 280 && stick.angle() >= 260) && level_cursor_y_pos < level_cursor_y_pos_end) {
wminix3 14:4e7608619043 814 level_cursor_y_pos += 2;
wminix3 16:e4e6515bdabb 815 //} else if (myNav.up() && level_cursor_y_pos > level_cursor_y_pos_start) {
wminix3 16:e4e6515bdabb 816 } else if ((stick.angle() <= 100 && stick.angle() >= 80) && level_cursor_y_pos > level_cursor_y_pos_start) {
wminix3 14:4e7608619043 817 level_cursor_y_pos -= 2;
wminix3 2:4a3f97570578 818 }
wminix3 18:16a2d4343ae4 819 // Control Menu with IMU
wminix3 17:843a874eb4e3 820 if (IMU.accelAvailable()) {
wminix3 17:843a874eb4e3 821 IMU.readAccel();
wminix3 17:843a874eb4e3 822 accelY = IMU.calcAccel(IMU.ay);
wminix3 17:843a874eb4e3 823 //pc.printf("Calc Accel: %f", accelY);
wminix3 17:843a874eb4e3 824 }
wminix3 17:843a874eb4e3 825 if ((accelY >= 0.25) && level_cursor_y_pos < level_cursor_y_pos_end) {
wminix3 17:843a874eb4e3 826 level_cursor_y_pos += 2;
wminix3 17:843a874eb4e3 827 //} else if (myNav.up() && level_cursor_y_pos > level_cursor_y_pos_start) {
wminix3 17:843a874eb4e3 828 } else if ((accelY <= -0.25) && level_cursor_y_pos > level_cursor_y_pos_start) {
wminix3 17:843a874eb4e3 829 level_cursor_y_pos -= 2;
wminix3 17:843a874eb4e3 830 }
wminix3 2:4a3f97570578 831 // end of movable cursor
michaeljson 0:3817adfaeb06 832 uLCD.locate(level_cursor_x_pos,level_cursor_y_pos); // draws cursor next to "START" label
michaeljson 0:3817adfaeb06 833 uLCD.printf("->");
michaeljson 0:3817adfaeb06 834
michaeljson 0:3817adfaeb06 835 uLCD.locate(title_x_pos,title_y_pos); // "SPACE INVADERS" title position
michaeljson 0:3817adfaeb06 836 uLCD.printf("SPACE INVADERS"); // Title
michaeljson 0:3817adfaeb06 837
wminix3 14:4e7608619043 838 //uLCD.locate(start_label_x_pos,start_label_y_pos); // "START" label position
wminix3 14:4e7608619043 839 //uLCD.printf("ONE-PLAYER");
wminix3 14:4e7608619043 840
wminix3 14:4e7608619043 841 //uLCD.locate(start_label_x_pos,start_label_y_pos + 1);
wminix3 14:4e7608619043 842 //uLCD.printf("TWO-PLAYER");
wminix3 2:4a3f97570578 843
wminix3 14:4e7608619043 844 uLCD.locate(start_label_x_pos,start_label_y_pos); // "START" label position
wminix3 14:4e7608619043 845 uLCD.printf("LEVEL 1");
wminix3 14:4e7608619043 846
wminix3 14:4e7608619043 847 uLCD.locate(start_label_x_pos,start_label_y_pos + 2);
wminix3 14:4e7608619043 848 uLCD.printf("LEVEL 2");
wminix3 14:4e7608619043 849
wminix3 14:4e7608619043 850 uLCD.locate(start_label_x_pos,start_label_y_pos + 4);
wminix3 14:4e7608619043 851 uLCD.printf("LEVEL 3");
michaeljson 0:3817adfaeb06 852 // if pushbutton is pressed, game menu is exited and game begins
michaeljson 0:3817adfaeb06 853 if(!pb)
michaeljson 0:3817adfaeb06 854 {
michaeljson 0:3817adfaeb06 855 game_menu = false;
wminix3 2:4a3f97570578 856 if (level_cursor_y_pos == start_label_y_pos) {
wminix3 14:4e7608619043 857 //numPlayers = 1;
wminix3 14:4e7608619043 858 level = 1;
wminix3 14:4e7608619043 859 } else if (level_cursor_y_pos == start_label_y_pos + 2) {
wminix3 14:4e7608619043 860 //numPlayers = 2;
wminix3 14:4e7608619043 861 //pc.printf("num players: 2");
wminix3 14:4e7608619043 862 level = 2;
wminix3 14:4e7608619043 863 } else if (level_cursor_y_pos == start_label_y_pos + 4) {
wminix3 14:4e7608619043 864 level = 3;
wminix3 2:4a3f97570578 865 }
wminix3 3:e6c081c7f6f1 866 Thread::wait(500); // changed this to Thread::wait ... originally wait(0.5);
michaeljson 0:3817adfaeb06 867 }
michaeljson 0:3817adfaeb06 868 }
wminix3 14:4e7608619043 869 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 870 if (numPlayers == 2 && begin_game2) {
wminix3 5:b7934866b264 871 numWins = 0;
wminix3 5:b7934866b264 872 } else {
wminix3 5:b7934866b264 873 begin_game = true; // defaults begin_game to true
wminix3 13:36cc024dcf6b 874
wminix3 13:36cc024dcf6b 875 // Start timer to check for best time in single player.
wminix3 13:36cc024dcf6b 876 bestTimer.start();
wminix3 5:b7934866b264 877 }
michaeljson 0:3817adfaeb06 878
michaeljson 0:3817adfaeb06 879 uLCD.cls();
michaeljson 0:3817adfaeb06 880
michaeljson 0:3817adfaeb06 881 // Draw the enemies
michaeljson 0:3817adfaeb06 882 draw_enemies_level();
michaeljson 0:3817adfaeb06 883
michaeljson 0:3817adfaeb06 884 // Draw the player
michaeljson 0:3817adfaeb06 885 draw_initial_player();
michaeljson 0:3817adfaeb06 886
michaeljson 0:3817adfaeb06 887 // sets the initial position of player missile and enemy missile (later updated)
michaeljson 0:3817adfaeb06 888 blk_x = player.player_blk_x+(player.player_width/2);
michaeljson 0:3817adfaeb06 889 blk_y = player.player_blk_y;
michaeljson 0:3817adfaeb06 890 missile_init(&missile, blk_x, blk_y, WHITE);
michaeljson 1:618aa2c4ca6a 891 int e_blk_x = 0;
michaeljson 1:618aa2c4ca6a 892 int e_blk_y = 2;
michaeljson 1:618aa2c4ca6a 893 enemy_missile_init(&enemy_missile, e_blk_x, e_blk_y, WHITE);
michaeljson 0:3817adfaeb06 894
michaeljson 0:3817adfaeb06 895 // prints lives
wminix3 14:4e7608619043 896 if (level == 1) {
wminix3 14:4e7608619043 897 uLCD.locate(0,0);
wminix3 14:4e7608619043 898 uLCD.printf("Lives:%i", 3);
wminix3 14:4e7608619043 899 } else if (level == 2) {
wminix3 14:4e7608619043 900 uLCD.locate(0,0);
wminix3 14:4e7608619043 901 uLCD.printf("Lives:%i", 2);
wminix3 14:4e7608619043 902 } else if (level == 3) {
wminix3 14:4e7608619043 903 uLCD.locate(0,0);
wminix3 14:4e7608619043 904 uLCD.printf("Lives:%i", 1);
wminix3 14:4e7608619043 905 }
wminix3 14:4e7608619043 906 //uLCD.locate(0,0);
wminix3 14:4e7608619043 907 //uLCD.printf("Lives:%i", lives);
michaeljson 0:3817adfaeb06 908
michaeljson 0:3817adfaeb06 909 // prints score
michaeljson 0:3817adfaeb06 910 uLCD.locate(9,0);
michaeljson 0:3817adfaeb06 911 uLCD.printf("Score:%i", score);
michaeljson 0:3817adfaeb06 912
michaeljson 0:3817adfaeb06 913 // game play loop
michaeljson 0:3817adfaeb06 914 while(begin_game)
michaeljson 0:3817adfaeb06 915 {
michaeljson 0:3817adfaeb06 916 // updates score
michaeljson 0:3817adfaeb06 917 temp = score;
michaeljson 0:3817adfaeb06 918 score = (15-numOfEnemies)*15;
michaeljson 0:3817adfaeb06 919
michaeljson 0:3817adfaeb06 920 // prints score if score changes
michaeljson 0:3817adfaeb06 921 if (score != temp)
michaeljson 0:3817adfaeb06 922 {
michaeljson 0:3817adfaeb06 923 uLCD.locate(9,0);
michaeljson 0:3817adfaeb06 924 uLCD.printf("Score:%i", score);
michaeljson 0:3817adfaeb06 925 }
michaeljson 0:3817adfaeb06 926
michaeljson 0:3817adfaeb06 927 // move enemy
michaeljson 0:3817adfaeb06 928 enemy_motion();
michaeljson 0:3817adfaeb06 929
michaeljson 0:3817adfaeb06 930 // checks if player missile passes y-pos of row1
michaeljson 0:3817adfaeb06 931 if (missile.missile_blk_y+1-missile.missile_height <= enemy_1.enemy_blk_y
michaeljson 0:3817adfaeb06 932 && missile.missile_blk_y+1-missile.missile_height >= enemy_1.enemy_blk_y-enemy_1.enemy_height)
michaeljson 0:3817adfaeb06 933 {
michaeljson 0:3817adfaeb06 934 check_hit_enemy_row1();
michaeljson 0:3817adfaeb06 935 }
michaeljson 0:3817adfaeb06 936
michaeljson 0:3817adfaeb06 937 // checks if player missile passes y-pos of row2
michaeljson 0:3817adfaeb06 938 if (missile.missile_blk_y+1-missile.missile_height <= enemy_6.enemy_blk_y
michaeljson 0:3817adfaeb06 939 && missile.missile_blk_y+1-missile.missile_height >= enemy_6.enemy_blk_y-enemy_6.enemy_height)
michaeljson 0:3817adfaeb06 940 {
michaeljson 0:3817adfaeb06 941 check_hit_enemy_row2();
michaeljson 0:3817adfaeb06 942 }
michaeljson 0:3817adfaeb06 943
michaeljson 0:3817adfaeb06 944 // checks if player missile passes y-pos of row3
michaeljson 0:3817adfaeb06 945 if (missile.missile_blk_y+1-missile.missile_height <= enemy_11.enemy_blk_y
michaeljson 0:3817adfaeb06 946 && missile.missile_blk_y+1-missile.missile_height >= enemy_11.enemy_blk_y-enemy_11.enemy_height)
michaeljson 0:3817adfaeb06 947 {
michaeljson 0:3817adfaeb06 948 check_hit_enemy_row3();
michaeljson 0:3817adfaeb06 949 }
michaeljson 0:3817adfaeb06 950
michaeljson 0:3817adfaeb06 951 // Random Enemy Fire
michaeljson 0:3817adfaeb06 952 if (enemy_missile.status == ENEMY_MISSILE_INACTIVE)
michaeljson 0:3817adfaeb06 953 {
michaeljson 0:3817adfaeb06 954 random_attack_gen();
michaeljson 0:3817adfaeb06 955 }
michaeljson 0:3817adfaeb06 956
michaeljson 0:3817adfaeb06 957 // checks if enemy missile passes y-pos of player
michaeljson 0:3817adfaeb06 958 if (enemy_missile.missile_blk_y >= player.player_blk_y
michaeljson 0:3817adfaeb06 959 && enemy_missile.missile_blk_y <= player.player_blk_y+player.player_height)
michaeljson 0:3817adfaeb06 960 {
michaeljson 0:3817adfaeb06 961 check_player_hit();
michaeljson 0:3817adfaeb06 962 }
michaeljson 0:3817adfaeb06 963
michaeljson 0:3817adfaeb06 964 update_missile_pos(&missile); // updates player missile position
michaeljson 0:3817adfaeb06 965 update_enemy_missile_pos(&enemy_missile); // updates enemy missile position
michaeljson 0:3817adfaeb06 966
michaeljson 0:3817adfaeb06 967 // Player Movement checked with navigation switch
wminix3 16:e4e6515bdabb 968 //if (myNav.left() && ((player.player_blk_x-3) > 0))
wminix3 18:16a2d4343ae4 969 // With joystick click, change color of player from GREEN -> BLUE -> PINK -> PURPLE -> YELLOW (and loop).
wminix3 18:16a2d4343ae4 970 if (stick.button()) {
wminix3 18:16a2d4343ae4 971 if (player.player_color == 0x00FF00) { // if GREEN (start)
wminix3 18:16a2d4343ae4 972 player.player_color = 0x0000FF; // BLUE
wminix3 18:16a2d4343ae4 973 } else if (player.player_color == 0x0000FF) { // if BLUE
wminix3 18:16a2d4343ae4 974 player.player_color = 0xFFC0CB; // pink. hot pink: 0xFF69B4
wminix3 18:16a2d4343ae4 975 } else if (player.player_color == 0xFFC0CB) { // if pink
wminix3 18:16a2d4343ae4 976 player.player_color = 0x800080; // Purple: 0x800080. periwinkle purple: 0xCCCCFF
wminix3 18:16a2d4343ae4 977 } else if (player.player_color == 0x800080) { // if purple
wminix3 18:16a2d4343ae4 978 player.player_color = 0xFFFF00; // yellow. metallic gold: 0xD4AF37
wminix3 18:16a2d4343ae4 979 } else if (player.player_color == 0xFFFF00) { // if yellow
wminix3 18:16a2d4343ae4 980 player.player_color = 0x00FF00; // set back to GREEN
wminix3 18:16a2d4343ae4 981 }
wminix3 18:16a2d4343ae4 982 }
wminix3 18:16a2d4343ae4 983 // Control Player with Analog Joystick -- Brice
wminix3 16:e4e6515bdabb 984 float stickDist = stick.xAxis();
wminix3 17:843a874eb4e3 985 if ((stickDist < 0.0) && (player.player_blk_x + stickDist*3 > 0.0))
michaeljson 0:3817adfaeb06 986 {
michaeljson 0:3817adfaeb06 987 player_erase(&player);
wminix3 16:e4e6515bdabb 988 //player.player_blk_x -= 3;
wminix3 16:e4e6515bdabb 989 player.player_blk_x += (int)(stickDist*3);
michaeljson 0:3817adfaeb06 990 player_show(&player);
michaeljson 0:3817adfaeb06 991 }
wminix3 16:e4e6515bdabb 992 //else if (myNav.right() && ((player.player_blk_x+3) < (128-player.player_width)))
wminix3 17:843a874eb4e3 993 else if ((stickDist > 0.0) && ((player.player_blk_x + stickDist*3) < (128 - player.player_width)))
michaeljson 0:3817adfaeb06 994 {
michaeljson 0:3817adfaeb06 995 player_erase(&player);
wminix3 16:e4e6515bdabb 996 player.player_blk_x += (int)(stickDist*3);
michaeljson 0:3817adfaeb06 997 player_show(&player);
michaeljson 0:3817adfaeb06 998 }
wminix3 18:16a2d4343ae4 999 // Control Player with IMU -- Brice
wminix3 17:843a874eb4e3 1000 float accelX = 0.0; // x acceleration
wminix3 17:843a874eb4e3 1001 if (IMU.accelAvailable()) {
wminix3 17:843a874eb4e3 1002 IMU.readAccel();
wminix3 17:843a874eb4e3 1003 accelX = IMU.calcAccel(IMU.ax);
wminix3 17:843a874eb4e3 1004 //pc.printf("Calc Accel: %f", accelY);
wminix3 17:843a874eb4e3 1005 }
wminix3 17:843a874eb4e3 1006 if ((accelX <= -0.25) && (player.player_blk_x + accelX*5) > 0.0) {
wminix3 17:843a874eb4e3 1007 player_erase(&player);
wminix3 17:843a874eb4e3 1008 //player.player_blk_x -= 3;
wminix3 17:843a874eb4e3 1009 player.player_blk_x += (int)(accelX*5);
wminix3 17:843a874eb4e3 1010 player_show(&player);
wminix3 17:843a874eb4e3 1011 //} else if (myNav.up() && level_cursor_y_pos > level_cursor_y_pos_start) {
wminix3 17:843a874eb4e3 1012 } else if ((accelX >= 0.25) && ((player.player_blk_x + accelX*5) < (128 - player.player_width))) {
wminix3 17:843a874eb4e3 1013 player_erase(&player);
wminix3 17:843a874eb4e3 1014 //player.player_blk_x -= 3;
wminix3 17:843a874eb4e3 1015 player.player_blk_x += (int)(accelX*5);
wminix3 17:843a874eb4e3 1016 player_show(&player);
wminix3 17:843a874eb4e3 1017 }
michaeljson 0:3817adfaeb06 1018 // Player Fire
michaeljson 0:3817adfaeb06 1019 if (pb == 0 && missile.status == PLAYER_MISSILE_INACTIVE)
michaeljson 0:3817adfaeb06 1020 {
michaeljson 0:3817adfaeb06 1021 missile.missile_blk_x = player.player_blk_x+(player.player_width/2);
michaeljson 0:3817adfaeb06 1022 missile.missile_blk_y = player.player_blk_y;
michaeljson 0:3817adfaeb06 1023 missile.status = PLAYER_MISSILE_ACTIVE;
michaeljson 0:3817adfaeb06 1024 }
michaeljson 0:3817adfaeb06 1025
michaeljson 0:3817adfaeb06 1026 // checks if player destroyed all enemies
michaeljson 0:3817adfaeb06 1027 if (numOfEnemies == 0)
michaeljson 0:3817adfaeb06 1028 {
wminix3 13:36cc024dcf6b 1029 // 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 1030 int time = bestTimer.read();
wminix3 13:36cc024dcf6b 1031 bestTimer.stop();
wminix3 13:36cc024dcf6b 1032 bestTimer.reset();
michaeljson 0:3817adfaeb06 1033 uLCD.cls();
wminix3 13:36cc024dcf6b 1034 char buffer[3] = {0};
wminix3 13:36cc024dcf6b 1035 char c = {0};
wminix3 13:36cc024dcf6b 1036 char *token;
wminix3 13:36cc024dcf6b 1037 int i = 0;
wminix3 13:36cc024dcf6b 1038 int storedTime = 999;
wminix3 13:36cc024dcf6b 1039 SDLock.lock();
wminix3 13:36cc024dcf6b 1040 FILE *sdtime;
wminix3 13:36cc024dcf6b 1041 sdtime=fopen("/sd/besttime.txt","r");
wminix3 13:36cc024dcf6b 1042 if(sdtime==NULL) pc.printf("file open error!\n\n\r"); // added this for open error check
wminix3 13:36cc024dcf6b 1043 while ((c != '\n') && (i < 3)) {
wminix3 13:36cc024dcf6b 1044 c = fgetc(sdtime);
wminix3 13:36cc024dcf6b 1045 buffer[i] = c;
wminix3 13:36cc024dcf6b 1046 i++;
wminix3 13:36cc024dcf6b 1047 }
wminix3 13:36cc024dcf6b 1048 token = strtok(buffer, "\n");
wminix3 13:36cc024dcf6b 1049 storedTime = atoi(token); // convert string from file to integer
wminix3 13:36cc024dcf6b 1050 fclose(sdtime);
wminix3 13:36cc024dcf6b 1051 if (time < storedTime) {
wminix3 13:36cc024dcf6b 1052 uLCD.locate(2,10);
wminix3 13:36cc024dcf6b 1053 uLCD.printf("NEW BEST TIME!");
wminix3 13:36cc024dcf6b 1054 uLCD.locate(2,11);
wminix3 13:36cc024dcf6b 1055 uLCD.printf("%d seconds!", time);
wminix3 13:36cc024dcf6b 1056 sdtime = fopen("/sd/besttime.txt", "w");
wminix3 13:36cc024dcf6b 1057 if (sdtime != NULL) {
wminix3 13:36cc024dcf6b 1058 fprintf(sdtime, "%d\r\n", time);
wminix3 13:36cc024dcf6b 1059 fclose(sdtime);
wminix3 13:36cc024dcf6b 1060 } else {
wminix3 13:36cc024dcf6b 1061 pc.printf("write: failed!\r\n");
wminix3 13:36cc024dcf6b 1062 }
wminix3 13:36cc024dcf6b 1063 }
wminix3 13:36cc024dcf6b 1064 SDLock.unlock();
wminix3 13:36cc024dcf6b 1065
michaeljson 0:3817adfaeb06 1066
michaeljson 0:3817adfaeb06 1067 bool win = true; // sets win to true, for win screen
michaeljson 0:3817adfaeb06 1068 begin_game = false;
michaeljson 0:3817adfaeb06 1069
wminix3 13:36cc024dcf6b 1070 // displays video clip ????
wminix3 13:36cc024dcf6b 1071 /*
michaeljson 0:3817adfaeb06 1072 uLCD.cls();
michaeljson 0:3817adfaeb06 1073 uLCD.media_init();
michaeljson 0:3817adfaeb06 1074 uLCD.set_sector_address(0x00, 0x00);
michaeljson 0:3817adfaeb06 1075 uLCD.display_video(0,0);
wminix3 5:b7934866b264 1076 Thread::wait(1000); // changed from wait(1) to Thread::wait(1000) since we're using threads -- Brice
wminix3 13:36cc024dcf6b 1077 */
michaeljson 0:3817adfaeb06 1078
wminix3 13:36cc024dcf6b 1079 //uLCD.cls();
michaeljson 0:3817adfaeb06 1080
michaeljson 0:3817adfaeb06 1081 // prints "Congratulations" on uLCD
michaeljson 0:3817adfaeb06 1082 uLCD.locate(win_x_pos,win_y_pos);
michaeljson 0:3817adfaeb06 1083 uLCD.printf("CONGRATULATIONS!");
michaeljson 0:3817adfaeb06 1084
michaeljson 0:3817adfaeb06 1085 // prints "Play Again?" and "Press pb..."
michaeljson 0:3817adfaeb06 1086 uLCD.locate(startover_x_pos, startover_y_pos);
michaeljson 0:3817adfaeb06 1087 uLCD.printf("Play Again?");
michaeljson 0:3817adfaeb06 1088 uLCD.locate(startover_x_pos, startover_y_pos+1);
michaeljson 0:3817adfaeb06 1089 uLCD.printf("Press pb...");
michaeljson 0:3817adfaeb06 1090
michaeljson 0:3817adfaeb06 1091 // waits at win screen until pushbutton is pressed
michaeljson 0:3817adfaeb06 1092 while (win)
michaeljson 0:3817adfaeb06 1093 {
michaeljson 0:3817adfaeb06 1094 // if pb is pressed, reset game to start menu
michaeljson 0:3817adfaeb06 1095 if (!pb)
michaeljson 0:3817adfaeb06 1096 {
michaeljson 0:3817adfaeb06 1097 win = false;
wminix3 5:b7934866b264 1098 Thread::wait(500); // changed from wait(0.5) to Thread::wait(500) since we're using threads
michaeljson 0:3817adfaeb06 1099 }
michaeljson 0:3817adfaeb06 1100 }
michaeljson 0:3817adfaeb06 1101
michaeljson 0:3817adfaeb06 1102 }
michaeljson 0:3817adfaeb06 1103
michaeljson 0:3817adfaeb06 1104 // checks if player was hit
michaeljson 0:3817adfaeb06 1105 if (hit_player)
michaeljson 0:3817adfaeb06 1106 {
michaeljson 0:3817adfaeb06 1107 // updates lives
wminix3 14:4e7608619043 1108 if (level == 1) {
wminix3 14:4e7608619043 1109 lives1 -= 1;
wminix3 14:4e7608619043 1110 Thread::wait(500); // changed from wait(0.5) to Thread::wait since we're using threads -- Brice
wminix3 14:4e7608619043 1111 hit_player = 0;
wminix3 14:4e7608619043 1112 player_show(&player);
wminix3 14:4e7608619043 1113 player.status = PLAYER_ALIVE;
michaeljson 0:3817adfaeb06 1114
michaeljson 0:3817adfaeb06 1115 // prints updated lives number
wminix3 14:4e7608619043 1116 uLCD.locate(0,0);
wminix3 14:4e7608619043 1117 uLCD.printf("Lives:%i", lives1);
wminix3 14:4e7608619043 1118 } else if (level == 2) {
wminix3 14:4e7608619043 1119 lives2 -= 1;
wminix3 14:4e7608619043 1120 Thread::wait(500); // changed from wait(0.5) to Thread::wait since we're using threads -- Brice
wminix3 14:4e7608619043 1121 hit_player = 0;
wminix3 14:4e7608619043 1122 player_show(&player);
wminix3 14:4e7608619043 1123 player.status = PLAYER_ALIVE;
wminix3 14:4e7608619043 1124
wminix3 14:4e7608619043 1125 // prints updated lives number
wminix3 14:4e7608619043 1126 uLCD.locate(0,0);
wminix3 14:4e7608619043 1127 uLCD.printf("Lives:%i", lives2);
wminix3 14:4e7608619043 1128 } else if (level == 3) {
wminix3 14:4e7608619043 1129 lives3 -= 1;
wminix3 14:4e7608619043 1130 Thread::wait(500); // changed from wait(0.5) to Thread::wait since we're using threads -- Brice
wminix3 14:4e7608619043 1131 hit_player = 0;
wminix3 14:4e7608619043 1132 player_show(&player);
wminix3 14:4e7608619043 1133 player.status = PLAYER_ALIVE;
wminix3 14:4e7608619043 1134
wminix3 14:4e7608619043 1135 // prints updated lives number
wminix3 14:4e7608619043 1136 uLCD.locate(0,0);
wminix3 14:4e7608619043 1137 uLCD.printf("Lives:%i", lives3);
wminix3 14:4e7608619043 1138 }
michaeljson 0:3817adfaeb06 1139 }
michaeljson 0:3817adfaeb06 1140
michaeljson 0:3817adfaeb06 1141 // if player loses all lives or enemy reaches the player
wminix3 14:4e7608619043 1142 if (lose || lives1 == 0 || lives2 == 0 || lives3 == 0)
michaeljson 0:3817adfaeb06 1143 {
michaeljson 0:3817adfaeb06 1144 begin_game = false; // set to false to end game
michaeljson 0:3817adfaeb06 1145 uLCD.cls();
michaeljson 0:3817adfaeb06 1146
michaeljson 0:3817adfaeb06 1147 gameover = true; // set to go to display gameover screen
michaeljson 0:3817adfaeb06 1148
michaeljson 0:3817adfaeb06 1149 // prints "GAMEOVER" to uLCD
michaeljson 0:3817adfaeb06 1150 uLCD.locate(gameover_x_pos, gameover_y_pos);
michaeljson 0:3817adfaeb06 1151 uLCD.printf("GAMEOVER");
wminix3 5:b7934866b264 1152 Thread::wait(1000); // changed from wait(1) to thread::wait since we're using threads -- Brice
michaeljson 0:3817adfaeb06 1153
michaeljson 0:3817adfaeb06 1154 // prints "Play Again?" and "Press pb..."
michaeljson 0:3817adfaeb06 1155 uLCD.locate(startover_x_pos, startover_y_pos);
michaeljson 0:3817adfaeb06 1156 uLCD.printf("Play Again?");
michaeljson 0:3817adfaeb06 1157 uLCD.locate(startover_x_pos, startover_y_pos+1);
michaeljson 0:3817adfaeb06 1158 uLCD.printf("Press pb...");
michaeljson 0:3817adfaeb06 1159
michaeljson 0:3817adfaeb06 1160 // stays in gameover screen until pb is pressed
michaeljson 0:3817adfaeb06 1161 while (gameover)
michaeljson 0:3817adfaeb06 1162 {
michaeljson 0:3817adfaeb06 1163 // if pb is pressed, game is reset to the game menu screen
michaeljson 0:3817adfaeb06 1164 if (!pb)
michaeljson 0:3817adfaeb06 1165 {
michaeljson 0:3817adfaeb06 1166 gameover = false;
michaeljson 0:3817adfaeb06 1167 game_menu = true;
wminix3 5:b7934866b264 1168 Thread::wait(500); // changed wait(0.5) to Thread::wait since we're using threads -- Brice
wminix3 5:b7934866b264 1169 }
wminix3 5:b7934866b264 1170 }
wminix3 5:b7934866b264 1171 }
wminix3 5:b7934866b264 1172
wminix3 5:b7934866b264 1173 }
wminix3 5:b7934866b264 1174 // game play loop
wminix3 5:b7934866b264 1175 while(begin_game2)
wminix3 5:b7934866b264 1176 {
wminix3 5:b7934866b264 1177 // updates score
wminix3 5:b7934866b264 1178 temp = score;
wminix3 5:b7934866b264 1179 score = (15-numOfEnemies)*15;
wminix3 5:b7934866b264 1180
wminix3 5:b7934866b264 1181 // prints score if score changes
wminix3 5:b7934866b264 1182 if (score != temp)
wminix3 5:b7934866b264 1183 {
wminix3 5:b7934866b264 1184 uLCD.locate(9,0);
wminix3 5:b7934866b264 1185 uLCD.printf("Score:%i", score);
wminix3 5:b7934866b264 1186 }
wminix3 5:b7934866b264 1187
wminix3 5:b7934866b264 1188 // move enemy
wminix3 5:b7934866b264 1189 enemy_motion();
wminix3 5:b7934866b264 1190
wminix3 5:b7934866b264 1191 // checks if player missile passes y-pos of row1
wminix3 5:b7934866b264 1192 if (missile.missile_blk_y+1-missile.missile_height <= enemy_1.enemy_blk_y
wminix3 5:b7934866b264 1193 && missile.missile_blk_y+1-missile.missile_height >= enemy_1.enemy_blk_y-enemy_1.enemy_height)
wminix3 5:b7934866b264 1194 {
wminix3 5:b7934866b264 1195 check_hit_enemy_row1();
wminix3 5:b7934866b264 1196 }
wminix3 5:b7934866b264 1197
wminix3 5:b7934866b264 1198 // checks if player missile passes y-pos of row2
wminix3 5:b7934866b264 1199 if (missile.missile_blk_y+1-missile.missile_height <= enemy_6.enemy_blk_y
wminix3 5:b7934866b264 1200 && missile.missile_blk_y+1-missile.missile_height >= enemy_6.enemy_blk_y-enemy_6.enemy_height)
wminix3 5:b7934866b264 1201 {
wminix3 5:b7934866b264 1202 check_hit_enemy_row2();
wminix3 5:b7934866b264 1203 }
wminix3 5:b7934866b264 1204
wminix3 5:b7934866b264 1205 // checks if player missile passes y-pos of row3
wminix3 5:b7934866b264 1206 if (missile.missile_blk_y+1-missile.missile_height <= enemy_11.enemy_blk_y
wminix3 5:b7934866b264 1207 && missile.missile_blk_y+1-missile.missile_height >= enemy_11.enemy_blk_y-enemy_11.enemy_height)
wminix3 5:b7934866b264 1208 {
wminix3 5:b7934866b264 1209 check_hit_enemy_row3();
wminix3 5:b7934866b264 1210 }
wminix3 5:b7934866b264 1211
wminix3 5:b7934866b264 1212 // Random Enemy Fire
wminix3 5:b7934866b264 1213 if (enemy_missile.status == ENEMY_MISSILE_INACTIVE)
wminix3 5:b7934866b264 1214 {
wminix3 5:b7934866b264 1215 random_attack_gen();
wminix3 5:b7934866b264 1216 }
wminix3 5:b7934866b264 1217
wminix3 5:b7934866b264 1218 // checks if enemy missile passes y-pos of player
wminix3 5:b7934866b264 1219 if (enemy_missile.missile_blk_y >= player.player_blk_y
wminix3 5:b7934866b264 1220 && enemy_missile.missile_blk_y <= player.player_blk_y+player.player_height)
wminix3 5:b7934866b264 1221 {
wminix3 5:b7934866b264 1222 check_player_hit();
wminix3 5:b7934866b264 1223 }
wminix3 5:b7934866b264 1224
wminix3 5:b7934866b264 1225 update_missile_pos(&missile); // updates player missile position
wminix3 5:b7934866b264 1226 update_enemy_missile_pos(&enemy_missile); // updates enemy missile position
wminix3 5:b7934866b264 1227
wminix3 5:b7934866b264 1228 // Player Movement checked with navigation switch
wminix3 16:e4e6515bdabb 1229 //if (myNav.left() && ((player.player_blk_x-3) > 0))
wminix3 16:e4e6515bdabb 1230 //{
wminix3 16:e4e6515bdabb 1231 // player_erase(&player);
wminix3 16:e4e6515bdabb 1232 // player.player_blk_x -= 3;
wminix3 16:e4e6515bdabb 1233 // player_show(&player);
wminix3 16:e4e6515bdabb 1234 //}
wminix3 16:e4e6515bdabb 1235 //else if (myNav.right() && ((player.player_blk_x+3) < (128-player.player_width)))
wminix3 16:e4e6515bdabb 1236 //{
wminix3 16:e4e6515bdabb 1237 // player_erase(&player);
wminix3 16:e4e6515bdabb 1238 // player.player_blk_x += 3;
wminix3 16:e4e6515bdabb 1239 // player_show(&player);
wminix3 16:e4e6515bdabb 1240 //}
wminix3 16:e4e6515bdabb 1241 //if (myNav.left() && ((player.player_blk_x-3) > 0))
wminix3 16:e4e6515bdabb 1242 float stickDist = stick.xAxis();
wminix3 16:e4e6515bdabb 1243 if ((stickDist < 0.0) && (player.player_blk_x + stickDist > 0.0))
wminix3 5:b7934866b264 1244 {
wminix3 5:b7934866b264 1245 player_erase(&player);
wminix3 16:e4e6515bdabb 1246 //player.player_blk_x -= 3;
wminix3 16:e4e6515bdabb 1247 player.player_blk_x += (int)(stickDist);
wminix3 5:b7934866b264 1248 player_show(&player);
wminix3 5:b7934866b264 1249 }
wminix3 16:e4e6515bdabb 1250 //else if (myNav.right() && ((player.player_blk_x+3) < (128-player.player_width)))
wminix3 16:e4e6515bdabb 1251 else if ((stickDist > 0.0) && ((player.player_blk_x + stickDist) < (128 - player.player_width)))
wminix3 5:b7934866b264 1252 {
wminix3 5:b7934866b264 1253 player_erase(&player);
wminix3 16:e4e6515bdabb 1254 player.player_blk_x += (int)(stickDist);
wminix3 5:b7934866b264 1255 player_show(&player);
wminix3 5:b7934866b264 1256 }
wminix3 5:b7934866b264 1257
wminix3 5:b7934866b264 1258 // Player Fire
wminix3 5:b7934866b264 1259 if (pb == 0 && missile.status == PLAYER_MISSILE_INACTIVE)
wminix3 5:b7934866b264 1260 {
wminix3 5:b7934866b264 1261 missile.missile_blk_x = player.player_blk_x+(player.player_width/2);
wminix3 5:b7934866b264 1262 missile.missile_blk_y = player.player_blk_y;
wminix3 5:b7934866b264 1263 missile.status = PLAYER_MISSILE_ACTIVE;
wminix3 5:b7934866b264 1264 }
wminix3 5:b7934866b264 1265
wminix3 5:b7934866b264 1266 // checks if player destroyed all enemies
wminix3 5:b7934866b264 1267 if (numOfEnemies == 0)
wminix3 5:b7934866b264 1268 {
wminix3 5:b7934866b264 1269 uLCD.cls();
wminix3 5:b7934866b264 1270
wminix3 5:b7934866b264 1271 bool win = true; // sets win to true, for win screen
wminix3 5:b7934866b264 1272 numWins += 1;
wminix3 5:b7934866b264 1273 if (numWins == 3) {
wminix3 10:1eeb21ef7b2c 1274 //begin_game2 = false; // Allow the mbed communication thread to change the begin_game2 bool to false after letting other mbed know.
wminix3 5:b7934866b264 1275 two_player_win = true;
wminix3 5:b7934866b264 1276 }
wminix3 5:b7934866b264 1277
wminix3 13:36cc024dcf6b 1278 // displays video clip ???
wminix3 13:36cc024dcf6b 1279 /*
wminix3 5:b7934866b264 1280 uLCD.cls();
wminix3 5:b7934866b264 1281 uLCD.media_init();
wminix3 5:b7934866b264 1282 uLCD.set_sector_address(0x00, 0x00);
wminix3 5:b7934866b264 1283 uLCD.display_video(0,0);
wminix3 13:36cc024dcf6b 1284 Thread::wait(1000); // changed from wait(1)
wminix3 13:36cc024dcf6b 1285 */
wminix3 5:b7934866b264 1286
wminix3 5:b7934866b264 1287 uLCD.cls();
wminix3 5:b7934866b264 1288 if (!two_player_win) {
wminix3 5:b7934866b264 1289 // prints "Number of Wins" on uLCD -- Brice. A step towards victory, not a complete victory.
wminix3 5:b7934866b264 1290 uLCD.locate(win_x_pos,win_y_pos);
wminix3 5:b7934866b264 1291 uLCD.printf("YOU HAVE %d WINS!", numWins);
wminix3 5:b7934866b264 1292
wminix3 5:b7934866b264 1293 // prints "Continue?" and "Press pb..." Keep trying to get points --Brice.
wminix3 5:b7934866b264 1294 uLCD.locate(startover_x_pos, startover_y_pos);
wminix3 5:b7934866b264 1295 uLCD.printf("Continue?");
wminix3 5:b7934866b264 1296 uLCD.locate(startover_x_pos, startover_y_pos+1);
wminix3 5:b7934866b264 1297 uLCD.printf("Press pb...");
wminix3 5:b7934866b264 1298
wminix3 5:b7934866b264 1299 // waits at win screen until pushbutton is pressed
wminix3 5:b7934866b264 1300 while (win)
wminix3 5:b7934866b264 1301 {
wminix3 5:b7934866b264 1302 // if pb is pressed, reset game to start menu
wminix3 5:b7934866b264 1303 if (!pb)
wminix3 5:b7934866b264 1304 {
wminix3 5:b7934866b264 1305 win = false;
wminix3 5:b7934866b264 1306 Thread::wait(500); // changed from wait(0.5) since we have threads -- Brice
wminix3 5:b7934866b264 1307 }
wminix3 5:b7934866b264 1308 }
wminix3 5:b7934866b264 1309 } else {
wminix3 5:b7934866b264 1310 // prints CONGRATULATIONS! since player has won. --Brice
wminix3 5:b7934866b264 1311 uLCD.locate(win_x_pos,win_y_pos);
wminix3 5:b7934866b264 1312 uLCD.printf("CONGRATULATIONS!");
wminix3 5:b7934866b264 1313
wminix3 5:b7934866b264 1314 // prints "Return to menu?" and "Press pb..." --Brice.
wminix3 5:b7934866b264 1315 uLCD.locate(startover_x_pos, startover_y_pos);
wminix3 5:b7934866b264 1316 uLCD.printf("Return to menu?");
wminix3 5:b7934866b264 1317 uLCD.locate(startover_x_pos, startover_y_pos+1);
wminix3 5:b7934866b264 1318 uLCD.printf("Press pb...");
wminix3 5:b7934866b264 1319
wminix3 5:b7934866b264 1320 // waits at win screen until pushbutton is pressed
wminix3 5:b7934866b264 1321 while (two_player_win)
wminix3 5:b7934866b264 1322 {
wminix3 5:b7934866b264 1323 // if pb is pressed, reset game to start menu
wminix3 5:b7934866b264 1324 if (!pb)
wminix3 5:b7934866b264 1325 {
wminix3 5:b7934866b264 1326 two_player_win = false; // close win -- Brice
wminix3 5:b7934866b264 1327 game_menu = true; // go back to menu -- Brice
wminix3 5:b7934866b264 1328 Thread::wait(500); // changed from wait(0.5) since we have threads -- Brice
wminix3 5:b7934866b264 1329 }
wminix3 5:b7934866b264 1330 }
wminix3 5:b7934866b264 1331 }
wminix3 5:b7934866b264 1332 }
wminix3 5:b7934866b264 1333
wminix3 5:b7934866b264 1334 // checks if player was hit
wminix3 5:b7934866b264 1335 if (hit_player)
wminix3 5:b7934866b264 1336 {
wminix3 5:b7934866b264 1337 // updates lives
wminix3 14:4e7608619043 1338 lives1 -= 1;
wminix3 5:b7934866b264 1339 Thread::wait(500); // changed from wait(0.5) since we're using threads --Brice
wminix3 5:b7934866b264 1340 hit_player = 0;
wminix3 5:b7934866b264 1341 player_show(&player);
wminix3 5:b7934866b264 1342 player.status = PLAYER_ALIVE;
wminix3 5:b7934866b264 1343
wminix3 5:b7934866b264 1344 // prints updated lives number
wminix3 5:b7934866b264 1345 uLCD.locate(0,0);
wminix3 14:4e7608619043 1346 uLCD.printf("Lives:%i", lives1);
wminix3 5:b7934866b264 1347 }
wminix3 5:b7934866b264 1348
wminix3 5:b7934866b264 1349 // if player loses all lives or enemy reaches the player
wminix3 14:4e7608619043 1350 if (lose || lives1 == 0)
wminix3 5:b7934866b264 1351 {
wminix3 5:b7934866b264 1352 //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 1353 uLCD.cls();
wminix3 5:b7934866b264 1354
wminix3 5:b7934866b264 1355 gameover = true; // set to go to display gameover screen
wminix3 5:b7934866b264 1356
wminix3 5:b7934866b264 1357 // prints "GAMEOVER" to uLCD
wminix3 5:b7934866b264 1358 uLCD.locate(gameover_x_pos, gameover_y_pos);
wminix3 5:b7934866b264 1359 uLCD.printf("YOU DIED");
wminix3 5:b7934866b264 1360 Thread::wait(1000); // changed from wait(1) since we have multiple threads -- Brice
wminix3 5:b7934866b264 1361
wminix3 5:b7934866b264 1362 // prints "Play Again?" and "Press pb..."
wminix3 5:b7934866b264 1363 uLCD.locate(startover_x_pos, startover_y_pos);
wminix3 5:b7934866b264 1364 uLCD.printf("Keep trying!");
wminix3 5:b7934866b264 1365 uLCD.locate(startover_x_pos, startover_y_pos+1);
wminix3 5:b7934866b264 1366 uLCD.printf("Press pb...");
wminix3 5:b7934866b264 1367
wminix3 5:b7934866b264 1368 // stays in gameover screen until pb is pressed
wminix3 5:b7934866b264 1369 while (gameover)
wminix3 5:b7934866b264 1370 {
wminix3 5:b7934866b264 1371 // if pb is pressed, game is reset to the game menu screen
wminix3 5:b7934866b264 1372 if (!pb)
wminix3 5:b7934866b264 1373 {
wminix3 5:b7934866b264 1374 gameover = false;
wminix3 5:b7934866b264 1375 //game_menu = true; // removed since other player must win three times for game to be over.
wminix3 5:b7934866b264 1376 Thread::wait(500); // changed from wait(0.5) since we have threads.
wminix3 5:b7934866b264 1377 }
wminix3 5:b7934866b264 1378 }
wminix3 5:b7934866b264 1379 }
wminix3 5:b7934866b264 1380 if (two_player_lose)
wminix3 5:b7934866b264 1381 {
wminix3 5:b7934866b264 1382 begin_game2 = false; // set to false to end game. End game since other player won.
wminix3 5:b7934866b264 1383 uLCD.cls();
wminix3 5:b7934866b264 1384
wminix3 5:b7934866b264 1385 gameover = true; // set to go to display gameover screen
wminix3 5:b7934866b264 1386
wminix3 5:b7934866b264 1387 // prints "GAMEOVER" to uLCD
wminix3 5:b7934866b264 1388 uLCD.locate(gameover_x_pos, gameover_y_pos);
wminix3 5:b7934866b264 1389 uLCD.printf("GAMEOVER");
wminix3 5:b7934866b264 1390 Thread::wait(1000); // thread wait since we have multiple threads -- Brice
wminix3 5:b7934866b264 1391
wminix3 5:b7934866b264 1392 // prints "Return to menu?" and "Press pb..."
wminix3 5:b7934866b264 1393 uLCD.locate(startover_x_pos, startover_y_pos);
wminix3 5:b7934866b264 1394 uLCD.printf("Return to menu?");
wminix3 5:b7934866b264 1395 uLCD.locate(startover_x_pos, startover_y_pos+1);
wminix3 5:b7934866b264 1396 uLCD.printf("Press pb...");
wminix3 5:b7934866b264 1397
wminix3 5:b7934866b264 1398 // stays in gameover screen until pb is pressed
wminix3 5:b7934866b264 1399 while (gameover)
wminix3 5:b7934866b264 1400 {
wminix3 5:b7934866b264 1401 // if pb is pressed, game is reset to the game menu screen
wminix3 5:b7934866b264 1402 if (!pb)
wminix3 5:b7934866b264 1403 {
wminix3 5:b7934866b264 1404 gameover = false;
wminix3 5:b7934866b264 1405 two_player_lose = false; // end lose.
wminix3 5:b7934866b264 1406 game_menu = true; // removed since other player must win three times for game to be over.
wminix3 5:b7934866b264 1407 Thread::wait(500); // changed from wait(0.5) since we have threads.
michaeljson 0:3817adfaeb06 1408 }
michaeljson 0:3817adfaeb06 1409 }
michaeljson 0:3817adfaeb06 1410 }
michaeljson 0:3817adfaeb06 1411
michaeljson 0:3817adfaeb06 1412 }
michaeljson 0:3817adfaeb06 1413 }
michaeljson 0:3817adfaeb06 1414 }