-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
345 lines (308 loc) · 8.58 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
#include "mainwindow.h"
#include <cmath>
#include <ctime>
#include <QGraphicsRectItem>
#include <qapplication.h>
#include "cuttle.h"
#include <iostream>
#include "graphicwindow.h"
#include "whale.h"
#include "bfish.h"
#include "dolphin.h"
#include "crab.h"
#include "fisher.h"
/** MainWindow class is the class that operates as a main method
* and contains and controls all
*/
MainWindow::MainWindow() {
scene = new QGraphicsScene();
view = new GraphicWindow( scene,this );
int sizeMod=1;
view->setFixedSize( WINDOW_MAX_X*sizeMod+20, WINDOW_MAX_Y*sizeMod+20 );
view->setWindowTitle( "Cuttle Scuttle");
scene->setSceneRect(0,0,WINDOW_MAX_X*sizeMod-10, WINDOW_MAX_Y*sizeMod);
bounds=new QRectF(scene->sceneRect());
timer = new QTimer(this);
timer->setInterval(5);
connect(timer,SIGNAL(timeout()),this,SLOT(move()));
connect(timer,SIGNAL(timeout()),this,SLOT(animate()));
connect(timer,SIGNAL(timeout()),this,SLOT(populate()));
cuttlePos=new QPointF(20,WINDOW_MAX_Y/2);
cuttle=new Cuttle(cuttlePos,this);
scene->addItem(cuttle);
mainVLayout=new QVBoxLayout();
topHLayout=new QHBoxLayout();
view->setLayout(mainVLayout);
start=new QPushButton("Start");
connect(start,SIGNAL(clicked()),this,SLOT(startPressed()));
pause=new QPushButton("Pause");
connect(pause,SIGNAL(clicked()),this,SLOT(pausePressed()));
exit=new QPushButton("Quit");
connect(exit,SIGNAL(clicked()),qApp,SLOT(quit()));
rankings=new QPushButton("Rankings/Submit Score");
connect(rankings,SIGNAL(clicked()),this,SLOT(rankingsPressed()));
topHLayout->addWidget(start);
topHLayout->addWidget(pause);
topHLayout->addWidget(rankings);
topHLayout->addWidget(exit);
health=new QLabel("Health: ");
topHLayout->addWidget(health);
score=new QLabel("SCORE: ");
topHLayout->addWidget(score);
player=new QFormLayout();
name=new QLabel("Name");
nameField=new QLineEdit();
player->addRow(name,nameField);
topHLayout->addLayout(player);
gameOver=new QLabel("GAME OVER");
pause->resize(40,20);
mainVLayout->addLayout(topHLayout);
mainVLayout->addSpacing(WINDOW_MAX_Y*sizeMod);
level=points=0;
multiplier=mod=1;
defaultBMP=new QPixmap("cuttle/c1.png");
started=firing=hypnosis=false;
for(int i=1;i<4;i++){
QString fn="backgrounds/b";fn.append(QString::number(i));fn.append(".png");
QPixmap* t=new QPixmap(fn);
backgrounds.push_back(t);
}
scene->setBackgroundBrush(QBrush(*(backgrounds[0])));
rank=new Scores("rankings.txt");
}
/** Passed to main(), displays the GUI*/
void MainWindow::show() {
view->show();
}
/** Destructor. Removes scene and view*/
MainWindow::~MainWindow()
{
rankingsPressed();
delete scene;
delete view;
}
/** handles a key being pressed
* @param e is instance of the key event
*/
void MainWindow::keyPressEvent(QKeyEvent* e){
int v=50;
switch(e->key()){
case Qt::Key_W:
cuttle->setYVel(-v);
break;
case Qt::Key_A:
cuttle->setXVel(-v);
break;
case Qt::Key_S:
cuttle->setYVel(v);
break;
case Qt::Key_D:
cuttle->setXVel(v);
break;
}
}
/** handles a key being releaseds
* @param e is instance of the key event
*/
void MainWindow::keyReleaseEvent(QKeyEvent* e){
switch(e->key()){
case Qt::Key_W:
cuttle->setYVel(0);
break;
case Qt::Key_A:
cuttle->setXVel(0);
break;
case Qt::Key_S:
cuttle->setYVel(0);
break;
case Qt::Key_D:
cuttle->setXVel(0);
break;
}
}
/** handles pressing of mouse buttons and their corrosponding actions
* in terms of the game mechanics
* @param e is instance of the mouse event
*/
void MainWindow::mousePressEvent(QMouseEvent* e){
if(e->button()==Qt::RightButton){
hypnosis=true;
cuttle->startHypnotizing();
}
else{
firing=true;
target=new QPoint(e->globalPos());
cuttle->setTarget(target);
QList<QGraphicsItem*> childrens=cuttle->childItems();
if(!childrens.empty()){
QGraphicsItem* t=childrens.front();
childrens.pop_front();
t->setZValue(-1);
}
}
}
/** handles mouse buttons being released
* @param e is instance of the mouse event
*/
void MainWindow::mouseReleaseEvent(QMouseEvent* e){
if(e->button()==Qt::RightButton){
hypnosis=false;
cuttle->stopHypnotizing();
}else{
firing=false;
}
}
/** moves all the Things and helps handle collisions and deaths*/
void MainWindow::move(){
for(unsigned int i=0;i<things.size();i++){
if(things[i]->collidesWithItem(cuttle)){
switch(things[i]->isType()){
case 1:
cuttle->unbind();
cuttle->hitByWhale(things[i]->getVel());
break;
case 2:
if(hypnosis) things[i]->hypnotize(cuttle);
break;
case 3:
if(!cuttle->isInvincible()){
cuttle->hurt(25);
points-=500;
cuttle->becomesInvincible();
}
break;
case 4:
cuttle->heal(50);
things[i]->setZValue(3);
break;
case 5:
things[i]->setZValue(-15);
cuttle->catchHook(things[i]->getVel());
break;
}
}
things[i]->move();
health->setText(cuttle->getHealth());
multiplier=cuttle->childItems().size();
if(multiplier==0) multiplier=1;
score->setText(getScore());
if((cuttle->death)>1000) lose();
if(!scene->sceneRect().contains(things[i]->pos()))
if((things[i]->death++)>4000){
if(things[i]->zValue()==-12) points+=500;
if(things[i]->zValue()==3) points+=200;
scene->removeItem(things[i]);
things.erase(things.begin()+i);
}
}
if(!scene->sceneRect().contains(cuttle->pos())) cuttle->death++;
else{ cuttle->death=0;}
}
/** animates all the Things*/
void MainWindow::animate(){
for(unsigned int i=0;i<things.size();i++){
things[i]->animate();
}
}
/** Populates the game with Thing items, and updates level*/
void MainWindow::populate(){
if(level%(5000/mod)==0){
Whale* whale=new Whale(defaultBMP, WINDOW_MAX_X+250,rand()%(WINDOW_MAX_Y-50)+20);
scene->addItem(whale);
things.push_back(whale);
}
if(level%(1000/mod)==0){
Dolphin* dolphin=new Dolphin(
defaultBMP, WINDOW_MAX_X+250,rand()%WINDOW_MAX_Y);
scene->addItem(dolphin);
dolphin->giveTarget(cuttle);
things.push_back(dolphin);
for(int i=0;i<mod;i++){
if(mod<4) dolphin->increaseAttackSpeed();
else{
Dolphin* bonus=new Dolphin(defaultBMP, WINDOW_MAX_X+250,rand()%WINDOW_MAX_Y);
scene->addItem(bonus);
bonus->giveTarget(cuttle);
things.push_back(bonus);
}
}
}
if(level%(200/mod)==0){
points+=10*multiplier;
for(int i=0;i<rand()%(5/mod);i++){
BFish* bfish=new BFish(defaultBMP, WINDOW_MAX_X+250,rand()%WINDOW_MAX_Y);
scene->addItem(bfish);
things.push_back(bfish);
}
}
if(level%(1500)==0){
Crab* crab=new Crab(defaultBMP, WINDOW_MAX_X+250,WINDOW_MAX_Y-25);
scene->addItem(crab);
things.push_back(crab);
}
if(level%(3500/mod)==0){
Fisher* fisher=new Fisher(defaultBMP, WINDOW_MAX_X+250,rand()%115+30*mod);
scene->addItem(fisher);
things.push_back(fisher);
for(int i=0;i<mod;i++){
fisher->increaseAttackSpeed();
}
}
level++;
if(level%10000==0) mod++;
scene->setBackgroundBrush(QBrush(*(backgrounds[mod-1])));
}
/** pauses or resumes game*/
void MainWindow::pausePressed(){
if(started){
if(timer->isActive()) timer->stop();
else timer->start();
}
}
/** starts game*/
void MainWindow::startPressed(){
if(!started){
started=true;
start->setText("Restart");
health->setText(cuttle->getHealth());
timer->start();
}
else{
while(!things.empty()){
scene->removeItem(things.back());
things.pop_back();
cuttle->setPos(*cuttlePos);
cuttle->bind();
mod=1;
timer->start();
}
gameOver->setText(" ");
cuttle->heal(1000);
points=0;
}
}
/** stops game and spams game over*/
void MainWindow::lose(){
if(timer->isActive()){
timer->stop();
gameOver->setText("GAME OVER");
mainVLayout->addWidget(gameOver);
}
}
/** returns score in QString format for qlabel score*/
QString MainWindow::getScore(){
QString s("SCORE: ");
s.append(QString::number(points));
return s;
}
/** displays rankings*/
void MainWindow::rankingsPressed(){
if(started){
if(nameField->text()!=QString("")){
QString name=nameField->text();
name.replace(QString(" "),QString("_"));
rank->give(name,points);
}
}
rank->display();
}