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 Gamepad N5110 mbed-rtos
Diff: GameEngine/GameEngine.cpp
- Revision:
- 6:1fcfd331c047
- Parent:
- 5:016a7315b75d
--- a/GameEngine/GameEngine.cpp Mon May 06 18:29:49 2019 +0000
+++ b/GameEngine/GameEngine.cpp Wed May 08 10:03:24 2019 +0000
@@ -163,6 +163,9 @@
_e21.init(_e2a,_e21b,_speed);
_e22.init(_e2a,_e22b,_speed);
_e3.init(_e3a,_e3b,_e3c,_e3d,_e3c,_e3f,_speed);
+ _e3.motion();
+
+ //_beam.init(_beam_size,p1_pos.x,p1_pos.y);
// Initialising the stage of the game
_stage = 0;
@@ -290,6 +293,7 @@
_p1.update(_d,_mag); // Player spacecraft movement update
_beam.update(); // Player spacecraft beam update
_enemybeam3.update(); // Third enemy's spacecraft's beam update
+ //_e3.motion();
_e3.update(); // Third enemy spacecraft movement update
check_enemy3_collisions(pad); // Check if the player spacecraft beam collides with the third enemy spacecraft
check_spacecraft3_collisions(pad); // Check if the third enemy spacecraft beam collides with the player spacecraft
@@ -315,6 +319,7 @@
// add first enemy health
_e1.add_health();
spacebeam = true;
+ _e1.location();
// audio feedback
pad.tone(1000.0,0.1);
}
@@ -429,8 +434,8 @@
Vector2D beam_pos = _beam.get_pos(); // Get player's spacecraft beam position
if((_R == true)|| (_L == true)){
- _beam.init(_beam_size,p1_pos.x,p1_pos.y); //Initialise the beam
if ((beam_pos.x + _beam_size > WIDTH) || (spacebeam == true)){ // if hits the enemy or the wall
+ _beam.init(_beam_size,p1_pos.x,p1_pos.y);
_R = false; // Then set the boolean _R and _L to false
_L = false;
}
@@ -439,10 +444,11 @@
void GameEngine::check_enemybeam_collisions(Gamepad &pad) // check if enemy beam has collided with the edges of screen or spacecraft in stage one
{
+ Vector2D enemy_pos = _e1.get_enemy_pos(); // Position of the first enemy
Vector2D enemybeam_pos = _enemybeam.get_pos(); // Get first enemy's beam position
if ((_ebeam == true)||(enemybeam_pos.x - _beam_size < 0)) { // if hits the speacecraft or the wall
- _enemybeam.init(_beam_size,_e1a,_e1b); // Initialise the enemy beam in stage one
+ _enemybeam.init(_beam_size,enemy_pos.x,enemy_pos.y); // Initialise the enemy beam in stage one
_ebeam = false; // Then set the boolean _ebeam to false
}
}
@@ -493,7 +499,7 @@
) {
_p1.add_health(); // adds health
_ebeam = true; // sets boolean to true to initialise beam
- pad.tone(1000.0,0.1); // outputs sound if it hits the player's spacecraft
+ pad.tone(87.3,0.1); // outputs sound if it hits the player's spacecraft
}
// write new attributes
_enemybeam.set_pos(enemybeam_pos);
@@ -515,7 +521,7 @@
) {
_p1.add_health(); // adds health
_ebeam21 = true; // sets boolean to true to initialise beam
- pad.tone(1000.0,0.1); // outputs sound if it hits the player's spacecraft
+ pad.tone(87.3,0.1); // outputs sound if it hits the player's spacecraft
}
else if (
(enemybeam22_pos.y >= p1_pos.y) && //top
@@ -525,7 +531,7 @@
) {
_p1.add_health(); // adds health
_ebeam22 = true; // sets boolean to true to initialise beam
- pad.tone(1000.0,0.1); // outputs sound if it hits the player's spacecraft
+ pad.tone(87.3,0.1); // outputs sound if it hits the player's spacecraft
}
// write new attributes
_enemybeam2.set_pos_21(enemybeam21_pos);
@@ -550,7 +556,7 @@
) {
_p1.add_health(); // adds health
_ebeam3boss = true; // sets boolean to true to initialise beam
- pad.tone(1000.0,0.1);
+ pad.tone(87.3,0.1);
}
else if (
(enemy1beam_pos.y >= p1_pos.y) && //top
@@ -560,7 +566,7 @@
) {
_p1.add_health(); // adds health
_ebeam31 = true; // sets boolean to true to initialise beam
- pad.tone(1000.0,0.1);
+ pad.tone(87.3,0.1);
}
else if (
(enemy2beam_pos.y >= p1_pos.y) && //top
@@ -570,7 +576,7 @@
) {
_p1.add_health(); // adds health
_ebeam32 = true; // sets boolean to true to initialise beam
- pad.tone(1000.0,0.1); // outputs sound if it hits the player's spacecraft
+ pad.tone(87.3,0.1); // outputs sound if it hits the player's spacecraft
}
// write new attributes
_enemybeam3.set_pos_boss(enemybossbeam_pos);
@@ -578,7 +584,29 @@
_enemybeam3.set_pos_enemy2(enemy2beam_pos);
}
+void GameEngine::check_wall_collision(Gamepad &pad) // check wall collision for enemy in stage one
+{
+ // read current enemy attributes for boss in stage three
+ Vector2D enemy_pos = _e1.get_enemy_pos();
+ // check if hit top wall
+ if (enemy_pos.y <= 1) { // 1 due to 1 pixel boundary
+ enemy_pos.y = 1; // bounce off ceiling without going off screen
+ }
+ // check if hit bottom wall
+ else if (enemy_pos.y + 12 >= (HEIGHT-1) ) { // bottom pixel is 47
+ enemy_pos.y = (HEIGHT-1) - 12; // stops ball going off screen
+ // check if hit right wall
+ }else if (enemy_pos.x + 12 >= (WIDTH-1) ) { // right pixel is 83
+ enemy_pos.x = (WIDTH-1) - 12; // stops ball going off screen
+ // check if left right wall
+ }else if (enemy_pos.x <= WIDTH/2) { // half the width (42)
+ enemy_pos.x = WIDTH/2; // bounce off ceiling without going halfway
+ }
+ _e1.set_enemy_pos(enemy_pos);
+
+}
+
void GameEngine::check_wall_collisions(Gamepad &pad) // check wall collision for enemies in stage two
{
// read current attributes of the enemies
@@ -626,20 +654,20 @@
// check if hit top wall
if (enemyboss_pos.y <= 1) { // 1 due to 1 pixel boundary
enemyboss_pos.y = 1; // bounce off ceiling without going off screen
- enemyboss_movement.y = -enemyboss_movement.y;
+ enemyboss_movement = _e3.motion();
}
// check if hit bottom wall
else if (enemyboss_pos.y + 12 >= (HEIGHT-1) ) { // bottom pixel is 47
enemyboss_pos.y = (HEIGHT-1) - 12; // stops ball going off screen
- enemyboss_movement.y = -enemyboss_movement.y;
+ enemyboss_movement = _e3.motion();
// check if hit right wall
}else if (enemyboss_pos.x + 12 >= (WIDTH-1) ) { // right pixel is 83
enemyboss_pos.x = (WIDTH-1) - 12; // stops ball going off screen
- enemyboss_movement.x = -enemyboss_movement.x;
+ enemyboss_movement = _e3.motion();
// check if left right wall
}else if (enemyboss_pos.x <= WIDTH/2) { // half the width (42)
enemyboss_pos.x = WIDTH/2; // bounce off ceiling without going halfway
- enemyboss_movement.x = -enemyboss_movement.x;
+ enemyboss_movement = _e3.motion();
}
// update boss enemy parameters
_e3.set_movement(enemyboss_movement);
@@ -842,12 +870,12 @@
void GameEngine::draw_enemyboss_health(N5110 &lcd)
{
- // get health of the enemy
+ // get health of the enemy in stage three
int e3boss_health = _e3.get_health_boss();
int e3enemy1_health = _e3.get_health_enemy1();
int e3enemy2_health = _e3.get_health_enemy2();
- if (e3boss_health <= 10){
+ if (e3boss_health <= 10){
lcd.drawRect(62,42,20,4,FILL_TRANSPARENT);
lcd.drawRect(62,42,20-2*e3boss_health,4,FILL_BLACK);
}
@@ -922,7 +950,7 @@
}
}
-
+
int GameEngine::get_game_stage() {
int stage = _stage;
return stage; // Gets the value of the stage