-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.cpp
353 lines (305 loc) · 8.28 KB
/
scene.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
#include "scene.h"
Scene::Scene()
{
screenMode = 0;
}
void Scene::Init()
{
//start
btn_start = new Btn();
QPixmap start;
start.load(":/image/image/play.png");
start = start.scaled(110,110,Qt::KeepAspectRatio);
btn_start->setPixmap(start);
btn_start->setPos(310,300);
btn_start_w = 50;
btn_start_h = 50;
addItem(btn_start);
//exit
btn_exit = new Btn();
QPixmap exit;
exit.load(":/image/image/exit.png");
exit = exit.scaled(150,150,Qt::KeepAspectRatio);
btn_exit->setPixmap(exit);
btn_exit->setPos(180,290);
btn_exit_w = 50;
btn_exit_h = 50;
addItem(btn_exit);
//sound
bgm = new QSound(":/sound/bgm1.wav");
bgm->setLoops(INFINITE);
bgm2 = new QSound(":/sound/bgm2.wav");
bgm2->setLoops(INFINITE);
shit_hit1 = new QSound(":/sound/0177.WAV");
shit_hit2 = new QSound(":/sound/0178.WAV");
end = new QSound(":/sound/_cnwav_mp3.wav");
haha = new QSound(":/sound/haha.wav");
bgm->play();
}
void Scene::bgChange()
{
//A && D
A = new Btn;
QPixmap a;
a.load(":/image/image/A.png");
a = a.scaled(150,150);
A->setPixmap(a);
A->setPos(180,280);
addItem(A);
D = new Btn;
QPixmap d;
d.load(":/image/image/D.png");
d = d.scaled(170,170);
D->setPixmap(d);
D->setPos(270,270);
addItem(D);
/*connect*/
dis = 0;
/*change background*/
QImage bg;
bg.load(":/image/image/7658225_656869.jpg");
bg = bg.scaled(610,410);
this->setBackgroundBrush(bg);
/*remove button*/
this->removeItem(btn_start);
this->removeItem(btn_exit);
/*rail picture setting*/
rail = new Rail();
QPixmap rl;
rl.load(":/image/image/panel.png");
rl = rl.scaled(610,100);
rail->setPixmap(rl);
rail->setPos(0,170);
addItem(rail);
/*matong pic setting*/
matong = new Rail();
QPixmap mt;
mt.load(":/image/image/toilet-toilet-bowl_small.png");
mt = mt.scaled(300,300);
matong->setPixmap(mt);
matong->setPos(-100,35);
addItem(matong);
/*back button setting*/
btn_back = new Btn();
QPixmap bk;
bk.load(":/image/image/go.png");
bk = bk.scaled(90,90);
btn_back->setPixmap(bk);
btn_back->setPos(0,5);
btn_back_w = 80;
btn_back_h = 85;
addItem(btn_back);
/*labi*/
labi = new Rail();
QPixmap lb;
lb.load(":/image/image/0.gif");
lb = lb.scaled(250,250);
labi->setPixmap(lb);
labi->setPos(450,40);
addItem(labi);
/*Score*/
score1 = new Score();
score1->setPos(420,35);
addItem(score1);
/*time count*/
mytime = new myTimer();
mytime->setPos(200,35);
addItem(mytime);
}
void Scene::bgRecover()
{
/*disconnect*/
dis = 1;
/*remove item*/
removeItem(btn_back);
removeItem(rail);
removeItem(labi);
removeItem(matong);
removeItem(A);
removeItem(D);
/*remove drum*/
for(int i=0;i<num_drum;i++)
{
removeItem(drum_list[i]);
}
/*add back start page button*/
addItem(btn_start);
addItem(btn_exit);
/*add back background*/
QImage bg;
bg.load(":/image/image/8345569094.jpg");
bg = bg.scaled(610,410);
this->setBackgroundBrush(bg);
}
void Scene::gameInit()
{
if(dis == 0)
{
timer = new QTimer(this);
timer_drum = new QTimer(this);
connect(timer_drum,SIGNAL(timeout()),this,SLOT(random()));
connect(timer,SIGNAL(timeout()),this,SLOT(advance()));
connect(timer,SIGNAL(timeout()),this,SLOT(timeup()));
timer_drum->start(2000);
timer->start(20);
}
if(dis == 1)
{
disconnect(timer_drum,SIGNAL(timeout()),this,SLOT(random()));
disconnect(timer,SIGNAL(timeout()),this,SLOT(advance()));
disconnect(timer,SIGNAL(timeout()),this,SLOT(timeup()));
}
}
void Scene::timeup()
{
if(mytime->time <= 0)
{
mybox = new QMessageBox;
mybox->setIconPixmap(QPixmap(":/image/image/5_141211115056_17.gif"));
mybox->setWindowTitle("Time up");
mybox->setText(QString("Score: ")+QString::number(score1->score)+(QString("\n\nPlay again?")));
mybox->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
mybox->setButtonText(QMessageBox::No,("Play Again"));
mybox->setButtonText(QMessageBox::Yes,("Home"));
mybox->show();
end->play();
dis = 1;
gameInit();
mytime->initialize();
score1->initialize();
bgm2->stop();
//設定yes no按鈕事件
//Play again
if(mybox->exec() == QMessageBox::No)
{
for(int i=0;i<num_drum;i++)
{
removeItem(drum_list[i]);
}
drum_list.clear();
rand_list.clear();
num =0;
num_drum = 0;
mytime->startTime();
dis = 0;
gameInit();
bgm2->play();
}
//Home
else if(mybox->exec() == QMessageBox::Yes)
{
removeItem(A);
removeItem(D);
removeItem(score1);
removeItem(mytime);
bgRecover();
gameInit();
//Initialize
drum_list.clear();
rand_list.clear();
num =0;
num_drum = 0;
screenMode = 0;
bgm->play();
}
}
}
void Scene::endofgame()
{
QApplication::exit();
}
void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(screenMode == 0)
{
/*Beginning page*/
//Start Game button
if(event->scenePos().x() > btn_start->pos().x()+20 && event->scenePos().x() <= btn_start->pos().x()+80)
{
if(event->scenePos().y() > btn_start->pos().y()+20 && event->scenePos().y() <= btn_start->pos().y()+80)
{
if(x == 1)
{
cout<<"Start Game!"<<endl;
bgChange();
gameInit();
screenMode = 1;
/*start counting time*/
mytime->startTime();
//stop bgm
bgm->stop();
bgm2->play();
}
}
}
/*Beginning page*/
//Exit button
else if(event->scenePos().x() > btn_exit->pos().x()+20 && event->scenePos().x() <= btn_exit->pos().x()+100)
{
if(event->scenePos().y() > btn_exit->pos().y()+20 && event->scenePos().y() <= btn_exit->pos().y()+110)
{
if(x == 1)
{
bgm->stop();
haha->play();
QTimer::singleShot(8000,this,SLOT(endofgame()));
//QApplication::exit();
x = 0;
}
}
}
}
if(screenMode == 1)
{
/*Playing page*/
//Back button setting
if(event->scenePos().x() > btn_back->pos().x() && event->scenePos().x() <= btn_back->pos().x()+btn_back_w)
{
if(event->scenePos().y() > btn_back->pos().y() && event->scenePos().y() <= btn_back->pos().y()+btn_back_h)
{
bgRecover();
gameInit();
removeItem(score1);
removeItem(mytime);
//Initialize
drum_list.clear();
rand_list.clear();
num =0;
num_drum = 0;
screenMode = 0;
//add back sound
bgm->play();
bgm2->stop();
}
}
}
}
void Scene::keyPressEvent(QKeyEvent *event)
{
if(screenMode == 1 && num_drum > 1)
{
if(20 > drum_list[num]->pos().x() && -19< drum_list[num]->pos().x())
{
if(event->key() == Qt::Key_A)
{
if(rand_list[num]==1)
{
removeItem(drum_list[num]);
num++;
score1->increase_shit1();
shit_hit1->play();
}
}
if(event->key() == Qt::Key_D)
{
if(rand_list[num]==2)
{
removeItem(drum_list[num]);
num++;
score1->increase_shit2();
shit_hit2->play();
}
}
}
}
}