-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmainwindow.cpp
227 lines (194 loc) · 6.88 KB
/
mainwindow.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
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
#include <QtGui>
#include <QMessageBox>
#include "mainwindow.h"
#include "plantyard.h"
#include "plant.h"
#include "sunlight.h"
#include "field.h"
#include "allzombies.h"
#include "garden.h"
#include "carddialog.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
createActions();
createMenus();
setMouseTracking(true);
SunLight *sun = new SunLight();
QHBoxLayout *top = new QHBoxLayout;
QVBoxLayout *layout = new QVBoxLayout;
top->addWidget(sun);
QPixmap* pixmap = new QPixmap(":/images/remove.png");
QPushButton* removeButton = new QPushButton;
removeButton->setIcon(QIcon(*pixmap));
removeButton->setIconSize(pixmap->rect().size());
QPushButton* pauseButton = new QPushButton(tr("Pause/\nRestore"));
connect(pauseButton,SIGNAL(clicked()),this,SLOT(pauseOrRestore()));
AllZombies* allZombies = new AllZombies();
Field *f = new Field(sun, allZombies);
allZombies->setField(f);
Garden *g = new Garden(sun);
yard = new PlantYard(sun,f);
top->addWidget(yard);
top->addWidget(removeButton);
top->addWidget(pauseButton);
layout->addLayout(top);
connect(f,SIGNAL(addPlantPic(Plant*,int,int)),g,SLOT(addPlant(Plant*,int,int)));
connect(f,SIGNAL(removePlantPic(Plant*)),g,SLOT(removePlant(Plant*)));
connect(f,SIGNAL(prepareToPlant(Plant*)),g,SLOT(prepareToPlant(Plant*)));
connect(g,SIGNAL(addPlantAt(Plant*,int,int)),f,SLOT(addPlant(Plant*, int,int)));
connect(f,SIGNAL(subSun(int)),sun,SLOT(subtractSunLight(int)));
connect(g,SIGNAL(removePlantAt(int,int)),f,SLOT(removePlant(int,int)));
connect(g,SIGNAL(destroyPlant(Plant*)),f,SLOT(deletePlant(Plant*)));
connect(removeButton, SIGNAL(clicked()),g,SLOT(prepareToRemove()));
layout->addWidget(g);
// QTimer::singleShot(10000, f, SLOT(removePlantDebug()));
connect(allZombies,SIGNAL(addZombieAt(Zombie*,int,int)),g,SLOT(addZombieAt(Zombie*,int,int)));
connect(g,SIGNAL(sendHit(Zombie*,int,int)),allZombies,SIGNAL(receiveHit(Zombie*,int,int)));
connect(allZombies,SIGNAL(deleteZombie(Zombie*)),g,SLOT(deleteZombie(Zombie*)));
connect(g,SIGNAL(explodeAt(int,int)),allZombies,SLOT(clearBox(int,int)));
connect(allZombies,SIGNAL(youLose()),this,SLOT(youLose()));
connect(g,SIGNAL(clearLine(int)),allZombies,SLOT(clearRow(int)));
// connect(this,SIGNAL(sendZombies()),allZombies,SLOT(startSendZombies()));
// allZombies->addZombie(0, 1);
// restart
connect(this,SIGNAL(restart()),allZombies,SLOT(restart()));
connect(this,SIGNAL(restart()),f,SLOT(restart()));
connect(this,SIGNAL(restart()),g,SLOT(restart()));
connect(this,SIGNAL(restart()),yard,SLOT(restart()));
connect(this,SIGNAL(restart()),sun,SLOT(restart()));
// pause
connect(this,SIGNAL(pause()),allZombies,SLOT(pause()));
connect(this,SIGNAL(pause()),f,SLOT(pause()));
connect(this,SIGNAL(pause()),g,SLOT(pause()));
connect(this,SIGNAL(pause()),yard,SLOT(pause()));
// restore
connect(this,SIGNAL(restore()),allZombies,SLOT(restore()));
connect(this,SIGNAL(restore()),f,SLOT(restore()));
connect(this,SIGNAL(restore()),g,SLOT(restore()));
connect(this,SIGNAL(restore()),yard,SLOT(restore()));
layout->setSpacing(0);
QWidget *widget = new QWidget;
widget->setLayout(layout);
widget->show();
setWindowTitle(QObject::tr("Plant VS Zombies"));
setCentralWidget(widget);
music = new BackgroundMusic;
music->startPlaying();
this->setFixedSize(1200,700);
// layout->setSizeConstraint(QLayout::SetFixedSize);
// emit sendZombies();
paused = false;
}
MainWindow::~MainWindow()
{
delete(gameMenu);
delete(helpMenu);
delete(music);
//delete ui;
}
void MainWindow::createMenus()
{
gameMenu = menuBar()->addMenu(tr("&Game"));
gameMenu->addAction(newGameAct);
gameMenu->addAction(pauseGameAct);
gameMenu->addSeparator();
gameMenu->addAction(exitAct);
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAct);
}
void MainWindow::createActions()
{
newGameAct = new QAction(tr("&New"),this);
newGameAct->setShortcut(tr("Ctrl+N"));
connect(newGameAct, SIGNAL(triggered()), this, SLOT(newGame()));
pauseGameAct = new QAction(tr("&Pause/Restore"),this);
pauseGameAct->setShortcut(tr("Ctrl+P"));
connect(pauseGameAct, SIGNAL(triggered()),this,SLOT(pauseOrRestore()));
exitAct = new QAction(tr("E&xit"),this);
exitAct->setShortcut(tr("Ctrl+g"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
aboutAct = new QAction(tr("&About"), this);
aboutAct->setStatusTip(tr("Show the application's About box"));
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
}
void MainWindow::about()
{
QMessageBox::about(this, tr("About Application"),
tr("The <b>PVZ</b> was written by "
"Yang Sheng <[email protected]>"
" as a homework"));
}
void MainWindow::newGame()
{
emit pause();
CardDialog dialog(yard,this);
int ret =dialog.exec();
// qDebug()<<"ret of dialog"<<ret;
if (ret == 0)
emit restore();
else
emit restart();
/*
int ret = QMessageBox::information(this, tr("Restart Game"),
tr("All current status will be lost.\n"
"Are you sure to restart?"),
QMessageBox::Cancel,QMessageBox::Ok);
switch (ret){
case QMessageBox::Ok:
emit restart();
break;
case QMessageBox::Cancel:
emit restore();
break;
default:
//should never be reached
break;
}
//QMessageBox::information(this, tr("Newly implemented!"),tr("This function has just implemented!"));
*/
}
void MainWindow::pauseOrRestore()
{
if (paused)
emit restore();
else
emit pause();
paused = !paused;
}
void MainWindow::youLose()
{
emit pause();
int ret = QMessageBox::information(this, tr("You Lose!!"),
tr("You Lose!!!\n"
"Do you wish to try again?"),
QMessageBox::Cancel,QMessageBox::Ok);
/*
switch (ret){
case QMessageBox::Ok:
emit restart();
return;// added to prevent error
break;
case QMessageBox::Cancel:
break;
default:
//should never be reached
break;
}
*/
if (ret == QMessageBox::Ok){
// emit restart();
QTimer::singleShot(200,this,SLOT(newRestart()));
}
}
void MainWindow::newRestart()
{
emit restart();
/*
Note, this slot is used only to avoid segment fault
in MainWindow::youLose() when emitting restart(),
this is perhaps because of revisiting some classes
that has already been deleted by ``restart()''
signal.
*/
}