-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
406 lines (390 loc) · 12.4 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->Win->setVisible(false);
ui->Lose->setVisible(false);
scene = new QGraphicsScene(0, 0, ui->graphicsView->width() - 2, ui->graphicsView->height() - 2);
scene->addPixmap(QPixmap(":/img/img/background"));
ui->graphicsView->setScene(scene);
for(int i = 0; i < 29; i++){
for(int j = 0; j < 26; j++){
Smallpoint[i][j] = new smallpoint(18 + j * 28.5, 8 + i * 26.1, i, j);
scene->addItem(Smallpoint[i][j]);
}
}
smallpointNum = 0;
Bigpoint[0] = new bigpoint();
Bigpoint[0]->setPos(18, 60.2);
scene->addItem(Bigpoint[0]);
Bigpoint[1] = new bigpoint();
Bigpoint[1]->setPos(730.5, 60.2);
scene->addItem(Bigpoint[1]);
Bigpoint[2] = new bigpoint();
Bigpoint[2]->setPos(18, 582.2);
scene->addItem(Bigpoint[2]);
Bigpoint[3] = new bigpoint();
Bigpoint[3]->setPos(730.5, 582.2);
scene->addItem(Bigpoint[3]);
bigpointNum = 0;
Pacman = new pacman();
Pacman->setPos(376, 576);
Pacman->setFlag(QGraphicsItem::ItemIsFocusable);
Pacman->setFocus();
scene->addItem(Pacman);
ghost1 = new red();
scene->addItem(ghost1);
ghost2 = new pink();
scene->addItem(ghost2);
ghost3 = new cyan();
scene->addItem(ghost3);
ghost4 = new orange();
scene->addItem(ghost4);
point = new QLabel(this);
point->setGeometry(0, 0, 49, 17);
point->setFont(QFont("Ubuntu Regular", 14));
QPalette pa;
pa.setColor(QPalette::WindowText, Qt::darkGreen);
point->setPalette(pa);
point->setVisible(false);
Cherry = new cherry();
scene->addItem(Cherry);
srandom(time(NULL));
timer1 = new QTimer(this);
connect(timer1, SIGNAL(timeout()), this, SLOT(allMove()));
timer1->start(13);
timer2 = new QTimer(this);
connect(timer2, SIGNAL(timeout()), this, SLOT(allChangePics()));
timer2->start(100);
timer3 = new QTimer(this);
connect(timer3, SIGNAL(timeout()), this, SLOT(bigpointChangePics()));
timer3->start(200);
modeRed = modePink = modeCyan = modeOrange = 0;
addpoint = 200;
pause = false;
isBonus2 = false;
timer8 = new QTimer(this);
timer8->setSingleShot(true);
connect(timer8, SIGNAL(timeout()), this, SLOT(pauseTime()));
timer8->start(2000);
timer9 = new QTimer(this);
connect(timer9, SIGNAL(timeout()), this, SLOT(slowMove()));
timer9->start(26);
timer10 = new QTimer(this);
timer10->setSingleShot(true);
connect(timer10, SIGNAL(timeout()), this, SLOT(cyanStartSlot()));
timer10->start(7000);
timer11 = new QTimer(this);
timer11->setSingleShot(true);
connect(timer11, SIGNAL(timeout()), this, SLOT(orangeStartSlot()));
timer11->start(17000);
timer12 = new QTimer(this);
connect(timer12, SIGNAL(timeout()), this, SLOT(cherrySetVisibleTrue()));
timer12->start(60000);
pause = true;
slowRed = false;
slowPink = slowCyan = slowOrange = true;
}
void MainWindow::allMove(){
if(!pause){
Pacman->move();
if(modeRed == 0 && !slowRed)
ghost1->move();
if(modePink == 0 && !slowPink)
ghost2->move();
if(modeCyan == 0 && !slowCyan)
ghost3->move();
if(modeOrange == 0 && !slowOrange)
ghost4->move();
for(int i = 0; i < 29; i++){
for(int j = 0; j < 26; j++){
bool isCollided = Pacman->collidesWithItem(Smallpoint[i][j]);
if(isCollided){
Smallpoint[i][j]->collide();
ui->lcdNumber->display(ui->lcdNumber->value() + 10);
smallpointNum++;
}
}
}
for(int i = 0; i < 4; i++){
bool isCollided = Pacman->collidesWithItem(Bigpoint[i]);
if(isCollided){
if(!q.empty()){
int temp = q.front();
q.pop_front();
delete timer4[temp];
delete timer5[temp];
delete timer6[temp];
delete timer7[temp];
addpoint = 200;
isBonus2 = false;
}
Bigpoint[i]->collide();
ui->lcdNumber->display(ui->lcdNumber->value() + 50);
modeRed = modePink = modeCyan = modeOrange = 1;
timer4[i] = new QTimer(this);
timer4[i]->setSingleShot(true);
connect(timer4[i], SIGNAL(timeout()), this, SLOT(bonusTime()));
timer5[i] = new QTimer(this);
timer5[i]->setSingleShot(true);
connect(timer5[i], SIGNAL(timeout()), this, SLOT(bonusTime2()));
timer6[i] = new QTimer(this);
connect(timer6[i], SIGNAL(timeout()), this, SLOT(flicker()));
timer7[i] = new QTimer(this);
connect(timer7[i], SIGNAL(timeout()), this, SLOT(ghostmove2()));
timer4[i]->start(9000);
timer5[i]->start(6500);
timer6[i]->start(200);
timer7[i]->start(20);
q.push_back(i);
bigpointNum++;
}
}
bool isCollided1 = Pacman->collidesWithItem(ghost1);
bool isCollided2 = Pacman->collidesWithItem(ghost2);
bool isCollided3 = Pacman->collidesWithItem(ghost3);
bool isCollided4 = Pacman->collidesWithItem(ghost4);
if((isCollided1 && !modeRed) || (isCollided2 && !modePink) || (isCollided3 && !modeCyan) || (isCollided4 && !modeOrange)){
pause = true;
ui->Lose->setVisible(true);
}
if(isCollided1 && modeRed){
modeRed = 0;
point->setText(QString::number(addpoint));
point->setGeometry(ghost1->x(), ghost1->y() + 30, 49, 17);
point->setVisible(true);
ui->lcdNumber->display(ui->lcdNumber->value() + addpoint);
pause = true;
timer8 = new QTimer(this);
timer8->setSingleShot(true);
connect(timer8, SIGNAL(timeout()), this, SLOT(getPointTime()));
timer8->start(1000);
ghost1->setPos(376, 342);
ghost1->setDirection(0);
slowRed = true;
addpoint *= 2;
}
if(isCollided2 && modePink){
modePink = 0;
point->setText(QString::number(addpoint));
point->setGeometry(ghost2->x(), ghost2->y() + 30, 49, 17);
point->setVisible(true);
ui->lcdNumber->display(ui->lcdNumber->value() + addpoint);
pause = true;
timer8 = new QTimer(this);
timer8->setSingleShot(true);
connect(timer8, SIGNAL(timeout()), this, SLOT(getPointTime()));
timer8->start(1000);
ghost2->setPos(376, 342);
ghost2->setDirection(0);
slowPink = true;
addpoint *= 2;
}
if(isCollided3 && modeCyan){
modeCyan = 0;
point->setText(QString::number(addpoint));
point->setGeometry(ghost3->x(), ghost3->y() + 30, 49, 17);
point->setVisible(true);
ui->lcdNumber->display(ui->lcdNumber->value() + addpoint);
pause = true;
timer8 = new QTimer(this);
timer8->setSingleShot(true);
connect(timer8, SIGNAL(timeout()), this, SLOT(getPointTime()));
timer8->start(1000);
ghost3->setPos(376, 342);
ghost3->setDirection(0);
slowCyan = true;
addpoint *= 2;
}
if(isCollided4 && modeOrange){
modeOrange = 0;
point->setText(QString::number(addpoint));
point->setGeometry(ghost4->x(), ghost4->y() + 30, 49, 17);
point->setVisible(true);
ui->lcdNumber->display(ui->lcdNumber->value() + addpoint);
pause = true;
timer8 = new QTimer(this);
timer8->setSingleShot(true);
connect(timer8, SIGNAL(timeout()), this, SLOT(getPointTime()));
timer8->start(1000);
ghost4->setPos(376, 342);
ghost4->setDirection(0);
slowOrange = true;
addpoint *= 2;
}
bool isCollided = Pacman->collidesWithItem(Cherry);
if(isCollided){
Cherry->changePics(1);
ui->lcdNumber->display(ui->lcdNumber->value() + 100);
}
if(smallpointNum == 242 && bigpointNum == 4){
pause = true;
ui->Win->setVisible(true);
}
}
}
void MainWindow::allChangePics(){
if(!pause){
Pacman->changePics();
if(modeRed == 0){
ghost1->changePics();
}
else if(modeRed == 1){
ghost1->changePics2();
}
else if(modeRed == 2){
ghost1->changePics3();
}
if(modePink == 0){
ghost2->changePics();
}
else if(modePink == 1){
ghost2->changePics2();
}
else if(modePink == 2){
ghost2->changePics3();
}
if(modeCyan == 0){
ghost3->changePics();
}
else if(modeCyan == 1){
ghost3->changePics2();
}
else if(modeCyan == 2){
ghost3->changePics3();
}
if(modeOrange == 0){
ghost4->changePics();
}
else if(modeOrange == 1){
ghost4->changePics2();
}
else if(modeOrange == 2){
ghost4->changePics3();
}
}
}
void MainWindow::bigpointChangePics(){
if(!pause){
Bigpoint[0]->changePics();
Bigpoint[1]->changePics();
Bigpoint[2]->changePics();
Bigpoint[3]->changePics();
}
}
void MainWindow::bonusTime(){
int i = q.front();
q.pop_front();
modeRed = modePink = modeCyan = modeOrange = 0;
isBonus2 = false;
addpoint = 200;
delete timer4[i];
delete timer5[i];
delete timer6[i];
delete timer7[i];
}
void MainWindow::bonusTime2(){
isBonus2 = true;
}
void MainWindow::flicker(){
if(!pause){
if(isBonus2){
if(modeRed == 1){
modeRed = 2;
}
else if(modeRed == 2){
modeRed = 1;
}
if(modePink == 1){
modePink = 2;
}
else if(modePink == 2){
modePink = 1;
}
if(modeCyan == 1){
modeCyan = 2;
}
else if(modeCyan == 2){
modeCyan = 1;
}
if(modeOrange == 1){
modeOrange = 2;
}
else if(modeOrange == 2){
modeOrange = 1;
}
}
}
}
void MainWindow::ghostmove2(){
if(!pause){
if((modeRed == 1 || modeRed == 2) && !slowRed)
ghost1->move();
if((modePink == 1 || modePink == 2) && !slowPink)
ghost2->move();
if((modeCyan == 1 || modeCyan == 2) && !slowCyan)
ghost3->move();
if((modeOrange == 1 || modeOrange == 2) && !slowOrange)
ghost4->move();
}
}
void MainWindow::getPointTime(){
delete timer8;
point->setVisible(false);
pause = false;
}
void MainWindow::pauseTime(){
delete timer8;
ui->ready->setVisible(false);
pause = false;
}
void MainWindow::slowMove(){
if(!pause){
if(slowRed)
ghost1->move();
if(slowPink)
ghost2->move();
if(slowCyan)
ghost3->move();
if(slowOrange)
ghost4->move();
}
if(ghost1->x() == 376.0 && ghost1->y() == 272.0){
slowRed = false;
}
if(ghost2->x() == 376.0 && ghost2->y() == 272.0){
slowPink = false;
}
if(ghost3->x() == 376.0 && ghost3->y() == 272.0){
slowCyan = false;
}
if(ghost4->x() == 376.0 && ghost4->y() == 272.0){
slowOrange = false;
}
}
void MainWindow::cyanStartSlot(){
delete timer10;
ghost3->setCyanStart();
}
void MainWindow::orangeStartSlot(){
delete timer11;
ghost4->setOrangeStart();
}
void MainWindow::cherrySetVisibleTrue(){
Cherry->changePics(0);
timer13 = new QTimer(this);
timer13->setSingleShot(true);
connect(timer13, SIGNAL(timeout()), this, SLOT(cherrySetVisibleFalse()));
timer13->start(10000);
}
void MainWindow::cherrySetVisibleFalse(){
Cherry->changePics(1);
delete timer13;
}
MainWindow::~MainWindow()
{
delete ui;
}