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:
- 15:dde4ce4bf7fe
- Parent:
- 14:e88bcf5c0887
- Child:
- 16:987f72d9bb8f
--- a/SpaceInvaderEngine/SpaceInvaderEngine.cpp Fri Apr 03 11:19:53 2020 +0000
+++ b/SpaceInvaderEngine/SpaceInvaderEngine.cpp Mon Apr 06 15:37:02 2020 +0000
@@ -1,8 +1,6 @@
#include "SpaceInvaderEngine.h"
-// N5110 lcd1;
-
SpaceInvaderEngine::SpaceInvaderEngine()
{
@@ -13,7 +11,7 @@
}
-void SpaceInvaderEngine::init(int ship_height, int ship_width, int alien_size, int no_aliens, int column_size, int row_size) {
+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) {
S1_height = ship_height;
S1_width = ship_width;
@@ -24,12 +22,21 @@
S1y = ship_pos.y;
N = no_aliens;
- CS = column_size;
- RS = row_size;
+ CS = armada_column_size;
+ RS = armada_row_size;
+
+ Cy = cover_y;
+ C1x = cover1_x;
+ C2x = cover2_x;
+ C3x = cover3_x;
+ NR = no_rocks;
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);
}
@@ -44,6 +51,9 @@
S1.render(lcd);
A1.render(lcd);
BS1.render(lcd);
+ C1.render(lcd);
+ C2.render(lcd);
+ C3.render(lcd);
}
void SpaceInvaderEngine::update(Gamepad &pad, N5110 &lcd)
@@ -53,6 +63,9 @@
BS1.update(pad, lcd, S1x+4, S1y);
A1.update(pad);
ship_bullet_alien_collision(pad, lcd);
+ ship_bullet_cover1_collision(pad, lcd);
+ ship_bullet_cover2_collision(pad, lcd);
+ ship_bullet_cover3_collision(pad, lcd);
}
void SpaceInvaderEngine::get_ship_pos()
@@ -69,14 +82,18 @@
Vector2D alien_position = alien_vector[i].get_position();
int alien_x = alien_position.x;
int alien_y = alien_position.y;
+ bool alien_life = alien_vector[i].get_life();
for (int n = 0; n < bullet_vector.size(); n++){
Vector2D bullet_position = bullet_vector[n].get_position();
int bullet_x = bullet_position.x;
int bullet_y = bullet_position.y;
- if((alien_x < bullet_x) &&
- (bullet_x < alien_x + 4)&&
- (alien_y < bullet_y) &&
- (bullet_y < alien_y + 4)) {
+ bool hit = bullet_vector[n].get_hit();
+ if((alien_x <= bullet_x) &&
+ (bullet_x <= alien_x + A1_size - 1)&&
+ (alien_y <= bullet_y) &&
+ (bullet_y <= alien_y + A1_size - 1)&&
+ (hit==false)&&
+ (alien_life == true)) {
bool T = true;
bool F = false;
A1.set_life(i,F);
@@ -86,3 +103,87 @@
}
}
+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();
+ 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;
+ int rock_y = rock_position.y;
+ bool rock_life = cover_vector_1[i].get_life();
+ for (int n = 0; n < bullet_vector.size(); n++){
+ Vector2D bullet_position = bullet_vector[n].get_position();
+ int bullet_x = bullet_position.x;
+ int bullet_y = bullet_position.y;
+ bool hit = bullet_vector[n].get_hit();
+ if((rock_x <= bullet_x) &&
+ (bullet_x <= rock_x + 2)&&
+ (rock_y <= bullet_y) &&
+ (bullet_y <= rock_y + 2)&&
+ (hit==false)&&
+ (rock_life==true)){
+ bool T = true;
+ bool F = false;
+ C1.set_life(i,F);
+ BS1.set_hit(n,T);
+ }
+ }
+ }
+}
+
+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();
+ 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;
+ int rock_y = rock_position.y;
+ bool rock_life = cover_vector_2[i].get_life();
+ for (int n = 0; n < bullet_vector.size(); n++){
+ Vector2D bullet_position = bullet_vector[n].get_position();
+ int bullet_x = bullet_position.x;
+ int bullet_y = bullet_position.y;
+ bool hit = bullet_vector[n].get_hit();
+ if((rock_x <= bullet_x) &&
+ (bullet_x <= rock_x + 2)&&
+ (rock_y <= bullet_y) &&
+ (bullet_y <= rock_y + 2)&&
+ (hit==false)&&
+ (rock_life==true)){
+ bool T = true;
+ bool F = false;
+ C2.set_life(i,F);
+ BS1.set_hit(n,T);
+ }
+ }
+ }
+}
+
+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();
+ 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;
+ int rock_y = rock_position.y;
+ bool rock_life = cover_vector_3[i].get_life();
+ for (int n = 0; n < bullet_vector.size(); n++){
+ Vector2D bullet_position = bullet_vector[n].get_position();
+ int bullet_x = bullet_position.x;
+ int bullet_y = bullet_position.y;
+ bool hit = bullet_vector[n].get_hit();
+ if((rock_x <= bullet_x) &&
+ (bullet_x <= rock_x + 2)&&
+ (rock_y <= bullet_y) &&
+ (bullet_y <= rock_y + 2)&&
+ (hit==false)&&
+ (rock_life==true)){
+ bool T = true;
+ bool F = false;
+ C3.set_life(i,F);
+ BS1.set_hit(n,T);
+ }
+ }
+ }
+}
+