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
Diff: SpaceInvaderEngine/SpaceInvaderEngine.cpp
- Revision:
- 21:970807533b10
- Parent:
- 20:0b6f1cfc5be6
- Child:
- 22:3e978b1d7958
diff -r 0b6f1cfc5be6 -r 970807533b10 SpaceInvaderEngine/SpaceInvaderEngine.cpp --- a/SpaceInvaderEngine/SpaceInvaderEngine.cpp Fri May 01 19:41:24 2020 +0000 +++ b/SpaceInvaderEngine/SpaceInvaderEngine.cpp Tue May 12 15:33:50 2020 +0000 @@ -33,7 +33,6 @@ A1.init(N,A1_size,CS,RS); S1.init(S1_height,S1_width); - BS1.init(); C1.init(C1x,Cy,NR); C2.init(C2x,Cy,NR); C3.init(C3x,Cy,NR); @@ -50,7 +49,6 @@ { S1.render(lcd); A1.render(lcd); - BS1.render(lcd); C1.render(lcd); C2.render(lcd); C3.render(lcd); @@ -59,8 +57,7 @@ void SpaceInvaderEngine::update(Gamepad &pad, N5110 &lcd, int counter) { get_ship_pos(); - S1.update(D,Mag); - BS1.update(pad, lcd, S1x+4, S1y, counter); + S1.update(D,Mag,pad,lcd,counter); A1.update(pad, counter); ship_bullet_alien_collision(pad, lcd); ship_bullet_cover1_collision(pad, lcd); @@ -70,6 +67,10 @@ alien_bullet_cover1_collision(pad, lcd); alien_bullet_cover2_collision(pad, lcd); alien_bullet_cover3_collision(pad, lcd); + alien_ship_collision(pad, lcd); + alien_cover1_collision(pad, lcd); + alien_cover2_collision(pad, lcd); + alien_cover3_collision(pad, lcd); } void SpaceInvaderEngine::get_ship_pos() @@ -81,7 +82,7 @@ void SpaceInvaderEngine::ship_bullet_alien_collision(Gamepad &pad, N5110 &lcd){ vector<Alien> alien_vector = A1.get_vector(); - vector<Bullet> bullet_vector = BS1.get_vector(); + vector<Bullet> bullet_vector = S1.get_bullet_vector(); for(int i = 0; i < alien_vector.size(); i++){ Vector2D alien_position = alien_vector[i].get_position(); int alien_x = alien_position.x; @@ -101,7 +102,7 @@ bool T = true; bool F = false; A1.set_life(i,F); - BS1.set_hit(n,T); + S1.set_bullet_hit(n,T); } } } @@ -109,7 +110,7 @@ void SpaceInvaderEngine::ship_bullet_cover1_collision(Gamepad &pad, N5110 &lcd){ vector<Rock> cover_vector_1 = C1.get_vector(); - vector<Bullet> bullet_vector = BS1.get_vector(); + vector<Bullet> bullet_vector = S1.get_bullet_vector(); for(int i = 0; i < cover_vector_1.size(); i++){ Vector2D rock_position = cover_vector_1[i].get_position(); int rock_x = rock_position.x; @@ -129,7 +130,7 @@ bool T = true; bool F = false; C1.set_life(i,F); - BS1.set_hit(n,T); + S1.set_bullet_hit(n,T); } } } @@ -137,7 +138,7 @@ void SpaceInvaderEngine::ship_bullet_cover2_collision(Gamepad &pad, N5110 &lcd){ vector<Rock> cover_vector_2 = C2.get_vector(); - vector<Bullet> bullet_vector = BS1.get_vector(); + vector<Bullet> bullet_vector = S1.get_bullet_vector(); for(int i = 0; i < cover_vector_2.size(); i++){ Vector2D rock_position = cover_vector_2[i].get_position(); int rock_x = rock_position.x; @@ -157,7 +158,7 @@ bool T = true; bool F = false; C2.set_life(i,F); - BS1.set_hit(n,T); + S1.set_bullet_hit(n,T); } } } @@ -165,7 +166,7 @@ void SpaceInvaderEngine::ship_bullet_cover3_collision(Gamepad &pad, N5110 &lcd){ vector<Rock> cover_vector_3 = C3.get_vector(); - vector<Bullet> bullet_vector = BS1.get_vector(); + vector<Bullet> bullet_vector = S1.get_bullet_vector(); for(int i = 0; i < cover_vector_3.size(); i++){ Vector2D rock_position = cover_vector_3[i].get_position(); int rock_x = rock_position.x; @@ -185,7 +186,7 @@ bool T = true; bool F = false; C3.set_life(i,F); - BS1.set_hit(n,T); + S1.set_bullet_hit(n,T); } } } @@ -276,26 +277,141 @@ void SpaceInvaderEngine::alien_bullet_cover2_collision(Gamepad &pad, N5110 &lcd){ vector<Rock> cover_vector_2 = C2.get_vector(); vector<Alien> alien_vector = A1.get_vector(); - for(int q = 0; q < cover_vector_2.size(); q++){ + for(int q = 0; q < cover_vector_2.size(); q++){ Vector2D rock_position = cover_vector_2[q].get_position(); int rock_x = rock_position.x; int rock_y = rock_position.y; bool rock_life = cover_vector_2[q].get_life(); - for(int i = 0; i < alien_vector.size(); i++){ - vector<AlienBullet> alien_bullets = A1.get_alien_bullet_vector(i); - for(int n = 0; n < alien_bullets.size(); n++){ - Vector2D bullet_position = alien_bullets[n].get_position(); - bool hit = alien_bullets[n].get_hit(); - int bullet_x = bullet_position.x; - int bullet_y = bullet_position.y; - if((rock_x <= bullet_x) && - (bullet_x <= rock_x + 2)&& - (rock_y <= bullet_y) && - (bullet_y <= rock_y + 2)&& - (hit==false)&& - (rock_life==true)){ - C2.set_life(q,false); - A1.set_bullet_hit(i,n,true); + for(int i = 0; i < alien_vector.size(); i++){ + vector<AlienBullet> alien_bullets = A1.get_alien_bullet_vector(i); + for(int n = 0; n < alien_bullets.size(); n++){ + Vector2D bullet_position = alien_bullets[n].get_position(); + bool hit = alien_bullets[n].get_hit(); + int bullet_x = bullet_position.x; + int bullet_y = bullet_position.y; + if((rock_x <= bullet_x) && + (bullet_x <= rock_x + 2)&& + (rock_y <= bullet_y) && + (bullet_y <= rock_y + 2)&& + (hit==false)&& + (rock_life==true)){ + C2.set_life(q,false); + A1.set_bullet_hit(i,n,true); + } + } + } + } +} + +void SpaceInvaderEngine::alien_ship_collision(Gamepad &pad, N5110 &lcd){ + Vector2D ship_position = S1.get_position(); + int ship_x = ship_position.x; + int ship_y = ship_position.y; + vector<Alien> alien_vector = A1.get_vector(); + for(int i = 0; i < alien_vector.size(); i++){ + Vector2D alien_position = alien_vector[i].get_position(); + int alien_x = alien_position.x; + int alien_y = alien_position.y; + for(int q = 0; q < 4; q++){ + for(int n = 0; n < 4; n++){ + if((ship_x - 1 < alien_x + q) && + (ship_x + 9 > alien_x + q)&& + (ship_y - 1 < alien_y + n)&& + (S1.get_life()==true)&& + (A1.get_life(i)==true)){ + S1.set_life(false); + A1.set_life(i,false); + pad.leds_on(); + } + } + } + } +} + +bool SpaceInvaderEngine::check_armada_alive(){ + for(int i; i < A1.size(); i++) + +void SpaceInvaderEngine::alien_cover1_collision(Gamepad &pad, N5110 &lcd){ + vector<Alien> alien_vector = A1.get_vector(); + vector<Rock> cover1 = C1.get_vector(); + for(int i = 0; i < alien_vector.size(); i++){ + Vector2D alien_position = alien_vector[i].get_position(); + int alien_x = alien_position.x; + int alien_y = alien_position.y; + for(int p = 0; p < cover1.size(); p++){ + Vector2D rock_position = cover1[p].get_position(); + int rock_x = rock_position.x; + int rock_y = rock_position.y; + for(int q = 0; q < 4; q++){ + for(int s = 0; s < 4; s++){ + if((rock_x < alien_x + q) && + (rock_x + 2 > alien_x + q)&& + (rock_y < alien_y + s)&& + (rock_y + 2 > alien_y + s)&& + (C1.get_life(p)==true)&& + (A1.get_life(i)==true)){ + C1.set_life(p,false); + A1.set_life(i,false); + pad.leds_on(); + } + } + } + } + } +} + +void SpaceInvaderEngine::alien_cover2_collision(Gamepad &pad, N5110 &lcd){ + vector<Alien> alien_vector = A1.get_vector(); + vector<Rock> cover2 = C2.get_vector(); + for(int i = 0; i < alien_vector.size(); i++){ + Vector2D alien_position = alien_vector[i].get_position(); + int alien_x = alien_position.x; + int alien_y = alien_position.y; + for(int p = 0; p < cover2.size(); p++){ + Vector2D rock_position = cover2[p].get_position(); + int rock_x = rock_position.x; + int rock_y = rock_position.y; + for(int q = 0; q < 4; q++){ + for(int s = 0; s < 4; s++){ + if((rock_x < alien_x + q) && + (rock_x + 2 > alien_x + q)&& + (rock_y < alien_y + s)&& + (rock_y + 2 > alien_y + s)&& + (C2.get_life(p)==true)&& + (A1.get_life(i)==true)){ + C2.set_life(p,false); + A1.set_life(i,false); + pad.leds_on(); + } + } + } + } + } +} + +void SpaceInvaderEngine::alien_cover3_collision(Gamepad &pad, N5110 &lcd){ + vector<Alien> alien_vector = A1.get_vector(); + vector<Rock> cover3 = C3.get_vector(); + for(int i = 0; i < alien_vector.size(); i++){ + Vector2D alien_position = alien_vector[i].get_position(); + int alien_x = alien_position.x; + int alien_y = alien_position.y; + for(int p = 0; p < cover3.size(); p++){ + Vector2D rock_position = cover3[p].get_position(); + int rock_x = rock_position.x; + int rock_y = rock_position.y; + for(int q = 0; q < 4; q++){ + for(int s = 0; s < 4; s++){ + if((rock_x < alien_x + q) && + (rock_x + 2 > alien_x + q)&& + (rock_y < alien_y + s)&& + (rock_y + 2 > alien_y + s)&& + (C3.get_life(p)==true)&& + (A1.get_life(i)==true)){ + C3.set_life(p,false); + A1.set_life(i,false); + pad.leds_on(); + } } } }