-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.cpp
146 lines (132 loc) · 4.48 KB
/
update.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "game.hpp"
void savetofile(int score)
{
fstream file;
file.open("score.txt", ios::out);
file << score;
file.close();
}
void Game::update()
{
if (timeSinceLastMove.asSeconds() >= seconds(1.f / float(speed)).asSeconds())
{
Vector2f thisSectionPosition = snake[0].getPosition();
Vector2f lastSectionPosition = thisSectionPosition;
if (!NextMove.empty()) //no turning 180*
{
switch (snakeDirection)
{
case Direction::UP:
if (NextMove.front() != Direction::DOWN)
{
snakeDirection = NextMove.front();
}
break;
case Direction::DOWN:
if (NextMove.front() != Direction::UP)
{
snakeDirection = NextMove.front();
}
break;
case Direction::LEFT:
if (NextMove.front() != Direction::RIGHT)
{
snakeDirection = NextMove.front();
}
break;
case Direction::RIGHT:
if (NextMove.front() != Direction::LEFT)
{
snakeDirection = NextMove.front();
}
break;
}
NextMove.pop_front();
}
// Score //todo
score += snake.size() * (applesEatenCountTotal + 1);
scoreText.setString(to_string(score));
FloatRect scoreTextBounds = scoreText.getLocalBounds();
scoreText.setPosition(Vector2f(resolution.x - scoreTextBounds.width - 15, -9));
// Growing
if (BodyToAdd)
{
addSnakeBody();
BodyToAdd--;
}
// Update head
switch (snakeDirection)
{
case Direction::RIGHT:
snake[0].setPosition(Vector2f(thisSectionPosition.x + 20, thisSectionPosition.y));
break;
case Direction::DOWN:
snake[0].setPosition(Vector2f(thisSectionPosition.x, thisSectionPosition.y + 20));
break;
case Direction::LEFT:
snake[0].setPosition(Vector2f(thisSectionPosition.x - 20, thisSectionPosition.y));
break;
case Direction::UP:
snake[0].setPosition(Vector2f(thisSectionPosition.x, thisSectionPosition.y - 20));
break;
}
// Update the snake tail positions
for (int s = 1; s < snake.size(); s++)
{
thisSectionPosition = snake[s].getPosition();
snake[s].setPosition(lastSectionPosition);
lastSectionPosition = thisSectionPosition;
}
// update snake sections
for (auto& s : snake)
{
s.update();
}
// Collision detection - Apple
if (snake[0].getShape().getGlobalBounds().intersects(apple.getSprite().getGlobalBounds()))
{
// If apple scored, increase speed and move the apple
applesEatenCount += 1;
applesEatenCountTotal += 1;
applesText.setString("apples " + to_string(applesEatenCountTotal));
FloatRect currentLevelTextBounds = currentLevelText.getGlobalBounds();
applesText.setPosition(
Vector2f(currentLevelTextBounds.left + currentLevelTextBounds.width + 20, -9));
bool beginningNewLevel = false;
if (applesEatenCount >= 8)
{
// Begin the next if exist
if (currentLevel < maxLevels)
{
beginningNewLevel = true;
beginNextLevel();
}
}
if (!beginningNewLevel)
{
BodyToAdd += 4;
speed++;
moveApple();
}
}
// Collision to body
for (int s = 1; s < snake.size(); s++)
{
if (snake[0].getShape().getGlobalBounds().intersects(snake[s].getShape().getGlobalBounds()))
{
currentState = GameState::GAMEOVER;
savetofile(applesEatenCountTotal);
}
}
// Collision to Wall
for (auto& w : wallSections)
{
if (snake[0].getShape().getGlobalBounds().intersects(w.getShape().getGlobalBounds()))
{
currentState = GameState::GAMEOVER;
savetofile(applesEatenCountTotal);
}
}
timeSinceLastMove = Time::Zero;
}
}