-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.cpp
executable file
·50 lines (43 loc) · 1.27 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
#include "player.h"
#include <QGraphicsScene>
#include <QKeyEvent>
#include "bullet.h"
#include "constants.h"
#include "enemy.h"
void Player::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Left){
if(pos().x() > 0){
setPos(x() - PLAYER_MOVE_POINTS, y());
}
}
else if(event->key() == Qt::Key_Right){
if((pos().x() + PLAYER_WIDTH) < SCENE_WIDTH){
setPos(x() + PLAYER_MOVE_POINTS, y());
}
}
else if(event->key() == Qt::Key_Space){
Bullet *bullet = new Bullet();
bullet->setPos(x(),y());
scene()->addItem(bullet);
// if(bulletSound->state() == QMediaPlayer.PlayingState){
// bulletSound->setPosition(0);
// }
// else if(bulletSound->state() == QMediaPlayer.StoppedState){
// //bulletSound->play();
// }
}
}
Player::Player(QGraphicsItem *parent) : QObject(), QGraphicsPixmapItem(parent)
{
bulletSound = new QMediaPlayer();
//bulletSound->setMedia(QUrl("qrc:/sounds/bulletsound.wmv"));
setPixmap(QPixmap(":/images/player.png"));
setTransformOriginPoint(50,50);
setRotation(180);
}
void Player::spawn()
{
Enemy *enemy = new Enemy();
scene()->addItem(enemy);
}