-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunderworldplayer.cpp
More file actions
70 lines (62 loc) · 1.84 KB
/
underworldplayer.cpp
File metadata and controls
70 lines (62 loc) · 1.84 KB
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
#include "underworldplayer.h"
#include <QKeyEvent>
UnderworldPlayer::UnderworldPlayer(QPixmap &pixmap): QObject(), QGraphicsPixmapItem(pixmap)
{
xprev_ = pos().x();
yprev_ = pos().y();
setPos(200, 200);
}
void UnderworldPlayer::keyPressEvent(QKeyEvent *event){
if(
event->key() == Qt::Key_W ||
event->key() == Qt::Key_A ||
event->key() == Qt::Key_S ||
event->key() == Qt::Key_D
)
{
keysPressed.insert(event->key());
}
int STEP_SIZE = 10;
//if only 1 key is being pressed, simply move in that direction
if (keysPressed.size() == 1){
switch (event->key()){
case Qt::Key_D:
setPos(x()+STEP_SIZE,y());
break;
case Qt::Key_A:
setPos(x()-STEP_SIZE,y());
break;
case Qt::Key_W:
setPos(x(),y()-STEP_SIZE);
break;
case Qt::Key_S:
setPos(x(),y()+STEP_SIZE);
break;
}
}
//if two keys are being pressed, move diagonally
if (keysPressed.size() > 1){
// up right
if (keysPressed.contains(Qt::Key_W) && keysPressed.contains(Qt::Key_D)){
setPos(x()+STEP_SIZE,y()-STEP_SIZE);
}
// up left
if (keysPressed.contains(Qt::Key_W) && keysPressed.contains(Qt::Key_A)){
setPos(x()-STEP_SIZE,y()-STEP_SIZE);
}
// down right
if (keysPressed.contains(Qt::Key_S) && keysPressed.contains(Qt::Key_D)){
setPos(x()+STEP_SIZE,y()+STEP_SIZE);
}
// down left
if (keysPressed.contains(Qt::Key_S) && keysPressed.contains(Qt::Key_A)){
setPos(x()-STEP_SIZE,y()+STEP_SIZE);
}
}
xprev_ = pos().x();
yprev_ = pos().y();
}
void UnderworldPlayer::keyReleaseEvent(QKeyEvent *event)
{
keysPressed.remove(event->key());
}