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