A game for Lab 4 of ECE 4180
Dependencies: 4DGL-uLCD-SE LSM9DS1_Library SDFileSystem mbed-rtos mbed wave_player
Player.cpp@5:68f6014f10bd, 2016-03-14 (annotated)
- Committer:
- Dogstopper
- Date:
- Mon Mar 14 18:54:12 2016 +0000
- Revision:
- 5:68f6014f10bd
- Parent:
- 3:27889fffc2f7
Final;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Dogstopper | 0:6a49493943be | 1 | #include "Player.h" |
Dogstopper | 0:6a49493943be | 2 | |
Dogstopper | 0:6a49493943be | 3 | #define WIDTH 10 |
Dogstopper | 0:6a49493943be | 4 | #define HEIGHT 10 |
Dogstopper | 0:6a49493943be | 5 | |
Dogstopper | 0:6a49493943be | 6 | Player::Player(uLCD_4DGL* screenInit, InputHandler* input, int startX, int startY) |
Dogstopper | 0:6a49493943be | 7 | { |
Dogstopper | 0:6a49493943be | 8 | screen = screenInit; |
Dogstopper | 0:6a49493943be | 9 | x = startX; |
Dogstopper | 0:6a49493943be | 10 | y = startY; |
Dogstopper | 3:27889fffc2f7 | 11 | lastX = x-1; |
Dogstopper | 0:6a49493943be | 12 | lastY = y; |
Dogstopper | 0:6a49493943be | 13 | |
Dogstopper | 0:6a49493943be | 14 | inputManager = input; |
Dogstopper | 0:6a49493943be | 15 | } |
Dogstopper | 0:6a49493943be | 16 | |
Dogstopper | 3:27889fffc2f7 | 17 | void Player::update(int windowWidth, int windowHeight) |
Dogstopper | 0:6a49493943be | 18 | { |
Dogstopper | 0:6a49493943be | 19 | float xAccel = inputManager->getXAccel() * 4; |
Dogstopper | 0:6a49493943be | 20 | float yAccel = inputManager->getYAccel() * 4; |
Dogstopper | 0:6a49493943be | 21 | |
Dogstopper | 0:6a49493943be | 22 | int speedX = (int)(xAccel + (xAccel > 0 ? 0.5 : -0.5)) ; // Maximum Speed of 4 |
Dogstopper | 0:6a49493943be | 23 | int speedY = (int)(yAccel + (yAccel > 0 ? 0.5 : -0.5)) ; // Maximum Speed of 4 |
Dogstopper | 0:6a49493943be | 24 | |
Dogstopper | 0:6a49493943be | 25 | x += speedX; |
Dogstopper | 0:6a49493943be | 26 | y += speedY; |
Dogstopper | 3:27889fffc2f7 | 27 | |
Dogstopper | 3:27889fffc2f7 | 28 | // Adjust to be in the window |
Dogstopper | 3:27889fffc2f7 | 29 | if (x <= WIDTH/2) x = WIDTH/2; |
Dogstopper | 3:27889fffc2f7 | 30 | if (y <= WIDTH/2) y = HEIGHT/2; |
Dogstopper | 3:27889fffc2f7 | 31 | if (x + WIDTH/2 >= windowWidth) x = windowWidth - WIDTH/2; |
Dogstopper | 3:27889fffc2f7 | 32 | if (y + HEIGHT/2 >= windowHeight) y = windowHeight - HEIGHT/2; |
Dogstopper | 0:6a49493943be | 33 | } |
Dogstopper | 0:6a49493943be | 34 | |
Dogstopper | 0:6a49493943be | 35 | void Player::draw() |
Dogstopper | 0:6a49493943be | 36 | { |
Dogstopper | 0:6a49493943be | 37 | screen_mutex.lock(); |
Dogstopper | 3:27889fffc2f7 | 38 | if (lastX != x || lastY != y) { |
Dogstopper | 3:27889fffc2f7 | 39 | screen->filled_circle(lastX, lastY, HEIGHT, BLACK); |
Dogstopper | 3:27889fffc2f7 | 40 | screen->filled_circle(x, y, HEIGHT/2, GREEN); |
Dogstopper | 3:27889fffc2f7 | 41 | } |
Dogstopper | 3:27889fffc2f7 | 42 | lastX = x; |
Dogstopper | 3:27889fffc2f7 | 43 | lastY = y; |
Dogstopper | 0:6a49493943be | 44 | screen_mutex.unlock(); |
Dogstopper | 0:6a49493943be | 45 | } |
Dogstopper | 0:6a49493943be | 46 | |
Dogstopper | 3:27889fffc2f7 | 47 | // Bounding Box x |
Dogstopper | 0:6a49493943be | 48 | int Player::getX() |
Dogstopper | 0:6a49493943be | 49 | { |
Dogstopper | 3:27889fffc2f7 | 50 | return x - WIDTH/2; |
Dogstopper | 0:6a49493943be | 51 | } |
Dogstopper | 0:6a49493943be | 52 | |
Dogstopper | 0:6a49493943be | 53 | int Player::getY() |
Dogstopper | 0:6a49493943be | 54 | { |
Dogstopper | 3:27889fffc2f7 | 55 | return y - HEIGHT/2; |
Dogstopper | 0:6a49493943be | 56 | } |
Dogstopper | 0:6a49493943be | 57 | |
Dogstopper | 0:6a49493943be | 58 | int Player::getWidth() |
Dogstopper | 0:6a49493943be | 59 | { |
Dogstopper | 0:6a49493943be | 60 | return WIDTH; |
Dogstopper | 0:6a49493943be | 61 | } |
Dogstopper | 0:6a49493943be | 62 | |
Dogstopper | 0:6a49493943be | 63 | int Player::getHeight() |
Dogstopper | 0:6a49493943be | 64 | { |
Dogstopper | 0:6a49493943be | 65 | return HEIGHT; |
Dogstopper | 0:6a49493943be | 66 | } |
Dogstopper | 0:6a49493943be | 67 |