forked from Haroooold/motaGroup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
344 lines (293 loc) · 10.7 KB
/
Game.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
/*
* File: Game.cpp
* --------------------
* This file implements Game.h interface.
*/
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <QBrush>
#include <QImage>
#include <stdlib.h>
#include <QApplication>
#include <QGraphicsTextItem>
#include <QTimer>
#include <errno.h>
#include <QFile>
#include <QTextStream>
#include "Game.h"
#include "Hero.h"
#include "Monster.h"
#include "Button.h"
#include "Unwalkable.h"
#include "npc.h"
#include "subtitle.h"
#include "saveload.h"
using namespace std;
Game::Game(QWidget* parent)
{
// set the scene & background
scene = new QGraphicsScene();
scene->setSceneRect(0,0,440+100*2,440);
setBackgroundBrush(QBrush(QImage(":/images/bg.png").scaled(440+100*2, 440)));
setScene(scene);
setFixedSize(640,440);
}
/*
* Implementation notes: subtitleMove()
* ------------------------------------
* Clear main menu and show subtitle. Choose <code>skip</code> to skip.
*/
void Game::subtitleMove()
{
// clear
scene->clear();
// subtitle Move
QString subtitleContent = "offer收割🐔陈教授🐂🍺!\n"\
"陈教授真的太强了,强无敌!!!\n"\
"陈教授怎么会这么强 其他人真的都是弟弟\n"\
"所以陈教授进入这个地方是干嘛???";
subtitle* beginSubtitle = new subtitle(subtitleContent);
}
/*
* Implementation notes: start() / restartGame()
* -----------------------------
* Start the game and initialize the information.
*/
void Game::start()
{
scene->clear(); // clear
// new backpack
backpackSys = new backpack();
// draw the map
maps = new Map("map.dat"); // TO DO: use relative path
maps->show(0); // initial render & show floor 0
// draw the hero
hero = new Hero();
hero -> setPos(300, 400); // Initial Position
hero->show();
scene->addItem(hero);
hero ->setFlag(QGraphicsItem::ItemIsFocusable);
drawGUI(); // draw the information
// add BGM
// TO DO: use Button to control BGM
QMediaPlayer * music = new QMediaPlayer();
music->setMedia(QUrl("qrc:/sounds/bgm.mp3"));
music->play();
hero->setFocus(); // set focus to Hero
show(); // show game
}
void Game::restartGame()
{
start();
}
/*
* Implementation notes: drawPanel(int x, int y, int width, int height, QColor color, double opacity)
* --------------------------------------------------------------------------------------------------
* Draw the background of information window.
*/
void Game::drawPanel(int x, int y, int width, int height, QColor color, double opacity)
{
// draw the panel
QGraphicsRectItem* panel = new QGraphicsRectItem(x, y, width, height);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(color);
panel->setBrush(brush);
panel->setOpacity(opacity);
scene->addItem(panel);
}
/*
* Implementation notes: displayMainMenu()
* -------------------------------
* Main menu includes three Buttons:
* <code>Play</code>
* <code>Load</code>
* <code>Quit</code>
*/
void Game::displayMainMenu()
{
// show game title
QGraphicsTextItem* titleText = new QGraphicsTextItem(QString("Magic Tower"));
QFont titleFont("comic sans", 50);
titleText->setFont(titleFont);
double txPos = this->width()/2 - titleText->boundingRect().width()/2;
double tyPos = 150;
titleText->setPos(txPos, tyPos);
scene->addItem(titleText);
// Play button
Button* playButton = new Button(QString("Play"), 200, 40);
double bxPos = this->width()/2 - titleText->boundingRect().width()/2+50;
double byPos = 250;
playButton->setPos(bxPos, byPos);
connect(playButton, SIGNAL(clicked()), this, SLOT(subtitleMove()));
scene->addItem(playButton);
// Load button
Button* loadButton = new Button(QString("Load"), 200, 40);
double lxPos = this->width()/2 - titleText->boundingRect().width()/2+50;
double lyPos = 300;
loadButton->setPos(lxPos, lyPos);
connect(loadButton, SIGNAL(clicked()), this, SLOT(showLoadOnMainMenu()));
scene->addItem(loadButton);
// Quit button
Button* quitButton = new Button(QString("Quit"), 200, 40);
double qxPos = this->width()/2 - titleText->boundingRect().width()/2+50;
double qyPos = 350;
quitButton->setPos(qxPos, qyPos);
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
scene->addItem(quitButton);
}
/*
* Implementation notes: drawGUI()
* -------------------------------
* Draw the information about item and hero.
*/
void Game::drawGUI()
{
// left side about hero information
drawPanel(0, 0, 100, 440, Qt::darkCyan, 1);
QGraphicsTextItem* HeroInfo = new QGraphicsTextItem(QString("Hero"));
HeroInfo->setPos(25, 0);
scene->addItem(HeroInfo);
lvText = new QGraphicsTextItem(QString("Lv :\t") + QString::number(hero->getLv()));
hpText = new QGraphicsTextItem(QString("HP :\t") + QString::number(hero->getHp()));
atkText = new QGraphicsTextItem(QString("Attack :\t") + QString::number(hero->getAtk()));
defText = new QGraphicsTextItem(QString("Defence :\t") + QString::number(hero->getDef()));
expText = new QGraphicsTextItem(QString("Exp :\t") + QString::number(hero->getExperience()));
moneyText = new QGraphicsTextItem(QString("Money :\t") + QString::number(hero->getMoney()));
floorText = new QGraphicsTextItem(QString("Floor :\t") + QString::number(hero->getFloor()));
lvText->setPos(0, 40);
hpText->setPos(0, 80);
atkText->setPos(0, 120);
defText->setPos(0, 160);
expText->setPos(0, 200);
moneyText->setPos(0, 240);
floorText->setPos(0, 280);
scene->addItem(lvText);
scene->addItem(hpText);
scene->addItem(atkText);
scene->addItem(defText);
scene->addItem(expText);
scene->addItem(moneyText);
scene->addItem(floorText);
// right side about item information
drawPanel(540, 0, 100, 440, Qt::darkCyan, 1);
QGraphicsTextItem* OtherInfo = new QGraphicsTextItem(QString("Item"));
OtherInfo->setPos(540, 0);
scene->addItem(OtherInfo);
yellowKeyText = new QGraphicsTextItem(QString("Yellow key :\t") + QString::number(hero->getYellowKey()));
blueKeyText = new QGraphicsTextItem(QString("Blue key :\t") + QString::number(hero->getBlueKey()));
redKeyText = new QGraphicsTextItem(QString("Red key :\t") + QString::number(hero->getRedKey()));
yellowKeyText->setPos(540, 40);
blueKeyText->setPos(540, 80);
redKeyText->setPos(540, 120);
scene->addItem(yellowKeyText);
scene->addItem(blueKeyText);
scene->addItem(redKeyText);
// if special tool is obtained, show how to use
// TO DO: special functions for tools
if (hero->getFloor() == true) {
floorFlyText = new QGraphicsTextItem(QString("Fly :\t") + QString("Press F"));
floorFlyText->setPos(540, 160);
scene->addItem(floorFlyText);
}
if (hero->getMonsterInfo() == true) {
monsterSearchText = new QGraphicsTextItem(QString("Search :\t") + QString("Press I"));
monsterSearchText->setPos(540, 200);
scene->addItem(monsterSearchText);
}
}
/*
* Implementation notes: showLoad(), showSave, showLoadOnMainMenu()
* ----------------------------------------------------------------
* Show frame of load, save or load on main menu. The difference between
* <code>showLoad()</code> and <code>showLoadOnMainMenu()</code> is
* <code>backButton</code>.
*/
void Game::showLoad()
{
SaveLoad *loadFrame = new SaveLoad();
loadFrame->showLoadRecord();
}
void Game::showSave()
{
SaveLoad *saveFrame = new SaveLoad();
saveFrame->showSaveRecord();
}
void Game::showLoadOnMainMenu()
{
SaveLoad *loadFrame = new SaveLoad();
loadFrame->showLoadRecordOnMainMenu();
}
/*
* Implementation notes: gameOver(), displayGameoverWindow()
* ---------------------------------------------------------
* Gameover and show information. Choose to start again or quit.
*/
void Game::gameOver()
{
// Display gameover window
QString msg = "YOU WON! \nThis game is our childhood, \n if you like it, give me an A.";
displayGameoverWindow(msg);
}
void Game::displayGameoverWindow(QString textToDisplay)
{
// disable all items
for (int i = 0, n = scene->items().size(); i < n; i++) scene->items()[i]->setEnabled(false);
// draw the background and text
drawPanel(0, 0, 640, 440, Qt::darkCyan, 0.65);
QGraphicsTextItem* titleText = new QGraphicsTextItem(QString(textToDisplay));
QFont titleFont("comic sans", 20);
titleText->setFont(titleFont);
titleText->setPos(60, 100);
scene->addItem(titleText);
// Play again Button
Button* playAgain = new Button(QString("Play Again"), 200, 40);
playAgain->setPos(110, 300);
scene->addItem(playAgain);
connect(playAgain, SIGNAL(clicked()), this, SLOT(restartGame()));
// Quit Button
Button* quitButton = new Button(QString("Quit"), 200, 40);
quitButton->setPos(410, 300);
scene->addItem(quitButton);
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
}
/*
* Implementation notes: renderFloor(int floor)
* -------------------------------
* Re-show map.
*/
void Game::renderFloor(int floor)
{
maps->show(floor); // refresh floor
}
/*
* Implementation note: updateInfo()
* ------------------------------------------------
* Update information on the window.
*/
void Game::updateInfo()
{
// update text
hpText->setPlainText(QString("HP :\t") + QString::number(hero->getHp()));
atkText->setPlainText(QString("Attack :\t") + QString::number(hero->getAtk()));
defText->setPlainText(QString("Defence :\t") + QString::number(hero->getDef()));
lvText->setPlainText(QString("Lv :\t") + QString::number(hero->getLv()));
expText->setPlainText(QString("Exp :\t") + QString::number(hero->getExperience()));
moneyText->setPlainText(QString("Money :\t") + QString::number(hero->getMoney()));
floorText->setPlainText(QString("Floor :\t") + QString::number(hero->getFloor()));
yellowKeyText->setPlainText(QString("Yellow key :\t") + QString::number(hero->getYellowKey()));
blueKeyText->setPlainText(QString("Blue key :\t") + QString::number(hero->getBlueKey()));
redKeyText->setPlainText(QString("Red key :\t") + QString::number(hero->getRedKey()));
// update special tool status
if (hero->getFlyFloor() == true) {
floorFlyText = new QGraphicsTextItem(QString("Fly :\t") + QString("Press F"));
floorFlyText->setPos(540, 160);
scene->addItem(floorFlyText);
}
if (hero->getMonsterInfo() == true) {
monsterSearchText = new QGraphicsTextItem(QString("Search :\t") + QString("Press I"));
monsterSearchText->setPos(540, 200);
scene->addItem(monsterSearchText);
}
}