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.
 
 

57 lines
1.5 KiB

#include "MyRect.h"
#include <QDebug>
#include <QKeyEvent>
#include "bullet.h"
#include "qgraphicsscene.h"
#include <QDebug>
#include "enemy.h"
#include <QAudioOutput>
#include <QMediaDevices>
#include <QAudioDevice>
MyRect::MyRect(QGraphicsItem *parent): QGraphicsPixmapItem(parent){
bulletsound = new QMediaPlayer;
audioOutput = new QAudioOutput;
const QAudioDevice tempAudi = QAudioDevice(QMediaDevices::defaultAudioOutput());
audioOutput->setDevice(tempAudi);
bulletsound->setAudioOutput(audioOutput);
//connect(bulletsound, SIGNAL(positionChanged(qint64)), audioOutput, SLOT(positionChanged(qint64)));
bulletsound->setSource(QUrl("qrc:/sounds/laser-cannon-shot.wav"));
audioOutput->setVolume(50);
setPixmap(QPixmap(":/images/player.png"));
}
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()+45,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);
}