![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Helios Lyons 201239214
Dependencies: mbed
Brief
My aim for this project was to create a FRDM K64F adapted version of the classic Space Invaders by Tomohiro Nishikado. The game itself has a number of clear features to implement;
- Left to right movement for the player 'canon'
- A fixed amount of player lives
- Firing mechanics for both canon and invaders (hence collision systems)
- Random firing from remaining invaders
- Wave based combat
My own addition to these established ideas was Boss waves, featuring a single, larger sprite which fires at a faster interval than previous waves. The addition of a movement system using a basic for loop, as opposed to a velocity based system, will enhance the nostalgic feel of the game.
Gameplay
Movement is controlled with the joystick, moving the canon left or right. Fire by pressing A. Invaders spawn at set positions, but randomly fire at a set interval, which is higher for boss waves. Time is taken during each wave, and displayed at wave intervals, and if the play wins.
Controls are shown on the Gamepad below: (attribution: Craig A. Evans, ELEC2645 University of Leeds)
Battle/Battle.cpp@10:19b250c7de5e, 2020-05-18 (annotated)
- Committer:
- helioslyons
- Date:
- Mon May 18 18:14:35 2020 +0000
- Revision:
- 10:19b250c7de5e
- Parent:
- 9:6a245c8ce08e
- Child:
- 11:1fd8fca23375
Helios Lyons 201239214
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
helioslyons | 9:6a245c8ce08e | 1 | #include "Battle.h" |
helioslyons | 10:19b250c7de5e | 2 | #include "Bullet.h" |
helioslyons | 10:19b250c7de5e | 3 | #include "Invader.h" |
helioslyons | 10:19b250c7de5e | 4 | #include "Army.h" |
helioslyons | 10:19b250c7de5e | 5 | |
helioslyons | 10:19b250c7de5e | 6 | #include "N5110.h" |
helioslyons | 10:19b250c7de5e | 7 | #include "Gamepad.h" |
helioslyons | 10:19b250c7de5e | 8 | |
helioslyons | 10:19b250c7de5e | 9 | #include <vector> |
helioslyons | 9:6a245c8ce08e | 10 | |
helioslyons | 9:6a245c8ce08e | 11 | Battle::Battle() |
helioslyons | 9:6a245c8ce08e | 12 | { |
helioslyons | 9:6a245c8ce08e | 13 | |
helioslyons | 9:6a245c8ce08e | 14 | } |
helioslyons | 9:6a245c8ce08e | 15 | |
helioslyons | 9:6a245c8ce08e | 16 | Battle::~Battle() |
helioslyons | 9:6a245c8ce08e | 17 | { |
helioslyons | 9:6a245c8ce08e | 18 | |
helioslyons | 9:6a245c8ce08e | 19 | } |
helioslyons | 9:6a245c8ce08e | 20 | |
helioslyons | 10:19b250c7de5e | 21 | void Battle::init(int invaders, int speed, int interval) |
helioslyons | 9:6a245c8ce08e | 22 | { |
helioslyons | 10:19b250c7de5e | 23 | _invaders = invaders; |
helioslyons | 10:19b250c7de5e | 24 | _speed = speed; |
helioslyons | 10:19b250c7de5e | 25 | _interval = interval; |
helioslyons | 9:6a245c8ce08e | 26 | |
helioslyons | 10:19b250c7de5e | 27 | _army.create_army(); |
helioslyons | 10:19b250c7de5e | 28 | //_army.start_pos(5,5); |
helioslyons | 10:19b250c7de5e | 29 | //_canon.init(44); |
helioslyons | 9:6a245c8ce08e | 30 | |
helioslyons | 10:19b250c7de5e | 31 | //interval.attach(&Battle::player_fire, 8.0); |
helioslyons | 10:19b250c7de5e | 32 | |
helioslyons | 9:6a245c8ce08e | 33 | } |
helioslyons | 9:6a245c8ce08e | 34 | |
helioslyons | 9:6a245c8ce08e | 35 | void Battle::read_input(Gamepad &pad) |
helioslyons | 9:6a245c8ce08e | 36 | { |
helioslyons | 10:19b250c7de5e | 37 | _d = pad.get_direction(); |
helioslyons | 10:19b250c7de5e | 38 | _mag = pad.get_mag(); |
helioslyons | 10:19b250c7de5e | 39 | } |
helioslyons | 10:19b250c7de5e | 40 | |
helioslyons | 10:19b250c7de5e | 41 | void Battle::draw(N5110 &lcd) |
helioslyons | 10:19b250c7de5e | 42 | { |
helioslyons | 10:19b250c7de5e | 43 | //lcd.drawLine(1,1,WIDTH/2,HEIGHT-1,2); |
helioslyons | 10:19b250c7de5e | 44 | print_score(lcd); // score |
helioslyons | 9:6a245c8ce08e | 45 | |
helioslyons | 10:19b250c7de5e | 46 | _canon.draw(lcd); // canon |
helioslyons | 10:19b250c7de5e | 47 | _army.draw(lcd); // invaders |
helioslyons | 10:19b250c7de5e | 48 | |
helioslyons | 10:19b250c7de5e | 49 | vector<Bullet>::iterator it1; |
helioslyons | 10:19b250c7de5e | 50 | int i; |
helioslyons | 10:19b250c7de5e | 51 | vector<Bullet>::iterator it2; |
helioslyons | 10:19b250c7de5e | 52 | int n; |
helioslyons | 10:19b250c7de5e | 53 | |
helioslyons | 10:19b250c7de5e | 54 | for (it1 = invBomb.begin(); it1 != invBomb.end(); ++it1, ++i) { |
helioslyons | 10:19b250c7de5e | 55 | invBomb[i].draw(lcd); |
helioslyons | 10:19b250c7de5e | 56 | } |
helioslyons | 10:19b250c7de5e | 57 | for (it2 = playBul.begin(); it2 != playBul.end(); ++it2, ++n) { |
helioslyons | 10:19b250c7de5e | 58 | playBul[i].draw(lcd); |
helioslyons | 10:19b250c7de5e | 59 | } |
helioslyons | 10:19b250c7de5e | 60 | print_score(lcd); |
helioslyons | 10:19b250c7de5e | 61 | } |
helioslyons | 10:19b250c7de5e | 62 | |
helioslyons | 10:19b250c7de5e | 63 | void Battle::update(Gamepad &pad) |
helioslyons | 10:19b250c7de5e | 64 | { |
helioslyons | 10:19b250c7de5e | 65 | // important to update paddles and ball before checking collisions so can |
helioslyons | 10:19b250c7de5e | 66 | // correct for it before updating the display |
helioslyons | 10:19b250c7de5e | 67 | _canon.update(_d,_mag); |
helioslyons | 10:19b250c7de5e | 68 | |
helioslyons | 10:19b250c7de5e | 69 | invader_fire(); |
helioslyons | 10:19b250c7de5e | 70 | canon_fire(pad); |
helioslyons | 10:19b250c7de5e | 71 | |
helioslyons | 10:19b250c7de5e | 72 | int i, n; |
helioslyons | 10:19b250c7de5e | 73 | vector<Bullet>::iterator it1; |
helioslyons | 10:19b250c7de5e | 74 | vector<Bullet>::iterator it2; |
helioslyons | 10:19b250c7de5e | 75 | |
helioslyons | 10:19b250c7de5e | 76 | for (it1 = invBomb.begin(); it1 != invBomb.end(); ++it1, ++i) { |
helioslyons | 10:19b250c7de5e | 77 | invBomb[i].update(); |
helioslyons | 10:19b250c7de5e | 78 | } |
helioslyons | 10:19b250c7de5e | 79 | for (it2 = playBul.begin(); it2 != playBul.end(); ++it2, ++n) { |
helioslyons | 10:19b250c7de5e | 80 | playBul[i].update(); |
helioslyons | 10:19b250c7de5e | 81 | } |
helioslyons | 10:19b250c7de5e | 82 | |
helioslyons | 10:19b250c7de5e | 83 | bullet_collisions(); |
helioslyons | 10:19b250c7de5e | 84 | check_invader_hit(pad); |
helioslyons | 10:19b250c7de5e | 85 | check_canon_hit(pad); |
helioslyons | 9:6a245c8ce08e | 86 | } |
helioslyons | 9:6a245c8ce08e | 87 | |
helioslyons | 9:6a245c8ce08e | 88 | void Battle::invader_fire() |
helioslyons | 9:6a245c8ce08e | 89 | { |
helioslyons | 10:19b250c7de5e | 90 | _army.rand_invader(); |
helioslyons | 10:19b250c7de5e | 91 | |
helioslyons | 10:19b250c7de5e | 92 | int x1 = _army._fire_x; |
helioslyons | 10:19b250c7de5e | 93 | int y1 = _army._fire_y; |
helioslyons | 10:19b250c7de5e | 94 | |
helioslyons | 10:19b250c7de5e | 95 | Bullet bomb; // instantiate new bullet object |
helioslyons | 10:19b250c7de5e | 96 | bomb.init(false, x1, y1); // initialise |
helioslyons | 10:19b250c7de5e | 97 | |
helioslyons | 10:19b250c7de5e | 98 | invBomb.push_back(bomb); // add to invader bomb vector |
helioslyons | 9:6a245c8ce08e | 99 | } |
helioslyons | 9:6a245c8ce08e | 100 | |
helioslyons | 10:19b250c7de5e | 101 | void Battle::canon_fire(Gamepad &pad) |
helioslyons | 9:6a245c8ce08e | 102 | { |
helioslyons | 10:19b250c7de5e | 103 | bool A = pad.A_pressed(); |
helioslyons | 10:19b250c7de5e | 104 | if (A) { |
helioslyons | 10:19b250c7de5e | 105 | // |
helioslyons | 10:19b250c7de5e | 106 | |
helioslyons | 10:19b250c7de5e | 107 | Vector2D canon_pos = _canon.get_pos(); |
helioslyons | 10:19b250c7de5e | 108 | int x1 = canon_pos.x; |
helioslyons | 10:19b250c7de5e | 109 | int y1 = canon_pos.y; |
helioslyons | 10:19b250c7de5e | 110 | |
helioslyons | 10:19b250c7de5e | 111 | Bullet shot; |
helioslyons | 10:19b250c7de5e | 112 | shot.init(true, x1, y1); |
helioslyons | 10:19b250c7de5e | 113 | playBul.push_back(shot); |
helioslyons | 10:19b250c7de5e | 114 | } |
helioslyons | 9:6a245c8ce08e | 115 | } |
helioslyons | 9:6a245c8ce08e | 116 | |
helioslyons | 9:6a245c8ce08e | 117 | void Battle::check_invader_hit(Gamepad &pad) |
helioslyons | 9:6a245c8ce08e | 118 | { |
helioslyons | 10:19b250c7de5e | 119 | vector<Bullet>::iterator it1; // Iterate through canon bullet vector |
helioslyons | 10:19b250c7de5e | 120 | Vector2D playBulPos; // Use collision function from army to iterate through invaders |
helioslyons | 10:19b250c7de5e | 121 | int i; // Match deletes bullet, removes invader |
helioslyons | 10:19b250c7de5e | 122 | |
helioslyons | 10:19b250c7de5e | 123 | for (it1 = playBul.begin(); it1 != playBul.end(); ++it1, ++i) { |
helioslyons | 10:19b250c7de5e | 124 | |
helioslyons | 10:19b250c7de5e | 125 | playBulPos = playBul[i].get_pos(); |
helioslyons | 10:19b250c7de5e | 126 | int playBulX = playBulPos.x; |
helioslyons | 10:19b250c7de5e | 127 | int playBulY = playBulPos.y; |
helioslyons | 10:19b250c7de5e | 128 | |
helioslyons | 10:19b250c7de5e | 129 | _army.check_col(playBulX, playBulY); |
helioslyons | 10:19b250c7de5e | 130 | _canon.kill(_army._sprite_pass); |
helioslyons | 10:19b250c7de5e | 131 | playBul.erase(playBul.begin() + i - 1); |
helioslyons | 10:19b250c7de5e | 132 | |
helioslyons | 10:19b250c7de5e | 133 | } |
helioslyons | 9:6a245c8ce08e | 134 | } |
helioslyons | 9:6a245c8ce08e | 135 | |
helioslyons | 9:6a245c8ce08e | 136 | void Battle::check_canon_hit(Gamepad &pad) |
helioslyons | 9:6a245c8ce08e | 137 | { |
helioslyons | 10:19b250c7de5e | 138 | vector<Bullet>::iterator it2; // Iterate through invader bomb vector |
helioslyons | 10:19b250c7de5e | 139 | Vector2D invBombPos; // If positions are equal, delete bullet and remove life |
helioslyons | 10:19b250c7de5e | 140 | Vector2D canonPos; |
helioslyons | 10:19b250c7de5e | 141 | |
helioslyons | 10:19b250c7de5e | 142 | int i; |
helioslyons | 10:19b250c7de5e | 143 | |
helioslyons | 10:19b250c7de5e | 144 | for (it2 = invBomb.begin(); it2 != invBomb.end(); ++it2, ++i) { |
helioslyons | 10:19b250c7de5e | 145 | |
helioslyons | 10:19b250c7de5e | 146 | invBombPos = invBomb[i].get_pos(); |
helioslyons | 10:19b250c7de5e | 147 | canonPos = _canon.get_pos(); |
helioslyons | 10:19b250c7de5e | 148 | |
helioslyons | 10:19b250c7de5e | 149 | if (invBombPos.x == canonPos.x && invBombPos.y == canonPos.y) { |
helioslyons | 10:19b250c7de5e | 150 | invBomb.erase(invBomb.begin() + 1 - 1); |
helioslyons | 10:19b250c7de5e | 151 | _canon.remove_life(); |
helioslyons | 10:19b250c7de5e | 152 | } |
helioslyons | 10:19b250c7de5e | 153 | } |
helioslyons | 9:6a245c8ce08e | 154 | } |
helioslyons | 9:6a245c8ce08e | 155 | |
helioslyons | 10:19b250c7de5e | 156 | void Battle::bullet_collisions() |
helioslyons | 9:6a245c8ce08e | 157 | { |
helioslyons | 10:19b250c7de5e | 158 | vector<Bullet>::iterator it1; |
helioslyons | 10:19b250c7de5e | 159 | vector<Bullet>::iterator it2; |
helioslyons | 10:19b250c7de5e | 160 | |
helioslyons | 10:19b250c7de5e | 161 | Vector2D invBombCol; |
helioslyons | 10:19b250c7de5e | 162 | Vector2D playBulCol; |
helioslyons | 10:19b250c7de5e | 163 | int i; |
helioslyons | 10:19b250c7de5e | 164 | |
helioslyons | 10:19b250c7de5e | 165 | for (it2 = invBomb.begin(); it2 != invBomb.end(); ++it2, ++i) { |
helioslyons | 10:19b250c7de5e | 166 | invBombCol = invBomb[i].get_pos(); |
helioslyons | 10:19b250c7de5e | 167 | |
helioslyons | 10:19b250c7de5e | 168 | if (invBombCol.y >= 48){ |
helioslyons | 10:19b250c7de5e | 169 | invBomb.erase(invBomb.begin() + i - 1); |
helioslyons | 10:19b250c7de5e | 170 | } |
helioslyons | 10:19b250c7de5e | 171 | } |
helioslyons | 10:19b250c7de5e | 172 | |
helioslyons | 10:19b250c7de5e | 173 | int n; |
helioslyons | 10:19b250c7de5e | 174 | |
helioslyons | 10:19b250c7de5e | 175 | for (it1 = playBul.begin(); it1 != playBul.end(); ++it1, ++n) { |
helioslyons | 10:19b250c7de5e | 176 | playBulCol = playBul[n].get_pos(); |
helioslyons | 10:19b250c7de5e | 177 | |
helioslyons | 10:19b250c7de5e | 178 | if (playBulCol.x >= 0){ |
helioslyons | 10:19b250c7de5e | 179 | playBul.erase(playBul.begin() + n - 1); |
helioslyons | 10:19b250c7de5e | 180 | } |
helioslyons | 10:19b250c7de5e | 181 | } |
helioslyons | 9:6a245c8ce08e | 182 | } |
helioslyons | 9:6a245c8ce08e | 183 | |
helioslyons | 10:19b250c7de5e | 184 | void Battle::print_score(N5110 &lcd) |
helioslyons | 10:19b250c7de5e | 185 | { |
helioslyons | 10:19b250c7de5e | 186 | int total_score = _canon.get_score(); |
helioslyons | 10:19b250c7de5e | 187 | char buffer[14]; |
helioslyons | 10:19b250c7de5e | 188 | sprintf(buffer,"%2d",total_score); |
helioslyons | 10:19b250c7de5e | 189 | lcd.printString(buffer,42,1); |
helioslyons | 10:19b250c7de5e | 190 | } |