-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShooter.cpp
42 lines (34 loc) · 1.07 KB
/
Shooter.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
#include "Shooter.h"
#include <QGraphicsScene>
Shooter::Shooter(QTimer *shooterTimer,
QGraphicsItem *parent)
: QObject() , Plant(parent) ,
shooterTimer(shooterTimer) , timeIntervals(0)
{
//set picture
setPixmap(QPixmap(":/images/shooter1.png"));
// create shooter player
shooterPlayer = new QMediaPlayer();
shooterPlayer->setMedia(QUrl("qrc:/musics/throwBullet.mp3"));
// connect shoot slot
connect(shooterTimer , SIGNAL(timeout()) , this , SLOT(shoot()));
}
void Shooter::shoot()
{
// shoot every 500 miliSec
if(timeIntervals % 10 == 0 ){
// create bullet
auto bullet = new Bullet(shooterTimer , 20);
scene()->addItem(bullet);
bullet->setPos(x() + 60 , y() + 15);
// play shooter player
if(shooterPlayer->state() == QMediaPlayer::PlayingState){
shooterPlayer->setPosition(0);
}
else if (shooterPlayer->state() == QMediaPlayer::StoppedState){
shooterPlayer->play();
}
}
// increment timeIntervals
++timeIntervals;
}