-
Notifications
You must be signed in to change notification settings - Fork 0
/
myplayer.cpp
48 lines (42 loc) · 1.21 KB
/
myplayer.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
#include "myplayer.h"
#include<QDebug>
#include <QKeyEvent>
#include<QGraphicsScene>
#include<QGraphicsItem>
#include<QMediaPlayer>
#include "bullet.h"
#include "myplayer.h"
#include "enemy.h"
MyPlayer::MyPlayer(QGraphicsItem *parent): QGraphicsPixmapItem(parent)
{
bulletSound = new QMediaPlayer();
bulletSound->setMedia(QUrl("qrc:/new/sounds/resource/sounds/Cannon.mp3"));
QPixmap pixmap(":/new/images/resource/images/player.png");
setPixmap(pixmap.scaled(50,50));
}
void MyPlayer::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()+25,y());
scene()->addItem(bullet);
if (bulletSound->state() == QMediaPlayer::PlayingState){
bulletSound->setPosition(0);
} else if (bulletSound->state() == QMediaPlayer::StoppedState){
bulletSound->play();
}
}
}
void MyPlayer::spawn()
{
Enemy * enemy = new Enemy();
scene()->addItem(enemy);
}