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.
Game/Game.cpp@11:db27d3838514, 2019-05-06 (annotated)
- Committer:
- joshdavy
- Date:
- Mon May 06 14:43:01 2019 +0000
- Revision:
- 11:db27d3838514
- Parent:
- 10:58cf89dd878c
- Child:
- 12:5549a299d41e
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joshdavy | 1:37802772843e | 1 | #include "Game.h" |
joshdavy | 8:21b6d4dbce44 | 2 | |
joshdavy | 10:58cf89dd878c | 3 | /** Game Class |
joshdavy | 10:58cf89dd878c | 4 | |
joshdavy | 10:58cf89dd878c | 5 | @brief Class for the main game. Responsible for level loading, updating and |
joshdavy | 10:58cf89dd878c | 6 | drawing the game screen. |
joshdavy | 10:58cf89dd878c | 7 | |
joshdavy | 10:58cf89dd878c | 8 | @version 1.0 |
joshdavy | 10:58cf89dd878c | 9 | |
joshdavy | 11:db27d3838514 | 10 | @author Joshua Davy el17jd |
joshdavy | 10:58cf89dd878c | 11 | |
joshdavy | 11:db27d3838514 | 12 | @date April 2019 |
joshdavy | 11:db27d3838514 | 13 | |
joshdavy | 11:db27d3838514 | 14 | */ |
joshdavy | 11:db27d3838514 | 15 | |
joshdavy | 11:db27d3838514 | 16 | /** |
joshdavy | 11:db27d3838514 | 17 | * @brief Constructor (no paramateters) |
joshdavy | 11:db27d3838514 | 18 | */ |
joshdavy | 1:37802772843e | 19 | Game::Game() |
joshdavy | 1:37802772843e | 20 | { |
joshdavy | 1:37802772843e | 21 | |
joshdavy | 10:58cf89dd878c | 22 | } |
joshdavy | 11:db27d3838514 | 23 | /** |
joshdavy | 11:db27d3838514 | 24 | * @brief Deconstructor |
joshdavy | 11:db27d3838514 | 25 | */ |
joshdavy | 10:58cf89dd878c | 26 | Game::~Game() |
joshdavy | 10:58cf89dd878c | 27 | { |
joshdavy | 10:58cf89dd878c | 28 | |
joshdavy | 10:58cf89dd878c | 29 | } |
joshdavy | 7:68e06dda79f7 | 30 | |
joshdavy | 11:db27d3838514 | 31 | /** |
joshdavy | 11:db27d3838514 | 32 | * @brief Loads the specified level. |
joshdavy | 11:db27d3838514 | 33 | * @param level_number @details The level number to load. |
joshdavy | 11:db27d3838514 | 34 | */ |
joshdavy | 9:96969b1c6bde | 35 | void Game::load_level(int level_number) |
joshdavy | 9:96969b1c6bde | 36 | { |
joshdavy | 10:58cf89dd878c | 37 | // I would of done this large switch statement with a an array |
joshdavy | 10:58cf89dd878c | 38 | // of LevelDefiniton as it would be a lot neater. However this causes |
joshdavy | 10:58cf89dd878c | 39 | // issue with the infamous "empty execution error" bug |
joshdavy | 9:96969b1c6bde | 40 | LevelDefinition level_def; |
joshdavy | 9:96969b1c6bde | 41 | switch(level_number) { |
joshdavy | 9:96969b1c6bde | 42 | case 1 : |
joshdavy | 9:96969b1c6bde | 43 | level_def = level_1; |
joshdavy | 9:96969b1c6bde | 44 | break; |
joshdavy | 9:96969b1c6bde | 45 | case 2: |
joshdavy | 9:96969b1c6bde | 46 | level_def = level_2; |
joshdavy | 9:96969b1c6bde | 47 | break; |
joshdavy | 9:96969b1c6bde | 48 | case 3: |
joshdavy | 9:96969b1c6bde | 49 | level_def = level_3; |
joshdavy | 9:96969b1c6bde | 50 | break; |
joshdavy | 9:96969b1c6bde | 51 | case 4: |
joshdavy | 9:96969b1c6bde | 52 | level_def = level_4; |
joshdavy | 9:96969b1c6bde | 53 | break; |
joshdavy | 9:96969b1c6bde | 54 | case 5: |
joshdavy | 9:96969b1c6bde | 55 | level_def = level_5; |
joshdavy | 9:96969b1c6bde | 56 | break; |
joshdavy | 9:96969b1c6bde | 57 | case 6: |
joshdavy | 9:96969b1c6bde | 58 | level_def = level_6; |
joshdavy | 9:96969b1c6bde | 59 | break; |
joshdavy | 9:96969b1c6bde | 60 | case 7: |
joshdavy | 9:96969b1c6bde | 61 | level_def = level_7; |
joshdavy | 9:96969b1c6bde | 62 | break; |
joshdavy | 9:96969b1c6bde | 63 | case 8: |
joshdavy | 10:58cf89dd878c | 64 | level_def = level_8; |
joshdavy | 10:58cf89dd878c | 65 | break; |
joshdavy | 10:58cf89dd878c | 66 | case 9: |
joshdavy | 10:58cf89dd878c | 67 | // Then the game is complete |
joshdavy | 9:96969b1c6bde | 68 | _game_won = true; |
joshdavy | 9:96969b1c6bde | 69 | return; |
joshdavy | 9:96969b1c6bde | 70 | |
joshdavy | 9:96969b1c6bde | 71 | |
joshdavy | 9:96969b1c6bde | 72 | } |
joshdavy | 9:96969b1c6bde | 73 | |
joshdavy | 10:58cf89dd878c | 74 | // Initialises the player position |
joshdavy | 10:58cf89dd878c | 75 | _player.init(level_def.initial_pos); |
joshdavy | 10:58cf89dd878c | 76 | // Initalises level |
joshdavy | 9:96969b1c6bde | 77 | _level.init(level_def.blocks, |
joshdavy | 9:96969b1c6bde | 78 | level_def.number_of_blocks, |
joshdavy | 11:db27d3838514 | 79 | level_def.goal, |
joshdavy | 11:db27d3838514 | 80 | level_def.moving_blocks, |
joshdavy | 11:db27d3838514 | 81 | level_def.number_of_moving_blocks); |
joshdavy | 9:96969b1c6bde | 82 | } |
joshdavy | 11:db27d3838514 | 83 | /** |
joshdavy | 11:db27d3838514 | 84 | * @brief Initalises the game and loads the first level. |
joshdavy | 11:db27d3838514 | 85 | */ |
joshdavy | 7:68e06dda79f7 | 86 | void Game::init() |
joshdavy | 7:68e06dda79f7 | 87 | { |
joshdavy | 10:58cf89dd878c | 88 | // Loads the first level |
joshdavy | 9:96969b1c6bde | 89 | load_level(1); |
joshdavy | 9:96969b1c6bde | 90 | _current_level = 1; |
joshdavy | 10:58cf89dd878c | 91 | |
joshdavy | 9:96969b1c6bde | 92 | _game_won = false; |
joshdavy | 7:68e06dda79f7 | 93 | } |
joshdavy | 1:37802772843e | 94 | |
joshdavy | 11:db27d3838514 | 95 | /** |
joshdavy | 11:db27d3838514 | 96 | * @brief Updates the game this should be called every frame. Updates player |
joshdavy | 11:db27d3838514 | 97 | * and block positions and loads the next level if the goal is reached. |
joshdavy | 11:db27d3838514 | 98 | * @param pad @details the Gamepad object. |
joshdavy | 11:db27d3838514 | 99 | */ |
joshdavy | 1:37802772843e | 100 | void Game::update(Gamepad &pad) |
joshdavy | 1:37802772843e | 101 | { |
joshdavy | 10:58cf89dd878c | 102 | // Update player position based on gamepad presses/ collisions |
joshdavy | 9:96969b1c6bde | 103 | _player.update(pad, _level.get_blocks(),_level.get_number_of_blocks()); |
joshdavy | 10:58cf89dd878c | 104 | // Update location of any moving blocks |
joshdavy | 9:96969b1c6bde | 105 | _level.update_moving_blocks(); |
joshdavy | 10:58cf89dd878c | 106 | |
joshdavy | 10:58cf89dd878c | 107 | // If the player has reached the goal |
joshdavy | 9:96969b1c6bde | 108 | if (_player.check_goal_reached(_level.get_goal())) { |
joshdavy | 10:58cf89dd878c | 109 | // Load next level |
joshdavy | 9:96969b1c6bde | 110 | _current_level += 1; |
joshdavy | 9:96969b1c6bde | 111 | load_level(_current_level); |
joshdavy | 9:96969b1c6bde | 112 | } |
joshdavy | 1:37802772843e | 113 | } |
joshdavy | 1:37802772843e | 114 | |
joshdavy | 11:db27d3838514 | 115 | /** |
joshdavy | 11:db27d3838514 | 116 | * @brief Draws the current state on the screen. Should be called every frame. |
joshdavy | 11:db27d3838514 | 117 | * @param lcd @details the N5110 lcd object |
joshdavy | 11:db27d3838514 | 118 | */ |
joshdavy | 2:b62e8be35a5d | 119 | void Game::draw(N5110 &lcd) |
joshdavy | 1:37802772843e | 120 | { |
joshdavy | 10:58cf89dd878c | 121 | // Clear screen buffer |
joshdavy | 2:b62e8be35a5d | 122 | lcd.clear(); |
joshdavy | 10:58cf89dd878c | 123 | |
joshdavy | 10:58cf89dd878c | 124 | // Render Level And Player |
joshdavy | 8:21b6d4dbce44 | 125 | _level.render(lcd); |
joshdavy | 1:37802772843e | 126 | _player.render(lcd); |
joshdavy | 10:58cf89dd878c | 127 | |
joshdavy | 10:58cf89dd878c | 128 | // refresh screen |
joshdavy | 2:b62e8be35a5d | 129 | lcd.refresh(); |
joshdavy | 2:b62e8be35a5d | 130 | |
joshdavy | 1:37802772843e | 131 | } |
joshdavy | 11:db27d3838514 | 132 | /** |
joshdavy | 11:db27d3838514 | 133 | * @brief Returns if the game has been won |
joshdavy | 11:db27d3838514 | 134 | * @returns if the game has been won. |
joshdavy | 11:db27d3838514 | 135 | */ |
joshdavy | 9:96969b1c6bde | 136 | bool Game::game_won() |
joshdavy | 9:96969b1c6bde | 137 | { |
joshdavy | 9:96969b1c6bde | 138 | return _game_won; |
joshdavy | 9:96969b1c6bde | 139 | } |