Basic game using accelerometer and LCD screen. You can move and dodge asteroids coming from both left and right. There is no scoring at the moment.
Dependencies: C12832 FXOS8700Q mbed-rtos mbed
Player.cpp@2:9e0c826103d7, 2016-02-17 (annotated)
- Committer:
- co838_gtvl2
- Date:
- Wed Feb 17 10:14:33 2016 +0000
- Revision:
- 2:9e0c826103d7
- Parent:
- 1:c6734b909bf0
Unrealisable because memory in cpp isn't that good :(
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
co838_gtvl2 | 0:9ec880239b3c | 1 | #include "Player.h" |
co838_gtvl2 | 0:9ec880239b3c | 2 | |
co838_gtvl2 | 0:9ec880239b3c | 3 | Player::Player() |
co838_gtvl2 | 0:9ec880239b3c | 4 | { |
co838_gtvl2 | 0:9ec880239b3c | 5 | this->x = DISPLAY_X / 2; |
co838_gtvl2 | 0:9ec880239b3c | 6 | this->y = DISPLAY_Y / 2; |
co838_gtvl2 | 0:9ec880239b3c | 7 | this->alive = true; |
co838_gtvl2 | 0:9ec880239b3c | 8 | } |
co838_gtvl2 | 0:9ec880239b3c | 9 | |
co838_gtvl2 | 0:9ec880239b3c | 10 | bool Player::move(const int &x, const int &y) |
co838_gtvl2 | 0:9ec880239b3c | 11 | { |
co838_gtvl2 | 0:9ec880239b3c | 12 | bool okMove = true; |
co838_gtvl2 | 0:9ec880239b3c | 13 | float newX = this->x + static_cast<float>(x) / DELTA_MOVE; |
co838_gtvl2 | 0:9ec880239b3c | 14 | float newY = this->y + static_cast<float>(y) / DELTA_MOVE; |
co838_gtvl2 | 0:9ec880239b3c | 15 | |
co838_gtvl2 | 0:9ec880239b3c | 16 | // TODO, opti this but clearer at the moment |
co838_gtvl2 | 0:9ec880239b3c | 17 | if (newX < 0 || newY < 0) { |
co838_gtvl2 | 0:9ec880239b3c | 18 | newX = (newX < 0 ? 0 : newX); |
co838_gtvl2 | 0:9ec880239b3c | 19 | newY = (newY < 0 ? 0 : newY); |
co838_gtvl2 | 0:9ec880239b3c | 20 | okMove = false; |
co838_gtvl2 | 0:9ec880239b3c | 21 | } |
co838_gtvl2 | 0:9ec880239b3c | 22 | |
co838_gtvl2 | 0:9ec880239b3c | 23 | if (newX > DISPLAY_X - 1 || newY > DISPLAY_Y - 1) { |
co838_gtvl2 | 0:9ec880239b3c | 24 | newX = (newX > DISPLAY_X - 1 ? DISPLAY_X - 1 : newX); |
co838_gtvl2 | 0:9ec880239b3c | 25 | newY = (newY > DISPLAY_Y - 1 ? DISPLAY_Y - 1 : newY); |
co838_gtvl2 | 0:9ec880239b3c | 26 | okMove = false; |
co838_gtvl2 | 0:9ec880239b3c | 27 | } |
co838_gtvl2 | 0:9ec880239b3c | 28 | |
co838_gtvl2 | 0:9ec880239b3c | 29 | this->x = newX; |
co838_gtvl2 | 0:9ec880239b3c | 30 | this->y = newY; |
co838_gtvl2 | 0:9ec880239b3c | 31 | return okMove; |
co838_gtvl2 | 0:9ec880239b3c | 32 | } |
co838_gtvl2 | 0:9ec880239b3c | 33 | |
co838_gtvl2 | 0:9ec880239b3c | 34 | bool Player::die() |
co838_gtvl2 | 0:9ec880239b3c | 35 | { |
co838_gtvl2 | 0:9ec880239b3c | 36 | this->alive = false; |
co838_gtvl2 | 0:9ec880239b3c | 37 | } |
co838_gtvl2 | 0:9ec880239b3c | 38 | |
co838_gtvl2 | 0:9ec880239b3c | 39 | bool Player::isAlive() const |
co838_gtvl2 | 0:9ec880239b3c | 40 | { |
co838_gtvl2 | 1:c6734b909bf0 | 41 | return this->alive; |
co838_gtvl2 | 0:9ec880239b3c | 42 | } |
co838_gtvl2 | 0:9ec880239b3c | 43 | |
co838_gtvl2 | 0:9ec880239b3c | 44 | int Player::getX() const |
co838_gtvl2 | 0:9ec880239b3c | 45 | { |
co838_gtvl2 | 0:9ec880239b3c | 46 | return static_cast<int>(this->x); |
co838_gtvl2 | 0:9ec880239b3c | 47 | } |
co838_gtvl2 | 0:9ec880239b3c | 48 | |
co838_gtvl2 | 0:9ec880239b3c | 49 | int Player::getY() const |
co838_gtvl2 | 0:9ec880239b3c | 50 | { |
co838_gtvl2 | 0:9ec880239b3c | 51 | return static_cast<int>(this->y); |
co838_gtvl2 | 0:9ec880239b3c | 52 | } |