-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.cpp
42 lines (32 loc) · 954 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
42
#include "Enemy.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QList>
#include <stdlib.h> // rand() -> really large int
#include "Game.h"
extern Game * game;
Enemy::Enemy(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent){
//set random x position
int random_number = rand() % 700;
setPos(random_number,0);
// drew the rect
setPixmap(QPixmap(":/static/enemy.png"));
setTransformOriginPoint(50,50);
// setRotation(90);
// make/connect a timer to move() the enemy every so often
QTimer * timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(move()));
// start the timer
timer->start(50);
}
void Enemy::move(){
// move enemy down
setPos(x(),y()+5);
// destroy enemy when it goes out of the screen
if (pos().y() > 600){
//decrease the health
game->health->decrease();
scene()->removeItem(this);
delete this;
}
}