-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnorlax.cpp
71 lines (62 loc) · 2.11 KB
/
snorlax.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
/**@file snorlax.cpp
* @brief Contains the implementation for the snorlax class
*/
#include "snorlax.h"
#include "ash.h"
#include "game.h"
#include <QGraphicsScene>
#include <QList>
#include <QTimer>
//external global varaible Game* game
extern Game* game;
/**This constructor sets the Pixmap for Snorlax
* @brief Snorlax::Snorlax sets the Pixmap for a Snorlax object.
* @param parent makes QGraphicsPixmapItem a parent of Snorlax
*/
Snorlax::Snorlax(QGraphicsItem *parent) : QObject(), QGraphicsPixmapItem(parent)
{
setPixmap(QPixmap(":/new/prefix1/Snorlax.gif"));
setScale(.2);
setPos(800,345);
}
/**This constructor sets the Pixmap for Button
* @brief Button::Button sets the Pixmap for a Button object.
* @param parent makes QGraphicsPixmapItem a parent of Button
*/
Button::Button(QGraphicsItem *parent) : QObject(), QGraphicsPixmapItem(parent)
{
setPixmap(QPixmap(":/new/prefix1/Button.png"));
setScale(.13);
setPos(20,270);
// make/connect a timer to turnedOn()
QTimer* timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(turnedOn()));
// start the timer
timer->start(100);
// add button sound
button_sound = new QMediaPlayer();
button_sound->setMedia(QUrl("qrc:/sounds/sound/beep18.mp3"));
}
/**@ implement Ash turning on the button
* @ check if it collides with the player and react to collision
* @ delete if collision
*/
void Button::turnedOn(){
QList<QGraphicsItem*> colliding_items = collidingItems();
// if one of the colliding items is an player, increast the score
for (size_t i = 0, n = colliding_items.size(); i < n; ++i){
if (typeid(*(colliding_items[i])) == typeid(Ash)){
// play button sound
button_sound->play();
// create a new Snorlax
Snorlax* snorlax2 = new Snorlax;
snorlax2->setPos(100,260);
game->scene->addItem(snorlax2);
// remove them from the scene (still on the heap)
scene()->removeItem(this);
// delete them from the heap to save memory
delete this;
return;
}
}
}