Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: PORGFINAL SpaceInvadersFINAL
SIgame.cpp@1:91edc119bf6d, 2013-03-07 (annotated)
- Committer:
- zlee9
- Date:
- Thu Mar 07 22:36:07 2013 +0000
- Revision:
- 1:91edc119bf6d
- Parent:
- 0:4564efa43302
Fixed laser collision (lasers were previously bypassing enemy laser after touching them)
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| zlee9 | 0:4564efa43302 | 1 | #include "SIgame.h" |
| zlee9 | 0:4564efa43302 | 2 | #include "mbed.h" |
| zlee9 | 0:4564efa43302 | 3 | |
| zlee9 | 0:4564efa43302 | 4 | /***************************************************************/ |
| zlee9 | 0:4564efa43302 | 5 | /* */ |
| zlee9 | 0:4564efa43302 | 6 | /* Game-Related Functions */ |
| zlee9 | 0:4564efa43302 | 7 | /* */ |
| zlee9 | 0:4564efa43302 | 8 | /***************************************************************/ |
| zlee9 | 0:4564efa43302 | 9 | OBJECT startShip() |
| zlee9 | 0:4564efa43302 | 10 | {/****** |
| zlee9 | 0:4564efa43302 | 11 | * startShip |
| zlee9 | 0:4564efa43302 | 12 | * Sets up structure for player's ship |
| zlee9 | 0:4564efa43302 | 13 | ******/ |
| zlee9 | 0:4564efa43302 | 14 | OBJECT ship; |
| zlee9 | 0:4564efa43302 | 15 | |
| zlee9 | 0:4564efa43302 | 16 | ship.height = SHIP_HEIGHT; |
| zlee9 | 0:4564efa43302 | 17 | ship.width = SHIP_WIDTH; |
| zlee9 | 0:4564efa43302 | 18 | ship.y = SCREENHEIGHT - ship.height + 1; // Starting location |
| zlee9 | 0:4564efa43302 | 19 | ship.x = SCREENWIDTH >> 1; |
| zlee9 | 0:4564efa43302 | 20 | ship.color = GREEN; // Player's ship is green |
| zlee9 | 0:4564efa43302 | 21 | ship.killed = false; |
| zlee9 | 0:4564efa43302 | 22 | |
| zlee9 | 0:4564efa43302 | 23 | return ship; |
| zlee9 | 0:4564efa43302 | 24 | } |
| zlee9 | 0:4564efa43302 | 25 | |
| zlee9 | 0:4564efa43302 | 26 | void summonWave() |
| zlee9 | 0:4564efa43302 | 27 | {/****** |
| zlee9 | 0:4564efa43302 | 28 | * summonWave |
| zlee9 | 0:4564efa43302 | 29 | * Initialize 2-D array containing columns of aliens and their starting |
| zlee9 | 0:4564efa43302 | 30 | * indicies |
| zlee9 | 0:4564efa43302 | 31 | ******/ |
| zlee9 | 0:4564efa43302 | 32 | int r, c, i, j; |
| zlee9 | 0:4564efa43302 | 33 | j = 1; |
| zlee9 | 0:4564efa43302 | 34 | |
| zlee9 | 0:4564efa43302 | 35 | for (c = 0; c < NUM_ALIEN_COLS; c++) |
| zlee9 | 0:4564efa43302 | 36 | { |
| zlee9 | 0:4564efa43302 | 37 | i = 0; |
| zlee9 | 0:4564efa43302 | 38 | for (r = 0; r < NUM_ALIEN_ROWS; r++) |
| zlee9 | 0:4564efa43302 | 39 | { // Within current column, initialize aliens' locations |
| zlee9 | 0:4564efa43302 | 40 | wave[r][c].x = j; |
| zlee9 | 0:4564efa43302 | 41 | wave[r][c].width = ALIEN_WIDTH; |
| zlee9 | 0:4564efa43302 | 42 | wave[r][c].y = i; |
| zlee9 | 0:4564efa43302 | 43 | wave[r][c].height = ALIEN_HEIGHT; |
| zlee9 | 0:4564efa43302 | 44 | wave[r][c].color = WHITE; // Aliens are white |
| zlee9 | 0:4564efa43302 | 45 | wave[r][c].killed = false; |
| zlee9 | 0:4564efa43302 | 46 | i += (ALIEN_HEIGHT + 1); |
| zlee9 | 0:4564efa43302 | 47 | } |
| zlee9 | 0:4564efa43302 | 48 | |
| zlee9 | 0:4564efa43302 | 49 | j += (ALIEN_WIDTH + 3); // Ready gap for next column of aliens |
| zlee9 | 0:4564efa43302 | 50 | } |
| zlee9 | 0:4564efa43302 | 51 | } |
| zlee9 | 0:4564efa43302 | 52 | |
| zlee9 | 0:4564efa43302 | 53 | |
| zlee9 | 0:4564efa43302 | 54 | void destroyAlien(int *pals_rem, POINT *pylaser, OBJECT *frontline[]) |
| zlee9 | 0:4564efa43302 | 55 | {/****** |
| zlee9 | 0:4564efa43302 | 56 | * destroyAlien |
| zlee9 | 0:4564efa43302 | 57 | * Checks if ship's laser hits any alien and kill alien if so |
| zlee9 | 0:4564efa43302 | 58 | ******/ |
| zlee9 | 0:4564efa43302 | 59 | bool hit = false; |
| zlee9 | 0:4564efa43302 | 60 | int r, c = 0; |
| zlee9 | 0:4564efa43302 | 61 | |
| zlee9 | 0:4564efa43302 | 62 | do |
| zlee9 | 0:4564efa43302 | 63 | { // Go to column from wave of aliens of which laser may hit alien |
| zlee9 | 0:4564efa43302 | 64 | if (pylaser->x >= wave[0][c].x && pylaser->x <= wave[0][c].x + |
| zlee9 | 0:4564efa43302 | 65 | wave[0][c].width - 1) |
| zlee9 | 0:4564efa43302 | 66 | { |
| zlee9 | 0:4564efa43302 | 67 | r = 0; |
| zlee9 | 0:4564efa43302 | 68 | do // Find alien in column that laser touches if possible |
| zlee9 | 0:4564efa43302 | 69 | { |
| zlee9 | 0:4564efa43302 | 70 | if (pylaser->y >= wave[r][c].y && pylaser->y <= wave[r][c].y + |
| zlee9 | 0:4564efa43302 | 71 | wave[r][c].height && !(wave[r][c].killed) && |
| zlee9 | 0:4564efa43302 | 72 | !(pylaser->collide)) |
| zlee9 | 0:4564efa43302 | 73 | { // Alien is killed and not be drawn on screen |
| zlee9 | 0:4564efa43302 | 74 | (*pals_rem)--; |
| zlee9 | 0:4564efa43302 | 75 | wave[r][c].killed = pylaser->collide = hit = true; |
| zlee9 | 0:4564efa43302 | 76 | |
| zlee9 | 0:4564efa43302 | 77 | // Adjust pointer to front alien of selected column if killed |
| zlee9 | 0:4564efa43302 | 78 | // alien was one |
| zlee9 | 0:4564efa43302 | 79 | if (frontline[c] == &wave[r][c]) |
| zlee9 | 0:4564efa43302 | 80 | { |
| zlee9 | 0:4564efa43302 | 81 | if (!r) // All aliens on that are wiped out |
| zlee9 | 0:4564efa43302 | 82 | frontline[c] = NULL; |
| zlee9 | 0:4564efa43302 | 83 | else // Point to alien behind killed one |
| zlee9 | 0:4564efa43302 | 84 | frontline[c] = &wave[r-1][c]; |
| zlee9 | 0:4564efa43302 | 85 | } |
| zlee9 | 0:4564efa43302 | 86 | } |
| zlee9 | 0:4564efa43302 | 87 | r++; |
| zlee9 | 0:4564efa43302 | 88 | } while (!hit && r < NUM_ALIEN_ROWS); |
| zlee9 | 0:4564efa43302 | 89 | } |
| zlee9 | 0:4564efa43302 | 90 | c++; |
| zlee9 | 0:4564efa43302 | 91 | } while (!hit && c < NUM_ALIEN_COLS); |
| zlee9 | 0:4564efa43302 | 92 | } |
| zlee9 | 0:4564efa43302 | 93 | |
| zlee9 | 0:4564efa43302 | 94 | |
| zlee9 | 0:4564efa43302 | 95 | bool twoLasersCollide(POINT *pylaser, POINT elaser[]) |
| zlee9 | 0:4564efa43302 | 96 | {/****** |
| zlee9 | 0:4564efa43302 | 97 | * twoLasersCollide |
| zlee9 | 0:4564efa43302 | 98 | * Checks if player's laser touches any of alien's lasers and removes them if |
| zlee9 | 0:4564efa43302 | 99 | * so |
| zlee9 | 0:4564efa43302 | 100 | ******/ |
| zlee9 | 0:4564efa43302 | 101 | int i; |
| zlee9 | 0:4564efa43302 | 102 | |
| zlee9 | 0:4564efa43302 | 103 | for (i = 0; i < ELASER_CAP; i++) |
| zlee9 | 0:4564efa43302 | 104 | { // Find enemy laser in same location as player's laser if so |
| zlee9 | 1:91edc119bf6d | 105 | if (pylaser->x == elaser[i].x && pylaser->y <= elaser[i].y+2 && |
| zlee9 | 0:4564efa43302 | 106 | !(pylaser->collide || elaser[i].collide)) |
| zlee9 | 0:4564efa43302 | 107 | { // Both lasers cancel each other out and disappear on screen |
| zlee9 | 0:4564efa43302 | 108 | pylaser->collide = elaser[i].collide = true; |
| zlee9 | 0:4564efa43302 | 109 | return true; |
| zlee9 | 0:4564efa43302 | 110 | } |
| zlee9 | 0:4564efa43302 | 111 | } |
| zlee9 | 0:4564efa43302 | 112 | return false; |
| zlee9 | 0:4564efa43302 | 113 | } |
| zlee9 | 0:4564efa43302 | 114 | |
| zlee9 | 0:4564efa43302 | 115 | |
| zlee9 | 0:4564efa43302 | 116 | bool moveAlienWave(bool *pleft) |
| zlee9 | 0:4564efa43302 | 117 | {/****** |
| zlee9 | 0:4564efa43302 | 118 | * moveAlienWave |
| zlee9 | 0:4564efa43302 | 119 | * Changes all aliens' locations to move wave of aliens |
| zlee9 | 0:4564efa43302 | 120 | ******/ |
| zlee9 | 0:4564efa43302 | 121 | int r, c; |
| zlee9 | 0:4564efa43302 | 122 | bool reach, old_dir; |
| zlee9 | 0:4564efa43302 | 123 | reach = false; |
| zlee9 | 0:4564efa43302 | 124 | old_dir = *pleft; |
| zlee9 | 0:4564efa43302 | 125 | |
| zlee9 | 0:4564efa43302 | 126 | // Flip direction when wave reaches either side of screen |
| zlee9 | 0:4564efa43302 | 127 | if (wave[NUM_ALIEN_ROWS-1][0].x <= 0 && *pleft) |
| zlee9 | 0:4564efa43302 | 128 | *pleft = false; |
| zlee9 | 0:4564efa43302 | 129 | else if (wave[NUM_ALIEN_ROWS-1][NUM_ALIEN_COLS-1].x + ALIEN_WIDTH >= |
| zlee9 | 0:4564efa43302 | 130 | SCREENWIDTH && !(*pleft)) |
| zlee9 | 0:4564efa43302 | 131 | *pleft = true; |
| zlee9 | 0:4564efa43302 | 132 | |
| zlee9 | 0:4564efa43302 | 133 | // Change all aliens' locations in array |
| zlee9 | 0:4564efa43302 | 134 | for (c = 0; c < NUM_ALIEN_COLS; c++) |
| zlee9 | 0:4564efa43302 | 135 | { |
| zlee9 | 0:4564efa43302 | 136 | for (r = 0; r < NUM_ALIEN_ROWS; r++) |
| zlee9 | 0:4564efa43302 | 137 | { |
| zlee9 | 0:4564efa43302 | 138 | if (old_dir ^ *pleft) // Moves downward if end columns touch borders |
| zlee9 | 0:4564efa43302 | 139 | wave[r][c].y += ALIEN_HEIGHT; |
| zlee9 | 0:4564efa43302 | 140 | // If not moving down, then moves horizontally |
| zlee9 | 0:4564efa43302 | 141 | if (*pleft) |
| zlee9 | 0:4564efa43302 | 142 | (wave[r][c].x)--; |
| zlee9 | 0:4564efa43302 | 143 | else |
| zlee9 | 0:4564efa43302 | 144 | (wave[r][c].x)++; |
| zlee9 | 0:4564efa43302 | 145 | |
| zlee9 | 0:4564efa43302 | 146 | // Player loses if any alien reaches screen border at player's side |
| zlee9 | 1:91edc119bf6d | 147 | if (wave[r][c].y > SCREENHEIGHT - (wave[r][c].height << 1) + 1 |
| zlee9 | 1:91edc119bf6d | 148 | && !(wave[r][c].killed)) |
| zlee9 | 0:4564efa43302 | 149 | reach = true; |
| zlee9 | 0:4564efa43302 | 150 | } |
| zlee9 | 0:4564efa43302 | 151 | } |
| zlee9 | 0:4564efa43302 | 152 | return reach; |
| zlee9 | 0:4564efa43302 | 153 | } |