Skip to content

Function overview

Alexis edited this page May 10, 2020 · 1 revision

Classes Collectables:

There is 9 functions defined in this file which all looks like this one:

Small_collectable::Small_collectable(pair position, int creator_object_size_y): Collectable(position, creator_object_size_y)

The functions take 2 arguments: position and creator_object_size_y. The position is the position of the player and the creator_object_size_y is the size of the one that creates the collectable. This last one is useful for collectables as we do not want the collectable to pop through the box but pop at the top of the block. Thus thanks to this parameter the power up will pop up on top of it which is y + creator_object_size_y. In this function, it initialise what type of collectable it is, it’s state, it’s speed and the picture associated to it.

Class Music:

class Music: public QMediaPlayer

Music::Music(QObject* parent): QMediaPlayer (parent)

The music class works thanks to QMediaPlayer and QSound, classes from Qt inheriting from QObjects. It takes an object music that has a type QSound which is initialised in private in order to be stick to the heap and stay in memory when running the constructor. My class music is composed of

  • a constructor Music(QObject *parent = nullptr); that plays the music stock in the path
  • a destructor to erase the memory and that makes the music stop ~Music();
  • a function void stop(); that stops the music when called (when the game ends or when the player wins -> in the class game_over and you_win)

QSound has two public slots play() and stop() which we use to play and stop the music.

Tests:

bool Test::Test_Blink():

The function blink() (from the Block class) should change the image_count of an active block (a block of texture question_mark). This function checks that the image_count indeed changes.

bool Test::Test_Blink2():

The purpose of this second function is to check how blink() works inside the active block - each question mark block should by default use blink() to change its image_count every 0.5 seconds. The approach I initially took was to store the initial image_count of the block, wait 0.5 seconds, and see if it changed. When this approach failed, I assume it was because of the time shift between the level load and the test, or some other timer error, since it was obvious that blink() was working when playing the game. I tried waiting 0.52 seconds and trying the test 10 times - if it would work a majority of the times, it would mean the function worked. However, there must be still some shift between how the game loads and how the test does. The test is still not working.

void Test::Test_Blocks():

Function that lets us know if both the Block and the Blocks class work.

bool Test::Test_AddCoin():

Checking that by using add_coin() (from Coin_counter) we add the correct amount of coins.

bool Test::Test_UpdateCounter():

The update_counter() (from Coin_counter) is useful when we add coins such that a new counter is required (for example, if we have a one-digit amount of coins and we add enough such that we get a two-digit number, we have to create a new counter for the second digit). Therefore, this function tests that when this shift from one-digit to two-digits takes place through add_coin(), update_counter() creates a new counter.

bool Test::Test_CoinCount():

Function that lets us know if the Coin_counter class tests passed.

bool Test::Test_CreateColRange():

This function tests create_collision_range() (from tools.h). Since a Player object makes use of this in its collision_ranges, I decided to create such an object and check if the length of collision_ranges is equal to 3 (horizontal, vertical, corner) and if all the items in collision_range are invisible (have transparency 255).

bool Test::Test_UpdateColRange():

This function tests update_collision_range() (from tools.cpp). Again, I use an object of the Player class. I went with the most straightforward approach and created 4 different cases depending on the player’s speed.x and speed.y. It looks a bit messy but it is simple enough - it is just a direct verification of what the function should do.

bool Test::Test_Delay():

The delay() function (from tools.cpp) delays any action by n milliseconds. I use chrono to mark the current time and I create an “end time”, which is one second later than the initial time. I then delay the function for one second and see if the current time has passed the “end time” (which it should).

bool Test::Test_Tools():

Function that lets us know if all the tools tests are passed.

bool Test_Jump():

This is the function that tests the Jump() function inside the Enemy class. I started with a list that would store the results of the two cases the function tests (1 if the test passes, 0 if not). In the end, it check if the sum of the terms is equal to 2 and if that is the case, the function returns true (I did this quite often in the early tests, but preferred to simply return false each time a case would not work in later tests). In the first case, the enemy cannot jump anymore, so the function tests that jump() does not modify the times_jumped of the enemy. In the second case, the times_jumped should be bigger by one and the speed.y modifies.

bool Test_Move():

This tests the move() function of the Enemy class. At the time I made this, there was only one enemy, so I tested the function on it (the other two have minor modifications of this function). Since the function is relatively long, I divided its test into 3 smaller tests, so they would be easier to debug if it was the case.

bool Test_Move_First():

The first function checks the function up until boundaries. In the first case, the enemy is passive. The second case can get more confusing, because I combined it with the smooth motion conditions. In this case, the enemy is aggressive and we distinguish two cases, when the speed.y is bigger than speedMax.y and when it is lower. After this point, the tests are pretty straight forward.

bool Test_Move_Second():

It tests the boundaries inside the function. There are two sets of conditions that determine what this part of the function does - a, b, c, d and a1, b1, c1, d1. I expressed them in the beginning so the code would be cleaner.

bool Test_Move_Third():

Tests the movement, jump, and direction parts of the function through the two cases: aggressive and passive.

bool Test_TimerConnect():

bool Test_Enemy():

Tests if all Enemy tests are passed.

bool Test_UpdateCounter_SP():

bool Test_PowerUp():

bool Test_Speed_ThrowSpeed():

bool Test_SuperPow():

bool Test_Move_Projectile():

bool Test_KeyEvents(QKeyEvent *event):

bool Test_Move_Player():

bool Test_Jump_Player():

Game over class:

Game_over::Game_over(int final_score, QGraphicsItem* parent): QObject(), QGraphicsPixmapItem (parent)

This class implement the function when the player die. What it does is delete the main scene and replace it by the scene_game_over on which the image game over appears as well as the final score. In this class there is also another function keyPressEvent(QKeyEvent *event) that implements a button which, when pressing enter, restart the level.

Player class:

Player::Player(QGraphicsItem* parent) : QObject(), QGraphicsPixmapItem (parent)

This class defines the Player of our game, that we control and wish to lead to the end of the game.

void Player::move()

The function defines all the movements of the players: speed, collisions, interactions with other objects, jumps

void Player::throw_projectile()

The function allows the player to throw a projectile: a baguette by default or a wine bottle if he has picked up a wine glass collectable.

void Player::world_launch_level()

This function is called whenever the user presses Return and checks the position x of the player. Depending on its position on the x axis, a different level will be displayed.

void Player::create_animation()

This function generates useable images from the file of the sprite sheet.

Projectile Class:

Projectile::Projectile(pair position, bool direction, int character_size_x, Projectile_type type, pair size, QGraphicsItem* parent) : QObject (), QGraphicsPixmapItem (parent)

The class defines all projectiles, that the Player and the Enemy can throw.

void Projectile::move()

Defines the movements of the projectiles, with its collisions, and its effects on other Objects when colliding.

Enemy Class:

void Enemy::move()

Same principle as other Move functions, except this time, the Enemy must figure out that he shouldn’t fall to his death when there is a cliff. Hence we define two more collision boxes at his bottom left and bottom right where he checks for collision: collision_range_br and collision_range_bl. Also, his movements depend on his state (passiv or aggressiv), depending on how close the Player is.

View Class:

void View::world()

This method, initialise a new scene in a new view of the class World and launches it with clearing the other scenes.