-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
58 lines (48 loc) · 1.42 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
51
52
53
54
55
56
57
#include "Player.h"
#include <QKeyEvent>
#include <QGraphicsScene>
#include "Bullet.h"
#include "Enemy.h"
#include <QGraphicsTextItem>
Player::Player(QGraphicsItem *parent): QGraphicsPixmapItem(parent){
bulletsound = new QMediaPlayer();
bulletsound->setMedia(QUrl("qrc:/sounds/bullet.mp3"));
//set graphics
setPixmap(QPixmap(":/images/Player2.png"));
}
void Player::keyPressEvent(QKeyEvent * event){
//move player left or right
if (event->key() == Qt::Key_Left){
if(pos().x() > 0)
setPos(x()-20,y());
}
else if (event->key() == Qt::Key_Right){
if(pos().x() < 700)
setPos(x()+10,y());
}
else if (event->key() == Qt::Key_Space){
if (bullets > 0){
Bullet * bullet = new Bullet();
bullet->setPos(x()+40,y());
scene()->addItem(bullet);
//play bullet sound
if (bulletsound->state() == QMediaPlayer::PlayingState){
bulletsound->setPosition(0);
}
else if (bulletsound->state() == QMediaPlayer::StoppedState){
bulletsound->play();
}
}
}
}
void Player::spawn(){
Enemy * enemy = new Enemy();
scene()->addItem(enemy);
}
void Player::endgame(){
bullets = 0;
QGraphicsTextItem * text = new QGraphicsTextItem();
text->setPos(350,300);
text->setPlainText("GAME OVER!");
scene()->addItem(text);
}