-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy.cpp
41 lines (29 loc) · 819 Bytes
/
enemy.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
#include <QTimer>
#include <QGraphicsScene>
#include <QDebug>
#include <memory>
#include <stdlib.h> //rand()
#include "enemy.h"
#include "game.h"
extern std::unique_ptr<Game> game;
Enemy::Enemy(){
int randomNumber = rand() % 700;
setPos(randomNumber, 0);
setPixmap(QPixmap(":/images/enemy.png"));
setTransformOriginPoint(pixmap().width()/2, pixmap().height()/2);
qreal rotationAngle = 180.0;
setRotation(rotationAngle);
//connect to signal
auto timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(move()));
timer->start(50);
}
void Enemy::move(){
setPos(x(), y()+5);
const auto SCREEN_BOTTOM = scene()->height();
if(pos().y() > SCREEN_BOTTOM){
scene()->removeItem(this);
game->decreaseHealth();
delete this;
}
}