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.
Dependencies: mbed
SpaceInvaderEngine/SpaceInvaderEngine.cpp@16:987f72d9bb8f, 2020-05-01 (annotated)
- Committer:
- josh_ohara
- Date:
- Fri May 01 12:56:33 2020 +0000
- Revision:
- 16:987f72d9bb8f
- Parent:
- 15:dde4ce4bf7fe
- Child:
- 18:828e9f6ddfdb
Ship bullet timer added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
josh_ohara | 7:06a2558155f0 | 1 | |
josh_ohara | 7:06a2558155f0 | 2 | #include "SpaceInvaderEngine.h" |
josh_ohara | 7:06a2558155f0 | 3 | |
josh_ohara | 7:06a2558155f0 | 4 | SpaceInvaderEngine::SpaceInvaderEngine() |
josh_ohara | 7:06a2558155f0 | 5 | { |
josh_ohara | 7:06a2558155f0 | 6 | |
josh_ohara | 7:06a2558155f0 | 7 | } |
josh_ohara | 7:06a2558155f0 | 8 | |
josh_ohara | 8:86cb9a9f8a73 | 9 | SpaceInvaderEngine::~SpaceInvaderEngine() |
josh_ohara | 8:86cb9a9f8a73 | 10 | { |
josh_ohara | 8:86cb9a9f8a73 | 11 | |
josh_ohara | 8:86cb9a9f8a73 | 12 | } |
josh_ohara | 8:86cb9a9f8a73 | 13 | |
josh_ohara | 15:dde4ce4bf7fe | 14 | void SpaceInvaderEngine::init(int ship_height, int ship_width, int alien_size, int no_aliens, int armada_column_size, int armada_row_size, int cover_y, int cover1_x, int cover2_x, int cover3_x, int no_rocks) { |
josh_ohara | 7:06a2558155f0 | 15 | |
josh_ohara | 7:06a2558155f0 | 16 | S1_height = ship_height; |
josh_ohara | 7:06a2558155f0 | 17 | S1_width = ship_width; |
josh_ohara | 9:8e695df3cc36 | 18 | A1_size = alien_size; |
josh_ohara | 8:86cb9a9f8a73 | 19 | |
josh_ohara | 11:c174d84e4866 | 20 | Vector2D ship_pos = S1.get_position(); |
josh_ohara | 14:e88bcf5c0887 | 21 | S1x = ship_pos.x; |
josh_ohara | 14:e88bcf5c0887 | 22 | S1y = ship_pos.y; |
josh_ohara | 7:06a2558155f0 | 23 | |
josh_ohara | 12:be491ab6e742 | 24 | N = no_aliens; |
josh_ohara | 15:dde4ce4bf7fe | 25 | CS = armada_column_size; |
josh_ohara | 15:dde4ce4bf7fe | 26 | RS = armada_row_size; |
josh_ohara | 15:dde4ce4bf7fe | 27 | |
josh_ohara | 15:dde4ce4bf7fe | 28 | Cy = cover_y; |
josh_ohara | 15:dde4ce4bf7fe | 29 | C1x = cover1_x; |
josh_ohara | 15:dde4ce4bf7fe | 30 | C2x = cover2_x; |
josh_ohara | 15:dde4ce4bf7fe | 31 | C3x = cover3_x; |
josh_ohara | 15:dde4ce4bf7fe | 32 | NR = no_rocks; |
josh_ohara | 12:be491ab6e742 | 33 | |
josh_ohara | 12:be491ab6e742 | 34 | A1.init(N,A1_size,CS,RS); |
josh_ohara | 7:06a2558155f0 | 35 | S1.init(S1_height,S1_width); |
josh_ohara | 11:c174d84e4866 | 36 | BS1.init(); |
josh_ohara | 15:dde4ce4bf7fe | 37 | C1.init(C1x,Cy,NR); |
josh_ohara | 15:dde4ce4bf7fe | 38 | C2.init(C2x,Cy,NR); |
josh_ohara | 15:dde4ce4bf7fe | 39 | C3.init(C3x,Cy,NR); |
josh_ohara | 8:86cb9a9f8a73 | 40 | |
josh_ohara | 7:06a2558155f0 | 41 | } |
josh_ohara | 7:06a2558155f0 | 42 | |
josh_ohara | 7:06a2558155f0 | 43 | void SpaceInvaderEngine::read_input(Gamepad &pad) |
josh_ohara | 7:06a2558155f0 | 44 | { |
josh_ohara | 7:06a2558155f0 | 45 | D = pad.get_direction(); |
josh_ohara | 7:06a2558155f0 | 46 | Mag = pad.get_mag(); |
josh_ohara | 7:06a2558155f0 | 47 | } |
josh_ohara | 8:86cb9a9f8a73 | 48 | |
josh_ohara | 8:86cb9a9f8a73 | 49 | void SpaceInvaderEngine::render(N5110 &lcd) |
josh_ohara | 8:86cb9a9f8a73 | 50 | { |
josh_ohara | 8:86cb9a9f8a73 | 51 | S1.render(lcd); |
josh_ohara | 9:8e695df3cc36 | 52 | A1.render(lcd); |
josh_ohara | 11:c174d84e4866 | 53 | BS1.render(lcd); |
josh_ohara | 15:dde4ce4bf7fe | 54 | C1.render(lcd); |
josh_ohara | 15:dde4ce4bf7fe | 55 | C2.render(lcd); |
josh_ohara | 15:dde4ce4bf7fe | 56 | C3.render(lcd); |
josh_ohara | 8:86cb9a9f8a73 | 57 | } |
josh_ohara | 8:86cb9a9f8a73 | 58 | |
josh_ohara | 16:987f72d9bb8f | 59 | void SpaceInvaderEngine::update(Gamepad &pad, N5110 &lcd, int counter) |
josh_ohara | 14:e88bcf5c0887 | 60 | { |
josh_ohara | 14:e88bcf5c0887 | 61 | get_ship_pos(); |
josh_ohara | 8:86cb9a9f8a73 | 62 | S1.update(D,Mag); |
josh_ohara | 16:987f72d9bb8f | 63 | BS1.update(pad, lcd, S1x+4, S1y, counter); |
josh_ohara | 14:e88bcf5c0887 | 64 | A1.update(pad); |
josh_ohara | 14:e88bcf5c0887 | 65 | ship_bullet_alien_collision(pad, lcd); |
josh_ohara | 15:dde4ce4bf7fe | 66 | ship_bullet_cover1_collision(pad, lcd); |
josh_ohara | 15:dde4ce4bf7fe | 67 | ship_bullet_cover2_collision(pad, lcd); |
josh_ohara | 15:dde4ce4bf7fe | 68 | ship_bullet_cover3_collision(pad, lcd); |
josh_ohara | 8:86cb9a9f8a73 | 69 | } |
josh_ohara | 8:86cb9a9f8a73 | 70 | |
josh_ohara | 11:c174d84e4866 | 71 | void SpaceInvaderEngine::get_ship_pos() |
josh_ohara | 11:c174d84e4866 | 72 | { |
josh_ohara | 11:c174d84e4866 | 73 | Vector2D ship_pos = S1.get_position(); |
josh_ohara | 14:e88bcf5c0887 | 74 | S1x = ship_pos.x; |
josh_ohara | 14:e88bcf5c0887 | 75 | S1y = ship_pos.y; |
josh_ohara | 11:c174d84e4866 | 76 | } |
josh_ohara | 14:e88bcf5c0887 | 77 | |
josh_ohara | 14:e88bcf5c0887 | 78 | void SpaceInvaderEngine::ship_bullet_alien_collision(Gamepad &pad, N5110 &lcd){ |
josh_ohara | 14:e88bcf5c0887 | 79 | vector<Alien> alien_vector = A1.get_vector(); |
josh_ohara | 14:e88bcf5c0887 | 80 | vector<Bullet> bullet_vector = BS1.get_vector(); |
josh_ohara | 14:e88bcf5c0887 | 81 | for(int i = 0; i < alien_vector.size(); i++){ |
josh_ohara | 14:e88bcf5c0887 | 82 | Vector2D alien_position = alien_vector[i].get_position(); |
josh_ohara | 14:e88bcf5c0887 | 83 | int alien_x = alien_position.x; |
josh_ohara | 14:e88bcf5c0887 | 84 | int alien_y = alien_position.y; |
josh_ohara | 15:dde4ce4bf7fe | 85 | bool alien_life = alien_vector[i].get_life(); |
josh_ohara | 14:e88bcf5c0887 | 86 | for (int n = 0; n < bullet_vector.size(); n++){ |
josh_ohara | 14:e88bcf5c0887 | 87 | Vector2D bullet_position = bullet_vector[n].get_position(); |
josh_ohara | 14:e88bcf5c0887 | 88 | int bullet_x = bullet_position.x; |
josh_ohara | 14:e88bcf5c0887 | 89 | int bullet_y = bullet_position.y; |
josh_ohara | 15:dde4ce4bf7fe | 90 | bool hit = bullet_vector[n].get_hit(); |
josh_ohara | 15:dde4ce4bf7fe | 91 | if((alien_x <= bullet_x) && |
josh_ohara | 15:dde4ce4bf7fe | 92 | (bullet_x <= alien_x + A1_size - 1)&& |
josh_ohara | 15:dde4ce4bf7fe | 93 | (alien_y <= bullet_y) && |
josh_ohara | 15:dde4ce4bf7fe | 94 | (bullet_y <= alien_y + A1_size - 1)&& |
josh_ohara | 15:dde4ce4bf7fe | 95 | (hit==false)&& |
josh_ohara | 15:dde4ce4bf7fe | 96 | (alien_life == true)) { |
josh_ohara | 14:e88bcf5c0887 | 97 | bool T = true; |
josh_ohara | 14:e88bcf5c0887 | 98 | bool F = false; |
josh_ohara | 14:e88bcf5c0887 | 99 | A1.set_life(i,F); |
josh_ohara | 14:e88bcf5c0887 | 100 | BS1.set_hit(n,T); |
josh_ohara | 14:e88bcf5c0887 | 101 | } |
josh_ohara | 14:e88bcf5c0887 | 102 | } |
josh_ohara | 14:e88bcf5c0887 | 103 | } |
josh_ohara | 14:e88bcf5c0887 | 104 | } |
josh_ohara | 14:e88bcf5c0887 | 105 | |
josh_ohara | 15:dde4ce4bf7fe | 106 | void SpaceInvaderEngine::ship_bullet_cover1_collision(Gamepad &pad, N5110 &lcd){ |
josh_ohara | 15:dde4ce4bf7fe | 107 | vector<Rock> cover_vector_1 = C1.get_vector(); |
josh_ohara | 15:dde4ce4bf7fe | 108 | vector<Bullet> bullet_vector = BS1.get_vector(); |
josh_ohara | 15:dde4ce4bf7fe | 109 | for(int i = 0; i < cover_vector_1.size(); i++){ |
josh_ohara | 15:dde4ce4bf7fe | 110 | Vector2D rock_position = cover_vector_1[i].get_position(); |
josh_ohara | 15:dde4ce4bf7fe | 111 | int rock_x = rock_position.x; |
josh_ohara | 15:dde4ce4bf7fe | 112 | int rock_y = rock_position.y; |
josh_ohara | 15:dde4ce4bf7fe | 113 | bool rock_life = cover_vector_1[i].get_life(); |
josh_ohara | 15:dde4ce4bf7fe | 114 | for (int n = 0; n < bullet_vector.size(); n++){ |
josh_ohara | 15:dde4ce4bf7fe | 115 | Vector2D bullet_position = bullet_vector[n].get_position(); |
josh_ohara | 15:dde4ce4bf7fe | 116 | int bullet_x = bullet_position.x; |
josh_ohara | 15:dde4ce4bf7fe | 117 | int bullet_y = bullet_position.y; |
josh_ohara | 15:dde4ce4bf7fe | 118 | bool hit = bullet_vector[n].get_hit(); |
josh_ohara | 15:dde4ce4bf7fe | 119 | if((rock_x <= bullet_x) && |
josh_ohara | 15:dde4ce4bf7fe | 120 | (bullet_x <= rock_x + 2)&& |
josh_ohara | 15:dde4ce4bf7fe | 121 | (rock_y <= bullet_y) && |
josh_ohara | 15:dde4ce4bf7fe | 122 | (bullet_y <= rock_y + 2)&& |
josh_ohara | 15:dde4ce4bf7fe | 123 | (hit==false)&& |
josh_ohara | 15:dde4ce4bf7fe | 124 | (rock_life==true)){ |
josh_ohara | 15:dde4ce4bf7fe | 125 | bool T = true; |
josh_ohara | 15:dde4ce4bf7fe | 126 | bool F = false; |
josh_ohara | 15:dde4ce4bf7fe | 127 | C1.set_life(i,F); |
josh_ohara | 15:dde4ce4bf7fe | 128 | BS1.set_hit(n,T); |
josh_ohara | 15:dde4ce4bf7fe | 129 | } |
josh_ohara | 15:dde4ce4bf7fe | 130 | } |
josh_ohara | 15:dde4ce4bf7fe | 131 | } |
josh_ohara | 15:dde4ce4bf7fe | 132 | } |
josh_ohara | 15:dde4ce4bf7fe | 133 | |
josh_ohara | 15:dde4ce4bf7fe | 134 | void SpaceInvaderEngine::ship_bullet_cover2_collision(Gamepad &pad, N5110 &lcd){ |
josh_ohara | 15:dde4ce4bf7fe | 135 | vector<Rock> cover_vector_2 = C2.get_vector(); |
josh_ohara | 15:dde4ce4bf7fe | 136 | vector<Bullet> bullet_vector = BS1.get_vector(); |
josh_ohara | 15:dde4ce4bf7fe | 137 | for(int i = 0; i < cover_vector_2.size(); i++){ |
josh_ohara | 15:dde4ce4bf7fe | 138 | Vector2D rock_position = cover_vector_2[i].get_position(); |
josh_ohara | 15:dde4ce4bf7fe | 139 | int rock_x = rock_position.x; |
josh_ohara | 15:dde4ce4bf7fe | 140 | int rock_y = rock_position.y; |
josh_ohara | 15:dde4ce4bf7fe | 141 | bool rock_life = cover_vector_2[i].get_life(); |
josh_ohara | 15:dde4ce4bf7fe | 142 | for (int n = 0; n < bullet_vector.size(); n++){ |
josh_ohara | 15:dde4ce4bf7fe | 143 | Vector2D bullet_position = bullet_vector[n].get_position(); |
josh_ohara | 15:dde4ce4bf7fe | 144 | int bullet_x = bullet_position.x; |
josh_ohara | 15:dde4ce4bf7fe | 145 | int bullet_y = bullet_position.y; |
josh_ohara | 15:dde4ce4bf7fe | 146 | bool hit = bullet_vector[n].get_hit(); |
josh_ohara | 15:dde4ce4bf7fe | 147 | if((rock_x <= bullet_x) && |
josh_ohara | 15:dde4ce4bf7fe | 148 | (bullet_x <= rock_x + 2)&& |
josh_ohara | 15:dde4ce4bf7fe | 149 | (rock_y <= bullet_y) && |
josh_ohara | 15:dde4ce4bf7fe | 150 | (bullet_y <= rock_y + 2)&& |
josh_ohara | 15:dde4ce4bf7fe | 151 | (hit==false)&& |
josh_ohara | 15:dde4ce4bf7fe | 152 | (rock_life==true)){ |
josh_ohara | 15:dde4ce4bf7fe | 153 | bool T = true; |
josh_ohara | 15:dde4ce4bf7fe | 154 | bool F = false; |
josh_ohara | 15:dde4ce4bf7fe | 155 | C2.set_life(i,F); |
josh_ohara | 15:dde4ce4bf7fe | 156 | BS1.set_hit(n,T); |
josh_ohara | 15:dde4ce4bf7fe | 157 | } |
josh_ohara | 15:dde4ce4bf7fe | 158 | } |
josh_ohara | 15:dde4ce4bf7fe | 159 | } |
josh_ohara | 15:dde4ce4bf7fe | 160 | } |
josh_ohara | 15:dde4ce4bf7fe | 161 | |
josh_ohara | 15:dde4ce4bf7fe | 162 | void SpaceInvaderEngine::ship_bullet_cover3_collision(Gamepad &pad, N5110 &lcd){ |
josh_ohara | 15:dde4ce4bf7fe | 163 | vector<Rock> cover_vector_3 = C3.get_vector(); |
josh_ohara | 15:dde4ce4bf7fe | 164 | vector<Bullet> bullet_vector = BS1.get_vector(); |
josh_ohara | 15:dde4ce4bf7fe | 165 | for(int i = 0; i < cover_vector_3.size(); i++){ |
josh_ohara | 15:dde4ce4bf7fe | 166 | Vector2D rock_position = cover_vector_3[i].get_position(); |
josh_ohara | 15:dde4ce4bf7fe | 167 | int rock_x = rock_position.x; |
josh_ohara | 15:dde4ce4bf7fe | 168 | int rock_y = rock_position.y; |
josh_ohara | 15:dde4ce4bf7fe | 169 | bool rock_life = cover_vector_3[i].get_life(); |
josh_ohara | 15:dde4ce4bf7fe | 170 | for (int n = 0; n < bullet_vector.size(); n++){ |
josh_ohara | 15:dde4ce4bf7fe | 171 | Vector2D bullet_position = bullet_vector[n].get_position(); |
josh_ohara | 15:dde4ce4bf7fe | 172 | int bullet_x = bullet_position.x; |
josh_ohara | 15:dde4ce4bf7fe | 173 | int bullet_y = bullet_position.y; |
josh_ohara | 15:dde4ce4bf7fe | 174 | bool hit = bullet_vector[n].get_hit(); |
josh_ohara | 15:dde4ce4bf7fe | 175 | if((rock_x <= bullet_x) && |
josh_ohara | 15:dde4ce4bf7fe | 176 | (bullet_x <= rock_x + 2)&& |
josh_ohara | 15:dde4ce4bf7fe | 177 | (rock_y <= bullet_y) && |
josh_ohara | 15:dde4ce4bf7fe | 178 | (bullet_y <= rock_y + 2)&& |
josh_ohara | 15:dde4ce4bf7fe | 179 | (hit==false)&& |
josh_ohara | 15:dde4ce4bf7fe | 180 | (rock_life==true)){ |
josh_ohara | 15:dde4ce4bf7fe | 181 | bool T = true; |
josh_ohara | 15:dde4ce4bf7fe | 182 | bool F = false; |
josh_ohara | 15:dde4ce4bf7fe | 183 | C3.set_life(i,F); |
josh_ohara | 15:dde4ce4bf7fe | 184 | BS1.set_hit(n,T); |
josh_ohara | 15:dde4ce4bf7fe | 185 | } |
josh_ohara | 15:dde4ce4bf7fe | 186 | } |
josh_ohara | 15:dde4ce4bf7fe | 187 | } |
josh_ohara | 15:dde4ce4bf7fe | 188 | } |
josh_ohara | 15:dde4ce4bf7fe | 189 |