-
Notifications
You must be signed in to change notification settings - Fork 0
/
Score.cpp
124 lines (106 loc) · 3.09 KB
/
Score.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
#include "Score.h"
#include<QMessageBox>
#include<QInputDialog>
#include<QGraphicsTextItem>
#include<iostream>
GameWindow * Score::gameWindow = nullptr;
QGraphicsTextItem * Score::gameScore = nullptr;
int Score::level = 1;
int Score::points = 0;
Score::Score(GameWindow * game, QGraphicsTextItem * text)
{
gameWindow = game;
gameScore = text;
QFont f("Helvetica",30,QFont::Bold);
gameScore->setFont(f);
gameScore->setPlainText(QString("Score: ") + QString::number(points));
gameScore->setDefaultTextColor(Qt::red);
gameScore->setPos(280,-12);
GameWindow::addToScene(gameScore);
}
void Score::increaseScore()
{
points += level * 100;
++level;
gameScore->setPlainText(QString("Score: ") + QString::number(points));
gameScore->setPos(280,-12);
}
void Score::LoadScores()
{
std::ifstream file; //odczyt najlepszych wyników
file.open(std::string(PROJECT_PATH) + "bestScores.txt");
int score = 0;
std::string name;
QList<int> scores;
QList<std::string> names;
while(file >> name >> score)
{
scores.push_back(score);
names.push_back(name);
}
QString text = nullptr;
while(!scores.empty())
{
text += QString::fromStdString(names.front()) + " " + QString::number(scores.front()) + "\n";
scores.pop_front();
names.pop_front();
}
QMessageBox msg;
msg.setWindowTitle("Records");
msg.setText("Best scores: ");
msg.setInformativeText(text);
msg.exec();
file.close();
file.clear();
}
void Score::saveScore()
{
std::ifstream file; //odczyt najlepszych wyników
file.open(std::string(PROJECT_PATH) + "bestScores.txt");
int score = 0;
std::string name;
QList<int> scores;
QList<std::string> names;
while(file >> name >> score)
{
scores.push_back(score);
names.push_back(name);
}
if(points > scores.back())
{
QString playerName = (QInputDialog::getText(Score::gameWindow,"Save","Please write your name"));
for(int i = 0; i <10; ++i)
{
if(points > scores[i])
{
names.insert(i, playerName.toStdString());
scores.insert(i, points);
scores.pop_back();
names.pop_back();
break;
}
}
QString text = nullptr;
for(int i = 0; i < 10; ++i)
{
text += QString::fromStdString(names[i]) + " " + QString::number(scores[i]) + "\n";
}
QMessageBox msg;
msg.setWindowTitle("Records");
msg.setText("Best scores: ");
msg.setInformativeText(text);
msg.exec();
file.close();
file.clear();
std::ofstream p; //zapis do fileu
p.open(std::string(PROJECT_PATH) + "bestScores.txt", std::ofstream::out|std::ofstream::trunc);
while(!scores.empty())
{
p<<names.front()<<std::endl;
p<<scores.front()<<std::endl;
scores.pop_front();
names.pop_front();
}
file.close();
}
}