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

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

Fork of Two-PlayerSpaceInvaders by William Minix

test

Committer:
wminix3
Date:
Mon Apr 26 21:53:25 2021 +0000
Revision:
23:56f6a12aaebd
Parent:
22:a907eeb128a4
Child:
24:ea19c7e8a479
Added barriers that both enemies and the player can damage. More true to original space invaders.

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