-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
56 lines (49 loc) · 1.46 KB
/
player.cpp
File metadata and controls
56 lines (49 loc) · 1.46 KB
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
#include "player.h"
#include <QGraphicsScene>
#include <QKeyEvent>
#include "bullet.h"
#include "enemy.h"
#include <QDebug>
Player::Player(QGraphicsItem *parent): QGraphicsPixmapItem(parent){
bulletsound = new QMediaPlayer();
bulletsound->setMedia(QUrl("qrc:/sounds/bullet3.mp3"));
// set graphic
setPixmap(QPixmap(":/images/player.gif"));
}
void Player::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_Up){
setPos(x(),y()-10);
}
else if (event->key() == Qt::Key_Down){
setPos(x(),y()+10);
}*/
else if (event->key() == Qt::Key_Space){
// create a bullet
Bullet * bullet = new Bullet();
bullet->setPos(x()+90,y());
Bullet * bullet2 = new Bullet();
bullet2->setPos(x()+150,y());
scene()->addItem(bullet);
scene()->addItem(bullet2);
//play bulletsound
if(bulletsound->state() == QMediaPlayer::PlayingState){
bulletsound->setPosition(0);
}
else if(bulletsound->state()==QMediaPlayer::StoppedState){
bulletsound->play();
}
}
}
void Player::spawn(){
// create an enemy
Enemy * enemy = new Enemy();
scene()->addItem(enemy);
}