-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
executable file
·84 lines (71 loc) · 2.11 KB
/
player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "player.h"
#include "audio.h"
#include "appdialog.h"
#include <QDebug>
Player::Player(QSize size, QPoint startPoint, QPoint minXY, QPoint maxXY, \
int horizontalMove, int verticalMove, int maxLanes, QGraphicsItem *parent)
: QGraphicsPixmapItem(parent),
playerSize(size),
initPos(startPoint),
minXY(minXY),
maxXY(maxXY),
maxLanes(maxLanes),
horizontalMovementOffset(horizontalMove),
verticalMovementOffset(verticalMove)
{
currentLane = 1;
createPlayer();
}
void Player::createPlayer() {
QPixmap pixmap(":/images/player.png");
setPixmap(pixmap.scaled(playerSize));
resetPlayer();
}
int Player::getCurrentLane() {
return currentLane;
}
void Player::moveUp() {
if (y() - verticalMovementOffset >= minXY.y() && currentLane + 1 <= maxLanes) {
setPos(x(), y() - verticalMovementOffset);
currentLane++;
}
}
void Player::moveDown() {
if (y() + verticalMovementOffset <= maxXY.y() && currentLane - 1 > 0) {
setPos(x(), y() + verticalMovementOffset);
currentLane--;
}
}
void Player::moveLeft() {
if (x() - horizontalMovementOffset >= minXY.x()) {
setPos(x() - horizontalMovementOffset, y());
}
}
void Player::moveRight() {
if (x() + horizontalMovementOffset <= maxXY.x()) {
setPos(x() + horizontalMovementOffset, y());
}
}
void Player::resetPlayer() {
currentLane = 1;
setPos(initPos.x(), initPos.y());
}
void Player::setPlayerSpeed(int speed) {
playerSpeed = speed;
}
int Player::getPlayerSpeed() {
return playerSpeed;
}
void Player::updatePos() {
setPos(x() - playerSpeed, y());
if (x() < -15) {
Audio::playSound("die");
if (AppDialog::informationPopup("You died.", "No....", "Quit") == QMessageBox::Ok)
QCoreApplication::quit();
}
else if (x() > maxXY.x() + 15) {
Audio::playSound("die");
if (AppDialog::informationPopup("You died.", "No....", "Quit") == QMessageBox::Ok)
QCoreApplication::quit();
}
}