-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow1.cpp
More file actions
230 lines (189 loc) · 5.55 KB
/
window1.cpp
File metadata and controls
230 lines (189 loc) · 5.55 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "mywindow.h"
#include "mybutton.h"
#include "window1.h"
#include "position.h"
#include "plant.h"
#include "waypoint.h"
#include <QPixmap>
#include <QPainter>
#include <QTimer>
#include <QMovie>
#include <QLabel>
Window1::Window1(QWidget *parent)
: QMainWindow(parent)
, m_playerHp(5)
, m_playrGold(1000)
, m_waves(0)
, m_gameWin(false)
, m_gameEnded(false)
{
this->setFixedSize(1024,1024);
//返回按钮
MyButton * rbtn=new MyButton(":/returnbtn.png");
rbtn->setParent(this);
rbtn->move(50,50);
connect(rbtn,&QPushButton::clicked,this,[=](){
rbtn->down();
rbtn->up();
});
connect(rbtn,&MyButton::clicked,this,[=](){
QTimer::singleShot(200,this,[=](){
emit chooseBack(); //发送返回信号
});
});
//种花盆
loadTowerPositions();
//路线
addWayPoints();
//关联一个QTimer,每30ms发送一个信号,更新一次map,主要是为了移动敌人,模拟帧数
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateMap()));
timer->start(30);
// 设置300ms后游戏启动
QTimer::singleShot(300, this, SLOT(gameStart()));
}
void Window1::paintEvent(QPaintEvent *){
QPixmap pixmap(":/background1.png");
//画花盆
QPainter cachePainter(&pixmap);
foreach (const Position &Pos, m_towerPositionsList)
Pos.draw(&cachePainter);
//画植物
foreach(const Plant *plant,m_towerslist)
plant->draw(&cachePainter);
//画路线
foreach (const WayPoint *wayPoint, m_wayPointsList)
wayPoint->draw(&cachePainter);
//画僵尸
foreach (const Enemy *enemy, m_enemyList)
enemy->draw(&cachePainter);
QPainter painter(this);
painter.drawPixmap(0, 0, this->width(),this->height(),pixmap);
}
void Window1::loadTowerPositions(){
QPoint pos[] =
{
QPoint(300, 200),
QPoint(300, 400),
QPoint(300, 300),
QPoint(300, 500),
QPoint(370, 110),
QPoint(600, 200),
QPoint(530, 200),
QPoint(760, 200),
QPoint(760, 300),
QPoint(760, 400),
QPoint(760, 500),
QPoint(760, 100),
QPoint(680, 500),
QPoint(840, 300),
QPoint(840, 500),
QPoint(840, 400),
};
int len = sizeof(pos) / sizeof(pos[0]);
for (int i = 0; i < len; ++i)
m_towerPositionsList.push_back(pos[i]);
}
//里用鼠标点击放置塔
void Window1::mousePressEvent(QMouseEvent *event)
{
QPoint pressPos = event->pos();
auto it = m_towerPositionsList.begin();
while (it != m_towerPositionsList.end())
{
if (canBuyTower() && it->containPoint(pressPos)&& !it->hasTower() )
//可以买塔 && 点击部位是有效部位 && 此处没有塔
{
it->setHasTower();
Plant *plant = new Plant(it->centerPos(), this);
m_towerslist.push_back(plant);
update();
break;
}
++it;
}
}
//确认是否有足够金币购买塔
bool Window1::canBuyTower() const
{
return true;
}
//设置路线节点
void Window1::addWayPoints()
{
WayPoint *wayPoint1 = new WayPoint(QPoint(410, 580));
m_wayPointsList.push_back(wayPoint1);
WayPoint *wayPoint2 = new WayPoint(QPoint(410, 230));
m_wayPointsList.push_back(wayPoint2);
wayPoint2->setNextWayPoint(wayPoint1);
WayPoint *wayPoint3 = new WayPoint(QPoint(485, 230));
m_wayPointsList.push_back(wayPoint3);
wayPoint3->setNextWayPoint(wayPoint2);
WayPoint *wayPoint4 = new WayPoint(QPoint(485, 110));
m_wayPointsList.push_back(wayPoint4);
wayPoint4->setNextWayPoint(wayPoint3);
WayPoint *wayPoint5 = new WayPoint(QPoint(715, 110));
m_wayPointsList.push_back(wayPoint5);
wayPoint5->setNextWayPoint(wayPoint4);
WayPoint *wayPoint6 = new WayPoint(QPoint(715, 400));
m_wayPointsList.push_back(wayPoint6);
wayPoint6->setNextWayPoint(wayPoint5);
WayPoint *wayPoint7 = new WayPoint(QPoint(640,400));
m_wayPointsList.push_back(wayPoint7);
wayPoint7->setNextWayPoint(wayPoint6);
WayPoint *wayPoint8 = new WayPoint(QPoint(640,620));
m_wayPointsList.push_back(wayPoint8);
wayPoint8->setNextWayPoint(wayPoint7);
WayPoint *wayPoint9 = new WayPoint(QPoint(950,620));
m_wayPointsList.push_back(wayPoint9);
wayPoint9->setNextWayPoint(wayPoint8);
WayPoint *wayPoint10 = new WayPoint(QPoint(950,285));
m_wayPointsList.push_back(wayPoint10);
wayPoint10->setNextWayPoint(wayPoint9);
}
//敌人进入终点,玩家HP减少
void Window1::getHpDamage(int damage)
{
}
//敌人的死亡清除及波数的递增
void Window1::removedEnemy(Enemy *enemy)
{
m_enemyList.removeOne(enemy);
delete enemy;
if (m_enemyList.empty())
{
++m_waves;
if (!loadWave())
{ // 当没有下一波时,这里表示游戏胜利
// 设置游戏胜利标志为true
m_gameWin = true;
// 游戏胜利转到游戏胜利场景
// 这里暂时以打印处理
}
}
}
bool Window1::loadWave()
{
if (m_waves >= 6)
return false;
WayPoint *startWayPoint = m_wayPointsList.back(); // 这里是个逆序的,尾部才是其实节点
int enemyStartInterval[] = { 500, 5000, 7000, 9000, 10000, 000 };
for (int i = 0; i < 6; ++i)
{
Enemy *enemy = new Enemy(startWayPoint, this);
m_enemyList.push_back(enemy);
QTimer::singleShot(enemyStartInterval[i], enemy, SLOT(doActivate()));
}
return true;
}
void Window1::updateMap()
{
foreach (Enemy *enemy, m_enemyList)
enemy->move();
update();
}
//开始游戏
void Window1::gameStart()
{
loadWave();
}