-
Notifications
You must be signed in to change notification settings - Fork 75
/
mainwindow.cpp
125 lines (103 loc) · 3.49 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
#include <QGraphicsView>
#include <QTimer>
#include <qaction.h>
#include <qmenubar.h>
#include <qapplication.h>
#include <qmessagebox.h>
#include "constants.h"
#include "gamecontroller.h"
#include "mainwindow.h"
#include <QIcon>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
scene(new QGraphicsScene(this)),
view(new QGraphicsView(scene, this)),
game(new GameController(*scene, this))
{
setCentralWidget(view);
// resize(600, 600);
setFixedSize(600, 600);
setWindowIcon(QIcon(":/images/snake_ico"));
createActions();
createMenus();
initScene();
initSceneBackground();
QTimer::singleShot(0, this, SLOT(adjustViewSize()));
}
MainWindow::~MainWindow()
{
}
void MainWindow::adjustViewSize()
{
view->fitInView(scene->sceneRect(), Qt::KeepAspectRatioByExpanding);
}
void MainWindow::createActions()
{
newGameAction = new QAction(tr("&New Game"), this);
newGameAction->setShortcuts(QKeySequence::New);
newGameAction->setStatusTip(tr("Start a new game"));
connect(newGameAction, &QAction::triggered, this, &MainWindow::newGame);
exitAction = new QAction(tr("&Exit"), this);
exitAction->setShortcut(tr("Ctrl+Q"));
exitAction->setStatusTip(tr("Exit the game"));
connect(exitAction, &QAction::triggered, this, &MainWindow::close);
pauseAction = new QAction(tr("&Pause"), this);
pauseAction->setStatusTip(tr("Pause..."));
connect(pauseAction, &QAction::triggered, game, &GameController::pause);
resumeAction = new QAction(tr("&Resume"), this);
resumeAction->setStatusTip(tr("Resume..."));
resumeAction->setEnabled(false);
game->setResumeAction(resumeAction);
connect(resumeAction, &QAction::triggered, game, &GameController::resume);
gameHelpAction = new QAction(tr("Game &Help"), this);
gameHelpAction->setShortcut(tr("Ctrl+H"));
gameHelpAction->setStatusTip(tr("Help on this game"));
connect(gameHelpAction, &QAction::triggered, this, &MainWindow::gameHelp);
aboutAction = new QAction(tr("&About"), this);
aboutAction->setStatusTip(tr("Show the application's about box"));
connect(aboutAction, &QAction::triggered, this, &MainWindow::about);
aboutQtAction = new QAction(tr("About &Qt"), this);
aboutQtAction->setStatusTip(tr("Show the Qt library's About box"));
connect(aboutQtAction, &QAction::triggered, qApp, QApplication::aboutQt);
}
void MainWindow::createMenus()
{
QMenu *options = menuBar()->addMenu(tr("&Options"));
options->addAction(newGameAction);
options->addSeparator();
options->addAction(pauseAction);
options->addAction(resumeAction);
options->addSeparator();
options->addAction(exitAction);
QMenu *help = menuBar()->addMenu(tr("&Help"));
help->addAction(gameHelpAction);
help->addAction(aboutAction);
help->addAction(aboutQtAction);
}
void MainWindow::initScene()
{
scene->setSceneRect(-100, -100, 200, 200);
}
void MainWindow::initSceneBackground()
{
QPixmap bg(TILE_SIZE, TILE_SIZE);
QPainter p(&bg);
p.setBrush(QBrush(Qt::gray));
p.drawRect(0, 0, TILE_SIZE, TILE_SIZE);
view->setBackgroundBrush(QBrush(bg));
}
void MainWindow::newGame()
{
QTimer::singleShot(0, game, SLOT(gameOver()));
}
void MainWindow::about()
{
QMessageBox::about(this, tr("About this Game"), tr("<h2>Snake Game</h2>"
"<p>Copyright © XXX."
"<p>This game is a small Qt application. It is based on the demo in the GitHub written by Devbean."));
}
void MainWindow::gameHelp()
{
QMessageBox::about(this, tr("Game Help"), tr("Using direction keys to control the snake to eat the food"
"<p>Space - pause & resume"));
}