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:
Wed Apr 28 03:12:39 2021 +0000
Revision:
26:3270c6edd7d9
Parent:
25:17f2ec000357
Child:
27:9a17cf897bf8
Child:
28:a2dac56af32f
ONE-PLAYER SPACE INVADERS. With 2-Player primitives commented out. Works with 3 different difficulties.

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