-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
48 lines (40 loc) · 1.32 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
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QMessageBox>
#include "gamestage.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, scene_(new QGraphicsScene(this))
, view_(new QGraphicsView(scene_, this))
, stage_(new GameStage(scene_, this)) {
ui->setupUi(this);
setCentralWidget(view_);
setFixedSize(600, 600);
setWindowTitle("QSnake");
//QPixmap bg(SNAKE_SIZE, SNAKE_SIZE);
//QPainter p(&bg);
//p.setBrush(QBrush(Qt::gray));
//p.drawRect(0, 0, SNAKE_SIZE, SNAKE_SIZE);
//view_->setBackgroundBrush(QBrush(bg));
view_->setBackgroundBrush(QBrush(Qt::black));
view_->viewport()->setFocusProxy(nullptr);
scene_->setSceneRect(-100, -100, 200, 200);
QObject::connect(stage_, &GameStage::gameOver, this, &MainWindow::onGameOver);
}
void MainWindow::adjustViewSize() {
view_->fitInView(scene_->sceneRect(), Qt::KeepAspectRatioByExpanding);
}
void MainWindow::onGameOver() {
QMessageBox::information(nullptr,
"",
"GameOver",
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes);
stage_->reset();
}
MainWindow::~MainWindow() {
delete ui;
}