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