-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartup.cpp
More file actions
170 lines (141 loc) · 3.92 KB
/
startup.cpp
File metadata and controls
170 lines (141 loc) · 3.92 KB
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
#include "startup.h"
#include "ui_startup.h"
startup::startup(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::startup)
{
ui->setupUi(this);
optionsUi = new Options(this);
helpUi = new help(this);
}
startup::~startup()
{
delete ui;
}
void startup::on_optionsBtn_clicked()
{
optionsUi->show();
}
void startup::on_helpBtn_clicked()
{
helpUi->show();
}
void startup::on_newGameBtn_clicked()
{
gameScreen = new GameScreen(this);
gameScreen->show();
this->hide();
}
void startup::restartGame()
{
delete gameScreen;
gameScreen = new GameScreen(this);
gameScreen->show();
}
void startup::saveGame()
{
gameScreen->ball->object->stop();
gameScreen->paddle->object->stop();
gameScreen->time->stop();
gameScreen->spacePressed = false;
QList<QPushButton*> blocks = gameScreen->findChildren<QPushButton*>("block");
QString filename = QFileDialog::getSaveFileName(this, "Save Form", "", "");
QFile file(filename);
if (!file.open(QIODevice::WriteOnly))
{
QMessageBox::information(this, tr("Unable to open file"), file.errorString());
return;
}
QDataStream out(&file);
out.setVersion(QDataStream::Qt_4_7); // Change this based on current Qt version
out << blocks.length();
for (int i = 0; i < blocks.length(); ++i)
{
QPushButton *block = dynamic_cast<QPushButton*>(blocks[i]);
out << block->styleSheet();
out << block->geometry();
out << block->isEnabled();
}
out << gameScreen->ball->geometry();
Timer *tmr = dynamic_cast<Timer*>(gameScreen->ball->object);
out << tmr->interval();
out << tmr->isActive();
out << gameScreen->paddle->geometry();
tmr = dynamic_cast<Timer*>(gameScreen->paddle->object);
out << tmr->interval();
out << tmr->isActive();
out << gameScreen->numLives;
out << gameScreen->timeLeft;
out << gameScreen->score;
out << gameScreen->level;
out << gameScreen->getEventLog();
out << gameScreen->spacePressed;
delete gameScreen;
this->show();
file.close();
}
void startup::loadGame()
{
int numLives, score, level, timeLeft;
gameScreen = new GameScreen(this);
QList<QPushButton*> blocks = this->findChildren<QPushButton*>("block");
for (int i = 0; i < blocks.count(); i++)
{
delete blocks[i];
}
QString styleSheet, eventLog;
QRect geo;
bool enabled;
int numBlocks, interval;
QString filename = QFileDialog::getOpenFileName(this, "Load Form", "", "");
QFile file(filename);
if (!file.open(QIODevice::ReadOnly))
{
QMessageBox::information(this, tr("Unable to load file"), file.errorString());
return;
}
QDataStream in(&file);
in.setVersion(QDataStream::Qt_4_7);
in >> numBlocks;
for (int i = 0; i < numBlocks; i++)
{
QPushButton *block = new QPushButton(gameScreen);
in >> styleSheet;
in >> geo;
in >> enabled;
block->setStyleSheet(styleSheet);
block->setGeometry(geo);
block->setEnabled(enabled);
block->setObjectName("block");
}
in >> geo;
gameScreen->ball->setGeometry(geo);
in >> interval;
gameScreen->ball->object->setInterval(interval);
in >> enabled;
gameScreen->ball->object->setActive(enabled);
in >> geo;
gameScreen->paddle->setGeometry(geo);
in >> interval;
gameScreen->paddle->object->setInterval(interval);
in >> enabled;
gameScreen->paddle->object->setActive(enabled);
in >> numLives;
gameScreen->setLivesText(numLives);
in >> timeLeft;
gameScreen->setTimeText(timeLeft);
in >> score;
gameScreen->setScoreText(score);
in >> level;
gameScreen->setLevelText(level);
in >> eventLog;
gameScreen->setEventLog(eventLog);
in >> gameScreen->spacePressed;
in >> enabled;
file.close();
gameScreen->show();
}
void startup::on_loadGameBtn_clicked()
{
loadGame();
}