-
Notifications
You must be signed in to change notification settings - Fork 0
/
Score.cpp
61 lines (48 loc) · 1 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
#include "Score.h"
#include <string>
Score::Score(int x, int y) {
for(int i(0); i < 10; i++) {
std::string file = "Resources/";
file += std::to_string(i);
file += ".png";
numsTextures[i].loadFromFile(file);
nums[i].setTexture(numsTextures[i]);
}
xPos = x;
yPos = y;
score = 0;
}
int Score::getDigits(int num) const {
int digits = 0;
int temp = num;
do {
digits++;
temp/= 10;
} while(temp >= 1);
return digits;
}
int Score::setFirstDigitsPos(std::string num, int x) {
char digit = num[0];
int index = digit - '0';
nums[index].setPosition(x, yPos);
return index;
}
int Score::getScore() const {
return score;
}
void Score::addOne() {
score++;
}
void Score::reset() {
score = 0;
}
void Score::draw(sf::RenderTarget& target, sf::RenderStates states) const {
std::string temp = std::to_string(score);
int x(xPos);
for(int i(0); i < getDigits(score); i++) {
int a = const_cast<Score*>(this)->setFirstDigitsPos(temp, x);
target.draw(nums[a], states);
temp.erase(0,1);
x+= 12;
}
}