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