A reproduction of the 70's classic. Enjoy!
Dependencies: NokiaLCD SDFileSystem SIgame mbed-rtos mbed wave_player
main.cpp@1:02e3c9b77580, 2013-03-07 (annotated)
- Committer:
- zlee9
- Date:
- Thu Mar 07 22:01:46 2013 +0000
- Revision:
- 1:02e3c9b77580
- Parent:
- 0:fcfeec8463fa
Fixed winning message.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zlee9 | 0:fcfeec8463fa | 1 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 2 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 3 | /*** ***/ |
zlee9 | 0:fcfeec8463fa | 4 | /*** Space Invaders Simulator ***/ |
zlee9 | 0:fcfeec8463fa | 5 | /*** ***/ |
zlee9 | 0:fcfeec8463fa | 6 | /*** ECE 4180 Developer Team: Space Pirates ***/ |
zlee9 | 0:fcfeec8463fa | 7 | /*** Members: David Gaspard, Zhe Cheng Lee ***/ |
zlee9 | 0:fcfeec8463fa | 8 | /*** ***/ |
zlee9 | 0:fcfeec8463fa | 9 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 10 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 11 | |
zlee9 | 0:fcfeec8463fa | 12 | |
zlee9 | 0:fcfeec8463fa | 13 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 14 | /* */ |
zlee9 | 0:fcfeec8463fa | 15 | /* Libaries */ |
zlee9 | 0:fcfeec8463fa | 16 | /* */ |
zlee9 | 0:fcfeec8463fa | 17 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 18 | #include "mbed.h" |
zlee9 | 0:fcfeec8463fa | 19 | #include "NokiaLCD.h" |
zlee9 | 0:fcfeec8463fa | 20 | #include "SIgame.h" |
zlee9 | 0:fcfeec8463fa | 21 | #include "rtos.h" |
zlee9 | 0:fcfeec8463fa | 22 | #include "SDFileSystem.h" |
zlee9 | 0:fcfeec8463fa | 23 | #include "wave_player.h" |
zlee9 | 0:fcfeec8463fa | 24 | |
zlee9 | 0:fcfeec8463fa | 25 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 26 | /* */ |
zlee9 | 0:fcfeec8463fa | 27 | /* Classes and Global Variables */ |
zlee9 | 0:fcfeec8463fa | 28 | /* */ |
zlee9 | 0:fcfeec8463fa | 29 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 30 | NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type |
zlee9 | 0:fcfeec8463fa | 31 | DigitalIn button(p20); // Pushbutton for firing laser |
zlee9 | 0:fcfeec8463fa | 32 | AnalogIn horiz(p15); // From joystick for steering ship |
zlee9 | 0:fcfeec8463fa | 33 | SDFileSystem sd(p11, p12, p13, p14, "sd"); //SD card |
zlee9 | 0:fcfeec8463fa | 34 | AnalogOut DACout(p18);//Speaker out |
zlee9 | 0:fcfeec8463fa | 35 | wave_player waver(&DACout);//Wave player |
zlee9 | 0:fcfeec8463fa | 36 | |
zlee9 | 0:fcfeec8463fa | 37 | int state; |
zlee9 | 0:fcfeec8463fa | 38 | int doneski;//done playing start music? |
zlee9 | 0:fcfeec8463fa | 39 | |
zlee9 | 0:fcfeec8463fa | 40 | OBJECT wave[NUM_ALIEN_ROWS][NUM_ALIEN_COLS], |
zlee9 | 0:fcfeec8463fa | 41 | wave_old[NUM_ALIEN_ROWS][NUM_ALIEN_COLS]; |
zlee9 | 0:fcfeec8463fa | 42 | |
zlee9 | 0:fcfeec8463fa | 43 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 44 | /* */ |
zlee9 | 0:fcfeec8463fa | 45 | /* Prototypes */ |
zlee9 | 0:fcfeec8463fa | 46 | /* */ |
zlee9 | 0:fcfeec8463fa | 47 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 48 | void start(); |
zlee9 | 0:fcfeec8463fa | 49 | int game(); |
zlee9 | 0:fcfeec8463fa | 50 | void lose(); |
zlee9 | 0:fcfeec8463fa | 51 | void win(); |
zlee9 | 0:fcfeec8463fa | 52 | void updateScreen(OBJECT ship, OBJECT ship_old, POINT ylaser, POINT ylaser_old, |
zlee9 | 0:fcfeec8463fa | 53 | POINT elaser[], POINT elaser_old[], int lives_rem); |
zlee9 | 0:fcfeec8463fa | 54 | |
zlee9 | 0:fcfeec8463fa | 55 | void playstart(void const *args); |
zlee9 | 0:fcfeec8463fa | 56 | void startmusic(void const *args); |
zlee9 | 0:fcfeec8463fa | 57 | |
zlee9 | 0:fcfeec8463fa | 58 | |
zlee9 | 0:fcfeec8463fa | 59 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 60 | /* */ |
zlee9 | 0:fcfeec8463fa | 61 | /* Main program */ |
zlee9 | 0:fcfeec8463fa | 62 | /* */ |
zlee9 | 0:fcfeec8463fa | 63 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 64 | int main() |
zlee9 | 0:fcfeec8463fa | 65 | { |
zlee9 | 0:fcfeec8463fa | 66 | doneski=0;//Variable to declare that start screen music thread |
zlee9 | 0:fcfeec8463fa | 67 | //was terminated |
zlee9 | 0:fcfeec8463fa | 68 | Thread thread(playstart);//Satrt the sound effect thread |
zlee9 | 0:fcfeec8463fa | 69 | Thread startmu(startmusic);//start the trigger fire thread |
zlee9 | 0:fcfeec8463fa | 70 | state = START;//Begin the game |
zlee9 | 0:fcfeec8463fa | 71 | button.mode(PullUp);//Pull up the pushbutton |
zlee9 | 0:fcfeec8463fa | 72 | |
zlee9 | 0:fcfeec8463fa | 73 | while (1) { // Game loop |
zlee9 | 0:fcfeec8463fa | 74 | switch (state) { |
zlee9 | 0:fcfeec8463fa | 75 | case START: |
zlee9 | 0:fcfeec8463fa | 76 | start(); // Start game |
zlee9 | 0:fcfeec8463fa | 77 | state = GAME; |
zlee9 | 0:fcfeec8463fa | 78 | break; |
zlee9 | 0:fcfeec8463fa | 79 | |
zlee9 | 0:fcfeec8463fa | 80 | case GAME: |
zlee9 | 0:fcfeec8463fa | 81 | startmu.terminate(); |
zlee9 | 0:fcfeec8463fa | 82 | doneski=1; |
zlee9 | 0:fcfeec8463fa | 83 | state = game(); // Actual game |
zlee9 | 0:fcfeec8463fa | 84 | break; |
zlee9 | 0:fcfeec8463fa | 85 | |
zlee9 | 0:fcfeec8463fa | 86 | case LOSE: |
zlee9 | 0:fcfeec8463fa | 87 | lose(); // Losing screen |
zlee9 | 0:fcfeec8463fa | 88 | state = START; |
zlee9 | 0:fcfeec8463fa | 89 | break; |
zlee9 | 0:fcfeec8463fa | 90 | |
zlee9 | 0:fcfeec8463fa | 91 | case WIN: |
zlee9 | 0:fcfeec8463fa | 92 | win(); // Winning screen |
zlee9 | 0:fcfeec8463fa | 93 | state = START; |
zlee9 | 0:fcfeec8463fa | 94 | break; |
zlee9 | 0:fcfeec8463fa | 95 | } |
zlee9 | 0:fcfeec8463fa | 96 | } |
zlee9 | 0:fcfeec8463fa | 97 | } |
zlee9 | 0:fcfeec8463fa | 98 | |
zlee9 | 0:fcfeec8463fa | 99 | |
zlee9 | 0:fcfeec8463fa | 100 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 101 | /* */ |
zlee9 | 0:fcfeec8463fa | 102 | /* Game-State Procedures */ |
zlee9 | 0:fcfeec8463fa | 103 | /* */ |
zlee9 | 0:fcfeec8463fa | 104 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 105 | void start() |
zlee9 | 0:fcfeec8463fa | 106 | { |
zlee9 | 0:fcfeec8463fa | 107 | /****** |
zlee9 | 0:fcfeec8463fa | 108 | * start |
zlee9 | 0:fcfeec8463fa | 109 | * Starts up game |
zlee9 | 0:fcfeec8463fa | 110 | ******/ |
zlee9 | 0:fcfeec8463fa | 111 | lcd.cls(); |
zlee9 | 0:fcfeec8463fa | 112 | lcd.locate(0,4); |
zlee9 | 0:fcfeec8463fa | 113 | lcd.printf("SPACE INVADERS"); |
zlee9 | 0:fcfeec8463fa | 114 | lcd.locate(0,10); |
zlee9 | 0:fcfeec8463fa | 115 | lcd.printf("PRESS BUTTON TO CONTINUE"); |
zlee9 | 0:fcfeec8463fa | 116 | WAIT4FULLPUSH(button); |
zlee9 | 0:fcfeec8463fa | 117 | } |
zlee9 | 0:fcfeec8463fa | 118 | |
zlee9 | 0:fcfeec8463fa | 119 | |
zlee9 | 0:fcfeec8463fa | 120 | int game() |
zlee9 | 0:fcfeec8463fa | 121 | { |
zlee9 | 0:fcfeec8463fa | 122 | /****** |
zlee9 | 0:fcfeec8463fa | 123 | * game |
zlee9 | 0:fcfeec8463fa | 124 | * Handles the main gameplay. Prepares for win state if player destroys all |
zlee9 | 0:fcfeec8463fa | 125 | * aliens and lose state if player loses all lives |
zlee9 | 0:fcfeec8463fa | 126 | ******/ |
zlee9 | 0:fcfeec8463fa | 127 | POINT ylaser, ylaser_old, elaser[ELASER_CAP], elaser_old[ELASER_CAP]; |
zlee9 | 0:fcfeec8463fa | 128 | OBJECT ship, ship_old, *sh_alien, *frontline[NUM_ALIEN_COLS]; |
zlee9 | 0:fcfeec8463fa | 129 | int time, lives_rem, als_rem, i; |
zlee9 | 0:fcfeec8463fa | 130 | float hper; |
zlee9 | 0:fcfeec8463fa | 131 | bool reach, left, lcancel; |
zlee9 | 0:fcfeec8463fa | 132 | |
zlee9 | 0:fcfeec8463fa | 133 | // Set display screen |
zlee9 | 0:fcfeec8463fa | 134 | lcd.cls(); |
zlee9 | 0:fcfeec8463fa | 135 | lcd.background(BLACK); |
zlee9 | 0:fcfeec8463fa | 136 | |
zlee9 | 0:fcfeec8463fa | 137 | // Initialize |
zlee9 | 0:fcfeec8463fa | 138 | lives_rem = NUM_LIVES_START; |
zlee9 | 0:fcfeec8463fa | 139 | als_rem = NUM_ALIENS_START; |
zlee9 | 0:fcfeec8463fa | 140 | reach = left = lcancel = false; |
zlee9 | 0:fcfeec8463fa | 141 | time = 0; |
zlee9 | 0:fcfeec8463fa | 142 | summonWave(); // Starting locations of all alien |
zlee9 | 0:fcfeec8463fa | 143 | memcpy(wave_old, wave, sizeof(wave)); // Make copy for aliens' old locations |
zlee9 | 0:fcfeec8463fa | 144 | |
zlee9 | 0:fcfeec8463fa | 145 | // Keep track of aliens in front of respective columns |
zlee9 | 0:fcfeec8463fa | 146 | for (i = 0; i < NUM_ALIEN_COLS; i++) |
zlee9 | 0:fcfeec8463fa | 147 | frontline[i] = &wave[NUM_ALIEN_ROWS][i]; |
zlee9 | 0:fcfeec8463fa | 148 | |
zlee9 | 0:fcfeec8463fa | 149 | // Set up laser structures |
zlee9 | 0:fcfeec8463fa | 150 | elaser[0].collide = elaser[1].collide = elaser[2].collide = |
zlee9 | 0:fcfeec8463fa | 151 | elaser_old[0].collide = elaser_old[1].collide = elaser_old[2].collide |
zlee9 | 0:fcfeec8463fa | 152 | = ylaser.collide = ylaser_old.collide = true; // No lasers on screen yet |
zlee9 | 0:fcfeec8463fa | 153 | // Enemy lasers are white |
zlee9 | 0:fcfeec8463fa | 154 | elaser[0].color = elaser[1].color = elaser[2].color = WHITE; |
zlee9 | 0:fcfeec8463fa | 155 | ylaser.color = GREEN; // Player's laser is green |
zlee9 | 0:fcfeec8463fa | 156 | |
zlee9 | 0:fcfeec8463fa | 157 | // Set up player's ship |
zlee9 | 0:fcfeec8463fa | 158 | ship = startShip(); |
zlee9 | 0:fcfeec8463fa | 159 | memcpy(&ship_old, &ship, sizeof(ship)); // Save copy for ship's old locations |
zlee9 | 0:fcfeec8463fa | 160 | |
zlee9 | 0:fcfeec8463fa | 161 | // Game goes on until player loses all lives, destroy all aliens, or allow |
zlee9 | 0:fcfeec8463fa | 162 | // any alien to reach screen border on player's side |
zlee9 | 0:fcfeec8463fa | 163 | while (!reach && lives_rem > 0 && als_rem > 0) { |
zlee9 | 0:fcfeec8463fa | 164 | /* Laser Movement */ |
zlee9 | 0:fcfeec8463fa | 165 | /******************/ |
zlee9 | 0:fcfeec8463fa | 166 | // Player's laser is on screen |
zlee9 | 0:fcfeec8463fa | 167 | if (!(ylaser.collide)) { |
zlee9 | 0:fcfeec8463fa | 168 | if (ylaser.y >= 0) |
zlee9 | 0:fcfeec8463fa | 169 | (ylaser.y)-= 3; // Laser travels up to aliens' side |
zlee9 | 0:fcfeec8463fa | 170 | else |
zlee9 | 0:fcfeec8463fa | 171 | ylaser.collide = true; // Laser vanishes at top of screen |
zlee9 | 0:fcfeec8463fa | 172 | } |
zlee9 | 0:fcfeec8463fa | 173 | |
zlee9 | 0:fcfeec8463fa | 174 | // Enemy lasers is on screen |
zlee9 | 0:fcfeec8463fa | 175 | for (i = 0; i < ELASER_CAP; i++) { |
zlee9 | 0:fcfeec8463fa | 176 | if (elaser[i].y < SCREENHEIGHT) // Enemy lasers travel down to player's side |
zlee9 | 0:fcfeec8463fa | 177 | (elaser[i].y)++; |
zlee9 | 0:fcfeec8463fa | 178 | else // Enemy laser vanishes at bottom of screen |
zlee9 | 0:fcfeec8463fa | 179 | elaser[i].collide = true; |
zlee9 | 0:fcfeec8463fa | 180 | } |
zlee9 | 0:fcfeec8463fa | 181 | |
zlee9 | 0:fcfeec8463fa | 182 | /* Controls */ |
zlee9 | 0:fcfeec8463fa | 183 | /******************/ |
zlee9 | 0:fcfeec8463fa | 184 | // Player firing laser |
zlee9 | 0:fcfeec8463fa | 185 | if (button && ylaser.collide) { |
zlee9 | 0:fcfeec8463fa | 186 | // Initialize laser's locations according to ship's |
zlee9 | 0:fcfeec8463fa | 187 | ylaser.y = ship.y - 1; |
zlee9 | 0:fcfeec8463fa | 188 | ylaser.x = ship.x + (ship.width >> 1); |
zlee9 | 0:fcfeec8463fa | 189 | // Can fire next laser after current hits top of screen or other objects |
zlee9 | 0:fcfeec8463fa | 190 | ylaser.collide = false; |
zlee9 | 0:fcfeec8463fa | 191 | } |
zlee9 | 0:fcfeec8463fa | 192 | |
zlee9 | 0:fcfeec8463fa | 193 | // Player steering ship |
zlee9 | 0:fcfeec8463fa | 194 | hper = horiz; // Read joystick's horizontal position. |
zlee9 | 0:fcfeec8463fa | 195 | if (GOLEFT(hper) && ship.x > 0) |
zlee9 | 0:fcfeec8463fa | 196 | (ship.x)--; // Ship moves left/ |
zlee9 | 0:fcfeec8463fa | 197 | |
zlee9 | 0:fcfeec8463fa | 198 | if (GORIGHT(hper) && ship.x < SCREENWIDTH - 1) |
zlee9 | 0:fcfeec8463fa | 199 | (ship.x)++; // Ship moves right |
zlee9 | 0:fcfeec8463fa | 200 | |
zlee9 | 0:fcfeec8463fa | 201 | /* Alien Activity */ |
zlee9 | 0:fcfeec8463fa | 202 | /******************/ |
zlee9 | 0:fcfeec8463fa | 203 | // Move wave of aliens. Update whether any alien reaches screen on |
zlee9 | 0:fcfeec8463fa | 204 | // player's side |
zlee9 | 0:fcfeec8463fa | 205 | reach = moveAlienWave(&left); |
zlee9 | 0:fcfeec8463fa | 206 | |
zlee9 | 0:fcfeec8463fa | 207 | // Enemy aliens firing lasers. Aliens fire lasers only after certain |
zlee9 | 0:fcfeec8463fa | 208 | // time intervals or when one of their lasers canceled out with player's |
zlee9 | 0:fcfeec8463fa | 209 | if (time >= RELOAD_TIME || lcancel) { |
zlee9 | 0:fcfeec8463fa | 210 | // Choose random front alien to fire laser. Ignore any invalid ones |
zlee9 | 0:fcfeec8463fa | 211 | // (those in columns that were completely wiped out have been set to |
zlee9 | 0:fcfeec8463fa | 212 | // NULL) |
zlee9 | 0:fcfeec8463fa | 213 | while ((sh_alien = frontline[rand() / (RAND_MAX/i + 1)]) == NULL); |
zlee9 | 0:fcfeec8463fa | 214 | |
zlee9 | 0:fcfeec8463fa | 215 | // Find enemy laser structure not in use for display |
zlee9 | 0:fcfeec8463fa | 216 | if (elaser[0].collide) |
zlee9 | 0:fcfeec8463fa | 217 | i = 0; |
zlee9 | 0:fcfeec8463fa | 218 | else if (elaser[1].collide) |
zlee9 | 0:fcfeec8463fa | 219 | i = 1; |
zlee9 | 0:fcfeec8463fa | 220 | else |
zlee9 | 0:fcfeec8463fa | 221 | i = 2; |
zlee9 | 0:fcfeec8463fa | 222 | |
zlee9 | 0:fcfeec8463fa | 223 | // Initialize laser's location according to chosen alien's |
zlee9 | 0:fcfeec8463fa | 224 | elaser[i].y = sh_alien->y + 1; |
zlee9 | 0:fcfeec8463fa | 225 | elaser[i].x = sh_alien->x + (ALIEN_WIDTH >> 1); |
zlee9 | 0:fcfeec8463fa | 226 | elaser[i].collide = lcancel = false; |
zlee9 | 0:fcfeec8463fa | 227 | time = 0; // Reset time for next enemy laser fire |
zlee9 | 0:fcfeec8463fa | 228 | } |
zlee9 | 0:fcfeec8463fa | 229 | |
zlee9 | 0:fcfeec8463fa | 230 | /* Collisions */ |
zlee9 | 0:fcfeec8463fa | 231 | /*******************/ |
zlee9 | 0:fcfeec8463fa | 232 | // Cancel out player's laser and enemy laser should they connect |
zlee9 | 0:fcfeec8463fa | 233 | lcancel = twoLasersCollide(&ylaser, elaser); |
zlee9 | 0:fcfeec8463fa | 234 | |
zlee9 | 0:fcfeec8463fa | 235 | // Kill alien if player's laser hits one |
zlee9 | 0:fcfeec8463fa | 236 | destroyAlien(&als_rem, &ylaser, frontline); |
zlee9 | 0:fcfeec8463fa | 237 | |
zlee9 | 0:fcfeec8463fa | 238 | // Check for any enemy laser touching ship |
zlee9 | 0:fcfeec8463fa | 239 | for (i = 0; i < ELASER_CAP; i++) { |
zlee9 | 0:fcfeec8463fa | 240 | if (!(elaser[i].collide) && elaser[i].y >= ship.y && elaser[i].y <= |
zlee9 | 0:fcfeec8463fa | 241 | ship.y + ship.height && elaser[i].x >= ship.x && elaser[i].x |
zlee9 | 0:fcfeec8463fa | 242 | <= ship.x + ship.width) { |
zlee9 | 0:fcfeec8463fa | 243 | // Plyaer loses life if hit by laser |
zlee9 | 0:fcfeec8463fa | 244 | lives_rem--; |
zlee9 | 0:fcfeec8463fa | 245 | ship.killed = elaser[i].collide = true; |
zlee9 | 0:fcfeec8463fa | 246 | } |
zlee9 | 0:fcfeec8463fa | 247 | } |
zlee9 | 0:fcfeec8463fa | 248 | |
zlee9 | 0:fcfeec8463fa | 249 | updateScreen(ship, ship_old, ylaser, ylaser_old, elaser, elaser_old, |
zlee9 | 0:fcfeec8463fa | 250 | lives_rem); // Update gameplay screen |
zlee9 | 0:fcfeec8463fa | 251 | ship.killed = false; |
zlee9 | 0:fcfeec8463fa | 252 | time++; // Time passes |
zlee9 | 0:fcfeec8463fa | 253 | |
zlee9 | 0:fcfeec8463fa | 254 | // Update old locations to current |
zlee9 | 0:fcfeec8463fa | 255 | memcpy(wave_old, wave, sizeof(wave)); |
zlee9 | 0:fcfeec8463fa | 256 | memcpy(elaser_old, elaser, sizeof(elaser)); |
zlee9 | 0:fcfeec8463fa | 257 | ylaser_old = ylaser; |
zlee9 | 0:fcfeec8463fa | 258 | ship_old = ship; |
zlee9 | 0:fcfeec8463fa | 259 | } // End of main gameplay loop |
zlee9 | 0:fcfeec8463fa | 260 | |
zlee9 | 0:fcfeec8463fa | 261 | |
zlee9 | 0:fcfeec8463fa | 262 | // Game's outcomes |
zlee9 | 0:fcfeec8463fa | 263 | if (als_rem) // Lost all lives or aliens reached your border |
zlee9 | 0:fcfeec8463fa | 264 | return LOSE; |
zlee9 | 0:fcfeec8463fa | 265 | else // Destroyed all aliens |
zlee9 | 0:fcfeec8463fa | 266 | return WIN; |
zlee9 | 0:fcfeec8463fa | 267 | } |
zlee9 | 0:fcfeec8463fa | 268 | |
zlee9 | 0:fcfeec8463fa | 269 | |
zlee9 | 0:fcfeec8463fa | 270 | void win() |
zlee9 | 0:fcfeec8463fa | 271 | { |
zlee9 | 0:fcfeec8463fa | 272 | /****** |
zlee9 | 0:fcfeec8463fa | 273 | * win |
zlee9 | 0:fcfeec8463fa | 274 | * Displays congratulation screen and prompt player to replay |
zlee9 | 0:fcfeec8463fa | 275 | ******/ |
zlee9 | 0:fcfeec8463fa | 276 | // Congratulation screen |
zlee9 | 0:fcfeec8463fa | 277 | lcd.cls(); |
zlee9 | 1:02e3c9b77580 | 278 | lcd.locate(0,2); |
zlee9 | 1:02e3c9b77580 | 279 | lcd.printf("WELL DONE"); |
zlee9 | 1:02e3c9b77580 | 280 | lcd.locate(0,5); |
zlee9 | 1:02e3c9b77580 | 281 | lcd.printf("EARTHLING"); |
zlee9 | 1:02e3c9b77580 | 282 | lcd.locate(0,8); |
zlee9 | 1:02e3c9b77580 | 283 | lcd.printf("THIS TIME YOU"); |
zlee9 | 1:02e3c9b77580 | 284 | lcd.locate(0,11); |
zlee9 | 1:02e3c9b77580 | 285 | lcd.printf("WIN"); |
zlee9 | 0:fcfeec8463fa | 286 | wait(2); |
zlee9 | 0:fcfeec8463fa | 287 | |
zlee9 | 0:fcfeec8463fa | 288 | // Prompt player to play game again |
zlee9 | 0:fcfeec8463fa | 289 | lcd.cls(); |
zlee9 | 1:02e3c9b77580 | 290 | lcd.locate(0,11); |
zlee9 | 0:fcfeec8463fa | 291 | lcd.printf("PRESS BUTTON TO PLAY AGAIN"); |
zlee9 | 0:fcfeec8463fa | 292 | WAIT4FULLPUSH(button); |
zlee9 | 0:fcfeec8463fa | 293 | } |
zlee9 | 0:fcfeec8463fa | 294 | |
zlee9 | 0:fcfeec8463fa | 295 | |
zlee9 | 0:fcfeec8463fa | 296 | void lose() |
zlee9 | 0:fcfeec8463fa | 297 | { |
zlee9 | 0:fcfeec8463fa | 298 | /****** |
zlee9 | 0:fcfeec8463fa | 299 | * lose |
zlee9 | 0:fcfeec8463fa | 300 | * Displays game-over screen and prompts player to replay |
zlee9 | 0:fcfeec8463fa | 301 | ******/ |
zlee9 | 0:fcfeec8463fa | 302 | // Game-over screen |
zlee9 | 0:fcfeec8463fa | 303 | lcd.cls(); |
zlee9 | 0:fcfeec8463fa | 304 | lcd.locate(4,3); |
zlee9 | 0:fcfeec8463fa | 305 | lcd.printf("YOU SUCK"); |
zlee9 | 0:fcfeec8463fa | 306 | wait(2); |
zlee9 | 0:fcfeec8463fa | 307 | |
zlee9 | 0:fcfeec8463fa | 308 | // Prompt player to play game again |
zlee9 | 0:fcfeec8463fa | 309 | lcd.locate(0,10); |
zlee9 | 0:fcfeec8463fa | 310 | lcd.printf("PRESS BUTTON TO PLAY AGAIN"); |
zlee9 | 0:fcfeec8463fa | 311 | WAIT4FULLPUSH(button); |
zlee9 | 0:fcfeec8463fa | 312 | } |
zlee9 | 0:fcfeec8463fa | 313 | |
zlee9 | 0:fcfeec8463fa | 314 | |
zlee9 | 0:fcfeec8463fa | 315 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 316 | /* */ |
zlee9 | 0:fcfeec8463fa | 317 | /* Game-Drawing Funntion */ |
zlee9 | 0:fcfeec8463fa | 318 | /* */ |
zlee9 | 0:fcfeec8463fa | 319 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 320 | void updateScreen(OBJECT ship, OBJECT ship_old, POINT ylaser, POINT ylaser_old, |
zlee9 | 0:fcfeec8463fa | 321 | POINT elaser[], POINT elaser_old[], int lives_rem) |
zlee9 | 0:fcfeec8463fa | 322 | { |
zlee9 | 0:fcfeec8463fa | 323 | /****** |
zlee9 | 0:fcfeec8463fa | 324 | * updateScreen |
zlee9 | 0:fcfeec8463fa | 325 | * Updates game display on Nokia LCD screen |
zlee9 | 0:fcfeec8463fa | 326 | ******/ |
zlee9 | 0:fcfeec8463fa | 327 | int r, c; |
zlee9 | 0:fcfeec8463fa | 328 | |
zlee9 | 0:fcfeec8463fa | 329 | // Old locations are drawn with black to erase previous images in display |
zlee9 | 0:fcfeec8463fa | 330 | // when redrawing images |
zlee9 | 0:fcfeec8463fa | 331 | wait(0.02); |
zlee9 | 0:fcfeec8463fa | 332 | |
zlee9 | 0:fcfeec8463fa | 333 | // Redraw surviving aliens from their positions |
zlee9 | 0:fcfeec8463fa | 334 | for (c = 0; c < NUM_ALIEN_COLS; c++) { |
zlee9 | 0:fcfeec8463fa | 335 | for (r = 0; r < NUM_ALIEN_ROWS; r++) { |
zlee9 | 0:fcfeec8463fa | 336 | lcd.fill(wave_old[r][c].x, wave_old[r][c].y, wave_old[r][c].width, |
zlee9 | 0:fcfeec8463fa | 337 | wave_old[r][c].height, BLACK); |
zlee9 | 0:fcfeec8463fa | 338 | // Aliens as white rectangles |
zlee9 | 0:fcfeec8463fa | 339 | if (!(wave[r][c].killed)) // Don't draw destroyed aliens |
zlee9 | 0:fcfeec8463fa | 340 | lcd.fill(wave[r][c].x, wave[r][c].y, wave[r][c].width, |
zlee9 | 0:fcfeec8463fa | 341 | wave[r][c].height, wave[r][c].color); |
zlee9 | 0:fcfeec8463fa | 342 | } |
zlee9 | 0:fcfeec8463fa | 343 | } |
zlee9 | 0:fcfeec8463fa | 344 | |
zlee9 | 0:fcfeec8463fa | 345 | // Redraw enemy lasers as single white pixels |
zlee9 | 0:fcfeec8463fa | 346 | for (c = 0; c < ELASER_CAP; c++) { |
zlee9 | 0:fcfeec8463fa | 347 | lcd.pixel(elaser_old[c].x, elaser_old[c].y, BLACK); |
zlee9 | 0:fcfeec8463fa | 348 | lcd.pixel(elaser_old[c].x+1, elaser_old[c].y, BLACK); |
zlee9 | 0:fcfeec8463fa | 349 | lcd.pixel(elaser_old[c].x, elaser_old[c].y+1, BLACK); |
zlee9 | 0:fcfeec8463fa | 350 | lcd.pixel(elaser_old[c].x+1, elaser_old[c].y+1, BLACK); |
zlee9 | 0:fcfeec8463fa | 351 | // Don't draw lasers that hit bottom screen border or other objects |
zlee9 | 0:fcfeec8463fa | 352 | if (!(elaser[c].collide)) { |
zlee9 | 0:fcfeec8463fa | 353 | lcd.pixel(elaser[c].x, elaser[c].y, elaser[c].color); |
zlee9 | 0:fcfeec8463fa | 354 | lcd.pixel(elaser[c].x+1, elaser[c].y, elaser[c].color); |
zlee9 | 0:fcfeec8463fa | 355 | lcd.pixel(elaser[c].x, elaser[c].y+1, elaser[c].color); |
zlee9 | 0:fcfeec8463fa | 356 | lcd.pixel(elaser[c].x+1, elaser[c].y+1, elaser[c].color); |
zlee9 | 0:fcfeec8463fa | 357 | } |
zlee9 | 0:fcfeec8463fa | 358 | } |
zlee9 | 0:fcfeec8463fa | 359 | |
zlee9 | 0:fcfeec8463fa | 360 | // Redraw player's laser as single green pixel. Check that laser doesn't |
zlee9 | 0:fcfeec8463fa | 361 | // hit top screen border or other objects |
zlee9 | 0:fcfeec8463fa | 362 | lcd.pixel(ylaser_old.x, ylaser_old.y, BLACK); |
zlee9 | 0:fcfeec8463fa | 363 | lcd.pixel(ylaser_old.x+1, ylaser_old.y, BLACK); |
zlee9 | 0:fcfeec8463fa | 364 | lcd.pixel(ylaser_old.x, ylaser_old.y+1, BLACK); |
zlee9 | 0:fcfeec8463fa | 365 | lcd.pixel(ylaser_old.x+1, ylaser_old.y+1, BLACK); |
zlee9 | 0:fcfeec8463fa | 366 | if (!(ylaser.collide)) |
zlee9 | 0:fcfeec8463fa | 367 | lcd.pixel(ylaser.x, ylaser.y, ylaser.color); |
zlee9 | 0:fcfeec8463fa | 368 | lcd.pixel(ylaser.x+1, ylaser.y, ylaser.color); |
zlee9 | 0:fcfeec8463fa | 369 | lcd.pixel(ylaser.x, ylaser.y+1, ylaser.color); |
zlee9 | 0:fcfeec8463fa | 370 | lcd.pixel(ylaser.x+1, ylaser.y+1, ylaser.color); |
zlee9 | 0:fcfeec8463fa | 371 | |
zlee9 | 0:fcfeec8463fa | 372 | if (ship.killed) { // Ship is destroyed from being hit by laser |
zlee9 | 0:fcfeec8463fa | 373 | lcd.locate(0,15); |
zlee9 | 0:fcfeec8463fa | 374 | lcd.printf("LIVES: %d", lives_rem); |
zlee9 | 0:fcfeec8463fa | 375 | wait(2); // Game pauses for few seconds before player uses next life |
zlee9 | 0:fcfeec8463fa | 376 | lcd.cls(); // Clear lives message |
zlee9 | 0:fcfeec8463fa | 377 | } |
zlee9 | 0:fcfeec8463fa | 378 | |
zlee9 | 0:fcfeec8463fa | 379 | // Redraw player's ship as green rectangle |
zlee9 | 0:fcfeec8463fa | 380 | lcd.fill(ship_old.x, ship_old.y, ship_old.width, ship_old.height, BLACK); |
zlee9 | 0:fcfeec8463fa | 381 | lcd.fill(ship.x, ship.y, ship.width, ship.height, ship.color); |
zlee9 | 0:fcfeec8463fa | 382 | } |
zlee9 | 0:fcfeec8463fa | 383 | |
zlee9 | 0:fcfeec8463fa | 384 | |
zlee9 | 0:fcfeec8463fa | 385 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 386 | /* */ |
zlee9 | 0:fcfeec8463fa | 387 | /* This function plays the lose/win screen music and generates */ |
zlee9 | 0:fcfeec8463fa | 388 | /* the firing sound effect */ |
zlee9 | 0:fcfeec8463fa | 389 | /* */ |
zlee9 | 0:fcfeec8463fa | 390 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 391 | void playstart(void const *args)//Th |
zlee9 | 0:fcfeec8463fa | 392 | { //Depending on the state of the game, |
zlee9 | 0:fcfeec8463fa | 393 | //queue either screen music or play sound effect upon fire |
zlee9 | 0:fcfeec8463fa | 394 | while(true) { |
zlee9 | 0:fcfeec8463fa | 395 | FILE *wave_file; |
zlee9 | 0:fcfeec8463fa | 396 | //At the start screen, do nothing |
zlee9 | 0:fcfeec8463fa | 397 | while(state==START) { |
zlee9 | 0:fcfeec8463fa | 398 | Thread::wait(50); |
zlee9 | 0:fcfeec8463fa | 399 | } |
zlee9 | 0:fcfeec8463fa | 400 | |
zlee9 | 0:fcfeec8463fa | 401 | //wait 500ms after start screen music has been cutoff before |
zlee9 | 0:fcfeec8463fa | 402 | //continuing to avoid conflicts |
zlee9 | 0:fcfeec8463fa | 403 | while(!doneski) { |
zlee9 | 0:fcfeec8463fa | 404 | Thread::wait(500); |
zlee9 | 0:fcfeec8463fa | 405 | } |
zlee9 | 0:fcfeec8463fa | 406 | |
zlee9 | 0:fcfeec8463fa | 407 | //While in the game, play firing sound when the player fires |
zlee9 | 0:fcfeec8463fa | 408 | while(state==GAME) { |
zlee9 | 0:fcfeec8463fa | 409 | if(button) { |
zlee9 | 0:fcfeec8463fa | 410 | |
zlee9 | 0:fcfeec8463fa | 411 | wave_file=fopen("/sd/mample4.wav","r"); |
zlee9 | 0:fcfeec8463fa | 412 | |
zlee9 | 0:fcfeec8463fa | 413 | |
zlee9 | 0:fcfeec8463fa | 414 | waver.play(wave_file); |
zlee9 | 0:fcfeec8463fa | 415 | fclose(wave_file); |
zlee9 | 0:fcfeec8463fa | 416 | } |
zlee9 | 0:fcfeec8463fa | 417 | } |
zlee9 | 0:fcfeec8463fa | 418 | |
zlee9 | 0:fcfeec8463fa | 419 | //Play losing screen music |
zlee9 | 0:fcfeec8463fa | 420 | while(state==LOSE) { |
zlee9 | 0:fcfeec8463fa | 421 | wave_file=fopen("/sd/mample3.wav","r"); |
zlee9 | 0:fcfeec8463fa | 422 | waver.play(wave_file); |
zlee9 | 0:fcfeec8463fa | 423 | fclose(wave_file); |
zlee9 | 0:fcfeec8463fa | 424 | } |
zlee9 | 0:fcfeec8463fa | 425 | |
zlee9 | 0:fcfeec8463fa | 426 | //Play winning screen music |
zlee9 | 0:fcfeec8463fa | 427 | while(state==WIN) { |
zlee9 | 0:fcfeec8463fa | 428 | wave_file=fopen("/sd/mample3.wav","r"); |
zlee9 | 0:fcfeec8463fa | 429 | waver.play(wave_file); |
zlee9 | 0:fcfeec8463fa | 430 | fclose(wave_file); |
zlee9 | 0:fcfeec8463fa | 431 | } |
zlee9 | 0:fcfeec8463fa | 432 | } |
zlee9 | 0:fcfeec8463fa | 433 | } |
zlee9 | 0:fcfeec8463fa | 434 | |
zlee9 | 0:fcfeec8463fa | 435 | |
zlee9 | 0:fcfeec8463fa | 436 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 437 | /* */ |
zlee9 | 0:fcfeec8463fa | 438 | /* startmusic() plays the start screen music */ |
zlee9 | 0:fcfeec8463fa | 439 | /* */ |
zlee9 | 0:fcfeec8463fa | 440 | /***************************************************************/ |
zlee9 | 0:fcfeec8463fa | 441 | void startmusic(void const *args) |
zlee9 | 0:fcfeec8463fa | 442 | { |
zlee9 | 0:fcfeec8463fa | 443 | |
zlee9 | 0:fcfeec8463fa | 444 | FILE *wave_file; |
zlee9 | 0:fcfeec8463fa | 445 | wave_file=fopen("/sd/mample2.wav","r"); |
zlee9 | 0:fcfeec8463fa | 446 | waver.play(wave_file); |
zlee9 | 0:fcfeec8463fa | 447 | fclose(wave_file); |
zlee9 | 0:fcfeec8463fa | 448 | |
zlee9 | 0:fcfeec8463fa | 449 | } |