-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
233 lines (221 loc) · 8.02 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "Player.h"
#include<QKeyEvent>
#include<iostream>
#include<GameWindow.h>
#include<Vehicle.h>
#include<QDateTime>
#include<qstring.h>
#include<Lane.h>
#include<Score.h>
Player::Player(GameWindow * game): gameWindow(game)
{
setPixmap(QPixmap(":/resources/images/zaba_gotowa.png"));
setPos(388,567);
setFlag(QGraphicsItem::ItemIsFocusable);
setData(0,GameWindow::itemType::frog);
setData(1,alive);
setFocus();
gameWindow->addToScene(this);
collDetectionTimer = std::make_unique<QTimer>();
QObject::connect(collDetectionTimer.get(),SIGNAL(timeout()), this, SLOT(collisionDetection()));
collDetectionTimer.get()->start(50);
lives = 3;
hearts = std::make_unique<QGraphicsPixmapItem>();
hearts.get()->setPixmap(QPixmap(":/resources/images/life3.png"));
hearts.get()->setPos(10,1);
gameWindow->addToScene(hearts.get());
jumpSound = std::make_unique<QMediaPlayer>();
deathSound = std::make_unique<QMediaPlayer>();
switchSound = std::make_unique<QMediaPlayer>();
endSound = std::make_unique<QMediaPlayer>();
jumpSound.get()->setMedia(QUrl("qrc:/resources/sounds/jump.wav"));
jumpSound.get()->setVolume(1);
deathSound.get()->setMedia(QUrl("qrc:/resources/sounds/rozjechanie.wav"));
deathSound.get()->setVolume(1);
switchSound.get()->setMedia(QUrl("qrc:/resources/sounds/switch.wav"));
switchSound.get()->setVolume(5);
endSound.get()->setMedia(QUrl("qrc:/resources/sounds/death.wav"));
endSound.get()->setVolume(1);
}
void Player::keyPressEvent(QKeyEvent *event) // controls
{
if(!event->isAutoRepeat())
{
if(!(gameWindow->isItemVisible(GameWindow::itemType::menuStart) ||
gameWindow->isItemVisible(GameWindow::itemType::menuPause) ||
gameWindow->isItemVisible(GameWindow::itemType::menuEnd) ||
data(1) == dead)) //controls outside the menu
{
switch (event->key())
{
case Qt::Key_Left:
if(pos().x() > 24)
setPos(x() - 24, y());
jumpSound.get()->setPosition(0);
jumpSound->play();
break;
case Qt::Key_Right:
if(pos().x() < 755)
setPos(x() + 24, y());
jumpSound.get()->setPosition(0);
jumpSound->play();
break;
case Qt::Key_Up:
if(pos().y() > 0)
setPos(x(), y() - 24);
jumpSound.get()->setPosition(0);
jumpSound->play();
if(int(pos().y()) < 0)
levelUp();
break;
case Qt::Key_Down:
if(pos().y() < 567)
setPos(x(), y() + 24);
jumpSound.get()->setPosition(0);
jumpSound->play();
break;
case Qt::Key_Escape:
gameWindow->displayMenu(GameWindow::itemType::menuPause);
break;
}
}
else if (data(1) != dead) //controls in the menu
{
switch (event->key())
{
case Qt::Key_Up:
gameWindow->moveCursor(GameWindow::direction::up);
switchSound.get()->setPosition(0);
switchSound->play();
break;
case Qt::Key_Down:
gameWindow->moveCursor(GameWindow::direction::down);
switchSound.get()->setPosition(0);
switchSound->play();
break;
case Qt::Key_Escape:
gameWindow->removeMenu();
break;
case Qt::Key_Return:
if(gameWindow->isItemVisible(GameWindow::itemType::menuStart))
{
gameWindow->removeMenu();
switch(gameWindow->getCursorPosition())
{
case 1: //start
break;
case 2: //best scores
Score::LoadScores();
gameWindow->displayMenu(GameWindow::menuStart);
break;
case 3: //exit
gameWindow->close();
break;
}
}
else if(gameWindow->isItemVisible(GameWindow::itemType::menuPause))
{
gameWindow->removeMenu();
switch(gameWindow->getCursorPosition())
{
case 1: //resume game
break;
case 2: //return to main menu
gameWindow->displayMenu(GameWindow::itemType::menuStart);
restart();
Score::resetLevel();
break;
case 3: //exit
gameWindow->close();
break;
}
}
else if(gameWindow->isItemVisible(GameWindow::itemType::menuEnd))
{
gameWindow->removeMenu();
switch(gameWindow->getCursorPosition())
{
case 1: //play again
restart();
Score::resetLevel();
break;
case 2: //return to main menu
gameWindow->displayMenu(GameWindow::itemType::menuStart);
restart();
Score::resetLevel();
break;
case 3: //exit
gameWindow->close();
break;
}
}
break;
}
}
}
}
void Player::decreaseLife()
{
QString path;
path = QString::fromStdString(":/resources/images/life") + QString::number(lives) + QString::fromStdString(".png");
hearts->setPixmap(QPixmap(path));
}
void Player::collisionDetection()
{
QList<QGraphicsItem * > collidingItems = this->collidingItems();
if(!(gameWindow->isItemVisible(GameWindow::itemType::menuStart)||
gameWindow->isItemVisible(GameWindow::itemType::menuStart))
&& data(1) == alive)
{
for(int i=0; i<collidingItems.size();i++)
{
if(collidingItems[i]->data(0) == GameWindow::itemType::vehicle)
{
deathSound.get()->play();
this->setPos(this->x()-5, this->y()); // so make the new axis of symmetry match the previous pixmap's
this->setPixmap(QPixmap(":/resources/images/przejechana.png"));
this->setData(1,dead);
timeOfDeath = QDateTime::currentMSecsSinceEpoch();
--lives;
decreaseLife();
}
}
}
else if(data(1) == dead && lives > 0 &&
QDateTime::currentMSecsSinceEpoch() - timeOfDeath > 1000)
{
setPixmap(QPixmap(":/resources/images/zaba_gotowa.png"));
this->setData(1,alive);
setPos(388,567);
}
else if(lives == 0 &&
QDateTime::currentMSecsSinceEpoch() - timeOfDeath < 70) /* resetting difficulty before spawning the Player to
allow the slowed down cars to fill the scene*/
{
endSound.get()->play();
Lane::resetDifficulty();
}
else if(data(1) == dead && lives == 0 &&
QDateTime::currentMSecsSinceEpoch() - timeOfDeath > 3000)
{
qDebug() << "before save";
Score::saveScore();
gameWindow->displayMenu(GameWindow::itemType::menuEnd);
restart();
}
}
void Player::restart()
{
lives = 3;
hearts.get()->setPixmap(QPixmap(":/resources/images/life3.png"));
Score::resetLevel();
setPixmap(QPixmap(":/resources/images/zaba_gotowa.png"));
setPos(388,567);
this->setData(1,alive);
}
void Player::levelUp()
{
Score::increaseScore();
Lane::increaseDifficulty();
setPos(388,567);
}