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
Revision 31:becb8f6bf7b7, committed 2019-04-23
- Comitter:
- Noximilien
- Date:
- Tue Apr 23 18:18:57 2019 +0000
- Parent:
- 30:d454d0cb72bc
- Child:
- 32:5403bb974294
- Commit message:
- Have changed comments structure as well as the content. Finished intro music. Have cleaned a code in some places to make it readable.
Changed in this revision
--- a/Models/models.cpp Tue Apr 16 21:16:33 2019 +0000
+++ b/Models/models.cpp Tue Apr 23 18:18:57 2019 +0000
@@ -1,13 +1,13 @@
#include "models.h"
-const int spaceship1_width = 15;
-const int spaceship1_height = 10;
+const int spaceship1_width = 12;
+const int spaceship1_height = 14;
const int enemy2_height = 7;
const int enemy2_width = 11;
-const int enemy1_height = 17;
-const int enemy1_width = 11;
+//const int enemy1_height = 17;
+//const int enemy1_width = 11;
const int cursor[84] = { //12 by 7
0,0,0,0,1,0,0,0,0,0,1,1,
--- a/game/enemies.h Tue Apr 16 21:16:33 2019 +0000
+++ b/game/enemies.h Tue Apr 23 18:18:57 2019 +0000
@@ -16,17 +16,19 @@
/** Maximum enemies' blasts allowed on the screen*/
const int max_enemy_blasts = max_enemies*2;
-/** Enemy Class
- * @brief A class to describe the states of one enemy ship.
+/**
+ * Enemy Class
+ * @brief A class to describe the states of an enemy ship.
* @author Dmitrijs Griskovs
* @date 15/04/2019
*/
class Enemy : public GameObject {
public:
-/** @brief Enemy spawn function.
- * @details This function spawns an enemy on the right side of the screen at the
- * x-direction LCD limit(84) and at random position in the y-direction.
- */
+ /**
+ * @brief Enemy spawn function.
+ * @details This function spawns an enemy on the right side of the screen
+ * and at random position in the y-direction.
+ */
void spawn() {
// giving the enemy the spawning positions
Point spawn_pos(screen_width, game_area_y + rand() % (game_area_height - enemy2_height));
@@ -35,30 +37,29 @@
dead_counter = 0;
blast_countdown = 0;
}
-/** @brief enemy death sound and status reset.
- * @details A function that resets the status of death of the enemy to be True,
- * and plays a death sound when an enemy was hit.
- */
+ /**
+ * @brief enemy death sound and status reset.
+ * @details Marks enemy as dead, so it can display explosion anation.
+ */
void die() {
gamepad.tone(123,0.3);
dead = true;
dead_counter = 3;
}
-/** @brief Draws and updates enemy sprites on the screen and substracts the score
- * if the ship leaves the screen limits.
- *
- * @details The function draws each individual enemy ship on the screen, and
- * changes their x and y positions each time the function is called. It
- * contains "if" statment for the the y-direction movement border limits so
- * that when they are reached, the ship would move the opposite way in y-direction.
- * Also, this function monitors the death status of the enemy ships, if
- * TRUE, then it calls for the enemy explosion animation to be executed.
- * To conclude, it sets the "active" status of the ship to FALSE when it leaves
- * the screen limits and substracts the in game score by -50.
- */
+
+ /**
+ * @brief Draws and updates enemy sprites on the screen and substracts the score
+ * if the ship leaves the screen limits.
+ *
+ * @details The function draws this enemy ship on the screen, and
+ * changes their x and y positions each time the function is called.
+ * If ship is dead then it draws enemy explosion animation and then disables it.
+ * Disables the ship when it leaves the screen limits and substracts
+ * the in game score by -50.
+ */
void updateAndDraw() {
pos.x -= enemy_speed;
- if (game_score >= 200){
+ if (GameGlobals::game_score >= 200){
if (switch_enemy_y_dir == false){pos.y -= 1;}
else if(switch_enemy_y_dir == true){pos.y += 1;}
if (pos.y > (game_area_height - enemy2_height)){
@@ -72,16 +73,17 @@
else { updateAndDrawDeathExplosion();}
if (pos.x < 0) {
- game_score -= 50;
- score_count_for_difficulty -= 50;
+ GameGlobals::game_score -= 50;
+ GameGlobals::score_count_for_difficulty -= 50;
active = false;
}
}
-/** @brief The function draws enemy explosion sprite
- * @details This is an explosion function that draws the enemy ships explosion
- * sprites when the player's shot hits them or when the player collides with them.
- * the animation consists of two sprites "Exploded" and "Half exploded".
- */
+ /**
+ * @brief Draws enemy explosion sprite
+ * @details Draws the enemy ships explosion sprites when the player's
+ * shot hits them or when the player collides with them.
+ * the animation consists of two sprites "Exploded" and "Half exploded".
+ */
void updateAndDrawDeathExplosion() {
if (dead_counter > 0) {
dead_counter--;
@@ -101,13 +103,14 @@
};
-/** Enemies Class
- * @brief The class that describes the states and actions of several enemy ships.
- * @details It includes random position generation (y - position),enemy ship
- * shoots drawing and control of time intervals between enemy shots.
- * @author Dmitrijs Griskovs
- * @date 15/04/2019
- */
+/**
+ * Enemies Class
+ * @brief The class manages all enemy ships.
+ * @details Responsible for spawning new ships and holding and updating them
+ * and controlling time intervals between enemy shots.
+ * @author Dmitrijs Griskovs
+ * @date 15/04/2019
+ */
class Enemies {
public:
Enemy enemy_blasts[max_enemy_blasts];
@@ -115,9 +118,10 @@
CircleBounds enemy_bounds;
CircleBounds enemy_blast_bounds;
-/** A constructor for the enemy's sprite body circle area and the blast circle
- * area. It sets the circle radius for collsion callculations.
- */
+ /**
+ * A constructor for the enemy's sprite body circle area and the blast circle
+ * area. It sets the circle radius for collsion callculations.
+ */
Enemies () {
enemy_bounds.center.x = 5;
enemy_bounds.center.y = 3;
@@ -127,10 +131,12 @@
enemy_blast_bounds.center.y = 1;
enemy_blast_bounds.radius = 1;
}
-/** @brief This function spawns a new enemy on the screen.
- * @details It spawns a new enemy on the screen when there is a free space
- * in the enemy aray.
- */
+
+ /**
+ * @brief This function spawns a new enemy on the screen.
+ * @details It spawns a new enemy on the screen when there is a free space
+ * in the enemy aray.
+ */
void spawnNewEnemy() {
int found = -1;
for (int i = 0; i < max_enemies; ++i) {
@@ -144,10 +150,12 @@
enemies[found].spawn();
}
}
-/** @brief Draws the ships and the blasts.
- * This function draws the enemy ships and enemy blasts whenever they are
- * avaialbe to move. Also, it limits the fire rate of the enemy ships.
- */
+
+ /**
+ * @brief Draws the ships and the blasts.
+ * @details Updates and draws all active enemy ships and decides when they
+ * should fire blasts.
+ */
void updateAndDrawEnemies() {
for (int i = 0; i < max_enemies; ++i) {
if (enemies[i].active){
@@ -164,12 +172,11 @@
}
}
}
-/** @brief activates a blast and gives the start positions
- * @details This function fires a blast whenever it is free in the blast array.
- * If the blast is free to be shot, it will become active and will get the.
- * positions of x and y in front of the enemy ship
- * @param enemy ~~~~~~~~~~~~~~~~~~~~~~~~~~(const Enemy&)
- */
+
+ /**
+ * @brief Spawns a blast at the position of given enemy.
+ * @param enemy (const Enemy&)
+ */
bool fireEnemyBlast(const Enemy& enemy) {
int found = -1;
for (int i = 0; i < max_enemy_blasts; ++i) {
@@ -187,11 +194,9 @@
}
return false;
}
-/** @brief draws the blasts on the screen.
- * @details Whenever the blast is active, this function draws the blast accross
- * the x-direction of the screen until it reaches the left screen limit and then
- * becomes inactive, therefore freeing a space in the blast array.
- */
+ /**
+ * @brief Moves and draws all active blasts.
+ */
void updateAndDrawEnemyBlasts() {
for (int i = 0; i < max_enemy_blasts; ++i) {
if (enemy_blasts[i].active) {
--- a/game/game.cpp Tue Apr 16 21:16:33 2019 +0000
+++ b/game/game.cpp Tue Apr 23 18:18:57 2019 +0000
@@ -14,22 +14,13 @@
#include "player.h"
#include "hud.h"
-// The speed of every counter is 1Hz
-bool game_over = true;
-bool is_shield_on = false;
-int game_score;
-int high_score = 0;
-int score_count_for_difficulty;
-int player_lifes = 3;
-int low_frequency_music_counter = 0;
-int high_frequency_music_counter = 0;
-bool red_led_state;
-int red_led_flashing;
-int enemy_ship_delay_counter;
+const int increase_difficulty = 50;
-const int increase_difficulty = 50;
-int enemy_ship_delay_max = 40;
-bool led_state = false;
+int GameGlobals::game_score = 0;
+int GameGlobals::score_count_for_difficulty = 0;
+int GameGlobals::player_lifes = 3;
+int GameGlobals::high_score = 0;
+bool GameGlobals::is_shield_on = false;
Enemies enemies;
Stars stars;
@@ -45,12 +36,11 @@
* returns back to main menu "main.cpp".
*/
bool Game::updateAndDraw() {
- //printf("update \n");
if (game_over) {
printf("start game \n");
startNewGame();
}
- if (gamepad.check_event(gamepad.X_PRESSED) && !is_shield_on){
+ if (gamepad.check_event(gamepad.X_PRESSED) && !GameGlobals::is_shield_on){
// Checking the button second time to prevent double blast.
gamepad.check_event(gamepad.X_PRESSED);
playerShip.fireNewBlast();
@@ -83,17 +73,17 @@
for (int i = 0; i < max_enemies; ++i) {
for (int j = 0; j < max_player_blasts; ++j) {
Enemy& enemy = enemies.enemies[i];
- GameObject& blast = blasts[j];
+ GameObject& blast = playerShip.blasts[j];
if (enemy.active && !enemy.dead && blast.active) {
bool collision = circleBounds.circleCollideTwoObjects(
enemy.pos, enemies.enemy_bounds,
- blast.pos, blast_bounds
+ blast.pos, playerShip.blast_bounds
);
if (collision) {
enemy.die();
printf("enemy got hit and dies from blast");
- game_score += 30;
- score_count_for_difficulty +=30;
+ GameGlobals::game_score += 30;
+ GameGlobals::score_count_for_difficulty +=30;
blast.active = false;
}
}
@@ -113,14 +103,14 @@
GameObject& blast = enemies.enemy_blasts[i];
if (blast.active) {
bool collision = circleBounds.circleCollideTwoObjects(
- player.pos, player_bounds,
+ playerShip.player.pos, playerShip.player_bounds,
blast.pos, enemies.enemy_blast_bounds
);
if (collision) {
- if (!is_shield_on){
+ if (!GameGlobals::is_shield_on){
gamepad.tone(423,0.4);
- player_lifes -= 1;
- //printf("lost a life from blast. left: %i \n", player_lifes);
+ GameGlobals::player_lifes -= 1;
+ printf("lost a life from blast. left: %i \n", GameGlobals::player_lifes);
blast.active = false;
}else{
blast.active = false;
@@ -140,12 +130,12 @@
Enemy& enemy = enemies.enemies[i];
if (enemy.active && !enemy.dead) {
bool collision = circleBounds.circleCollideTwoObjects(
- player.pos, player_bounds,
+ playerShip.player.pos, playerShip.player_bounds,
enemy.pos, enemies.enemy_bounds
);
if (collision) {
- player_lifes -= 1;
- printf("lost a life from enemy col. left: %i \n", player_lifes);
+ GameGlobals::player_lifes -= 1;
+ printf("lost a life from enemy col. left: %i \n", GameGlobals::player_lifes);
enemy.die();
printf("enemy got hit from collsion and dies");
}
@@ -159,29 +149,30 @@
* It does not reset the values when the game is paused.
*/
void Game::startNewGame() {
+ low_frequency_music_counter = 0;
+ high_frequency_music_counter = 0;
gameOverLogo.pos.x = game_area_x - 29; // 0 - the sprite length
gameOverLogo.pos.y = game_area_y;
youDied.pos.x = game_area_width;
youDied.pos.y = game_area_y;
game_over = false;
- is_shield_on = false;
- player.pos.x = 0; //player was defined in player.h
- player.pos.y = 24;
- stars_delay = 0;
+ playerShip.player.pos.x = 0; //player was defined in player.h
+ playerShip.player.pos.y = 24;
+ stars.stars_delay = 0;
enemy_ship_delay_max = 40;
enemy_ship_delay_counter = enemy_ship_delay_max;
- game_score = 0;
- score_count_for_difficulty = 0;
- player_lifes = 3;
- red_led_state = false;
- red_led_flashing = 0;
+ GameGlobals::is_shield_on = false;
+ GameGlobals::game_score = 0;
+ GameGlobals::score_count_for_difficulty = 0;
+ GameGlobals::player_lifes = 3;
+ hud.resetRedLed();
enemy_blast_speed = 3;
enemy_speed = 1;
for (int i = 0; i < max_enemies; ++i) {
enemies.enemies[i].active = false;
}
for (int i = 0; i < max_player_blasts; ++i) {
- blasts[i].active = false;
+ playerShip.blasts[i].active = false;
}
for (int i = 0; i < max_enemy_blasts; ++i) {
enemies.enemy_blasts[i].active = false;
@@ -199,7 +190,7 @@
drawGameOver();
lcd.normalMode();
char buffer[32];
- sprintf(buffer,"Your Score %i",game_score);
+ sprintf(buffer,"Your Score %i", GameGlobals::game_score);
lcd.printString(buffer,0,3);
wait(1);
lcd.printString("Press Y",0,4);
@@ -225,23 +216,25 @@
}
else { enemy_ship_delay_counter -= 1;}
- if (enemy_ship_delay_max >= 4 && score_count_for_difficulty >= increase_difficulty){
+ if (enemy_ship_delay_max >= 4 && GameGlobals::score_count_for_difficulty >= increase_difficulty){
//decrease enemy delay spawn.
enemy_ship_delay_max -= 3;
if (enemy_ship_delay_max <= 20 && enemy_ship_delay_max >= 15){
enemy_blast_speed += 1;
enemy_speed += 1;
}
- score_count_for_difficulty = 0;
+ GameGlobals::score_count_for_difficulty = 0;
}
- if (game_score >= 500){lcd.inverseMode();}
+ if (GameGlobals::game_score >= 500){
+ lcd.inverseMode();
+ }
}
bool Game::forceShildActivate(){
if (gamepad.check_event(gamepad.R_PRESSED)){
- is_shield_on = !is_shield_on;
+ GameGlobals::is_shield_on = !GameGlobals::is_shield_on;
}
- return is_shield_on;
+ return GameGlobals::is_shield_on;
}
/**
@@ -249,7 +242,7 @@
* game is over.
*/
bool Game::checkForGameOver() {
- return player_lifes == 0;
+ return GameGlobals::player_lifes == 0;
}
/**
--- a/game/game.h Tue Apr 16 21:16:33 2019 +0000
+++ b/game/game.h Tue Apr 23 18:18:57 2019 +0000
@@ -1,39 +1,58 @@
#ifndef GAME_H
#define GAME_H
-/** This variable is used in game.cpp, enemy.h, player.h and hud.h*/
-extern int game_score;
-extern int score_count_for_difficulty;
-extern int player_lifes;
-extern bool red_led_state;
-extern int red_led_flashing;
-extern int high_score;
+/**
+ * GameGlobals Class
+ * @brief Globals that different places in code can modify
+ * @author Dmitrijs Griskovs
+ * @date 22/04/2019
+ */
+class GameGlobals {
+public:
+ static int game_score;
+ static int score_count_for_difficulty;
+ static int player_lifes;
+ static int high_score;
+ static bool is_shield_on;
+};
-/** Game Class
- * @brief A library for drawing the game.
+/**
+ * Game Class
+ * @brief Stores general game logic.
* @author Dmitrijs Griskovs
* @date 15/04/2019
*/
class Game{
public:
-/** @brief The main game function where all the gamplay happens.
- * @details This is the main function of game.cpp, where the actual gameplay happens.
- * Here all other functions are activeated, and when the player dies, it
- * returns back to main menu "main.cpp".
- * @returns bool want_to_pause, when "START" button is pressed.
- */
+ /**
+ * Constructor.
+ * @brief It's crucial to init game_over to true, so it calls
+ * startNewGame on the first update. */
+ Game() : game_over(true) { }
+
+ /**
+ * @brief The main game function where all the gameplay and rendering happens.
+ * @details This is the main function of game.cpp, where the actual gameplay happens.
+ * Here all other functions are activeated, and when the player dies, it
+ * returns back to main menu "main.cpp" it also draws all sprites in the game.
+ * @returns bool want_to_pause, when "START" button is pressed.
+ */
bool updateAndDraw();
-/** @brief Resets all the in game variable when new game begins.
- * @details This function resets all the values to their intial states when
- * the game is first began when the player dies and wants to restart the game.
- * It does not reset the values when the game is paused.
- */
+
+ /**
+ * @brief Resets all the in game variable when new game begins.
+ * @details This function resets all the values to their intial states when
+ * the game is first began when the player dies and wants to restart the game.
+ * It does not reset the values when the game is paused.
+ */
void startNewGame();
-/** @brief Check whether the button R was pressed or not to turn ON/OFF the shield.
- * @details When the button R is pressed it sets bool "is_shield_active" to its opposite
- * value. If the shield is active then, a ship with force shield sprite is drawn and
- * the player's ability to shoot deactivates.
- */
+
+ /**
+ * @brief Check whether the button R was pressed or not to turn ON/OFF the shield.
+ * @details When the button R is pressed it sets bool "is_shield_active" to its opposite
+ * value. If the shield is active then, a ship with force shield sprite is drawn and
+ * the player's ability to shoot deactivates.
+ */
bool forceShildActivate();
private:
bool checkForGameOver();
@@ -50,6 +69,15 @@
void lowFrequencyPartMusic();
void highFrequencyPartMusic();
bool returnToMenu();
+
+ bool game_over;
+ // The speed of every counter is 1Hz
+ int low_frequency_music_counter;
+ int high_frequency_music_counter;
+ int enemy_ship_delay_counter;
+
+ int enemy_ship_delay_max;
+ bool led_state;
};
--- a/game/gameobject.h Tue Apr 16 21:16:33 2019 +0000
+++ b/game/gameobject.h Tue Apr 23 18:18:57 2019 +0000
@@ -4,15 +4,16 @@
#include "geometry.h"
/////////////////////////////////////////////////////////////////////
-/** GameObject Class
- * @brief A library for a pointing the positions of x and y int pos.
+/**
+ * GameObject Class
+ * @brief Base class for all objects in the game world.
* @author Dmitrijs Griskovs
* @date 15/04/2019
*/
class GameObject {
public:
-/** @brief This function assigns the position to the objects (sprites), using
- * using Point structure.
+/**
+ * @brief Activates the object at the given postion.
* @param spawn_pos sets position of x and y into pos (Point).
*/
void spawn(Point spawn_pos) {
--- a/game/geometry.h Tue Apr 16 21:16:33 2019 +0000
+++ b/game/geometry.h Tue Apr 23 18:18:57 2019 +0000
@@ -3,63 +3,78 @@
#include <math.h>
-////////////////////////////////////////////////////////////////////////
/**
- * A structure to represent the positions of a sprite on the screen.
+ * @struct Point
+ * @brief Position or vector on the screen. Supports basic vector arithmetics.
*/
struct Point {
/*@{*/
int x; /**< the x position */
int y; /**< the y position */
+ /** Default constructor */
Point() : x(0), y(0) {}
+ /** Constructor with x and y parameters */
Point(int _x, int _y) : x(_x), y(_y) {}
+ /**
+ * @brief Vector addition.
+ */
Point operator+ (const Point& rhs) const {
Point result(x + rhs.x, y + rhs.y);
return result;
}
+
+ /**
+ * @brief Vector substraction.
+ */
Point operator- (const Point& rhs) const {
Point result(x - rhs.x, y - rhs.y);
return result;
}
+
+ /**
+ * @brief Length of vector squared.
+ */
int lengthSquared() {
return x * x + y * y;
}
};
-/** CicleBounds Class
- * @brief A library for appointing the sprites' bounds.
+
+/**
+ * CicleBounds Class
+ * @brief Class to define object's bounds as a circle with center(e.g. offset).
* @author Dmitrijs Griskovs
* @date 15/04/2019
*/
class CircleBounds {
public:
-///////////////////////////////////////////////////////////////////////////////////////
- /** Constructor */
+ /** Default constructor */
CircleBounds() {};
+ /** Constructor with center and radius parameters */
CircleBounds(Point _center, float _radius): center(_center), radius(_radius) {};
Point center;
float radius;
-
-/** @brief a function that determines whether the two sprites have collided.
- * @details the function takes in the position and the bounds (center and radius)
- * two sprites (x and y positions and the circle area of a sprite) then performs the
- * pythagoras calaculation and returns a bool statment if there is an overlap
- * in the circle area of two sprites.
- *
- * @param pos_a positon of x and y of first sprite. (const Point&).
- * @param bounds_a center point and radius of the first sprite. (const CircleBounds&).
- * @param pos_b positon of x and y of second sprite. (const Point&).
- * @param bounds_b center point and radius of the second sprite. (const CircleBounds&).
- * @return difference.lengthSquared() if the areas of two sprites overlap, it will return true.
- */
+
+ /**
+ * @brief Determines whether the two circles collide.
+ * @details the function takes in position and circle bounds (center and radius)
+ * of two objects and returns wether they collide.
+ *
+ * @param pos_a positon of x and y of the first object. (const Point&).
+ * @param bounds_a center point and radius of the first object. (const CircleBounds&).
+ * @param pos_b positon of x and y of the second object. (const Point&).
+ * @param bounds_b center point and radius of the second object. (const CircleBounds&).
+ * @return true if the two circles overlap.
+ */
inline bool circleCollideTwoObjects(
- const Point& pos_a, const CircleBounds& bounds_a,
- const Point& pos_b, const CircleBounds& bounds_b) {
- Point center_a = pos_a + bounds_a.center;
- Point center_b = pos_b + bounds_b.center;
- Point difference = center_a - center_b;
- return difference.lengthSquared() <= pow(bounds_a.radius + bounds_b.radius, 2);
+ const Point& pos_a, const CircleBounds& bounds_a,
+ const Point& pos_b, const CircleBounds& bounds_b
+ ) {
+ Point center_a = pos_a + bounds_a.center;
+ Point center_b = pos_b + bounds_b.center;
+ Point difference = center_a - center_b;
+ return difference.lengthSquared() <= pow(bounds_a.radius + bounds_b.radius, 2);
}
};
--- a/game/hud.h Tue Apr 16 21:16:33 2019 +0000
+++ b/game/hud.h Tue Apr 23 18:18:57 2019 +0000
@@ -3,43 +3,53 @@
#include "game.h"
-/**Hud Class
- * @brief A library for describing the player's heads up display(hud).
+/**
+ * Hud Class
+ * @brief Renders interface elemets such as score and lifes
* @author Dmitrijs Griskovs
* @date 15/04/2019
*/
class Hud {
public:
-/** @brief Draws an in-game score on the screen during the gameplay. */
+
+ Hud() { resetRedLed(); }
+
+ void resetRedLed() {
+ red_led_flashing = 0;
+ red_led_state = false;
+ }
+
+ /** @brief Draws an in-game score on the screen during the gameplay. */
void drawScore(){
char buffer[16];
- sprintf(buffer," Score: %i",game_score);
+ sprintf(buffer," Score: %i", GameGlobals::game_score);
lcd.printString(buffer,0,0);
}
-/**@brief Displays the highest score reached in the main menu. */
+
+ /**@brief Displays the highest score reached in the main menu. */
void drawHighScore(){
- if (high_score < game_score){
- high_score = game_score;
+ if (GameGlobals::high_score < GameGlobals::game_score){
+ GameGlobals::high_score = GameGlobals::game_score;
}
- char buffer[16];
- sprintf(buffer,"High Score %i",high_score);
- lcd.printString(buffer,0,0);
+ char buffer[16];
+ sprintf(buffer,"High Score %i", GameGlobals::high_score);
+ lcd.printString(buffer,0,0);
}
-/** @brief Mointors, updates and shows the player's lives.
- * @details Checks the palyer's life value and lights the LEDs on/off accordingly to
- * how many lifes are left. green = 3, yellow = 2 and red = 1.
- */
+ /**
+ * @brief Display life counter using LEDs.
+ * @details Checks the palyer's life value and lights the LEDs on/off
+ * accordingly to how many lifes are left. green = 3, yellow = 2 and red = 1.
+ */
void displayLifes(){
- //printf("displayLifes:: %i\n", player_lifes);
- if (player_lifes == 3){
+ if (GameGlobals::player_lifes == 3){
playerHasThreeLives();
}
- else if (player_lifes == 2){
+ else if (GameGlobals::player_lifes == 2){
playerHasTwoLives();
}
- else if (player_lifes == 1){
+ else if (GameGlobals::player_lifes == 1){
playerHasOneLife();
}
else {
@@ -74,6 +84,8 @@
}
red_led_flashing += 1;
}
+ int red_led_flashing;
+ bool red_led_state;
};
#endif
\ No newline at end of file
--- a/game/player.h Tue Apr 16 21:16:33 2019 +0000
+++ b/game/player.h Tue Apr 23 18:18:57 2019 +0000
@@ -8,47 +8,50 @@
const int ship_speed = 2;
const int blast_speed = 5;
-GameObject blasts[max_player_blasts];
-GameObject player;
-CircleBounds player_bounds;
-CircleBounds blast_bounds;
-Game force_shield_check;
-
-/**Player Class
- * @brief A library for describing the player's ship.
+/**
+ * Player Class
+ * @brief Manages player's ship.
* @author Dmitrijs Griskovs
* @date 15/04/2019
*/
class Player{
public:
-/** @brief A constructor of the player's ship.
- * @details A constructor for the palyer's sprite body circle area and the
- * blast circle area. It sets the circle radius for collsion callculations.
- */
-Player() {
- player_bounds.center.x = 5;
- player_bounds.center.y = 8;
- player_bounds.radius = 7;
+ GameObject blasts[max_player_blasts];
+ GameObject player;
+ CircleBounds player_bounds;
+ CircleBounds blast_bounds;
+ Game force_shield_check;
+ /**
+ * @brief A constructor of the player's ship.
+ * @details A constructor for the palyer's sprite body circle area and the
+ * blast circle area. It sets the circle radius for collsion callculations.
+ */
+ Player() {
+ player_bounds.center.x = 5;
+ player_bounds.center.y = 8;
+ player_bounds.radius = 7;
+
+ blast_bounds.center.x = 0;
+ blast_bounds.center.y = 1;
+ blast_bounds.radius = 1;
+ }
- blast_bounds.center.x = 0;
- blast_bounds.center.y = 1;
- blast_bounds.radius = 1;
-}
-/** @brief Draws the player's blasts on the screen
- * @details This function Will draw every activated blast to the left with the blast_speed.
- * It will deactivate blasts when they leave the screen, for future reuse.
- * If the blast does miss the enemy and leaves the screen limits, the function will
- * substract 10 points in game_score.
- */
+ /**
+ * @brief Draws the player's blasts on the screen
+ * @details This function Will draw every activated blast to the left with the blast_speed.
+ * It will deactivate blasts when they leave the screen, for future reuse.
+ * If the blast does miss the enemy and leaves the screen limits, the function will
+ * substract 10 points in game_score.
+ */
void updateAndDrawBlasts() {
for (int i = 0; i < max_player_blasts; ++i) {
if (blasts[i].active) {
blasts[i].pos.x += blast_speed;
if (blasts[i].pos.x >= screen_width){
blasts[i].active = false;
- game_score -= 10;
- score_count_for_difficulty -= 10;
+ GameGlobals::game_score -= 10;
+ GameGlobals::score_count_for_difficulty -= 10;
}
lcd.setPixel(blasts[i].pos.x, blasts[i].pos.y, 1);
lcd.setPixel(blasts[i].pos.x+1, blasts[i].pos.y, 1);
@@ -57,13 +60,14 @@
}
}
-/** @brief Makes a blast active and gives it the start positions.
- * @details This function searches the array for the inactive blasts,
- * If a blast is not active, it will set it to active and will start drawing
- * it accross the screen until it reaches the LCD border line.
- */
+ /**
+ * @brief Makes a blast active and gives it the start positions.
+ * @details This function searches the array for the inactive blasts,
+ * If a blast is not active, it will set it to active and will start drawing
+ * it accross the screen until it reaches the LCD border line.
+ */
void fireNewBlast() {
- // Search the array of blasts if inactive we can use it.
+ // Search the array of blasts if inactive we can use it.
int found = -1;
for (int i = 0; i < max_player_blasts; ++i) {
if (!blasts[i].active) {
@@ -78,34 +82,34 @@
}
}
-/** @brief Updates and draws player's ship (including with force shield sprite) positon.
- * @details The function reads the analog input signal from joystick and
- * moves the player's ship on the LCD accordingly.(move joystick, the ship
- * moves up, move joystick right, the ship moves right). Also, this function checks
- * whether the force shield was activated in game.cpp, if it was then the player's
- * ship is replaced the exactky the same sip but with the added force shield.
- */
+ /**
+ * @brief Updates and draws player's ship (including with force shield sprite) positon.
+ * @details The function reads the analog input signal from joystick and
+ * moves the player's ship on the LCD accordingly.(move joystick, the ship
+ * moves up, move joystick right, the ship moves right). Also, this function checks
+ * whether the force shield was activated in game.cpp, if it was then the player's
+ * ship is replaced the exactky the same sip but with the added force shield.
+ */
void playerShipMovement(){
- if(x_dir.read() > joy_threshold_max_x){
+ if(x_dir.read() > joy_threshold_max_x) {
player.pos.x -= ship_speed;
- }
- else if(x_dir.read() < joy_threshold_min_x){
+ } else if(x_dir.read() < joy_threshold_min_x) {
player.pos.x += ship_speed;
}
- if(y_dir.read() > joy_threshold_max_y){
+ if(y_dir.read() > joy_threshold_max_y) {
player.pos.y -= ship_speed;
- }
- else if(y_dir.read() < joy_threshold_min_y){
+ } else if(y_dir.read() < joy_threshold_min_y) {
player.pos.y += ship_speed;
}
shipMovementLimits();
if (force_shield_check.forceShildActivate()){
drawSpriteOnTop(player.pos, player_spaceship1_shield_sprite);
+ } else {
+ drawSpriteOnTop(player.pos, player_spaceship1_sprite);
}
- else{ drawSpriteOnTop(player.pos, player_spaceship1_sprite);}
}
private:
-/** Prevents the player's ship to go beyond the playing zone limits.*/
+ /** Prevents the player's ship to go beyond the playing zone limits.*/
void shipMovementLimits(){
// Limits player ship on screen
if (player.pos.x < game_area_x){ player.pos.x = game_area_x;}
--- a/game/stars.h Tue Apr 16 21:16:33 2019 +0000
+++ b/game/stars.h Tue Apr 23 18:18:57 2019 +0000
@@ -1,28 +1,37 @@
#ifndef STARS_H
#define STARS_H
-const int max_small_stars = 5;
+/*const int max_small_stars = 5;
const int max_medium_stars = 5;
const int small_star_speed = 2;
const int medium_star_speed = 6;
const int stars_delay_max = 5;
-int stars_delay;
+int stars_delay;*/
-/**Stars Class
- * @brief A library for describing the background stars.
+/**
+ * Stars Class
+ * @brief Manages, updates and draws the background stars.
* @author Dmitrijs Griskovs
* @date 15/04/2019
*/
class Stars {
public:
+ static const int max_small_stars = 10;
+ static const int max_medium_stars = 5;
+ static const int small_star_speed = 2;
+ static const int medium_star_speed = 6;
+ static const int stars_delay_max = 5;
+ int stars_delay;
+
GameObject small_stars[max_small_stars];
GameObject medium_stars[max_medium_stars];
-/** @brief Makes a small star active and gives it the start positions.
- * @details Searches through the small stars array and if a star is not active,
- * it becomes active and is given the position of x (which is 0) and.
- * and random position of y.
- */
+ /**
+ * @brief Makes a small star active and gives it the start positions.
+ * @details Searches through the small stars array and if a star is not active,
+ * it becomes active and is given the position of x (which is 0) and.
+ * and random position of y.
+ */
void newSmallStarFlies() {
// Search the array of stars if inactive we can use it. - the same as with blasts
int found = -1;
@@ -39,11 +48,12 @@
small_stars[found].pos.y = rand() % screen_height + game_area_y;
}
}
-/** @brief draws small stars on the screen.
- * @details when a small star is active, this function updates the position and
- * draws it on the screen. Also, When the star leaves the screen limits it
- * deactivates the star for a new star to fly.
- */
+ /**
+ * @brief draws small stars on the screen.
+ * @details when a small star is active, this function updates the position and
+ * draws it on the screen. Also, When the star leaves the screen limits it
+ * deactivates the star for a new star to fly.
+ */
void updateAndDrawSmallStars(){
for (int i = 0; i < max_small_stars; ++i) {
if (small_stars[i].active) {
@@ -55,11 +65,12 @@
}
}
}
-/** @brief Makes a medium star active and gives it the start positions.
- * @details Searches through the medium stars array and if a star is not active,
- * it becomes active and is given the position of x (which is 0) and.
- * and random position of y.
- */
+ /**
+ * @brief Makes a medium star active and gives it the start positions.
+ * @details Searches through the medium stars array and if a star is not active,
+ * it becomes active and is given the position of x (which is 0) and.
+ * and random position of y.
+ */
void newMediumStarFlies() {
// Search the array of stars if inactive we can use it. - the same as with blasts
int found = -1;
@@ -75,11 +86,12 @@
medium_stars[found].pos.y = rand() % screen_height + game_area_y;
}
}
-/** @brief draws medium stars on the screen.
- * @details when a medium star is active, this function updates the position and
- * draws it on the screen. Also, When the star leaves the screen limits it
- * deactivates the star for a new star to fly.
- */
+ /**
+ * @brief draws medium stars on the screen.
+ * @details when a medium star is active, this function updates the position and
+ * draws it on the screen. Also, When the star leaves the screen limits it
+ * deactivates the star for a new star to fly.
+ */
void updateAndDrawMediumStars(){
for (int i = 0; i < max_medium_stars; ++i) {
if (medium_stars[i].active) {
@@ -92,15 +104,18 @@
}
}
-/** @brief A separate function for delaying the stars spawn time.*/
+ /** @brief A separate function for delaying the stars spawn time.*/
void starsSpawnDelay(){
if (stars_delay == stars_delay_max){
- //This is dealy between stars generation.
- newSmallStarFlies();
- newMediumStarFlies();
- stars_delay = 0;
+ //This is dealy between stars generation.
+ newSmallStarFlies();
+ newMediumStarFlies();
+ stars_delay = 0;
}
- else {stars_delay += 1;}
+ else {
+ stars_delay += 1;
+ }
}
+private:
};
#endif
\ No newline at end of file
--- a/main/main.cpp Tue Apr 16 21:16:33 2019 +0000
+++ b/main/main.cpp Tue Apr 23 18:18:57 2019 +0000
@@ -47,11 +47,11 @@
void pointer_position(int menu_number);
void ship_movement();
void menuSelection();
-//void introMusic();
+void introMusic();
ScreenOption current_screen = ScreenOption_Menu;
-//int low_frequency_music_counter = 0;
+int low_frequency_music_counter = 0;
void intro();
@@ -79,12 +79,14 @@
//Stop just a few pixels above the bottom screen border.
while (!gamepad.check_event(gamepad.START_PRESSED)){
lcd.clear();
+ //introMusic();
introPartOneText();
if (start_game_text_counter >= 2){
lcd.printString("Press START",10,5);
if (start_game_text_counter == 4){start_game_text_counter = 0;}
}
start_game_text_counter += 1;
+
lcd.refresh();
}
}
@@ -118,6 +120,7 @@
introPartOneText();
//introMusic();
+ gamepad.tone(200,2);
lcd.refresh();
wait(0.01);
@@ -132,6 +135,7 @@
drawSprite(lineThree.pos, intro_line_three_sprite);
lcd.refresh();
wait(0.1);
+ introMusic();
}
}
/**@brief
@@ -174,16 +178,16 @@
}
}
-/*void introMusic(){
- if (low_frequency_music_counter == 0){ gamepad.tone(90,1);}
- else if (low_frequency_music_counter == 1){gamepad.tone(60,1);}
- else if (low_frequency_music_counter == 2){gamepad.tone(90,1);}
- else if (low_frequency_music_counter == 3){gamepad.tone(60,1);}
- else if (low_frequency_music_counter == 4){gamepad.tone(90,1);}
- else if (low_frequency_music_counter == 5){gamepad.tone(60,1);}
- else if (low_frequency_music_counter== 6){
+void introMusic(){
+ if (low_frequency_music_counter == 0){ gamepad.tone(90,2);}
+ else if (low_frequency_music_counter == 2){gamepad.tone(60,2);}
+ else if (low_frequency_music_counter == 4){gamepad.tone(190,2);}
+ else if (low_frequency_music_counter == 6){gamepad.tone(60,2);}
+ else if (low_frequency_music_counter == 8){gamepad.tone(90,2);}
+ else if (low_frequency_music_counter == 10){gamepad.tone(160,2);}
+ else if (low_frequency_music_counter== 12){
gamepad.tone(90,1);
low_frequency_music_counter = 0;
}
low_frequency_music_counter++;
-}*/
\ No newline at end of file
+}
\ No newline at end of file
--- a/main/main.h Tue Apr 16 21:16:33 2019 +0000
+++ b/main/main.h Tue Apr 23 18:18:57 2019 +0000
@@ -7,8 +7,8 @@
#include "geometry.h"
#include "models.h"
-/** Global variable readings from the gamepad are shared for all the files to
- * use.
+/**
+ * Global variable readings from the gamepad are shared for all the files to use.
*/
extern N5110 lcd;
extern Gamepad gamepad;
@@ -18,21 +18,23 @@
static const int fps = 10;
-/** @brief A simplified function to draw sprites.
- * @details This is a specific function I made to simplify drawing the sprites.
- * It only works with spawn() function in gameobject.h.
- * The parameters for this function are given in the models.cpp for the
- * sprites.
- */
+/**
+ * @brief A simplified function to draw sprites.
+ * @details This is a specific function I made to simplify drawing the sprites.
+ * It only works with spawn() function in gameobject.h.
+ * The parameters for this function are given in the models.cpp for the
+ * sprites.
+ */
static void drawSprite(Point pos, const Sprite& sprite) {
lcd.drawSprite(pos.x, pos.y, sprite.height, sprite.width, (int*)sprite.data);
}
-/** @brief A simplified function to draw sprites. But this draw black pixels on top of white pixels.
- * @details This is an exactly the same function as before, but for the drawing
- * sprite function that draws the black pixels on top the white pixels when the
- * sprites overlap.
- */
+/**
+ * @brief A simplified function to draw sprites. But this draw black pixels on top of white pixels.
+ * @details This is an exactly the same function as before, but for the drawing
+ * sprite function that draws the black pixels on top the white pixels when the
+ * sprites overlap.
+ */
static void drawSpriteOnTop(Point pos, const Sprite& sprite) {
lcd.drawSpriteOnTop(pos.x, pos.y, sprite.height, sprite.width, (int*)sprite.data);
}
--- a/menu/menu.cpp Tue Apr 16 21:16:33 2019 +0000
+++ b/menu/menu.cpp Tue Apr 23 18:18:57 2019 +0000
@@ -6,11 +6,13 @@
#include "models.h"
#include "gameobject.h"
+
const int total_options = 3;
const float time_delay = 100;
Hud highScore;
GameObject cursor;
+//Stars menuStars;
Menu::Menu() { // NOTE to self: The constructor for declerering intial states of variables.
current_option = 0; // As soon as Menu menu; happens in main, the zero is addressed to the variable.
@@ -34,6 +36,9 @@
drawPointer(); // Drawing pointer only ones.
highScore.drawHighScore();
+ //stars.starsSpawnDelay();
+ //stars.updateAndDrawSmallStars();
+ //stars.updateAndDrawMediumStars();
lcd.printString("Start Game",1,2);
lcd.printString("Tutorial",1,3);
--- a/menu/menu.h Tue Apr 16 21:16:33 2019 +0000
+++ b/menu/menu.h Tue Apr 23 18:18:57 2019 +0000
@@ -1,37 +1,41 @@
#ifndef MENU_H
#define MENU_H
-/** Creates //////////////////////////////////////////////
- *
- */
-enum ScreenOption { //the available options in the menu.
+//#include "stars.h"
+
+/** Available options in the menu. */
+enum ScreenOption {
ScreenOption_Menu,
ScreenOption_Game,
ScreenOption_Tutorial,
ScreenOption_Settings
};
-/**Menu Class
- * @brief A library for describing the menu.
+/**
+ * Menu Class
+ * @brief Describes the menu.
* @author Dmitrijs Griskovs
* @date 15/04/2019
*/
class Menu {
- public:
-/** A constructor */
- Menu();
-/** brief@ Updates and Draws menu.
- * details@ Updates and draws menu input. Also, allows to select an option availabe
- * from the enum. It return "true" statment if the user picked a menu option.
- */
- bool updateAndDraw();
+public:
+ /** A constructor */
+ Menu();
- ScreenOption getCurrentScreenSelection();
+ /**
+ * @brief Updates and Draws menu.
+ * @details Updates and draws menu options. Also, allows to select an option availabe
+ * from the enum. It return "true" statment if the user picked a menu option.
+ */
+ bool updateAndDraw();
+
+ /** Returns currently selected meny option */
+ ScreenOption getCurrentScreenSelection();
- private:
- void pointerPosition(int menu_number);
- void drawPointer();
- int current_option; // NOTE to self: can declare variables in classes.
+private:
+ void pointerPosition(int menu_number);
+ void drawPointer();
+ int current_option;
};
--- a/settings/settings.h Tue Apr 16 21:16:33 2019 +0000
+++ b/settings/settings.h Tue Apr 23 18:18:57 2019 +0000
@@ -1,21 +1,24 @@
#ifndef SETTINGS_H
#define SETTINGS_H
-/**Settings Class
- * @brief A library for describing the Settings.
+/**
+ * Settings Class
+ * @brief describes the Settings.
* @author Dmitrijs Griskovs
* @date 15/04/2019
*/
class Settings{
- public:
-/** A constructor for the Tutorial's page number*/
+public:
+ /** A constructor for the Tutorial's page number*/
Settings();
-/** @brief Updates and draws the text and the percentage bar. Allows to choose
- * the level of contrast and screen brightness.
- */
+ /**
+ * @brief Allows to change contrast and brightness of the screen by using a knob.
+ * @details Updates and draws the text and the percentage bar. Allows to choose
+ * the level of contrast and screen brightness.
+ */
bool updateAndWriteSettings();
- private:
+private:
static const int total_pages = 2;
static const float time_delay = 100;
int current_page;
--- a/tutorial/tutorial.cpp Tue Apr 16 21:16:33 2019 +0000
+++ b/tutorial/tutorial.cpp Tue Apr 23 18:18:57 2019 +0000
@@ -27,6 +27,10 @@
else if (current_page == 4){ tutorialPage4();}
else if (current_page == 5){ tutorialPage5();}
else if (current_page == 6){ tutorialPage6();}
+ else if (current_page == 7){ tutorialPage7();}
+ else if (current_page == 8){ tutorialPage8();}
+ else if (current_page == 9){ tutorialPage9();}
+ else if (current_page == 10){ tutorialPage10();}
bool back_to_menu = false;
if (gamepad.check_event(gamepad.B_PRESSED)){
@@ -103,9 +107,9 @@
}
void Tutorial::tutorialPage5(){
- lcd.printString("Evade enemy ",0,1);
- lcd.printString("shots and",0,2);
- lcd.printString("don't let ",0,3);
+ lcd.printString("Evade or block",0,1);
+ lcd.printString("enemy shots",0,2);
+ lcd.printString("and don't let ",0,3);
lcd.printString("them pass.",0,4);
}
--- a/tutorial/tutorial.h Tue Apr 16 21:16:33 2019 +0000
+++ b/tutorial/tutorial.h Tue Apr 23 18:18:57 2019 +0000
@@ -1,11 +1,6 @@
#ifndef TUTORIAL_H
#define TUTORIAL_H
-const int right_arrow_pos_x = 66;
-const int left_arrow_pos_x = 5;
-const int arrows_pos_y = 0;
-const int total_pages = 11;
-const float time_delay = 100;
/**Tutorial Class
* @brief A library for describing the Tutorial.
@@ -13,13 +8,18 @@
* @date 15/04/2019
*/
class Tutorial{
- public:
+public:
/** A constructor for the Tutorial's page number*/
Tutorial();
/** @brief Updates and draws a text that explains the game rules and gameplay*/
bool updateAndWriteTutorial();
- private:
+private:
+ static const int right_arrow_pos_x = 70;
+ static const int left_arrow_pos_x = 4;
+ static const int arrows_pos_y = 0;
+ static const int total_pages = 11;
+ static const float time_delay = 100;
int current_page;
void drawArrowsAndExitButton();
void turnPages();
