#include "MyRect.h" #include #include #include "bullet.h" #include "qgraphicsscene.h" #include #include "enemy.h" #include #include MyRect::MyRect(QGraphicsItem *parent): QGraphicsRectItem(parent){ bulletsound = new QMediaPlayer; audioOutput = new QAudioOutput; //audioOutput->setDevice(QMediaDevices::defaultAudioOutput()); bulletsound->setAudioOutput(audioOutput); //connect(bulletsound, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64))); bulletsound->setSource(QUrl("qrc:/sounds/laser-cannon-shot.wav")); audioOutput->setVolume(50); } void MyRect::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_Left) { if (pos().x() >0) setPos(x() -10, y()); } else if( event->key() == Qt::Key_Right ) { if (pos().x() + 100 < 800) setPos(x() +10, y()); } else if(event->key() == Qt::Key_Space) { Bullet* bullet = new Bullet(); bullet->setPos(x(),y()); scene()->addItem(bullet); //play bulletsound if (bulletsound->playbackState() == QMediaPlayer::PlayingState){ bulletsound->setPosition(0); } else if (bulletsound->playbackState() == QMediaPlayer::StoppedState){ bulletsound->play(); } } } void MyRect::spawn(){ Enemy* enemy = new Enemy(); scene()->addItem(enemy); }