This repository has been archived by the owner on Dec 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
252 lines (211 loc) · 7.89 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLayout>
#include <QFileDialog>
#include <QMessageBox>
#include <QAction>
#include <QLCDNumber>
#include <QStyle>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle(tr("My Snake Game"));
gamearea = new GameArea(this);
createActions();
createMenus();
createToolbar();
createButtons();
QLCDNumber *clock = new QLCDNumber(this);
clock->setMaximumHeight(80);
clock->setSegmentStyle(QLCDNumber::Filled);
QWidget *buttonWidget = new QWidget(this);
buttonWidget->setLayout(new QVBoxLayout);
buttonWidget->layout()->addWidget(openButton);
buttonWidget->layout()->addWidget(saveButton);
buttonWidget->layout()->addWidget(startButton);
buttonWidget->layout()->addWidget(pauseButton);
buttonWidget->layout()->addWidget(resumeButton);
buttonWidget->layout()->addWidget(resetButton);
buttonWidget->layout()->addWidget(exitButton);
QWidget *c_widget = new QWidget(this);
setCentralWidget(c_widget);
QGridLayout *gridLayout = new QGridLayout;
gridLayout->setAlignment(Qt::AlignTop);
c_widget->setLayout(gridLayout);
gridLayout->addWidget(gamearea, 0, 0, -1, 6);
gridLayout->setRowStretch(0, 1);
gridLayout->addWidget(clock, 0, 7);
gridLayout->addWidget(buttonWidget, 1, 7);
connect(this, SIGNAL(toggleState()), gamearea, SLOT(stateChanged()));
connect(gamearea, SIGNAL(gameResumed()), this, SLOT(gameHasStarted()));
connect(gamearea, SIGNAL(gamePaused()), this, SLOT(gameHasPaused()));
connect(gamearea, SIGNAL(gameReset()), this, SLOT(gameHasReset()));
connect(gamearea, SIGNAL(gameEnded()), this, SLOT(gameHasStopped()));
connect(gamearea, SIGNAL(updateTimer(int)), clock, SLOT(display(int)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Space:
emit toggleState();
break;
default:
QWidget::keyPressEvent(event);
}
}
void MainWindow::createActions()
{
auto useIcon = [](QStyle::StandardPixmap sp) {
return QApplication::style()->standardIcon(sp);
};
openAct = new QAction(useIcon(QStyle::SP_DialogOpenButton), tr("&Load..."), this);
openAct->setShortcuts(QKeySequence::Open);
openAct->setStatusTip(tr("Open a saved game"));
connect(openAct, SIGNAL(triggered()), gamearea, SLOT(loadGame()));
saveAct = new QAction(useIcon(QStyle::SP_DialogSaveButton), tr("&Save..."), this);
saveAct->setShortcuts(QKeySequence::Save);
saveAct->setStatusTip(tr("Save the current game state to disk"));
saveAct->setEnabled(false);
connect(saveAct, SIGNAL(triggered()), gamearea, SLOT(saveGame()));
exitAct = new QAction(useIcon(QStyle::SP_DialogCloseButton), tr("&Exit"), this);
exitAct->setShortcuts(QKeySequence::Quit);
exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, &QAction::triggered, this, &QWidget::close);
startAct = new QAction(useIcon(QStyle::SP_MediaPlay), tr("Start"), this);
startAct->setStatusTip(tr("Begin the game"));
connect(startAct, SIGNAL(triggered()), gamearea, SLOT(startGame()));
pauseAct = new QAction(useIcon(QStyle::SP_MediaPause), tr("Pause"), this);
pauseAct->setStatusTip(tr("Pause the game"));
pauseAct->setEnabled(false);
connect(pauseAct, SIGNAL(triggered()), gamearea, SLOT(pauseGame()));
resumeAct = new QAction(useIcon(QStyle::SP_MediaSeekForward), tr("Resume"), this);
resumeAct->setStatusTip(tr("Continue the game"));
resumeAct->setEnabled(false);
connect(resumeAct, SIGNAL(triggered()), gamearea, SLOT(resumeGame()));
resetAct = new QAction(useIcon(QStyle::SP_DialogResetButton), tr("&Reset"), this);
resetAct->setStatusTip(tr("Reset the game and restore to default state"));
resetAct->setEnabled(false);
connect(resetAct, SIGNAL(triggered()), gamearea, SLOT(resetGame()));
}
void MainWindow::createMenus()
{
fileMenu = ui->menubar->addMenu(tr("&File"));
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
gameMenu = ui->menubar->addMenu(tr("Game"));
gameMenu->addAction(startAct);
gameMenu->addAction(pauseAct);
gameMenu->addAction(resumeAct);
gameMenu->addAction(resetAct);
}
void MainWindow::createToolbar()
{
ui->toolBar->addAction(openAct);
ui->toolBar->addAction(saveAct);
ui->toolBar->addSeparator();
ui->toolBar->addAction(startAct);
ui->toolBar->addAction(pauseAct);
ui->toolBar->addAction(resumeAct);
ui->toolBar->addAction(resetAct);
ui->toolBar->addSeparator();
ui->toolBar->addAction(exitAct);
}
void MainWindow::createButtons()
{
QSizePolicy policy(QSizePolicy::Minimum,
QSizePolicy::MinimumExpanding);
openButton = new QPushButton(tr("&Load..."));
openButton->setSizePolicy(policy);
connect(openButton, SIGNAL(pressed()), gamearea, SLOT(loadGame()));
saveButton = new QPushButton(tr("&Save..."));
saveButton->setSizePolicy(policy);
connect(saveButton, SIGNAL(pressed()), gamearea, SLOT(saveGame()));
saveButton->setEnabled(false);
exitButton = new QPushButton(tr("&Exit"));
exitButton->setSizePolicy(policy);
connect(exitButton, &QPushButton::pressed, this, &QWidget::close);
startButton = new QPushButton(tr("Start"));
startButton->setSizePolicy(policy);
connect(startButton, SIGNAL(pressed()), gamearea, SLOT(startGame()));
startButton->setEnabled(true);
pauseButton = new QPushButton(tr("Pause"));
pauseButton->setSizePolicy(policy);
connect(pauseButton, SIGNAL(pressed()), gamearea, SLOT(pauseGame()));
pauseButton->setEnabled(false);
resumeButton = new QPushButton(tr("Resume"));
resumeButton->setSizePolicy(policy);
connect(resumeButton, SIGNAL(pressed()), gamearea, SLOT(resumeGame()));
resumeButton->setEnabled(false);
resetButton = new QPushButton(tr("&Reset"));
resetButton->setSizePolicy(policy);
connect(resetButton, SIGNAL(pressed()), gamearea, SLOT(resetGame()));
resetButton->setEnabled(false);
}
void MainWindow::gameHasReset()
{
openAct->setEnabled(true);
saveAct->setEnabled(false);
startAct->setEnabled(true);
pauseAct->setEnabled(false);
resumeAct->setEnabled(false);
resetAct->setEnabled(false);
openButton->setEnabled(true);
saveButton->setEnabled(false);
startButton->setEnabled(true);
pauseButton->setEnabled(false);
resumeButton->setEnabled(false);
resetButton->setEnabled(false);
}
void MainWindow::gameHasStarted()
{
openAct->setEnabled(false);
saveAct->setEnabled(false);
startAct->setEnabled(false);
pauseAct->setEnabled(true);
resumeAct->setEnabled(false);
resetAct->setEnabled(false);
openButton->setEnabled(false);
saveButton->setEnabled(false);
startButton->setEnabled(false);
pauseButton->setEnabled(true);
resumeButton->setEnabled(false);
resetButton->setEnabled(false);
}
void MainWindow::gameHasStopped()
{
openAct->setEnabled(false);
saveAct->setEnabled(false);
startAct->setEnabled(false);
pauseAct->setEnabled(false);
resumeAct->setEnabled(false);
resetAct->setEnabled(true);
openButton->setEnabled(false);
saveButton->setEnabled(false);
startButton->setEnabled(false);
pauseButton->setEnabled(false);
resumeButton->setEnabled(false);
resetButton->setEnabled(true);
}
void MainWindow::gameHasPaused()
{
openAct->setEnabled(false);
saveAct->setEnabled(true);
startAct->setEnabled(false);
pauseAct->setEnabled(false);
resumeAct->setEnabled(true);
resetAct->setEnabled(true);
openButton->setEnabled(false);
saveButton->setEnabled(true);
startButton->setEnabled(false);
pauseButton->setEnabled(false);
resumeButton->setEnabled(true);
resetButton->setEnabled(true);
}