-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbullet.cpp
More file actions
57 lines (44 loc) · 1.25 KB
/
bullet.cpp
File metadata and controls
57 lines (44 loc) · 1.25 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
#include <bullet.h>
#include <QPixmap>
#include <QTimer>
#include <qmath.h>
#include <game.h>
#include <QList>
#include "enemy.h"
#include <typeinfo.h>
extern Game *game;
Bullet::Bullet(QGraphicsItem *parent)
{
//set graphics
setPixmap(QPixmap(":/images/arrow.png"));
//timer to move()
QTimer *move_timer = new QTimer(this);
connect(move_timer,SIGNAL(timeout()),this,SLOT(move()));
move_timer->start(50);
//initialize values
maxRange = 100;
distanceTravelled = 0;
}
void Bullet::move()
{
//if bullets collide, destroy the enemey
QList<QGraphicsItem *> colliding_items = collidingItems();
for(int i = 0,n=colliding_items.size();i<n;i++){
if(typeid(*(colliding_items[i]))==typeid(Enemy)){
//increase score
game->score->increase();
//remove both
scene()->removeItem(colliding_items[i]);
scene()->removeItem(this);
//delete both
delete colliding_items[i];
delete this;
return;
}
}
int STEP_SIZE = 3;
double theta = rotation(); //degres
double dy = STEP_SIZE * qSin(qDegreesToRadians(theta));
double dx = STEP_SIZE * qCos(qDegreesToRadians(theta));
setPos(x()+dx,y()+dy);
}