-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSnakeScreen.cpp
More file actions
57 lines (42 loc) · 1.27 KB
/
SnakeScreen.cpp
File metadata and controls
57 lines (42 loc) · 1.27 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
#include "SnakeScreen.h"
#include "Snake.h"
SnakeScreen::SnakeScreen(Pixel size, int fps): Screen(size, fps) {
hud = new HUD(this, 1);
scoreLabel = new Label(this, "Placeholder Text");
scoreLabel->position = {getSize().x / 4, 0};
scoreLabel->alignment = CENTER;
highscoreLabel = new Label(this, "Placeholder Text 2: Electric Boogaloo");
highscoreLabel->position = {3 * getSize().x / 4, 0};
highscoreLabel->alignment = CENTER;
snake = new Snake(this);
food = new Food(this);
initialize();
}
SnakeScreen::~SnakeScreen() = default;
void SnakeScreen::initialize() {
setScore(0);
snake->initialize();
food->goToRandomPosition();
}
int SnakeScreen::getScore() {
return score;
}
void SnakeScreen::setScore(int score) {
this->score = score;
// update highscore
if (score > highscore) {
highscore = score;
}
// update labels
scoreLabel->getText("SCORE: " + std::to_string(score));
highscoreLabel->getText("HIGHSCORE: " + std::to_string(highscore));
}
void SnakeScreen::gameOver() {
gameOverLabel = new Label(this, "GAME OVER - PRESS R");
gameOverLabel->position = getSize() / 2;
gameOverLabel->alignment = CENTER;
}
void SnakeScreen::restart() {
delete gameOverLabel;
initialize();
}