-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
87 lines (75 loc) · 2.17 KB
/
mainwindow.cpp
File metadata and controls
87 lines (75 loc) · 2.17 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
#include<QKeyEvent>
#include<QDateTime>
#include <string>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "gameresult.h"
#include "ui_gameresult.h"
#include "logic.h"
MainWindow::MainWindow(QWidget *parent, Logic *logic_)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
logic = logic_;
refreshPairs();
timer = Timer();
createTimeUpdater();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::updateTime()
{
ui->time->setText(QString("Time: ")
.append(timer.getTime()));
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if(event->key()== Qt::Key_Right){
logic->updatePoints(logic->isSameColor());
printPoints();
} else if(event->key()== Qt::Key_Left) {
logic->updatePoints(!logic->isSameColor());
printPoints();
} else {
QMainWindow::keyPressEvent(event);
}
refreshPairs();
}
void MainWindow::refreshPairs()
{
maybeGameEnd();
//PAIRS
logic->generatePair();
//onlyColor label set
ui->onlyColor->setText(logic->getRandomColorString());
QString styleStringColor = logic->generateStyleString(logic->getCurrentColor());
ui->onlyColor->setStyleSheet(styleStringColor);
//onlyText label set
ui->onlText->setText(logic->getCurrentColorText());
ui->onlText->setStyleSheet(logic->generateStyleString(logic->getRandomColorString()));
}
void MainWindow::printPoints()
{
this->ui->points->setText(
QString("Points: ")
.append(QString::number(logic->getPoints()))
);
}
void MainWindow::createTimeUpdater()
{
updateTimer = new QTimer(this);
connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateTime()));
updateTimer->start(10); //update every 10 miliseconds
}
void MainWindow::maybeGameEnd()
{
if(logic->getPoints() >= logic->getPointsToWin()){
GameResult* gameResult = new GameResult(this, timer.getTime());
//gameResult->gameFinishdTime; //PRINT TIME HERE
gameResult->setModal(true);
gameResult->exec();
}
}