-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
55 lines (43 loc) · 1.52 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
#include "player.h"
#include "bullet.h"
#include <QKeyEvent>
#include <QGraphicsScene>
#include "enemy.h"
#include <QDebug>
Player::Player(QGraphicsItem *parent): QGraphicsPixmapItem(parent)
{
bulletsound = new QMediaPlayer();
bulletsound->setMedia(QUrl("qrc:soundeffects/shot1-1.wav"));
setPixmap(QPixmap(":/images/turret.png"));
}
void Player::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_A || event->key() == Qt::Key_Left) {
if (this->pos().x() > 25)
this->setPos(x()-20, y());
}
else if (event->key() == Qt::Key_D || event->key() == Qt::Key_Right) {
if (this->pos().x() + this->pixmap().width() < 1175)
this->setPos(x()+20, y());
}
else if (event->key() == Qt::Key_Space) {
Bullet * bullet = new Bullet();
bullet->setPos(x() + this->pixmap().width()/2, y());
this->scene()->addItem(bullet);
// if (bulletsound->state() == QMediaPlayer::PlayingState){
if (bulletsound->state() == QMediaPlayer::StoppedState){
qDebug() << "attempted to play bulletsound from stopped state" << bulletsound->state() << bulletsound->error() << bulletsound->mediaStatus();
bulletsound->play();
}
else
{
qDebug() << "attempted to play bulletsound otherwise " << bulletsound->state() << bulletsound->error() << bulletsound->mediaStatus();
bulletsound->stop();
}
}
}
void Player::spawn()
{
Enemy * enemy = new Enemy();
scene()->addItem(enemy);
}