-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.cpp
43 lines (33 loc) · 1 KB
/
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
43
#include "Enemy.h"
#include <stdlib.h>
#include <QGraphicsItem>
#include <QTimer>
#include "Game.h"
extern Game * game;
Enemy::Enemy(QGraphicsItem *parent): QObject(),QGraphicsRectItem(parent){
//random number for random spawn location for enemies
int spawn_location = rand() % 700;
setPos(spawn_location,0);
//draw the enemy
setRect(0,0,100,100);
// move the enemy down (defined in class, slower compared to bullet)
QTimer * timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(move()));
// move effect
timer->start(50);
//connect the enemy to player's endgame
connect(this,SIGNAL(gameend()),game->player,SLOT(endgame()));
}
void Enemy::move(){
setPos(x(),y()+5);
//if enemy reaches bottom of the screen decrease health and remove enemy
if(pos().y() > 600){
game->health->decreaseHealth();
scene()->removeItem(this);
delete this;
}
int n=game->health->getHealth();
if (n == 0){
emit gameend();
}
}