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
Gameengine/Gameengine.cpp@24:9c7aca60e374, 2019-04-08 (annotated)
- Committer:
- el17arm
- Date:
- Mon Apr 08 18:17:10 2019 +0000
- Revision:
- 24:9c7aca60e374
- Parent:
- 23:1ee8686af747
- Child:
- 25:7fb1b6fa9137
- Child:
- 26:bce40bf4c9fc
the previous 5 seperate key functions now reduced to one. I achieved this using a struct. Now, rather than 5 functions for five keys, one function can call the a key variable from the struct.; ; This makes it simple to change the number of keys needed.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el17arm | 14:dc3524d11921 | 1 | #include "Gameengine.h" |
| el17arm | 4:b92da6a09c8b | 2 | |
| el17arm | 14:dc3524d11921 | 3 | Gameengine::Gameengine() |
| el17arm | 4:b92da6a09c8b | 4 | { |
| el17arm | 4:b92da6a09c8b | 5 | } |
| el17arm | 4:b92da6a09c8b | 6 | |
| el17arm | 14:dc3524d11921 | 7 | Gameengine::~Gameengine() |
| el17arm | 4:b92da6a09c8b | 8 | { |
| el17arm | 4:b92da6a09c8b | 9 | } |
| el17arm | 14:dc3524d11921 | 10 | |
| el17arm | 15:2bda80896a84 | 11 | void Gameengine::game_init() |
| el17arm | 15:2bda80896a84 | 12 | { |
| el17arm | 15:2bda80896a84 | 13 | _level_select = 1; |
| el17arm | 15:2bda80896a84 | 14 | _miner.miner_init(3, 33); |
| el17arm | 18:ff0a53dcb862 | 15 | _lives = 3; |
| el17arm | 15:2bda80896a84 | 16 | } |
| el17arm | 15:2bda80896a84 | 17 | |
| el17arm | 15:2bda80896a84 | 18 | void Gameengine::read_direction(Gamepad &pad) |
| el17arm | 15:2bda80896a84 | 19 | { |
| el17arm | 15:2bda80896a84 | 20 | _d = pad.get_direction(); |
| el17arm | 15:2bda80896a84 | 21 | } |
| el17arm | 15:2bda80896a84 | 22 | |
| el17arm | 15:2bda80896a84 | 23 | void Gameengine::update(N5110 &lcd, Gamepad &pad) |
| el17arm | 19:4789cb4ca550 | 24 | { |
| el17arm | 19:4789cb4ca550 | 25 | t.start(); |
| el17arm | 19:4789cb4ca550 | 26 | read_direction(pad); |
| el17arm | 21:7a7a115d910d | 27 | _miner.miner_move(_d, lcd); |
| el17arm | 15:2bda80896a84 | 28 | _miner.miner_gravity(lcd); |
| el17arm | 15:2bda80896a84 | 29 | _miner.miner_jump(lcd, pad); |
| el17arm | 15:2bda80896a84 | 30 | _miner.miner_land(lcd); |
| el17arm | 18:ff0a53dcb862 | 31 | lose_life(lcd); |
| el17arm | 19:4789cb4ca550 | 32 | game_over(); |
| el17arm | 20:78bd235f8caa | 33 | next_level(lcd); |
| el17arm | 15:2bda80896a84 | 34 | } |
| el17arm | 15:2bda80896a84 | 35 | |
| el17arm | 23:1ee8686af747 | 36 | void Gameengine::draw(Key _k, N5110 &lcd, Gamepad &pad) |
| el17arm | 20:78bd235f8caa | 37 | { |
| el17arm | 18:ff0a53dcb862 | 38 | if (_level_select == 1) { |
| el17arm | 19:4789cb4ca550 | 39 | |
| el17arm | 15:2bda80896a84 | 40 | _miner.miner_draw(lcd); |
| el17arm | 18:ff0a53dcb862 | 41 | _l1.level_platforms(lcd); |
| el17arm | 18:ff0a53dcb862 | 42 | _l1.soft_blocks(lcd); |
| el17arm | 18:ff0a53dcb862 | 43 | _l1.solid_blocks(lcd); |
| el17arm | 24:9c7aca60e374 | 44 | _l1.keys(lcd, pad); |
| el17arm | 18:ff0a53dcb862 | 45 | _l1.keys_collected(); |
| el17arm | 18:ff0a53dcb862 | 46 | _l1.spikes(lcd); |
| el17arm | 15:2bda80896a84 | 47 | } |
| el17arm | 20:78bd235f8caa | 48 | if(_level_select == 2) { |
| el17arm | 20:78bd235f8caa | 49 | |
| el17arm | 20:78bd235f8caa | 50 | _miner.miner_draw(lcd); |
| el17arm | 20:78bd235f8caa | 51 | lcd.drawLine(0,47,83,47,1); |
| el17arm | 21:7a7a115d910d | 52 | //printf("level 2 time = %f \n", t.read()); |
| el17arm | 20:78bd235f8caa | 53 | } |
| el17arm | 15:2bda80896a84 | 54 | } |
| el17arm | 18:ff0a53dcb862 | 55 | |
| el17arm | 18:ff0a53dcb862 | 56 | void Gameengine::lose_life(N5110 &lcd) |
| el17arm | 18:ff0a53dcb862 | 57 | { |
| el17arm | 19:4789cb4ca550 | 58 | |
| el17arm | 18:ff0a53dcb862 | 59 | if (_l1.spikes(lcd) == 1) { |
| el17arm | 18:ff0a53dcb862 | 60 | _lives--; |
| el17arm | 18:ff0a53dcb862 | 61 | _miner.miner_init(3, 33); |
| el17arm | 18:ff0a53dcb862 | 62 | wait(0.5); |
| el17arm | 18:ff0a53dcb862 | 63 | } |
| el17arm | 19:4789cb4ca550 | 64 | } |
| el17arm | 19:4789cb4ca550 | 65 | |
| el17arm | 19:4789cb4ca550 | 66 | bool Gameengine::game_over() |
| el17arm | 19:4789cb4ca550 | 67 | { |
| el17arm | 20:78bd235f8caa | 68 | if(_lives == 0 || t.read() > 60){ |
| el17arm | 19:4789cb4ca550 | 69 | t.stop(); |
| el17arm | 19:4789cb4ca550 | 70 | return true; |
| el17arm | 19:4789cb4ca550 | 71 | } else { |
| el17arm | 19:4789cb4ca550 | 72 | return false; |
| el17arm | 19:4789cb4ca550 | 73 | } |
| el17arm | 20:78bd235f8caa | 74 | } |
| el17arm | 20:78bd235f8caa | 75 | |
| el17arm | 20:78bd235f8caa | 76 | void Gameengine::next_level(N5110 &lcd) |
| el17arm | 20:78bd235f8caa | 77 | { |
| el17arm | 20:78bd235f8caa | 78 | if(_l1.level1_exit(lcd) == true){ |
| el17arm | 21:7a7a115d910d | 79 | t.stop(); |
| el17arm | 20:78bd235f8caa | 80 | _level_select = 2; |
| el17arm | 20:78bd235f8caa | 81 | _miner.miner_init(3,0); |
| el17arm | 21:7a7a115d910d | 82 | printf("Final score is %f \n ",_lives * (60 - t.read())+ (10 * _l1.keys_collected())); |
| el17arm | 20:78bd235f8caa | 83 | } |
| el17arm | 18:ff0a53dcb862 | 84 | } |