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.
Level/Level.cpp@9:96969b1c6bde, 2019-04-24 (annotated)
- Committer:
- joshdavy
- Date:
- Wed Apr 24 10:18:45 2019 +0000
- Revision:
- 9:96969b1c6bde
- Parent:
- 8:21b6d4dbce44
- Child:
- 10:58cf89dd878c
Added win screen aswell as multiple levels.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| joshdavy | 6:2ca1516ec1e2 | 1 | #include "Level.h" |
| joshdavy | 6:2ca1516ec1e2 | 2 | |
| joshdavy | 6:2ca1516ec1e2 | 3 | Level::Level() {} |
| joshdavy | 6:2ca1516ec1e2 | 4 | |
| joshdavy | 6:2ca1516ec1e2 | 5 | Level::~Level() {} |
| joshdavy | 6:2ca1516ec1e2 | 6 | |
| joshdavy | 9:96969b1c6bde | 7 | void Level::init(Block blocks [], |
| joshdavy | 8:21b6d4dbce44 | 8 | int number_of_blocks, |
| joshdavy | 8:21b6d4dbce44 | 9 | Vector2D goal) |
| joshdavy | 6:2ca1516ec1e2 | 10 | { |
| joshdavy | 8:21b6d4dbce44 | 11 | _number_of_blocks = number_of_blocks; |
| joshdavy | 9:96969b1c6bde | 12 | |
| joshdavy | 8:21b6d4dbce44 | 13 | |
| joshdavy | 8:21b6d4dbce44 | 14 | |
| joshdavy | 8:21b6d4dbce44 | 15 | for (int i = 0; i<_number_of_blocks; i++) { |
| joshdavy | 8:21b6d4dbce44 | 16 | _blocks[i].first.x = blocks[i].first.x; |
| joshdavy | 8:21b6d4dbce44 | 17 | _blocks[i].first.y = blocks[i].first.y; |
| joshdavy | 8:21b6d4dbce44 | 18 | _blocks[i].second.x = blocks[i].second.x; |
| joshdavy | 7:68e06dda79f7 | 19 | _blocks[i].second.y = blocks[i].second.y; |
| joshdavy | 6:2ca1516ec1e2 | 20 | } |
| joshdavy | 8:21b6d4dbce44 | 21 | |
| joshdavy | 9:96969b1c6bde | 22 | _goal = goal; |
| joshdavy | 9:96969b1c6bde | 23 | _number_of_moving_blocks = 0; |
| joshdavy | 9:96969b1c6bde | 24 | } |
| joshdavy | 9:96969b1c6bde | 25 | |
| joshdavy | 9:96969b1c6bde | 26 | Block * Level::get_blocks() |
| joshdavy | 9:96969b1c6bde | 27 | { |
| joshdavy | 9:96969b1c6bde | 28 | return _blocks; |
| joshdavy | 9:96969b1c6bde | 29 | } |
| joshdavy | 9:96969b1c6bde | 30 | |
| joshdavy | 9:96969b1c6bde | 31 | int Level::get_number_of_blocks() |
| joshdavy | 9:96969b1c6bde | 32 | { |
| joshdavy | 9:96969b1c6bde | 33 | return _number_of_blocks; |
| joshdavy | 9:96969b1c6bde | 34 | } |
| joshdavy | 9:96969b1c6bde | 35 | |
| joshdavy | 9:96969b1c6bde | 36 | Vector2D Level::get_goal() |
| joshdavy | 9:96969b1c6bde | 37 | { |
| joshdavy | 9:96969b1c6bde | 38 | return _goal; |
| joshdavy | 9:96969b1c6bde | 39 | } |
| joshdavy | 9:96969b1c6bde | 40 | |
| joshdavy | 9:96969b1c6bde | 41 | void Level::update_moving_blocks() |
| joshdavy | 9:96969b1c6bde | 42 | { |
| joshdavy | 9:96969b1c6bde | 43 | MovingBlock current_block; |
| joshdavy | 9:96969b1c6bde | 44 | for (int i = 0; i < _number_of_moving_blocks; i++) { |
| joshdavy | 9:96969b1c6bde | 45 | current_block = _moving_blocks[i]; |
| joshdavy | 9:96969b1c6bde | 46 | |
| joshdavy | 9:96969b1c6bde | 47 | |
| joshdavy | 9:96969b1c6bde | 48 | if (current_block.extending) { |
| joshdavy | 9:96969b1c6bde | 49 | |
| joshdavy | 9:96969b1c6bde | 50 | _blocks[current_block.index].first.x += 1; |
| joshdavy | 9:96969b1c6bde | 51 | _blocks[current_block.index].second.x += 1; |
| joshdavy | 9:96969b1c6bde | 52 | |
| joshdavy | 9:96969b1c6bde | 53 | } else { |
| joshdavy | 9:96969b1c6bde | 54 | |
| joshdavy | 9:96969b1c6bde | 55 | _blocks[current_block.index].first.x -= 1; |
| joshdavy | 9:96969b1c6bde | 56 | _blocks[current_block.index].second.x -= 1; |
| joshdavy | 9:96969b1c6bde | 57 | |
| joshdavy | 9:96969b1c6bde | 58 | } |
| joshdavy | 9:96969b1c6bde | 59 | |
| joshdavy | 9:96969b1c6bde | 60 | |
| joshdavy | 9:96969b1c6bde | 61 | |
| joshdavy | 9:96969b1c6bde | 62 | |
| joshdavy | 9:96969b1c6bde | 63 | if (_blocks[current_block.index].first.x > |
| joshdavy | 9:96969b1c6bde | 64 | (current_block.initial_pos + current_block.distance)) { |
| joshdavy | 9:96969b1c6bde | 65 | current_block.extending = false; |
| joshdavy | 9:96969b1c6bde | 66 | } |
| joshdavy | 9:96969b1c6bde | 67 | if (_blocks[current_block.index].first.x < current_block.initial_pos) { |
| joshdavy | 9:96969b1c6bde | 68 | current_block.extending = true; |
| joshdavy | 9:96969b1c6bde | 69 | |
| joshdavy | 9:96969b1c6bde | 70 | } |
| joshdavy | 9:96969b1c6bde | 71 | |
| joshdavy | 9:96969b1c6bde | 72 | _moving_blocks[i] = current_block; |
| joshdavy | 8:21b6d4dbce44 | 73 | } |
| joshdavy | 8:21b6d4dbce44 | 74 | |
| joshdavy | 8:21b6d4dbce44 | 75 | |
| joshdavy | 8:21b6d4dbce44 | 76 | |
| joshdavy | 8:21b6d4dbce44 | 77 | } |
| joshdavy | 8:21b6d4dbce44 | 78 | |
| joshdavy | 8:21b6d4dbce44 | 79 | |
| joshdavy | 9:96969b1c6bde | 80 | void Level::declare_moving_block(int index,bool extending,int distance) |
| joshdavy | 9:96969b1c6bde | 81 | { |
| joshdavy | 9:96969b1c6bde | 82 | MovingBlock new_moving_block; |
| joshdavy | 9:96969b1c6bde | 83 | new_moving_block.index = index; |
| joshdavy | 9:96969b1c6bde | 84 | new_moving_block.distance = distance; |
| joshdavy | 9:96969b1c6bde | 85 | new_moving_block.extending = extending; |
| joshdavy | 9:96969b1c6bde | 86 | new_moving_block.initial_pos = _blocks[index].first.x; |
| joshdavy | 9:96969b1c6bde | 87 | |
| joshdavy | 9:96969b1c6bde | 88 | if(!extending) { |
| joshdavy | 9:96969b1c6bde | 89 | _blocks[index].first.x += distance; |
| joshdavy | 9:96969b1c6bde | 90 | _blocks[index].second.x += distance; |
| joshdavy | 9:96969b1c6bde | 91 | } |
| joshdavy | 9:96969b1c6bde | 92 | |
| joshdavy | 9:96969b1c6bde | 93 | |
| joshdavy | 9:96969b1c6bde | 94 | |
| joshdavy | 9:96969b1c6bde | 95 | _moving_blocks[_number_of_moving_blocks] = new_moving_block; |
| joshdavy | 9:96969b1c6bde | 96 | _number_of_moving_blocks += 1; |
| joshdavy | 9:96969b1c6bde | 97 | |
| joshdavy | 9:96969b1c6bde | 98 | |
| joshdavy | 9:96969b1c6bde | 99 | } |
| joshdavy | 8:21b6d4dbce44 | 100 | |
| joshdavy | 6:2ca1516ec1e2 | 101 | void Level::render(N5110 &lcd) |
| joshdavy | 6:2ca1516ec1e2 | 102 | { |
| joshdavy | 8:21b6d4dbce44 | 103 | |
| joshdavy | 8:21b6d4dbce44 | 104 | |
| joshdavy | 8:21b6d4dbce44 | 105 | for (int i = 0; i<_number_of_blocks; i++) { |
| joshdavy | 8:21b6d4dbce44 | 106 | |
| joshdavy | 8:21b6d4dbce44 | 107 | lcd.drawRect(_blocks[i].first.x,_blocks[i].first.y, |
| joshdavy | 8:21b6d4dbce44 | 108 | _blocks[i].second.x - _blocks[i].first.x, |
| joshdavy | 8:21b6d4dbce44 | 109 | _blocks[i].second.y - _blocks[i].first.y, |
| joshdavy | 8:21b6d4dbce44 | 110 | FILL_BLACK); |
| joshdavy | 6:2ca1516ec1e2 | 111 | } |
| joshdavy | 8:21b6d4dbce44 | 112 | |
| joshdavy | 8:21b6d4dbce44 | 113 | lcd.drawSprite(_goal.x,_goal.y,11,6,(int *) goalMap); |
| joshdavy | 8:21b6d4dbce44 | 114 | |
| joshdavy | 6:2ca1516ec1e2 | 115 | } |