-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy.cpp
73 lines (65 loc) · 2.03 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "enemy.h"
#include "gamecontroller.h"
#include <QPainter>
#include <cmath>
#include <QDebug>
#include <QString>
Enemy::Enemy(GameController &controller):
controller(controller),
speedY(7),
speedX(qrand() % 7 - 3)
{
setData(GD_type, GO_Enemy);
QString str = QString("://images/Enemy%1.png").arg(qrand()%5+1);
pixMap.load(str); //五种敌机图
rad = -(atan(speedX*1.0/speedY) / 3.1415926) * 180.0;
}
QRectF Enemy::boundingRect() const
{
int w = pixMap.width(), h = pixMap.height();
return QRectF(-w/2, -h/2, w, h);
}
void Enemy::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option); Q_UNUSED(widget);
if(!pixMap.isNull()) {
painter->save();
// qDebug() << speedX << " " << speedY << " " << rad;
painter->rotate(rad);
int w = pixMap.width(), h = pixMap.height();
painter->drawPixmap(QPoint(-w/2, -h/2), pixMap);
painter->restore();
}
}
void Enemy::advance(int phace)
{
if(!phace) return;
moveBy(speedX, speedY);
if(!isInView(pos()))
controller.removeItem(this);
int r = qrand() % 600;
if(r < controller.getRank())
controller.shootBall(pos());
handleCollisions();
}
void Enemy::handleCollisions()
{
QList<QGraphicsItem *> collisions = collidingItems();
foreach (QGraphicsItem *item, collisions) {
if(item->data(GD_type) == GO_Bullet) {
controller.updateText(100 + qrand() % 20);
controller.removeItem(this);
controller.ariseCollision(pos());
controller.removeItem(item);
if((qrand()%100) < 25) controller.addLifeAdder(pos());
return;
}
if(item->data(GD_type) == GO_WingBullet) {
controller.updateText(50 + qrand() % 20);
controller.removeItem(this);
controller.ariseCollision(pos());
controller.removeItem(item);
return;
}
}
}