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