-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathromba.cpp
167 lines (141 loc) · 5.17 KB
/
romba.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
#include "robot.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QKeyEvent>
#include "Game.h"
#include "Dirt.h"
#include "romba.h"
extern Game *game; // there is an external global object called game
Romba::Romba(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent)
{
//set the vacuum sound
vacuumSound = new QMediaPlayer();
vacuumSound->setMedia(QUrl("qrc:/sounds/vacuumSound1.wav"));
//set the engine sound
engineSound = new QMediaPlayer();
engineSound->setMedia(QUrl("qrc:/sounds/rombaSound.wav"));
//set graphic for the robot
setPixmap(QPixmap(":/images/romba.png"));
}
void Romba::keyPressEvent(QKeyEvent *event)
{
// get a list of all the items that robot is vacuuming
QList<QGraphicsItem *> colliding_items = collidingItems();
// if one of the colliding items is a Dirt, vacuum the dirt
for (int i = 0, n = colliding_items.size(); i < n; ++i)
{
if (typeid(*(colliding_items[i])) == typeid(Dirt))
{
game->rombaScore->increase();
//play the vaccum sound
if(vacuumSound->state() == QMediaPlayer::PlayingState)
{
vacuumSound->setPosition(0);
}
else if(vacuumSound->state() == QMediaPlayer::StoppedState)
{
vacuumSound->play();
}
// remove the dirt from the scene (still on the heap)
scene()->removeItem(colliding_items[i]);
// delete the dirt from the heap to save memory
delete colliding_items[i];
if((game->score->getScore() + game->rombaScore->getScore()) == 4)
{
game->gameOver();
}
// return (all code below refers to a non existint bullet)
return;
}
else if(typeid(*(colliding_items[i])) == typeid(Robot))
{
if(pos().x() == game->robot->getRobotX() && pos().y() != game->robot->getRobotY() )
{
if(pos().y() < game->robot->getRobotY())
{
setPos(x(),y()+30);
}
else if(pos().y() > game->robot->getRobotY())
{
setPos(x(),y()-30);
}
}
else if(pos().x() != game->robot->getRobotX() && pos().y() == game->robot->getRobotY())
{
if(pos().x() < game->robot->getRobotX())
{
setPos(x()-30,y());
}
else if(pos().x() > game->robot->getRobotX())
{
setPos(x()+30,y());
}
}
else if(pos().x() != game->robot->getRobotX() && pos().y() != game->robot->getRobotY())
{
if(pos().x() < game->robot->getRobotX() && pos().y() > game->robot->getRobotY())
{
setPos(x()-20,y()+20);
}
else if(pos().x() < game->robot->getRobotX() && pos().y() < game->robot->getRobotY())
{
setPos(x()-20,y()-20);
}
else if(pos().x() > game->robot->getRobotX() && pos().y() < game->robot->getRobotY())
{
setPos(x()+20,y()-20);
}
else if(pos().x() > game->robot->getRobotX() && pos().y() > game->robot->getRobotY())
{
setPos(x()+20,y()+20);
}
}
game->health->decrease();
if(game->health->getHealth() == 0)
{
game->gameOver();
}
}
}
// move the player left and right
if (event->key() == Qt::Key_Left)
{
if (pos().x() > 0)
{
// engineSound->play();
setPos(x()-10,y());
}
}
else if (event->key() == Qt::Key_Right)
{
if (pos().x() < 1100)
{
// engineSound->play();
setPos(x()+10,y());
}
}
else if(event->key() == Qt::Key_Up)
{
if(pos().y() > 60)
{
// engineSound->play();
setPos(x(),y()-10);
}
}
else if(event->key() == Qt::Key_Down)
{
if(pos().y() < 900)
{
// engineSound->play();
setPos(x(),y()+10);
}
}
}
int Romba::getRombaX()
{
return pos().x();
}
int Romba::getRombaY()
{
return pos().y();
}