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