-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
79 lines (67 loc) · 1.92 KB
/
player.cpp
File metadata and controls
79 lines (67 loc) · 1.92 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "Player.h"
#include <QGraphicsScene>
#include <QKeyEvent>
#include "Bullet.h"
#include "Enemy.h"
#include "healthpack.h"
#include <QTimer>
#include <game.h>
#include <QList>
#include <TypeInfo>
extern Game * game;
Player::Player(QGraphicsItem *parent): QGraphicsPixmapItem(parent)
{
bulletsound = new QMediaPlayer();
bulletsound->setMedia(QUrl("qrc:/sounds/Bullet_sound.mp3"));
setPixmap(QPixmap(":/images/player.png"));
}
void Player::keyPressEvent(QKeyEvent *event)
{
int SPEED = 15;
if (event->key() == Qt::Key_Left)
{
if (pos().x() > 0)
setPos(x()-SPEED,y());
}
else if (event->key() == Qt::Key_Right)
{
if (pos().x() + game->player->pixmap().width() < 800)
setPos(x()+SPEED,y());
}
else if (event->key() == Qt::Key_Space)
{
if (can_shoot)
{
reload_timer->start(500);
can_shoot = false;
Bullet * bullet = new Bullet();
bullet->setPos(x()+45,y());
scene()->addItem(bullet);
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::spawn_healthpack()
{
//Добавить условие, что если уже существует healthpack, то удалить его и поставить новый, в другом месте
QList<QGraphicsItem *> items = scene()->items();
for (int i = 0, n = items.size(); i < n; ++i){
if (typeid(*(items[i])) == typeid(HealthPack))
return;
}
HealthPack * healthpack = new HealthPack();
scene()->addItem(healthpack);
}
void Player::set_reload()
{
reload_timer->stop();
can_shoot = true;
}