-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyplane.cpp
112 lines (100 loc) · 3.1 KB
/
myplane.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "myplane.h"
#include <QKeyEvent>
#include <QDebug>
#include <QtAlgorithms>
#include <QList>
#include <QDebug>
#include <QtAlgorithms>
MyPlane::MyPlane(GameController &controller):
controller(controller)
{
setData(GD_type, GO_MyPlane);
pixMap.load("://images/MyPlane2.png");
speedX = speedY = isFireing = 0;
gradient = new QLinearGradient(QPoint(-50, -50), QPoint(50, 50));
gradient->setColorAt(0, Qt::red);
gradient->setColorAt(0.2, Qt::yellow);
gradient->setColorAt(0.7, Qt::blue);
gradient->setColorAt(1, Qt::green);
}
QRectF MyPlane::boundingRect() const
{
qreal w = pixMap.width(), h = pixMap.height();
return QRectF(-w/2, -h/2, w, h);
}
void MyPlane::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option); Q_UNUSED(widget);
if(!pixMap.isNull()) {
painter->save();
qreal sc = qMin((1.0+controller.getRank()/10.0), 2.0);
painter->scale(0.6 * sc, 0.6 * sc);
int w = pixMap.width(), h = pixMap.height();
painter->drawPixmap(QPoint(-w/2, -h/2), pixMap);
painter->setBrush(*gradient);
painter->drawRect(-w/2, -h/2 - 10, qMin(w*controller.getMyPlaneLife()/myPlaneFullLife, w), 8);
painter->restore();
}
}
void MyPlane::advance(int phace)
{
if(!phace) return;
// 设置战机位置,放置超出屏幕
setPos(pos().x() + speedX, pos().y() + speedY);
if(pos().x() < 0) setPos(0, pos().y());
if(pos().x() > viewWidth) setPos(viewWidth, pos().y());
if(pos().y() < 0) setPos(pos().x(), 0);
if(pos().y() > viewHeight-50) setPos(pos().x(), viewHeight-50);
if(isFireing) {
if(isFireing % 7 == 1) fire(10);
isFireing++;
}
handleCollisions();
}
void MyPlane::handleCollisions()
{
QList<QGraphicsItem *> collisions = collidingItems();
foreach (QGraphicsItem *item, collisions) {
if(item->data(GD_type) == GO_Ball) {
controller.removeItem(item);
controller.updateLife(-1);
controller.ariseCollision(pos());
return;
}
if(item->data(GD_type) == GO_BossBall) {
controller.removeItem(item);
controller.updateLife(-1);
controller.ariseCollision(pos());
return;
}
if(item->data(GD_type) == GO_Enemy) {
controller.removeItem(item);
controller.updateLife(-1);
controller.ariseCollision(pos());
return;
}
if(item->data(GD_type) == GO_LifeAdder) {
controller.removeItem(item);
controller.updateLife(1);
return;
}
}
}
void MyPlane::fire(int speed)
{
qreal x = pos().x(), y = pos().y();
controller.shootBullet(QPointF(x, y - 20), speed);
}
void MyPlane::setSpeedX(int x)
{
speedX = x;
}
void MyPlane::setSpeedY(int y)
{
speedY = y;
}
void MyPlane::setFireStatus(bool can)
{
if(isFireing == 0 && can) isFireing = 1;
else if(isFireing > 0 && !can) isFireing = 0;
}