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