-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake.cpp
174 lines (148 loc) · 4.77 KB
/
Snake.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "Snake.hpp"
Snake::Snake(int l_blockSize, Textbox* l_log) {
m_log = l_log;
m_size = l_blockSize;
m_bodyRect.setSize(sf::Vector2f(m_size - 1, m_size - 1));
Reset();
}
Snake::~Snake(){}
sf::Vector2i Snake::GetPosition() {
return (!m_snakeBody.empty() ? m_snakeBody.front().position : sf::Vector2i(1,1));
}
int Snake::GetLives() { return m_lives; }
int Snake::GetScore() { return m_score; }
void Snake::IncreaseScore() {
m_score += 10;
m_log->Add("You ate an apple. Score: "
+ std::to_string((long long)m_score));
}
Direction Snake::GetPhysicalDirection() {
if(m_snakeBody.size() <= 1) {
return Direction::None;
}
SnakeSegment& head = m_snakeBody[0];
SnakeSegment& neck = m_snakeBody[1];
if(head.position.x == neck.position.x) {
return (head.position.y > neck.position.y
? Direction::Down : Direction::Up);
} else if (head.position.y == neck.position.y) {
return (head.position.x > neck.position.x
? Direction::Right : Direction::Left);
}
return Direction::None;
}
void Snake::Reset() {
m_snakeBody.clear();
m_snakeBody.push_back(SnakeSegment(5,7));
m_snakeBody.push_back(SnakeSegment(5,6));
m_snakeBody.push_back(SnakeSegment(5,5));
SetDirection(Direction::None);
m_speed = 15;
m_lives = 3;
m_score = 0;
m_lost = false;
}
void Snake::SetDirection(Direction l_dir) {
m_dir = l_dir;
}
Direction Snake::GetDirection() {
return m_dir;
}
int Snake::GetSpeed() {
return m_speed;
}
bool Snake::HasLost() { return m_lost; }
void Snake::Lose() { m_lost = true; }
void Snake::ToggleLost() { m_lost = !m_lost; }
void Snake::Extend(){
if (m_snakeBody.empty()){ return; }
SnakeSegment& tail_head =
m_snakeBody[m_snakeBody.size() - 1];
if(m_snakeBody.size() > 1){
SnakeSegment& tail_bone =
m_snakeBody[m_snakeBody.size() - 2];
if(tail_head.position.x == tail_bone.position.x){
if(tail_head.position.y > tail_bone.position.y){
m_snakeBody.push_back(SnakeSegment(
tail_head.position.x, tail_head.position.y + 1));
} else {
m_snakeBody.push_back(SnakeSegment(
tail_head.position.x, tail_head.position.y - 1));
}
} else if(tail_head.position.y == tail_bone.position.y){
if(tail_head.position.x > tail_bone.position.x){
m_snakeBody.push_back(SnakeSegment(
tail_head.position.x + 1, tail_head.position.y));
} else {
m_snakeBody.push_back(SnakeSegment(
tail_head.position.x - 1, tail_head.position.y));
}
}
} else {
if(m_dir == Direction::Up){
m_snakeBody.push_back(SnakeSegment(
tail_head.position.x, tail_head.position.y + 1));
} else if (m_dir == Direction::Down){
m_snakeBody.push_back(SnakeSegment(
tail_head.position.x, tail_head.position.y - 1));
} else if (m_dir == Direction::Left){
m_snakeBody.push_back(SnakeSegment(
tail_head.position.x + 1, tail_head.position.y));
} else if (m_dir == Direction::Right){
m_snakeBody.push_back(SnakeSegment(
tail_head.position.x - 1, tail_head.position.y));
}
}
}
void Snake::Tick() {
if (m_snakeBody.empty()) { return ; }
if (m_dir == Direction::None) { return; }
Move();
CheckCollision();
}
void Snake::Move() {
for (int i = m_snakeBody.size() - 1; i > 0; --i) {
m_snakeBody[i].position = m_snakeBody[i - 1].position;
}
if (m_dir == Direction::Left) {
--m_snakeBody[0].position.x;
} else if (m_dir == Direction::Right) {
++m_snakeBody[0].position.x;
} else if (m_dir == Direction::Up) {
--m_snakeBody[0].position.y;
} else if (m_dir == Direction::Down) {
++m_snakeBody[0].position.y;
}
}
void Snake::CheckCollision() {
if (m_snakeBody.size() < 5) { return; }
SnakeSegment& head = m_snakeBody.front();
for (auto itr = m_snakeBody.begin() + 1; itr != m_snakeBody.end(); ++itr) {
if (itr->position == head.position) {
int segments = m_snakeBody.end() - itr;
Cut(segments);
break;
}
}
}
void Snake::Cut(int l_segments) {
for (int i = 0; i < l_segments; ++i) {
m_snakeBody.pop_back();
}
--m_lives;
if (!m_lives) { Lose(); return; }
m_log->Add("You have lost a life! Lives left: "
+ std::to_string((long long)m_lives));
}
void Snake::Render(sf::RenderWindow& l_window) {
if (m_snakeBody.empty()) { return; }
auto head = m_snakeBody.begin();
m_bodyRect.setFillColor(sf::Color::Yellow);
m_bodyRect.setPosition(head->position.x * m_size, head->position.y * m_size);
l_window.draw(m_bodyRect);
m_bodyRect.setFillColor(sf::Color::Green);
for (auto itr = m_snakeBody.begin() + 1; itr != m_snakeBody.end(); ++itr) {
m_bodyRect.setPosition(itr->position.x * m_size, itr->position.y * m_size);
l_window.draw(m_bodyRect);
}
}