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:
- 13:5c3dc6e827c2
- Parent:
- 12:bfe3a3deaac3
- Child:
- 14:e8de27c4d0d4
--- a/game/game.cpp Sun Mar 17 09:44:59 2019 +0000
+++ b/game/game.cpp Sun Mar 17 22:56:24 2019 +0000
@@ -7,9 +7,6 @@
#include "models.h"
#include "main.h"
#include "game.h"
-#include "stars.h"
-//#include "planets.h"
-
int x_ship_pos = 0;
int y_ship_pos = 24;
@@ -37,10 +34,10 @@
const float blast_collision_offset_y = 0;
#define MAX_SMALL_STARS (5)
-Stars small_stars[MAX_SMALL_STARS];
+GameObject small_stars[MAX_SMALL_STARS];
#define MAX_MEDIUM_STARS (10)
-Stars medium_stars[MAX_MEDIUM_STARS];
+GameObject medium_stars[MAX_MEDIUM_STARS];
#define MAX_ENEMIES (1)
Enemy enemies[MAX_ENEMIES];
@@ -64,7 +61,7 @@
enemies[found].dead = false;
enemies[found].active = true;
enemies[found].x = screen_width;
- enemies[found].y = rand() % (screen_height - enemy_height);
+ enemies[found].y = rand() % (screen_height - enemy2_height);
}
}
@@ -149,6 +146,95 @@
}
}
+void newSmallStarFlies() {
+ // Search the array of stars if inactive we can use it. - the same as with blasts
+ int found = -1;
+ for (int i = 0; i < MAX_SMALL_STARS; ++i) {
+ if (!small_stars[i].active) {
+ found = i;
+ break;
+ }
+ }
+
+ if (found != -1) {
+ small_stars[found].active = true;
+ small_stars[found].x = screen_width;
+ small_stars[found].y = rand() % screen_height;
+ }
+}
+
+void updateAndDrawSmallStars(){
+ const int small_star_speed = 5;
+ for (int i = 0; i < MAX_SMALL_STARS; ++i) {
+ if (small_stars[i].active) {
+ small_stars[i].x -= small_star_speed;
+ if (small_stars[i].x <= 0){
+ small_stars[i].active = false;
+ }
+ lcd.drawSprite(small_stars[i].x, small_stars[i].y, 3, 3, (int*)starSmall);
+ }
+ }
+}
+
+void newMediumStarFlies() {
+ // Search the array of stars if inactive we can use it. - the same as with blasts
+ int found = -1;
+ for (int i = 0; i < MAX_MEDIUM_STARS; ++i) {
+ if (!medium_stars[i].active) {
+ found = i;
+ break;
+ }
+ }
+
+ if (found != -1) {
+ medium_stars[found].active = true;
+ medium_stars[found].x = screen_width;
+ medium_stars[found].y =rand() % screen_height;
+ }
+}
+
+void updateAndDrawMediumStars(){
+ const int medium_star_speed = 5;
+ for (int i = 0; i < MAX_MEDIUM_STARS; ++i) {
+ if (medium_stars[i].active) {
+ medium_stars[i].x -= medium_star_speed;
+ if (medium_stars[i].x <= -2){
+ medium_stars[i].active = false;
+ }
+ lcd.drawSprite(medium_stars[i].x, medium_stars[i].y, 5, 5, (int*)starMedium);
+ }
+ }
+}
+
+void shipMovment(){ // The position of the ship
+
+ if(x_ship_pos <= 48 && x_ship_pos >= 0){
+ if(x_dir.read() > 0.6f){
+ x_ship_pos -= ship_speed;
+ }
+ else if(x_dir.read() < 0.4f){
+ x_ship_pos += ship_speed;
+ }
+ }
+
+ else if (x_ship_pos <= 48){ x_ship_pos = 0;} //Limits for x direction border IMPROVE IF POSSIBLE.
+ else { x_ship_pos = 48;}
+
+
+ if (y_ship_pos <= (47 - spaceship1_height) && y_ship_pos >= 0){
+ if(y_dir.read() > 0.6f){
+ y_ship_pos -= ship_speed;
+ }
+ else if(y_dir.read() < 0.4f){
+ y_ship_pos += ship_speed;
+ }
+ }
+ else if (y_ship_pos >= (47 - spaceship1_height)){ y_ship_pos = 47 - spaceship1_height;} //Limits for y direction border IMPROVE IF POSSIBLE.
+ else if (y_ship_pos < 0){ y_ship_pos = 0;}
+
+}
+
+
bool Game::updateAndDraw() {
shipMovment();
@@ -185,80 +271,4 @@
want_to_pause = true;
}
return want_to_pause;
-}
-
-
-void Game::shipMovment(){ // The position of the ship
-
- if(x_ship_pos <= 48 && x_ship_pos >= 0){
- if(x_dir.read() > 0.6f){
- x_ship_pos -= ship_speed;
- }
- else if(x_dir.read() < 0.4f){
- x_ship_pos += ship_speed;
- }
- }
-
- else if (x_ship_pos <= 48){ x_ship_pos = 0;} //Limits for x direction border IMPROVE IF POSSIBLE.
- else { x_ship_pos = 48;}
-
-
- if (y_ship_pos <= (47 - spaceship1_height) && y_ship_pos >= 0){
- if(y_dir.read() > 0.6f){
- y_ship_pos -= ship_speed;
- }
- else if(y_dir.read() < 0.4f){
- y_ship_pos += ship_speed;
- }
- }
- else if (y_ship_pos >= (47 - spaceship1_height)){ y_ship_pos = 47 - spaceship1_height;} //Limits for y direction border IMPROVE IF POSSIBLE.
- else if (y_ship_pos < 0){ y_ship_pos = 0;}
-
-}
-
-void Game::newSmallStarFlies() {
- // Search the array of stars if inactive we can use it. - the same as with blasts
- int found_inactive_small_star = -1;
- for (int i = 0; i < MAX_SMALL_STARS; ++i) {
- if (!small_stars[i].isSmallStarActive()) {
- found_inactive_small_star = i;
- break;
- }
- }
-
- if (found_inactive_small_star != -1) {
- small_stars[found_inactive_small_star].smallStarActivate(screen_width, rand() % screen_height);
- }
-}
-
-void Game::updateAndDrawSmallStars(){
- for (int i = 0; i < MAX_SMALL_STARS; ++i) {
- if (small_stars[i].isSmallStarActive()) {
- small_stars[i].updateAndDrawSmallStar();
- }
- }
-}
-
-
-void Game::newMediumStarFlies() {
- // Search the array of stars if inactive we can use it. - the same as with blasts
- int found_inactive_medium_star = -1;
- for (int i = 0; i < MAX_MEDIUM_STARS; ++i) {
- if (!medium_stars[i].isMediumStarActive()) {
- found_inactive_medium_star = i;
- break;
- }
- }
-
- if (found_inactive_medium_star != -1) {
- medium_stars[found_inactive_medium_star].mediumStarActivate(screen_width, rand() % screen_height); /////////////////////////////////////////// CHANGE
- }
-}
-
-void Game::updateAndDrawMediumStars(){
- for (int i = 0; i < MAX_MEDIUM_STARS; ++i) {
- if (medium_stars[i].isMediumStarActive()) {
- medium_stars[i].updateAndDrawMediumStar();
- }
- }
-}
+}
\ No newline at end of file
