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:
michaeljson
Date:
Tue Mar 15 19:46:47 2016 +0000
Revision:
1:618aa2c4ca6a
Parent:
0:3817adfaeb06
Child:
2:4a3f97570578
fixed bug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
michaeljson 0:3817adfaeb06 1 #include "mbed.h"
michaeljson 0:3817adfaeb06 2 #include "SDFileSystem.h"
michaeljson 0:3817adfaeb06 3 #include "wave_player.h"
michaeljson 0:3817adfaeb06 4 #include "enemy.h"
michaeljson 0:3817adfaeb06 5 #include "player.h"
michaeljson 0:3817adfaeb06 6 #include "missile.h"
michaeljson 0:3817adfaeb06 7 #include "globals.h"
michaeljson 0:3817adfaeb06 8 #include "rtos.h"
michaeljson 0:3817adfaeb06 9 #include <string>
michaeljson 0:3817adfaeb06 10
michaeljson 0:3817adfaeb06 11 /* ==== Navigation Switch ===== */
michaeljson 0:3817adfaeb06 12 class Nav_Switch
michaeljson 0:3817adfaeb06 13 {
michaeljson 0:3817adfaeb06 14 public:
michaeljson 0:3817adfaeb06 15 Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire);
michaeljson 0:3817adfaeb06 16 int read();
michaeljson 0:3817adfaeb06 17 //boolean functions to test each switch
michaeljson 0:3817adfaeb06 18 bool up();
michaeljson 0:3817adfaeb06 19 bool down();
michaeljson 0:3817adfaeb06 20 bool left();
michaeljson 0:3817adfaeb06 21 bool right();
michaeljson 0:3817adfaeb06 22 bool fire();
michaeljson 0:3817adfaeb06 23 //automatic read on RHS
michaeljson 0:3817adfaeb06 24 operator int ();
michaeljson 0:3817adfaeb06 25 //index to any switch array style
michaeljson 0:3817adfaeb06 26 bool operator[](int index) {
michaeljson 0:3817adfaeb06 27 return _pins[index];
michaeljson 0:3817adfaeb06 28 };
michaeljson 0:3817adfaeb06 29 private:
michaeljson 0:3817adfaeb06 30 BusIn _pins;
michaeljson 0:3817adfaeb06 31
michaeljson 0:3817adfaeb06 32 };
michaeljson 0:3817adfaeb06 33 Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire):
michaeljson 0:3817adfaeb06 34 _pins(up, down, left, right, fire)
michaeljson 0:3817adfaeb06 35 {
michaeljson 0:3817adfaeb06 36 _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise
michaeljson 0:3817adfaeb06 37 wait(0.001); //delays just a bit for pullups to pull inputs high
michaeljson 0:3817adfaeb06 38 }
michaeljson 0:3817adfaeb06 39 inline bool Nav_Switch::up()
michaeljson 0:3817adfaeb06 40 {
michaeljson 0:3817adfaeb06 41 return !(_pins[0]);
michaeljson 0:3817adfaeb06 42 }
michaeljson 0:3817adfaeb06 43 inline bool Nav_Switch::down()
michaeljson 0:3817adfaeb06 44 {
michaeljson 0:3817adfaeb06 45 return !(_pins[1]);
michaeljson 0:3817adfaeb06 46 }
michaeljson 0:3817adfaeb06 47 inline bool Nav_Switch::left()
michaeljson 0:3817adfaeb06 48 {
michaeljson 0:3817adfaeb06 49 return !(_pins[2]);
michaeljson 0:3817adfaeb06 50 }
michaeljson 0:3817adfaeb06 51 inline bool Nav_Switch::right()
michaeljson 0:3817adfaeb06 52 {
michaeljson 0:3817adfaeb06 53 return !(_pins[3]);
michaeljson 0:3817adfaeb06 54 }
michaeljson 0:3817adfaeb06 55 inline bool Nav_Switch::fire()
michaeljson 0:3817adfaeb06 56 {
michaeljson 0:3817adfaeb06 57 return !(_pins[4]);
michaeljson 0:3817adfaeb06 58 }
michaeljson 0:3817adfaeb06 59 inline int Nav_Switch::read()
michaeljson 0:3817adfaeb06 60 {
michaeljson 0:3817adfaeb06 61 return _pins.read();
michaeljson 0:3817adfaeb06 62 }
michaeljson 0:3817adfaeb06 63 inline Nav_Switch::operator int ()
michaeljson 0:3817adfaeb06 64 {
michaeljson 0:3817adfaeb06 65 return _pins.read();
michaeljson 0:3817adfaeb06 66 }
michaeljson 0:3817adfaeb06 67
michaeljson 0:3817adfaeb06 68 // Platform initialization
michaeljson 0:3817adfaeb06 69 uLCD_4DGL uLCD(p28,p27,p29); // LCD (serial tx, serial rx, reset pin;)
michaeljson 0:3817adfaeb06 70 AnalogOut DACout(p18); // speaker
michaeljson 0:3817adfaeb06 71 wave_player waver(&DACout); // wav player
michaeljson 0:3817adfaeb06 72 SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD card and filesystem (mosi, miso, sck, cs)
michaeljson 0:3817adfaeb06 73 Nav_Switch myNav(p13, p10, p11, p9, p12); //pin order on Sparkfun breakout
michaeljson 0:3817adfaeb06 74 DigitalIn pb(p15); // push button for player misisle fire
michaeljson 0:3817adfaeb06 75
michaeljson 0:3817adfaeb06 76 // Initialize all global enemy objects
michaeljson 0:3817adfaeb06 77 enemy_t enemy_1;
michaeljson 0:3817adfaeb06 78 enemy_t enemy_2;
michaeljson 0:3817adfaeb06 79 enemy_t enemy_3;
michaeljson 0:3817adfaeb06 80 enemy_t enemy_4;
michaeljson 0:3817adfaeb06 81 enemy_t enemy_5;
michaeljson 0:3817adfaeb06 82 enemy_t enemy_6;
michaeljson 0:3817adfaeb06 83 enemy_t enemy_7;
michaeljson 0:3817adfaeb06 84 enemy_t enemy_8;
michaeljson 0:3817adfaeb06 85 enemy_t enemy_9;
michaeljson 0:3817adfaeb06 86 enemy_t enemy_10;
michaeljson 0:3817adfaeb06 87 enemy_t enemy_11;
michaeljson 0:3817adfaeb06 88 enemy_t enemy_12;
michaeljson 0:3817adfaeb06 89 enemy_t enemy_13;
michaeljson 0:3817adfaeb06 90 enemy_t enemy_14;
michaeljson 0:3817adfaeb06 91 enemy_t enemy_15;
michaeljson 0:3817adfaeb06 92
michaeljson 0:3817adfaeb06 93 // Initialize variables
michaeljson 0:3817adfaeb06 94 int numOfEnemies = 0;
michaeljson 0:3817adfaeb06 95 int ENEMY_MOVE = 1;
michaeljson 0:3817adfaeb06 96 int MOVE_DOWN = 0;
michaeljson 0:3817adfaeb06 97 int DIRECTION = 1;
michaeljson 0:3817adfaeb06 98 int firing_col = 0;
michaeljson 0:3817adfaeb06 99 int hit_player = 0;
michaeljson 0:3817adfaeb06 100 bool lose = false;
michaeljson 0:3817adfaeb06 101 int lives = 3;
michaeljson 0:3817adfaeb06 102 bool game_menu = false;
michaeljson 0:3817adfaeb06 103 bool begin_game = false;
michaeljson 0:3817adfaeb06 104 bool gameover = false;
michaeljson 0:3817adfaeb06 105
michaeljson 0:3817adfaeb06 106 // Initialize global player object
michaeljson 0:3817adfaeb06 107 player_t player;
michaeljson 0:3817adfaeb06 108
michaeljson 0:3817adfaeb06 109 // Intialize global player and enemy missile
michaeljson 0:3817adfaeb06 110 missile_t missile; // player missile
michaeljson 0:3817adfaeb06 111 missile_t enemy_missile; // enemy missile
michaeljson 0:3817adfaeb06 112
michaeljson 0:3817adfaeb06 113 // Array of enemy objects
michaeljson 0:3817adfaeb06 114 enemy_t * enemyArray[15];
michaeljson 0:3817adfaeb06 115
michaeljson 0:3817adfaeb06 116 // Function Prototypes
michaeljson 0:3817adfaeb06 117 void move_enemy_down();
michaeljson 0:3817adfaeb06 118 void playstart(void const *args);
michaeljson 0:3817adfaeb06 119
michaeljson 0:3817adfaeb06 120 // Draws the enemies at the initial starting location
michaeljson 0:3817adfaeb06 121 void draw_enemies_level()
michaeljson 0:3817adfaeb06 122 {
michaeljson 0:3817adfaeb06 123 // Initialize local variables
michaeljson 0:3817adfaeb06 124 unsigned int start_x_pos = 12;
michaeljson 0:3817adfaeb06 125 unsigned int start_enemy_y_pos = 20;
michaeljson 0:3817adfaeb06 126 numOfEnemies = 15;
michaeljson 0:3817adfaeb06 127
michaeljson 0:3817adfaeb06 128 // First Row of Enemies
michaeljson 0:3817adfaeb06 129 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 130 enemy_show(&enemy_1); // displays the enemy on uLCD
michaeljson 0:3817adfaeb06 131
michaeljson 0:3817adfaeb06 132 enemy_init(&enemy_2,start_x_pos+15,start_enemy_y_pos,WHITE);
michaeljson 0:3817adfaeb06 133 enemy_show(&enemy_2);
michaeljson 0:3817adfaeb06 134
michaeljson 0:3817adfaeb06 135 enemy_init(&enemy_3,start_x_pos+30,start_enemy_y_pos,WHITE);
michaeljson 0:3817adfaeb06 136 enemy_show(&enemy_3);
michaeljson 0:3817adfaeb06 137
michaeljson 0:3817adfaeb06 138 enemy_init(&enemy_4,start_x_pos+45,start_enemy_y_pos,WHITE);
michaeljson 0:3817adfaeb06 139 enemy_show(&enemy_4);
michaeljson 0:3817adfaeb06 140
michaeljson 0:3817adfaeb06 141 enemy_init(&enemy_5,start_x_pos+60,start_enemy_y_pos,WHITE);
michaeljson 0:3817adfaeb06 142 enemy_show(&enemy_5);
michaeljson 0:3817adfaeb06 143
michaeljson 0:3817adfaeb06 144 // Second Row of Enemies
michaeljson 0:3817adfaeb06 145 enemy_init(&enemy_6,start_x_pos,start_enemy_y_pos+12,WHITE);
michaeljson 0:3817adfaeb06 146 enemy_show(&enemy_6);
michaeljson 0:3817adfaeb06 147
michaeljson 0:3817adfaeb06 148 enemy_init(&enemy_7,start_x_pos+15,start_enemy_y_pos+12,WHITE);
michaeljson 0:3817adfaeb06 149 enemy_show(&enemy_7);
michaeljson 0:3817adfaeb06 150
michaeljson 0:3817adfaeb06 151 enemy_init(&enemy_8,start_x_pos+30,start_enemy_y_pos+12,WHITE);
michaeljson 0:3817adfaeb06 152 enemy_show(&enemy_8);
michaeljson 0:3817adfaeb06 153
michaeljson 0:3817adfaeb06 154 enemy_init(&enemy_9,start_x_pos+45,start_enemy_y_pos+12,WHITE);
michaeljson 0:3817adfaeb06 155 enemy_show(&enemy_9);
michaeljson 0:3817adfaeb06 156
michaeljson 0:3817adfaeb06 157 enemy_init(&enemy_10,start_x_pos+60,start_enemy_y_pos+12,WHITE);
michaeljson 0:3817adfaeb06 158 enemy_show(&enemy_10);
michaeljson 0:3817adfaeb06 159
michaeljson 0:3817adfaeb06 160 // Third Row of Enemies
michaeljson 0:3817adfaeb06 161 enemy_init(&enemy_11,start_x_pos,start_enemy_y_pos+24,WHITE);
michaeljson 0:3817adfaeb06 162 enemy_show(&enemy_11);
michaeljson 0:3817adfaeb06 163
michaeljson 0:3817adfaeb06 164 enemy_init(&enemy_12,start_x_pos+15,start_enemy_y_pos+24,WHITE);
michaeljson 0:3817adfaeb06 165 enemy_show(&enemy_12);
michaeljson 0:3817adfaeb06 166
michaeljson 0:3817adfaeb06 167 enemy_init(&enemy_13,start_x_pos+30,start_enemy_y_pos+24,WHITE);
michaeljson 0:3817adfaeb06 168 enemy_show(&enemy_13);
michaeljson 0:3817adfaeb06 169
michaeljson 0:3817adfaeb06 170 enemy_init(&enemy_14,start_x_pos+45,start_enemy_y_pos+24,WHITE);
michaeljson 0:3817adfaeb06 171 enemy_show(&enemy_14);
michaeljson 0:3817adfaeb06 172
michaeljson 0:3817adfaeb06 173 enemy_init(&enemy_15,start_x_pos+60,start_enemy_y_pos+24,WHITE);
michaeljson 0:3817adfaeb06 174 enemy_show(&enemy_15);
michaeljson 0:3817adfaeb06 175
michaeljson 0:3817adfaeb06 176 // Put enemy objects into array
michaeljson 0:3817adfaeb06 177 enemyArray[0] = &enemy_1;
michaeljson 0:3817adfaeb06 178 enemyArray[1] = &enemy_2;
michaeljson 0:3817adfaeb06 179 enemyArray[2] = &enemy_3;
michaeljson 0:3817adfaeb06 180 enemyArray[3] = &enemy_4;
michaeljson 0:3817adfaeb06 181 enemyArray[4] = &enemy_5;
michaeljson 0:3817adfaeb06 182 enemyArray[5] = &enemy_6;
michaeljson 0:3817adfaeb06 183 enemyArray[6] = &enemy_7;
michaeljson 0:3817adfaeb06 184 enemyArray[7] = &enemy_8;
michaeljson 0:3817adfaeb06 185 enemyArray[8] = &enemy_9;
michaeljson 0:3817adfaeb06 186 enemyArray[9] = &enemy_10;
michaeljson 0:3817adfaeb06 187 enemyArray[10] = &enemy_11;
michaeljson 0:3817adfaeb06 188 enemyArray[11] = &enemy_12;
michaeljson 0:3817adfaeb06 189 enemyArray[12] = &enemy_13;
michaeljson 0:3817adfaeb06 190 enemyArray[13] = &enemy_14;
michaeljson 0:3817adfaeb06 191 enemyArray[14] = &enemy_15;
michaeljson 0:3817adfaeb06 192 }
michaeljson 0:3817adfaeb06 193
michaeljson 0:3817adfaeb06 194 // Draws the player at the initial starting location
michaeljson 0:3817adfaeb06 195 void draw_initial_player()
michaeljson 0:3817adfaeb06 196 {
michaeljson 0:3817adfaeb06 197 int start_x_pos = 59;
michaeljson 0:3817adfaeb06 198 int start_y_pos = 110;
michaeljson 0:3817adfaeb06 199
michaeljson 0:3817adfaeb06 200 player_init(&player,start_x_pos,start_y_pos,WHITE); // intialize x-pos and y-pos and color of player
michaeljson 0:3817adfaeb06 201 player_show(&player); // display player on uLCD
michaeljson 0:3817adfaeb06 202 }
michaeljson 0:3817adfaeb06 203
michaeljson 0:3817adfaeb06 204 // Checks all enemies in row 1
michaeljson 0:3817adfaeb06 205 void check_hit_enemy_row1()
michaeljson 0:3817adfaeb06 206 {
michaeljson 0:3817adfaeb06 207 int hit_enemy;
michaeljson 0:3817adfaeb06 208
michaeljson 0:3817adfaeb06 209 // First Row of Enemies
michaeljson 0:3817adfaeb06 210 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 211 numOfEnemies = numOfEnemies - hit_enemy; // updates the number of enemies left
michaeljson 0:3817adfaeb06 212 hit_enemy = check_enemy(&enemy_2, &missile);
michaeljson 0:3817adfaeb06 213 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 214 hit_enemy = check_enemy(&enemy_3, &missile);
michaeljson 0:3817adfaeb06 215 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 216 hit_enemy = check_enemy(&enemy_4, &missile);
michaeljson 0:3817adfaeb06 217 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 218 hit_enemy = check_enemy(&enemy_5, &missile);
michaeljson 0:3817adfaeb06 219 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 220 }
michaeljson 0:3817adfaeb06 221
michaeljson 0:3817adfaeb06 222 // Same as check_hit_enemy_row1, but checks enemies at row 2
michaeljson 0:3817adfaeb06 223 void check_hit_enemy_row2()
michaeljson 0:3817adfaeb06 224 {
michaeljson 0:3817adfaeb06 225 int hit_enemy;
michaeljson 0:3817adfaeb06 226
michaeljson 0:3817adfaeb06 227 // Second Row of Enemies
michaeljson 0:3817adfaeb06 228 hit_enemy = check_enemy(&enemy_6, &missile);
michaeljson 0:3817adfaeb06 229 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 230 hit_enemy = check_enemy(&enemy_7, &missile);
michaeljson 0:3817adfaeb06 231 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 232 hit_enemy = check_enemy(&enemy_8, &missile);
michaeljson 0:3817adfaeb06 233 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 234 hit_enemy = check_enemy(&enemy_9, &missile);
michaeljson 0:3817adfaeb06 235 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 236 hit_enemy = check_enemy(&enemy_10, &missile);
michaeljson 0:3817adfaeb06 237 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 238 }
michaeljson 0:3817adfaeb06 239
michaeljson 0:3817adfaeb06 240 // Same as check_hit_enemy_row1, but checks enemies at row 3
michaeljson 0:3817adfaeb06 241 void check_hit_enemy_row3()
michaeljson 0:3817adfaeb06 242 {
michaeljson 0:3817adfaeb06 243 int hit_enemy;
michaeljson 0:3817adfaeb06 244
michaeljson 0:3817adfaeb06 245 // Third Row of Enemies
michaeljson 0:3817adfaeb06 246 hit_enemy = check_enemy(&enemy_11, &missile);
michaeljson 0:3817adfaeb06 247 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 248 hit_enemy = check_enemy(&enemy_12, &missile);
michaeljson 0:3817adfaeb06 249 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 250 hit_enemy = check_enemy(&enemy_13, &missile);
michaeljson 0:3817adfaeb06 251 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 252 hit_enemy = check_enemy(&enemy_14, &missile);
michaeljson 0:3817adfaeb06 253 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 254 hit_enemy = check_enemy(&enemy_15, &missile);
michaeljson 0:3817adfaeb06 255 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 256 }
michaeljson 0:3817adfaeb06 257
michaeljson 0:3817adfaeb06 258 // Checks if player is hit
michaeljson 0:3817adfaeb06 259 void check_player_hit()
michaeljson 0:3817adfaeb06 260 {
michaeljson 0:3817adfaeb06 261 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 262 }
michaeljson 0:3817adfaeb06 263
michaeljson 0:3817adfaeb06 264 // Randomly selects an enemy to fire and updates the position of where the missile will fire from
michaeljson 0:3817adfaeb06 265 void random_attack_gen()
michaeljson 0:3817adfaeb06 266 {
michaeljson 0:3817adfaeb06 267 firing_col = rand() % 5; // selects a random column
michaeljson 0:3817adfaeb06 268
michaeljson 0:3817adfaeb06 269 // first checks if the 3rd row closest to the player is alive
michaeljson 0:3817adfaeb06 270 if (enemyArray[firing_col+10]->status == ENEMY_ALIVE)
michaeljson 0:3817adfaeb06 271 {
michaeljson 0:3817adfaeb06 272 // If alive, the enemy missile position is updated to the center of the enemy
michaeljson 0:3817adfaeb06 273 enemy_missile.missile_blk_x = enemyArray[firing_col+10]->enemy_blk_x + (enemyArray[firing_col+10]->enemy_width/2);
michaeljson 0:3817adfaeb06 274 enemy_missile.missile_blk_y = enemyArray[firing_col+10]->enemy_blk_y + enemyArray[firing_col+10]->enemy_height + 1;
michaeljson 0:3817adfaeb06 275 enemy_missile.status = ENEMY_MISSILE_ACTIVE; // sets the enemy missile as active
michaeljson 0:3817adfaeb06 276 }
michaeljson 0:3817adfaeb06 277 // if enemy at 3rd row is dead, checks the enemy in the 2nd row
michaeljson 0:3817adfaeb06 278 else if (enemyArray[firing_col+5]->status == ENEMY_ALIVE)
michaeljson 0:3817adfaeb06 279 {
michaeljson 0:3817adfaeb06 280 // If alive, the enemy missile position is updated to the center of the enemy
michaeljson 0:3817adfaeb06 281 enemy_missile.missile_blk_x = enemyArray[firing_col+5]->enemy_blk_x + (enemyArray[firing_col+5]->enemy_width/2);
michaeljson 0:3817adfaeb06 282 enemy_missile.missile_blk_y = enemyArray[firing_col+5]->enemy_blk_y + enemyArray[firing_col+5]->enemy_height + 1;
michaeljson 0:3817adfaeb06 283 enemy_missile.status = ENEMY_MISSILE_ACTIVE;
michaeljson 0:3817adfaeb06 284 }
michaeljson 0:3817adfaeb06 285 // if enemy at 2nd and 3rd row is dead, checks the enemy in the 1st row
michaeljson 0:3817adfaeb06 286 else if (enemyArray[firing_col]->status == ENEMY_ALIVE)
michaeljson 0:3817adfaeb06 287 {
michaeljson 0:3817adfaeb06 288 // If alive, the enemy missile position is updated to the center of the enemy
michaeljson 0:3817adfaeb06 289 enemy_missile.missile_blk_x = enemyArray[firing_col]->enemy_blk_x + (enemyArray[firing_col]->enemy_width/2);
michaeljson 0:3817adfaeb06 290 enemy_missile.missile_blk_y = enemyArray[firing_col]->enemy_blk_y + enemyArray[firing_col]->enemy_height + 1;
michaeljson 0:3817adfaeb06 291 enemy_missile.status = ENEMY_MISSILE_ACTIVE;
michaeljson 0:3817adfaeb06 292 }
michaeljson 0:3817adfaeb06 293 }
michaeljson 0:3817adfaeb06 294
michaeljson 0:3817adfaeb06 295 // moves the enemy
michaeljson 0:3817adfaeb06 296 void enemy_motion()
michaeljson 0:3817adfaeb06 297 {
michaeljson 0:3817adfaeb06 298 // will move the enemy every 6 loops
michaeljson 0:3817adfaeb06 299 if (ENEMY_MOVE % 6 == 0)
michaeljson 0:3817adfaeb06 300 {
michaeljson 0:3817adfaeb06 301 // FIrst Row of Enemies
michaeljson 0:3817adfaeb06 302 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 303 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 304 MOVE_DOWN = move_enemy(&enemy_3, MOVE_DOWN, DIRECTION); // MOVE_DOWN will be 2 if enemies reach the player, otherwise 0
michaeljson 0:3817adfaeb06 305 MOVE_DOWN = move_enemy(&enemy_4, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 306 MOVE_DOWN = move_enemy(&enemy_5, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 307
michaeljson 0:3817adfaeb06 308 // Second Row of Enemies
michaeljson 0:3817adfaeb06 309 MOVE_DOWN = move_enemy(&enemy_6, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 310 MOVE_DOWN = move_enemy(&enemy_7, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 311 MOVE_DOWN = move_enemy(&enemy_8, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 312 MOVE_DOWN = move_enemy(&enemy_9, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 313 MOVE_DOWN = move_enemy(&enemy_10, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 314
michaeljson 0:3817adfaeb06 315 // Third Row of Enemies
michaeljson 0:3817adfaeb06 316 MOVE_DOWN = move_enemy(&enemy_11, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 317 MOVE_DOWN = move_enemy(&enemy_12, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 318 MOVE_DOWN = move_enemy(&enemy_13, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 319 MOVE_DOWN = move_enemy(&enemy_14, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 320 MOVE_DOWN = move_enemy(&enemy_15, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 321
michaeljson 0:3817adfaeb06 322 // if MOVE_DOWN is 2, then the enemies have reached the player
michaeljson 0:3817adfaeb06 323 if (MOVE_DOWN == 2)
michaeljson 0:3817adfaeb06 324 {
michaeljson 0:3817adfaeb06 325 lose = true; // set global lose to true to go to gameover screen
michaeljson 0:3817adfaeb06 326 }
michaeljson 0:3817adfaeb06 327
michaeljson 0:3817adfaeb06 328 // if MOVE_DOWN is 1, update the y-pos of the enemies
michaeljson 0:3817adfaeb06 329 if (MOVE_DOWN == 1)
michaeljson 0:3817adfaeb06 330 {
michaeljson 0:3817adfaeb06 331 move_enemy_down(); // updates the y-pos of the enemies
michaeljson 0:3817adfaeb06 332
michaeljson 0:3817adfaeb06 333 // Flips the direction when the enemy moves down
michaeljson 0:3817adfaeb06 334 if (DIRECTION == 1)
michaeljson 0:3817adfaeb06 335 {
michaeljson 0:3817adfaeb06 336 DIRECTION = 2;
michaeljson 0:3817adfaeb06 337 }
michaeljson 0:3817adfaeb06 338 else
michaeljson 0:3817adfaeb06 339 {
michaeljson 0:3817adfaeb06 340 DIRECTION = 1;
michaeljson 0:3817adfaeb06 341 }
michaeljson 0:3817adfaeb06 342 MOVE_DOWN = 0; // sets MOVE_DOWN back to 0 to stop down movement until
michaeljson 0:3817adfaeb06 343 }
michaeljson 0:3817adfaeb06 344
michaeljson 0:3817adfaeb06 345 ENEMY_MOVE += 1;
michaeljson 0:3817adfaeb06 346 }
michaeljson 0:3817adfaeb06 347 else
michaeljson 0:3817adfaeb06 348 {
michaeljson 0:3817adfaeb06 349 ENEMY_MOVE += 1;
michaeljson 0:3817adfaeb06 350 }
michaeljson 0:3817adfaeb06 351 }
michaeljson 0:3817adfaeb06 352
michaeljson 0:3817adfaeb06 353 // moves all the enemies down in the y-direction
michaeljson 0:3817adfaeb06 354 void move_enemy_down()
michaeljson 0:3817adfaeb06 355 {
michaeljson 0:3817adfaeb06 356 // First Row of Enemies
michaeljson 0:3817adfaeb06 357 enemy_erase(&enemy_1);
michaeljson 0:3817adfaeb06 358 enemy_erase(&enemy_2);
michaeljson 0:3817adfaeb06 359 enemy_erase(&enemy_3);
michaeljson 0:3817adfaeb06 360 enemy_erase(&enemy_4);
michaeljson 0:3817adfaeb06 361 enemy_erase(&enemy_5);
michaeljson 0:3817adfaeb06 362
michaeljson 0:3817adfaeb06 363 enemy_1.enemy_blk_y += enemy_1.enemy_height+4;
michaeljson 0:3817adfaeb06 364 enemy_2.enemy_blk_y += enemy_2.enemy_height+4;
michaeljson 0:3817adfaeb06 365 enemy_3.enemy_blk_y += enemy_3.enemy_height+4;
michaeljson 0:3817adfaeb06 366 enemy_4.enemy_blk_y += enemy_4.enemy_height+4;
michaeljson 0:3817adfaeb06 367 enemy_5.enemy_blk_y += enemy_5.enemy_height+4;
michaeljson 0:3817adfaeb06 368
michaeljson 0:3817adfaeb06 369 // Second Row of Enemies
michaeljson 0:3817adfaeb06 370 enemy_erase(&enemy_6);
michaeljson 0:3817adfaeb06 371 enemy_erase(&enemy_7);
michaeljson 0:3817adfaeb06 372 enemy_erase(&enemy_8);
michaeljson 0:3817adfaeb06 373 enemy_erase(&enemy_9);
michaeljson 0:3817adfaeb06 374 enemy_erase(&enemy_10);
michaeljson 0:3817adfaeb06 375
michaeljson 0:3817adfaeb06 376 enemy_6.enemy_blk_y += enemy_6.enemy_height+4;
michaeljson 0:3817adfaeb06 377 enemy_7.enemy_blk_y += enemy_7.enemy_height+4;
michaeljson 0:3817adfaeb06 378 enemy_8.enemy_blk_y += enemy_8.enemy_height+4;
michaeljson 0:3817adfaeb06 379 enemy_9.enemy_blk_y += enemy_9.enemy_height+4;
michaeljson 0:3817adfaeb06 380 enemy_10.enemy_blk_y += enemy_10.enemy_height+4;
michaeljson 0:3817adfaeb06 381
michaeljson 0:3817adfaeb06 382 // Third Row of Enemies
michaeljson 0:3817adfaeb06 383 enemy_erase(&enemy_11);
michaeljson 0:3817adfaeb06 384 enemy_erase(&enemy_12);
michaeljson 0:3817adfaeb06 385 enemy_erase(&enemy_13);
michaeljson 0:3817adfaeb06 386 enemy_erase(&enemy_14);
michaeljson 0:3817adfaeb06 387 enemy_erase(&enemy_15);
michaeljson 0:3817adfaeb06 388
michaeljson 0:3817adfaeb06 389 enemy_11.enemy_blk_y += enemy_11.enemy_height+4;
michaeljson 0:3817adfaeb06 390 enemy_12.enemy_blk_y += enemy_12.enemy_height+4;
michaeljson 0:3817adfaeb06 391 enemy_13.enemy_blk_y += enemy_13.enemy_height+4;
michaeljson 0:3817adfaeb06 392 enemy_14.enemy_blk_y += enemy_14.enemy_height+4;
michaeljson 0:3817adfaeb06 393 enemy_15.enemy_blk_y += enemy_15.enemy_height+4;
michaeljson 0:3817adfaeb06 394 }
michaeljson 0:3817adfaeb06 395
michaeljson 0:3817adfaeb06 396 // thread that plays sounds during game
michaeljson 0:3817adfaeb06 397 void playstart(void const *args)//Th
michaeljson 0:3817adfaeb06 398 { //Depending on the state of the game,
michaeljson 0:3817adfaeb06 399 //queue either screen music or play sound effect upon fire
michaeljson 0:3817adfaeb06 400 while(true) {
michaeljson 0:3817adfaeb06 401 FILE *wave_file;
michaeljson 0:3817adfaeb06 402
michaeljson 0:3817adfaeb06 403 // plays intro music during menu screen
michaeljson 0:3817adfaeb06 404 while(game_menu)
michaeljson 0:3817adfaeb06 405 {
michaeljson 0:3817adfaeb06 406 wave_file=fopen("/sd/wavfiles/spacey_sound.wav","r");
michaeljson 0:3817adfaeb06 407
michaeljson 0:3817adfaeb06 408 waver.play(wave_file);
michaeljson 0:3817adfaeb06 409 fclose(wave_file);
michaeljson 0:3817adfaeb06 410 }
michaeljson 0:3817adfaeb06 411
michaeljson 0:3817adfaeb06 412 // Checks in game sound conditions
michaeljson 0:3817adfaeb06 413 while(begin_game)
michaeljson 0:3817adfaeb06 414 {
michaeljson 0:3817adfaeb06 415 // play firing sound when the player fires
michaeljson 0:3817adfaeb06 416 if(!pb && missile.status == PLAYER_MISSILE_INACTIVE) {
michaeljson 0:3817adfaeb06 417
michaeljson 0:3817adfaeb06 418 wave_file=fopen("/sd/wavfiles/4.wav","r");
michaeljson 0:3817adfaeb06 419
michaeljson 0:3817adfaeb06 420
michaeljson 0:3817adfaeb06 421 waver.play(wave_file);
michaeljson 0:3817adfaeb06 422 fclose(wave_file);
michaeljson 0:3817adfaeb06 423 }
michaeljson 0:3817adfaeb06 424
michaeljson 0:3817adfaeb06 425 // if player hit, play hit sound
michaeljson 0:3817adfaeb06 426 if (hit_player)
michaeljson 0:3817adfaeb06 427 {
michaeljson 0:3817adfaeb06 428 wave_file=fopen("/sd/wavfiles/1.wav","r");
michaeljson 0:3817adfaeb06 429
michaeljson 0:3817adfaeb06 430 waver.play(wave_file);
michaeljson 0:3817adfaeb06 431 fclose(wave_file);
michaeljson 0:3817adfaeb06 432 }
michaeljson 0:3817adfaeb06 433 }
michaeljson 0:3817adfaeb06 434
michaeljson 0:3817adfaeb06 435 // players gameover voice if player loses
michaeljson 0:3817adfaeb06 436 while(gameover)
michaeljson 0:3817adfaeb06 437 {
michaeljson 0:3817adfaeb06 438 wave_file=fopen("/sd/wavfiles/game_over.wav","r");
michaeljson 0:3817adfaeb06 439
michaeljson 0:3817adfaeb06 440 waver.play(wave_file);
michaeljson 0:3817adfaeb06 441 fclose(wave_file);
michaeljson 0:3817adfaeb06 442 }
michaeljson 0:3817adfaeb06 443 }
michaeljson 0:3817adfaeb06 444 }
michaeljson 0:3817adfaeb06 445
michaeljson 0:3817adfaeb06 446 int main() {
michaeljson 0:3817adfaeb06 447
michaeljson 0:3817adfaeb06 448 // Initialization of Setup variables
michaeljson 0:3817adfaeb06 449 int blk_x, blk_y;
michaeljson 0:3817adfaeb06 450 pb.mode(PullUp);
michaeljson 0:3817adfaeb06 451
michaeljson 0:3817adfaeb06 452 Thread thread(playstart); // intializes the thread to play sound
michaeljson 0:3817adfaeb06 453
michaeljson 0:3817adfaeb06 454 uLCD.baudrate(500000); // set to 500000 to increase smooth gameplay
michaeljson 0:3817adfaeb06 455
michaeljson 0:3817adfaeb06 456 // Initialization of Game Menu variables
michaeljson 0:3817adfaeb06 457 const int title_x_pos = 2; // initial x-pos of title
michaeljson 0:3817adfaeb06 458 const int title_y_pos = 3; // initial y-pos of title
michaeljson 0:3817adfaeb06 459 int start_label_x_pos = 7; // start label x-pos
michaeljson 0:3817adfaeb06 460 int start_label_y_pos = 7; // start label y-pos
michaeljson 0:3817adfaeb06 461 int level_cursor_x_pos = 5; // level cursor x-position
michaeljson 0:3817adfaeb06 462 int level_cursor_y_pos = 7; // level cursor y-position
michaeljson 0:3817adfaeb06 463 int gameover_x_pos = 5; // gameover label x-position
michaeljson 0:3817adfaeb06 464 int gameover_y_pos = 5; // gameover label y-position
michaeljson 0:3817adfaeb06 465 int win_x_pos = 2; // congratulations label x-position
michaeljson 0:3817adfaeb06 466 int win_y_pos = 5; // congratulations label y-position
michaeljson 0:3817adfaeb06 467 int startover_x_pos = 3; // startover label x-position
michaeljson 0:3817adfaeb06 468 int startover_y_pos = 8; // startover label y-position
michaeljson 0:3817adfaeb06 469
michaeljson 0:3817adfaeb06 470 // intialize temporary score and current score
michaeljson 0:3817adfaeb06 471 int temp = 0;
michaeljson 0:3817adfaeb06 472 int score = 0;
michaeljson 0:3817adfaeb06 473
michaeljson 0:3817adfaeb06 474 // Begin game loop
michaeljson 0:3817adfaeb06 475 while(1)
michaeljson 0:3817adfaeb06 476 {
michaeljson 0:3817adfaeb06 477 // initialize all starting conditions for the beginning of the game
michaeljson 0:3817adfaeb06 478 game_menu = true; // defaults to display menu screen
michaeljson 0:3817adfaeb06 479 ENEMY_MOVE = true; // defaults to move enemy
michaeljson 0:3817adfaeb06 480 DIRECTION = 1; // default to move right
michaeljson 0:3817adfaeb06 481 hit_player = 0; // default to not player hit
michaeljson 0:3817adfaeb06 482 MOVE_DOWN = 0; // default to not move down
michaeljson 0:3817adfaeb06 483 lose = false; // default to not lose
michaeljson 0:3817adfaeb06 484 lives = 3; // defaults to 3 lives
michaeljson 0:3817adfaeb06 485 score = 0; // default to score of 0
michaeljson 0:3817adfaeb06 486
michaeljson 0:3817adfaeb06 487 uLCD.cls();
michaeljson 0:3817adfaeb06 488
michaeljson 0:3817adfaeb06 489 // Implementation of Game Menu
michaeljson 0:3817adfaeb06 490 while(game_menu)
michaeljson 0:3817adfaeb06 491 {
michaeljson 0:3817adfaeb06 492 uLCD.locate(level_cursor_x_pos,level_cursor_y_pos); // draws cursor next to "START" label
michaeljson 0:3817adfaeb06 493 uLCD.printf("->");
michaeljson 0:3817adfaeb06 494
michaeljson 0:3817adfaeb06 495 uLCD.locate(title_x_pos,title_y_pos); // "SPACE INVADERS" title position
michaeljson 0:3817adfaeb06 496 uLCD.printf("SPACE INVADERS"); // Title
michaeljson 0:3817adfaeb06 497
michaeljson 0:3817adfaeb06 498 uLCD.locate(start_label_x_pos,start_label_y_pos); // "START" label position
michaeljson 0:3817adfaeb06 499 uLCD.printf("START");
michaeljson 0:3817adfaeb06 500
michaeljson 0:3817adfaeb06 501 // if pushbutton is pressed, game menu is exited and game begins
michaeljson 0:3817adfaeb06 502 if(!pb)
michaeljson 0:3817adfaeb06 503 {
michaeljson 0:3817adfaeb06 504 game_menu = false;
michaeljson 0:3817adfaeb06 505 wait(0.5);
michaeljson 0:3817adfaeb06 506 }
michaeljson 0:3817adfaeb06 507 }
michaeljson 0:3817adfaeb06 508
michaeljson 0:3817adfaeb06 509 begin_game = true; // defaults begin_game to true
michaeljson 0:3817adfaeb06 510
michaeljson 0:3817adfaeb06 511 uLCD.cls();
michaeljson 0:3817adfaeb06 512
michaeljson 0:3817adfaeb06 513 // Draw the enemies
michaeljson 0:3817adfaeb06 514 draw_enemies_level();
michaeljson 0:3817adfaeb06 515
michaeljson 0:3817adfaeb06 516 // Draw the player
michaeljson 0:3817adfaeb06 517 draw_initial_player();
michaeljson 0:3817adfaeb06 518
michaeljson 0:3817adfaeb06 519 // sets the initial position of player missile and enemy missile (later updated)
michaeljson 0:3817adfaeb06 520 blk_x = player.player_blk_x+(player.player_width/2);
michaeljson 0:3817adfaeb06 521 blk_y = player.player_blk_y;
michaeljson 0:3817adfaeb06 522 missile_init(&missile, blk_x, blk_y, WHITE);
michaeljson 1:618aa2c4ca6a 523 int e_blk_x = 0;
michaeljson 1:618aa2c4ca6a 524 int e_blk_y = 2;
michaeljson 1:618aa2c4ca6a 525 enemy_missile_init(&enemy_missile, e_blk_x, e_blk_y, WHITE);
michaeljson 0:3817adfaeb06 526
michaeljson 0:3817adfaeb06 527 // prints lives
michaeljson 0:3817adfaeb06 528 uLCD.locate(0,0);
michaeljson 0:3817adfaeb06 529 uLCD.printf("Lives:%i", lives);
michaeljson 0:3817adfaeb06 530
michaeljson 0:3817adfaeb06 531 // prints score
michaeljson 0:3817adfaeb06 532 uLCD.locate(9,0);
michaeljson 0:3817adfaeb06 533 uLCD.printf("Score:%i", score);
michaeljson 0:3817adfaeb06 534
michaeljson 0:3817adfaeb06 535 // game play loop
michaeljson 0:3817adfaeb06 536 while(begin_game)
michaeljson 0:3817adfaeb06 537 {
michaeljson 0:3817adfaeb06 538 // updates score
michaeljson 0:3817adfaeb06 539 temp = score;
michaeljson 0:3817adfaeb06 540 score = (15-numOfEnemies)*15;
michaeljson 0:3817adfaeb06 541
michaeljson 0:3817adfaeb06 542 // prints score if score changes
michaeljson 0:3817adfaeb06 543 if (score != temp)
michaeljson 0:3817adfaeb06 544 {
michaeljson 0:3817adfaeb06 545 uLCD.locate(9,0);
michaeljson 0:3817adfaeb06 546 uLCD.printf("Score:%i", score);
michaeljson 0:3817adfaeb06 547 }
michaeljson 0:3817adfaeb06 548
michaeljson 0:3817adfaeb06 549 // move enemy
michaeljson 0:3817adfaeb06 550 enemy_motion();
michaeljson 0:3817adfaeb06 551
michaeljson 0:3817adfaeb06 552 // checks if player missile passes y-pos of row1
michaeljson 0:3817adfaeb06 553 if (missile.missile_blk_y+1-missile.missile_height <= enemy_1.enemy_blk_y
michaeljson 0:3817adfaeb06 554 && missile.missile_blk_y+1-missile.missile_height >= enemy_1.enemy_blk_y-enemy_1.enemy_height)
michaeljson 0:3817adfaeb06 555 {
michaeljson 0:3817adfaeb06 556 check_hit_enemy_row1();
michaeljson 0:3817adfaeb06 557 }
michaeljson 0:3817adfaeb06 558
michaeljson 0:3817adfaeb06 559 // checks if player missile passes y-pos of row2
michaeljson 0:3817adfaeb06 560 if (missile.missile_blk_y+1-missile.missile_height <= enemy_6.enemy_blk_y
michaeljson 0:3817adfaeb06 561 && missile.missile_blk_y+1-missile.missile_height >= enemy_6.enemy_blk_y-enemy_6.enemy_height)
michaeljson 0:3817adfaeb06 562 {
michaeljson 0:3817adfaeb06 563 check_hit_enemy_row2();
michaeljson 0:3817adfaeb06 564 }
michaeljson 0:3817adfaeb06 565
michaeljson 0:3817adfaeb06 566 // checks if player missile passes y-pos of row3
michaeljson 0:3817adfaeb06 567 if (missile.missile_blk_y+1-missile.missile_height <= enemy_11.enemy_blk_y
michaeljson 0:3817adfaeb06 568 && missile.missile_blk_y+1-missile.missile_height >= enemy_11.enemy_blk_y-enemy_11.enemy_height)
michaeljson 0:3817adfaeb06 569 {
michaeljson 0:3817adfaeb06 570 check_hit_enemy_row3();
michaeljson 0:3817adfaeb06 571 }
michaeljson 0:3817adfaeb06 572
michaeljson 0:3817adfaeb06 573 // Random Enemy Fire
michaeljson 0:3817adfaeb06 574 if (enemy_missile.status == ENEMY_MISSILE_INACTIVE)
michaeljson 0:3817adfaeb06 575 {
michaeljson 0:3817adfaeb06 576 random_attack_gen();
michaeljson 0:3817adfaeb06 577 }
michaeljson 0:3817adfaeb06 578
michaeljson 0:3817adfaeb06 579 // checks if enemy missile passes y-pos of player
michaeljson 0:3817adfaeb06 580 if (enemy_missile.missile_blk_y >= player.player_blk_y
michaeljson 0:3817adfaeb06 581 && enemy_missile.missile_blk_y <= player.player_blk_y+player.player_height)
michaeljson 0:3817adfaeb06 582 {
michaeljson 0:3817adfaeb06 583 check_player_hit();
michaeljson 0:3817adfaeb06 584 }
michaeljson 0:3817adfaeb06 585
michaeljson 0:3817adfaeb06 586 update_missile_pos(&missile); // updates player missile position
michaeljson 0:3817adfaeb06 587 update_enemy_missile_pos(&enemy_missile); // updates enemy missile position
michaeljson 0:3817adfaeb06 588
michaeljson 0:3817adfaeb06 589 // Player Movement checked with navigation switch
michaeljson 0:3817adfaeb06 590 if (myNav.left() && ((player.player_blk_x-3) > 0))
michaeljson 0:3817adfaeb06 591 {
michaeljson 0:3817adfaeb06 592 player_erase(&player);
michaeljson 0:3817adfaeb06 593 player.player_blk_x -= 3;
michaeljson 0:3817adfaeb06 594 player_show(&player);
michaeljson 0:3817adfaeb06 595 }
michaeljson 0:3817adfaeb06 596 else if (myNav.right() && ((player.player_blk_x+3) < (128-player.player_width)))
michaeljson 0:3817adfaeb06 597 {
michaeljson 0:3817adfaeb06 598 player_erase(&player);
michaeljson 0:3817adfaeb06 599 player.player_blk_x += 3;
michaeljson 0:3817adfaeb06 600 player_show(&player);
michaeljson 0:3817adfaeb06 601 }
michaeljson 0:3817adfaeb06 602
michaeljson 0:3817adfaeb06 603 // Player Fire
michaeljson 0:3817adfaeb06 604 if (pb == 0 && missile.status == PLAYER_MISSILE_INACTIVE)
michaeljson 0:3817adfaeb06 605 {
michaeljson 0:3817adfaeb06 606 missile.missile_blk_x = player.player_blk_x+(player.player_width/2);
michaeljson 0:3817adfaeb06 607 missile.missile_blk_y = player.player_blk_y;
michaeljson 0:3817adfaeb06 608 missile.status = PLAYER_MISSILE_ACTIVE;
michaeljson 0:3817adfaeb06 609 }
michaeljson 0:3817adfaeb06 610
michaeljson 0:3817adfaeb06 611 // checks if player destroyed all enemies
michaeljson 0:3817adfaeb06 612 if (numOfEnemies == 0)
michaeljson 0:3817adfaeb06 613 {
michaeljson 0:3817adfaeb06 614 uLCD.cls();
michaeljson 0:3817adfaeb06 615
michaeljson 0:3817adfaeb06 616 bool win = true; // sets win to true, for win screen
michaeljson 0:3817adfaeb06 617 begin_game = false;
michaeljson 0:3817adfaeb06 618
michaeljson 0:3817adfaeb06 619 // displays video clip
michaeljson 0:3817adfaeb06 620 uLCD.cls();
michaeljson 0:3817adfaeb06 621 uLCD.media_init();
michaeljson 0:3817adfaeb06 622 uLCD.set_sector_address(0x00, 0x00);
michaeljson 0:3817adfaeb06 623 uLCD.display_video(0,0);
michaeljson 0:3817adfaeb06 624 wait(1);
michaeljson 0:3817adfaeb06 625
michaeljson 0:3817adfaeb06 626 uLCD.cls();
michaeljson 0:3817adfaeb06 627
michaeljson 0:3817adfaeb06 628 // prints "Congratulations" on uLCD
michaeljson 0:3817adfaeb06 629 uLCD.locate(win_x_pos,win_y_pos);
michaeljson 0:3817adfaeb06 630 uLCD.printf("CONGRATULATIONS!");
michaeljson 0:3817adfaeb06 631
michaeljson 0:3817adfaeb06 632 // prints "Play Again?" and "Press pb..."
michaeljson 0:3817adfaeb06 633 uLCD.locate(startover_x_pos, startover_y_pos);
michaeljson 0:3817adfaeb06 634 uLCD.printf("Play Again?");
michaeljson 0:3817adfaeb06 635 uLCD.locate(startover_x_pos, startover_y_pos+1);
michaeljson 0:3817adfaeb06 636 uLCD.printf("Press pb...");
michaeljson 0:3817adfaeb06 637
michaeljson 0:3817adfaeb06 638 // waits at win screen until pushbutton is pressed
michaeljson 0:3817adfaeb06 639 while (win)
michaeljson 0:3817adfaeb06 640 {
michaeljson 0:3817adfaeb06 641 // if pb is pressed, reset game to start menu
michaeljson 0:3817adfaeb06 642 if (!pb)
michaeljson 0:3817adfaeb06 643 {
michaeljson 0:3817adfaeb06 644 win = false;
michaeljson 0:3817adfaeb06 645 wait(0.5);
michaeljson 0:3817adfaeb06 646 }
michaeljson 0:3817adfaeb06 647 }
michaeljson 0:3817adfaeb06 648
michaeljson 0:3817adfaeb06 649 }
michaeljson 0:3817adfaeb06 650
michaeljson 0:3817adfaeb06 651 // checks if player was hit
michaeljson 0:3817adfaeb06 652 if (hit_player)
michaeljson 0:3817adfaeb06 653 {
michaeljson 0:3817adfaeb06 654 // updates lives
michaeljson 0:3817adfaeb06 655 lives -= 1;
michaeljson 0:3817adfaeb06 656 wait(0.5);
michaeljson 0:3817adfaeb06 657 hit_player = 0;
michaeljson 0:3817adfaeb06 658 player_show(&player);
michaeljson 0:3817adfaeb06 659 player.status = PLAYER_ALIVE;
michaeljson 0:3817adfaeb06 660
michaeljson 0:3817adfaeb06 661 // prints updated lives number
michaeljson 0:3817adfaeb06 662 uLCD.locate(0,0);
michaeljson 0:3817adfaeb06 663 uLCD.printf("Lives:%i", lives);
michaeljson 0:3817adfaeb06 664 }
michaeljson 0:3817adfaeb06 665
michaeljson 0:3817adfaeb06 666 // if player loses all lives or enemy reaches the player
michaeljson 0:3817adfaeb06 667 if (lose || lives == 0)
michaeljson 0:3817adfaeb06 668 {
michaeljson 0:3817adfaeb06 669 begin_game = false; // set to false to end game
michaeljson 0:3817adfaeb06 670 uLCD.cls();
michaeljson 0:3817adfaeb06 671
michaeljson 0:3817adfaeb06 672 gameover = true; // set to go to display gameover screen
michaeljson 0:3817adfaeb06 673
michaeljson 0:3817adfaeb06 674 // prints "GAMEOVER" to uLCD
michaeljson 0:3817adfaeb06 675 uLCD.locate(gameover_x_pos, gameover_y_pos);
michaeljson 0:3817adfaeb06 676 uLCD.printf("GAMEOVER");
michaeljson 0:3817adfaeb06 677 wait(1);
michaeljson 0:3817adfaeb06 678
michaeljson 0:3817adfaeb06 679 // prints "Play Again?" and "Press pb..."
michaeljson 0:3817adfaeb06 680 uLCD.locate(startover_x_pos, startover_y_pos);
michaeljson 0:3817adfaeb06 681 uLCD.printf("Play Again?");
michaeljson 0:3817adfaeb06 682 uLCD.locate(startover_x_pos, startover_y_pos+1);
michaeljson 0:3817adfaeb06 683 uLCD.printf("Press pb...");
michaeljson 0:3817adfaeb06 684
michaeljson 0:3817adfaeb06 685 // stays in gameover screen until pb is pressed
michaeljson 0:3817adfaeb06 686 while (gameover)
michaeljson 0:3817adfaeb06 687 {
michaeljson 0:3817adfaeb06 688 // if pb is pressed, game is reset to the game menu screen
michaeljson 0:3817adfaeb06 689 if (!pb)
michaeljson 0:3817adfaeb06 690 {
michaeljson 0:3817adfaeb06 691 gameover = false;
michaeljson 0:3817adfaeb06 692 game_menu = true;
michaeljson 0:3817adfaeb06 693 wait(0.5);
michaeljson 0:3817adfaeb06 694 }
michaeljson 0:3817adfaeb06 695 }
michaeljson 0:3817adfaeb06 696 }
michaeljson 0:3817adfaeb06 697
michaeljson 0:3817adfaeb06 698 }
michaeljson 0:3817adfaeb06 699 }
michaeljson 0:3817adfaeb06 700 }