-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.cpp
48 lines (41 loc) · 922 Bytes
/
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
#include "snake.hpp"
#include "renderer.hpp"
Snake::Snake(const stf::Vec2d startPos)
{
for(int i = 0; i < snake_settings::DEF_LENGHT; ++i) {
m_body.push_back({startPos.x + i, startPos.y});
}
}
void Snake::feed()
{
m_body.push_back(m_body.back());
}
bool Snake::isAteHerself() const
{
for(size_t i = 1; i < m_body.size(); ++i) {
if(head().diff(m_body.at(i)) < 1.f) {
return true;
}
}
return false;
}
void Snake::show(stf::Renderer &renderer, const stf::Vec2d &camera)
{
for (size_t i = 0; i < m_body.size(); ++i) {
char sym = 'o';
if(i != 0 && i % 2 == 0) {
sym = 'x';
}
renderer.drawPixel(m_body.at(i) - camera, sym);
}
}
void Snake::update()
{
moveBody();
m_body.at(0) += m_vel;
}
void Snake::moveBody()
{
for(auto it = m_body.end()-1; it > m_body.begin(); --it)
*it = *(it - 1);
}