-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaser.cpp
More file actions
46 lines (42 loc) · 1.11 KB
/
Copy pathlaser.cpp
File metadata and controls
46 lines (42 loc) · 1.11 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
#include "laser.h"
#include <QBrush>
#include <QTimer>
#include <QGraphicsScene>
#include "enemy.h"
#include "ufo.h"
#include <iostream>
using namespace std;
extern bool laserOnScreen;
Laser::Laser(int posX, int posY)
{
this->width = 4;
this->height = 10;
setRect(0, 0, width, height);
setPos(posX, posY);
setBrush(QBrush(Qt::green));
QTimer *timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(move()));
timer->start(50);
}
void Laser::move(){
QList<QGraphicsItem*> collidingItemsList = this->collidingItems();
bool isDelete = false;
foreach(QGraphicsItem *item, collidingItemsList){
if(typeid(*item) == typeid (Enemy) || typeid(*item) == typeid (Ufo)){
this->scene()->removeItem(item);
this->scene()->removeItem(this);
delete item;
delete this;
isDelete = true;
laserOnScreen = false;
}
}
if(!isDelete){
setPos(x(), y()-10);
if(y() <= 0) {
this->scene()->removeItem(this);
delete this;
laserOnScreen = false;
}
}
}