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/engine.cpp@8:b3738229ba85, 2019-05-04 (annotated)
- Committer:
- batJoro
- Date:
- Sat May 04 22:46:31 2019 +0000
- Revision:
- 8:b3738229ba85
- Parent:
- 6:4c55dd4b6d42
- Child:
- 9:dc13042b09f5
engine3
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| batJoro | 8:b3738229ba85 | 1 | |
| batJoro | 8:b3738229ba85 | 2 | #include "engine.h" |
| batJoro | 8:b3738229ba85 | 3 | |
| batJoro | 8:b3738229ba85 | 4 | Engine::Engine(){ |
| batJoro | 8:b3738229ba85 | 5 | |
| batJoro | 8:b3738229ba85 | 6 | |
| batJoro | 8:b3738229ba85 | 7 | } |
| batJoro | 8:b3738229ba85 | 8 | |
| batJoro | 8:b3738229ba85 | 9 | Engine::~Engine(){ |
| batJoro | 8:b3738229ba85 | 10 | |
| batJoro | 8:b3738229ba85 | 11 | } |
| batJoro | 8:b3738229ba85 | 12 | |
| batJoro | 8:b3738229ba85 | 13 | void Engine::init(int screenHeight, int screenWidth, int speed){ |
| batJoro | 8:b3738229ba85 | 14 | |
| batJoro | 8:b3738229ba85 | 15 | setSpeed(speed); |
| batJoro | 8:b3738229ba85 | 16 | _screen_height = screenHeight; |
| batJoro | 8:b3738229ba85 | 17 | _screen_width = screenWidth; |
| batJoro | 8:b3738229ba85 | 18 | } |
| batJoro | 8:b3738229ba85 | 19 | |
| batJoro | 8:b3738229ba85 | 20 | void Engine::setSpeed(int speed) { |
| batJoro | 8:b3738229ba85 | 21 | if (speed <= 0) _speed = 0; |
| batJoro | 8:b3738229ba85 | 22 | |
| batJoro | 8:b3738229ba85 | 23 | _speed = speed; |
| batJoro | 8:b3738229ba85 | 24 | } |
| batJoro | 8:b3738229ba85 | 25 | |
| batJoro | 8:b3738229ba85 | 26 | void Engine::update(Gamepad &pad, N5110 &lcd) { |
| batJoro | 8:b3738229ba85 | 27 | |
| batJoro | 8:b3738229ba85 | 28 | clearScreen(lcd); |
| batJoro | 8:b3738229ba85 | 29 | |
| batJoro | 8:b3738229ba85 | 30 | for (int y = 0; y < _screen_height / 2; y++) { |
| batJoro | 8:b3738229ba85 | 31 | for (int x = 0; x < _screen_width; x++) { |
| batJoro | 8:b3738229ba85 | 32 | |
| batJoro | 8:b3738229ba85 | 33 | float fPointCentre = 0.5f; |
| batJoro | 8:b3738229ba85 | 34 | float fRoadSpace = 0.6f; |
| batJoro | 8:b3738229ba85 | 35 | float fSideSpace = fRoadSpace * 0.15f; |
| batJoro | 8:b3738229ba85 | 36 | |
| batJoro | 8:b3738229ba85 | 37 | // for easier calculating the sides |
| batJoro | 8:b3738229ba85 | 38 | fRoadSpace *= 0.5f; |
| batJoro | 8:b3738229ba85 | 39 | |
| batJoro | 8:b3738229ba85 | 40 | int leftGrass = (fPointCentre - fRoadSpace - fSideSpace) * _screen_width; |
| batJoro | 8:b3738229ba85 | 41 | int leftSide = (fPointCentre - fRoadSpace) * _screen_width; |
| batJoro | 8:b3738229ba85 | 42 | int rightGrass = (fPointCentre + fRoadSpace + fSideSpace) * _screen_width; |
| batJoro | 8:b3738229ba85 | 43 | int rightSide = (fPointCentre + fRoadSpace) * _screen_width; |
| batJoro | 8:b3738229ba85 | 44 | |
| batJoro | 8:b3738229ba85 | 45 | int row = _screen_height / 2 + y; |
| batJoro | 8:b3738229ba85 | 46 | |
| batJoro | 8:b3738229ba85 | 47 | if ( x >= 0 && x < leftGrass) { |
| batJoro | 8:b3738229ba85 | 48 | if ( y % 2 == 0 ) |
| batJoro | 8:b3738229ba85 | 49 | lcd.setPixel(x, row, true); |
| batJoro | 8:b3738229ba85 | 50 | } |
| batJoro | 8:b3738229ba85 | 51 | if ( x >= leftGrass && x < leftSide) { |
| batJoro | 8:b3738229ba85 | 52 | if ( x % 2 == 0 ) |
| batJoro | 8:b3738229ba85 | 53 | lcd.setPixel(x, row, true); |
| batJoro | 8:b3738229ba85 | 54 | } |
| batJoro | 8:b3738229ba85 | 55 | if ( x >= rightSide && x < rightGrass ) { |
| batJoro | 8:b3738229ba85 | 56 | if ( x % 2 == 0 ) |
| batJoro | 8:b3738229ba85 | 57 | lcd.setPixel(x, row, true); |
| batJoro | 8:b3738229ba85 | 58 | } |
| batJoro | 8:b3738229ba85 | 59 | if ( x >= rightGrass && x < _screen_width) { |
| batJoro | 8:b3738229ba85 | 60 | if ( y % 2 == 0 ) |
| batJoro | 8:b3738229ba85 | 61 | lcd.setPixel(x, row, true); |
| batJoro | 8:b3738229ba85 | 62 | } |
| batJoro | 8:b3738229ba85 | 63 | } |
| batJoro | 8:b3738229ba85 | 64 | } |
| batJoro | 8:b3738229ba85 | 65 | lcd.refresh(); |
| batJoro | 8:b3738229ba85 | 66 | wait(200); |
| batJoro | 8:b3738229ba85 | 67 | } |
| batJoro | 8:b3738229ba85 | 68 | |
| batJoro | 8:b3738229ba85 | 69 | int Engine::car1[6*9] = { |
| batJoro | 8:b3738229ba85 | 70 | 0,0,0,0,1,0,0,0,0, |
| batJoro | 8:b3738229ba85 | 71 | 0,0,0,1,1,1,0,0,0, |
| batJoro | 8:b3738229ba85 | 72 | 0,0,0,1,1,1,0,0,0, |
| batJoro | 8:b3738229ba85 | 73 | 1,0,1,1,1,1,1,0,1, |
| batJoro | 8:b3738229ba85 | 74 | 1,1,1,1,1,1,1,1,1, |
| batJoro | 8:b3738229ba85 | 75 | 0,0,0,1,0,1,0,0,0 |
| batJoro | 8:b3738229ba85 | 76 | }; |
| batJoro | 8:b3738229ba85 | 77 | |
| batJoro | 8:b3738229ba85 | 78 | int Engine::car2[6*9] = { |
| batJoro | 8:b3738229ba85 | 79 | 0,0,1,0,0,1,1,0,0, |
| batJoro | 8:b3738229ba85 | 80 | 0,1,0,0,1,1,1,0,0, |
| batJoro | 8:b3738229ba85 | 81 | 0,1,1,1,1,1,0,0,0, |
| batJoro | 8:b3738229ba85 | 82 | 1,0,1,1,1,0,0,0,0, |
| batJoro | 8:b3738229ba85 | 83 | 0,0,0,1,1,1,1,0,0, |
| batJoro | 8:b3738229ba85 | 84 | 0,0,1,0,0,0,0,0,0 |
| batJoro | 8:b3738229ba85 | 85 | }; |
| batJoro | 8:b3738229ba85 | 86 | |
| batJoro | 8:b3738229ba85 | 87 | int Engine::car3[6*9] = { |
| batJoro | 8:b3738229ba85 | 88 | 0,0,1,1,0,0,1,0,0, |
| batJoro | 8:b3738229ba85 | 89 | 0,0,1,1,1,0,0,1,0, |
| batJoro | 8:b3738229ba85 | 90 | 0,0,0,1,1,1,1,1,0, |
| batJoro | 8:b3738229ba85 | 91 | 0,0,0,0,1,1,1,0,1, |
| batJoro | 8:b3738229ba85 | 92 | 0,0,1,1,1,1,0,0,0, |
| batJoro | 8:b3738229ba85 | 93 | 0,0,0,0,0,0,1,0,0 |
| batJoro | 8:b3738229ba85 | 94 | }; |