You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.4 KiB
53 lines
1.4 KiB
#include "MyRect.h" |
|
#include <QDebug> |
|
#include <QKeyEvent> |
|
#include "bullet.h" |
|
#include "qgraphicsscene.h" |
|
#include <QDebug> |
|
#include "enemy.h" |
|
#include <QAudioOutput> |
|
#include <QMediaDevices> |
|
|
|
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); |
|
}
|
|
|