forked from ProjectAzariah/tmproj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
332 lines (278 loc) · 9.13 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
#include <QKeyEvent>
#include <QRect>
#include <vector>
#include <QString>
#include <QObject>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "background.h"
#include "event.h"
int MainWindow::score = 0;
int MainWindow::health = 100;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mb = new MovingBackground(this);
connect(mb->startBtn, SIGNAL(clicked()), SLOT(on_startBtn_clicked()));
connect(mb->quitBtn, SIGNAL(clicked()), SLOT(on_quitBtn_clicked()));
connect(mb->playAgainBtn, SIGNAL(clicked()), SLOT(on_playAgainBtn_clicked()));
//background timers
backTimer = new QTimer(parent);
backTimer->setInterval(70);
connect(backTimer, SIGNAL(timeout()), this, SLOT(backTimerHit()));
backTimer->start();
//bool active = mb->backTimer->isActive();
//this->backTimerHit();
midTimer = new QTimer(parent);
midTimer->setInterval(40);
connect(midTimer, SIGNAL(timeout()), this, SLOT(midTimerHit()));
midTimer->start();
//this->midTimerHit();
frontTimer = new QTimer(parent);
frontTimer->setInterval(10);
connect(frontTimer, SIGNAL(timeout()), this, SLOT(frontTimerHit()));
frontTimer->start();
//furballtimer
furtime = new QTimer(this);
furtime->setInterval(10);
connect(furtime, SIGNAL(timeout()), this, SLOT(furTimeHit()));
cCat = new CuriousCat(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::backTimerHit()
{
if (mb->backLabel->x() >= -400 && mb->backLabel2->x() >= -400)
{
mb->backLabel->move(mb->backLabel->x() - 1, mb->backLabel->y());
mb->backLabel2->move(mb->backLabel2->x() - 1, mb->backLabel2->y());
}
else if (mb->backLabel->x() < -400)
{
mb->backLabel->hide();
mb->backLabel->move(mb->backLabel->x() + (mb->backLabel->width() * 2) - 1 , mb->backLabel->y());
mb->backLabel2->move(mb->backLabel2->x() - 1, mb->backLabel2->y());
mb->backLabel->show();
}
else if (mb->backLabel2->x() < -400)
{
mb->backLabel2->hide();
mb->backLabel2->move(mb->backLabel2->x() + (mb->backLabel2->width() * 2) - 1 , mb->backLabel2->y());
mb->backLabel->move(mb->backLabel->x() - 1, mb->backLabel->y());
mb->backLabel2->show();
}
}
void MainWindow::midTimerHit()
{
if (mb->midLabel->x() >= -400 && mb->midLabel2->x() >= -400)
{
mb->midLabel->move(mb->midLabel->x() - 1, mb->midLabel->y());
mb->midLabel2->move(mb->midLabel2->x() - 1, mb->midLabel2->y());
}
else if (mb->midLabel->x() < -400)
{
mb->midLabel->hide();
mb->midLabel->move(mb->midLabel->x() + (mb->midLabel->width() * 2) -1 , mb->midLabel->y());
mb->midLabel2->move(mb->midLabel2->x() - 1, mb->midLabel2->y());
mb->midLabel->show();
}
else if (mb->midLabel2->x() < -400)
{
mb->midLabel2->hide();
mb->midLabel2->move(mb->midLabel2->x() + (mb->midLabel2->width() * 2) - 1, mb->midLabel2->y());
mb->midLabel->move(mb->midLabel->x() - 1, mb->midLabel->y());
mb->midLabel2->show();
}
}
void MainWindow::frontTimerHit()
{
if (mb->frontLabel->x() >= -400 && mb->frontLabel2->x() >= -400)
{
mb->frontLabel->move(mb->frontLabel->x() - 1, mb->frontLabel->y());
mb->frontLabel2->move(mb->frontLabel2->x() - 1, mb->frontLabel2->y());
}
else if (mb->frontLabel->x() < -400)
{
mb->frontLabel->hide();
mb->frontLabel->move(mb->frontLabel->x() + (mb->frontLabel->width() * 2) - 1, mb->frontLabel->y());
mb->frontLabel2->move(mb->frontLabel2->x() - 1, mb->frontLabel2->y());
mb->frontLabel->show();
}
else if (mb->frontLabel2->x() < -400)
{
mb->frontLabel2->hide();
mb->frontLabel2->move(mb->frontLabel2->x() + (mb->frontLabel2->width() * 2) - 1, mb->frontLabel2->y());
mb->frontLabel->move(mb->frontLabel->x() - 1, mb->frontLabel->y());
mb->frontLabel2->show();
}
}
void MainWindow::on_startBtn_clicked(){
started=true;
mb->startBtn->setDisabled(true);
mb->startBtn->hide();
mb->introLabel->hide();
mb->quitBtn->show();
obstacleTimer = new QTimer(this);
obstacleTimer->setInterval(10);
connect(obstacleTimer, SIGNAL(timeout()), this, SLOT(obstacleTimerHit()));
obstacleTimer->start();
spawningTimer = new QTimer(this);
spawningTimer->setInterval(1750);
connect(spawningTimer, SIGNAL(timeout()), this, SLOT(spawningTimerHit()));
spawningTimer->start();
Obstacle& o = Obstacle::instance();
/*MadDog();
LawnMower();
Hole();*/
o.spawnObstacles(this);
}
void MainWindow::obstacleTimerHit()
{
Obstacle& o = Obstacle::instance();
for (unsigned int i = 0; i < o.spawnedObstacles.size(); i++)
{
QLabel * obst = new QLabel;
obst = o.spawnedObstacles[i];
obst->move(obst->x() - 1, obst->y());
}
}
void MainWindow::spawningTimerHit()
{
Obstacle& o = Obstacle::instance();
//enemyExists = true;
QLabel * obst = new QLabel(this);
obst = o.spawnObstacles(this);
obst->show();
obstacleTimer->start();
//label->move(0, label->y());
this->obstacleTimerHit();
if (started){
score+=2;
}
mb->scoreLabel->setText("Score: "+ QString::number(score));
}
void MainWindow::keyPressEvent(QKeyEvent* event) {
//qDebug() << event->key(); //uncomment this line if you want to figure out what key number a key is for the case in the
switch(event->key())
{
case 87:
cCat->cat->move(cCat->cat->x(), cCat->cat->y() - 76);
cCat->catMovie->stop();
break;
case 68:
furBall = new QLabel(this);
QPixmap fBall(":/furball2.png");
furBall->setPixmap(fBall);
furBall->setScaledContents(true);
furBall->setGeometry(cCat->cat->x() + 35 ,cCat->cat->y() + 30 ,30,20);
furBall->show();
furBalls.push_back(furBall);
//QTimer * flameTime = new QTimer(this);
//flameTime->setInterval(20);
//connect(flameTime, SIGNAL(timeout()), this, SLOT(flameTimeHit()));
furtime->start();
//label->move(0, label->y());
this->furTimeHit();
break;
}
}
void MainWindow::keyReleaseEvent(QKeyEvent * event)
{
switch(event->key())
{
case 87:
cCat->cat->move(cCat->cat->x(), cCat->cat->y() + 76);
cCat->catMovie->start();
break;
case 68:
break;
}
}
void MainWindow::furTimeHit()
{
Obstacle& o = Obstacle::instance();
for (unsigned int i = 0 ; i < furBalls.size(); i++)
{
QLabel * ball = furBalls[i];
ball->move(ball->x() + 2 , ball->y());
/*if (enemyExists == false)
{
}
else if (enemyExists)
{*/
for (unsigned int j = 0; j < o.spawnedObstacles.size(); j++)
{
QLabel * badGuy = new QLabel;
badGuy = o.spawnedObstacles[i];
if (/*enemyExists &&*/ ball->geometry().intersects(badGuy->geometry()))
{
//delete ball;
delete badGuy;
o.spawnedObstacles.erase(o.spawnedObstacles.cbegin());
furBalls.erase(furBalls.cbegin());
//ball = new QLabel(this);
badGuy = new QLabel(this);
delete ball;
ball = new QLabel(this);
}
else if (/*enemyExists && */badGuy->x() < -75)
{
delete badGuy;
o.spawnedObstacles.erase(o.spawnedObstacles.cbegin());
badGuy = new QLabel(this);
}
else if (ball->x() > this->width())
{
delete ball;
furBalls.erase(furBalls.cbegin());
ball = new QLabel(this);
}
}
// }
/*else if (ball->geometry().intersects(dog->geometry()))
{
delete dog;
//dog->deleteLater();
dog = new QLabel(this);
}*/
}
}
void MainWindow::on_quitBtn_clicked(){
mb->healthLabel->hide();
mb->scoreLabel->hide();
mb->quitBtn->hide();
mb->endScreen->show();
mb->endScreen->setText("Top Scores: ------\n ------\n ------\nYour Score:" + mb->scoreLabel->text());
mb->playAgainBtn->show();
obstacleTimer->stop();
spawningTimer->stop();
backTimer->stop();
midTimer->stop();
frontTimer->stop();
obstacleTimer->stop();
spawningTimer->stop();
//furTimer->stop();
cCat->cat->hide();
for(int i = 0; i < Obstacle::instance().obstacles.size(); i++){
Obstacle::instance().obstacles[i]->hide();
}
}
void MainWindow::on_playAgainBtn_clicked(){
health=100;
score=0;
mb->endScreen->hide();
mb->playAgainBtn->hide();
mb->scoreLabel->setText("Score: " + QString::number(score));
mb->scoreLabel->show();
mb->healthLabel->show();
mb->quitBtn->show();
cCat->cat->show();
backTimer->start();
midTimer->start();
frontTimer->start();
on_startBtn_clicked();
}