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