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
Fork of el17dg by
Diff: game/game.cpp
- Revision:
- 16:b7d0cac561cc
- Parent:
- 15:0145c5f0bea1
- Child:
- 17:69a85c909566
--- a/game/game.cpp Mon Mar 18 15:22:20 2019 +0000
+++ b/game/game.cpp Mon Mar 18 21:26:12 2019 +0000
@@ -11,10 +11,15 @@
int x_ship_pos = 0;
int y_ship_pos = 24;
int small_star_delay = 0;
+int enemy_blast_delay = 0;
+int game_score = 0;
const int ship_speed = 2;
const int small_star_delay_max = 3;
+const int enemy_blast_delay_max = 5;
+int player_lifes = 3;
+bool game_over = false;
struct GameObject {
int x;
@@ -29,6 +34,7 @@
#define MAX_BLASTS (5)
GameObject blasts[MAX_BLASTS];
+GameObject enemy_blasts[MAX_BLASTS];
const float blast_collision_radius = 1;
const float blast_collision_offset_x = 1;
const float blast_collision_offset_y = 0;
@@ -45,8 +51,6 @@
const float enemy_collision_offset_x = 5;
const float enemy_collision_offset_y = 3;
-int game_score = 0;
-
GameObject player;
@@ -113,6 +117,38 @@
}
}
+void fireNewEnemyBlast() {
+ // Search the array of blasts if inactive we can use it.
+ int found = -1;
+ for (int i = 0; i < MAX_BLASTS; ++i) {
+ if (!enemy_blasts[i].active) {
+ found = i;
+ break;
+ }
+ }
+
+ if (found != -1) { ///////////////////////////////////////////////////
+ enemy_blasts[found].active = true;
+ enemy_blasts[found].x = enemies[found].x;
+ enemy_blasts[found].y = enemies[found].y + (enemy2_height/2);
+ }
+}
+
+void updateAndDrawEnemyBlasts() {
+ const int blast_speed = 5;
+ for (int i = 0; i < MAX_BLASTS; ++i) {
+ if (enemy_blasts[i].active) {
+ enemy_blasts[i].x -= blast_speed;
+ if (enemy_blasts[i].x <= -1){
+ enemy_blasts[i].active = false;
+ }
+ lcd.setPixel(enemy_blasts[i].x, enemy_blasts[i].y, 1);
+ lcd.setPixel(enemy_blasts[i].x-1, enemy_blasts[i].y, 1);
+ lcd.setPixel(enemy_blasts[i].x-2, enemy_blasts[i].y, 1);
+ }
+ }
+}
+
void fireNewBlast() {
// Search the array of blasts if inactive we can use it.
int found = -1;
@@ -132,10 +168,10 @@
inline bool circleCollideTwoObjects(
int x1, int y1, float r1, float offset_x1, float offset_y1,
- int x2, int y2, float r2, float offset_x2, float offset_y2
-) {
- int distance = pow((x1 + offset_x1) - (x2 + offset_x2), 2) + pow((y1 + offset_y1) - (y2 + offset_y2), 2);
- return distance <= pow(r1 + r2, 2);
+ int x2, int y2, float r2, float offset_x2, float offset_y2) {
+
+ int distance = pow((x1 + offset_x1) - (x2 + offset_x2), 2) + pow((y1 + offset_y1) - (y2 + offset_y2), 2);
+ return distance <= pow(r1 + r2, 2);
}
void collideEnemiesAndBlasts() {
@@ -248,12 +284,29 @@
}
void highScore(){
+
char buffer[4];
sprintf(buffer," Score: %i",game_score);
- //printf(buffer);
lcd.printString(buffer,0,0);
}
+void playerLivesRemain(){////////////////////////////////////////////////////////////////////////////////
+ if (player_lifes == 3){
+ // all 3 LEDs lit (six total)
+ }
+ if (player_lifes == 2){
+ // only yelow and red are lit (to tal 4)
+ }
+ if (player_lifes == 1){
+ // red LED is lit and flashes.
+ }
+ else {
+ // all LEDs are flashing
+ game_over = true;
+ }
+
+}
+
bool Game::updateAndDraw() {
shipMovment();
@@ -273,11 +326,21 @@
small_star_delay += 1;
}
+ if (enemy_blast_delay == enemy_blast_delay_max){
+
+ fireNewEnemyBlast();
+ enemy_blast_delay = 0;
+ }
+ else {
+ enemy_blast_delay += 1;
+ }
+
updateAndDrawBlasts();
updateAndDrawSmallStars();
updateAndDrawMediumStars();
updateAndDrawEnemies();
collideEnemiesAndBlasts();
+ updateAndDrawEnemyBlasts();
highScore();
lcd.drawSpriteOnTop(x_ship_pos, y_ship_pos, spaceship1_width, spaceship1_height, (int *)spaceShip1);
