Repository navigation
Expand file tree
/
Copy pathSnake.cpp
More file actions
136 lines (111 loc) · 4.41 KB
/
Copy pathSnake.cpp
File metadata and controls
136 lines (111 loc) · 4.41 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
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
#include "Snake.hpp"
#include "Config.hpp"
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Keyboard.hpp>
Snake::Snake(sf::RenderWindow& window) : m_window(window), m_dir(Direction::Right) {
m_body.clear();
sf::RectangleShape head;
head.setSize({SNAKE_SEGMENT_SIZE, SNAKE_SEGMENT_SIZE});
head.setFillColor(sf::Color::Green);
head.setPosition({(float)WINDOW_WIDTH / 2, (float)WINDOW_HEIGHT / 2});
m_body.push_back(head);
sf::RectangleShape bodySegment;
bodySegment.setSize({SNAKE_SEGMENT_SIZE, SNAKE_SEGMENT_SIZE});
bodySegment.setFillColor(sf::Color(0, 150, 0));
bodySegment.setPosition({(float)WINDOW_WIDTH / 2 - SNAKE_SEGMENT_SIZE, (float)WINDOW_HEIGHT / 2});
m_body.push_back(bodySegment);
bodySegment.setPosition({(float)WINDOW_WIDTH / 2 - SNAKE_SEGMENT_SIZE * 2, (float)WINDOW_HEIGHT / 2});
m_body.push_back(bodySegment);
}
void Snake::Grow() {
m_shouldGrow = true;
}
void Snake::HandleInput() {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) && m_dir != Direction::Down) {
m_dir = Direction::Up;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) && m_dir != Direction::Up) {
m_dir = Direction::Down;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) && m_dir != Direction::Right) {
m_dir = Direction::Left;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) && m_dir != Direction::Left) {
m_dir = Direction::Right;
}
}
bool Snake::Update() {
if (m_moveClock.getElapsedTime() >= m_moveInterval) {
sf::Vector2f newHeadPos = m_body.front().getPosition();
switch (m_dir) {
case Direction::Up: newHeadPos.y -= SNAKE_SEGMENT_SIZE; break;
case Direction::Down: newHeadPos.y += SNAKE_SEGMENT_SIZE; break;
case Direction::Left: newHeadPos.x -= SNAKE_SEGMENT_SIZE; break;
case Direction::Right: newHeadPos.x += SNAKE_SEGMENT_SIZE; break;
case Direction::None: break;
}
sf::RectangleShape newHead;
newHead.setSize({SNAKE_SEGMENT_SIZE, SNAKE_SEGMENT_SIZE});
newHead.setFillColor(sf::Color::Green);
newHead.setPosition(newHeadPos);
m_body.push_front(newHead);
if (m_body.size() > 1) {
m_body[1].setFillColor(sf::Color(0, 150, 0));
}
if (m_shouldGrow) {
m_shouldGrow = false;
} else {
m_body.pop_back();
}
m_moveClock.restart();
return true;
}
return false;
}
sf::Vector2f lerp(const sf::Vector2f& start, const sf::Vector2f& end, float t) {
return start + (end - start) * t;
}
void Snake::Draw() {
float alpha = m_moveClock.getElapsedTime().asSeconds() / m_moveInterval.asSeconds();
if (alpha > 1.0f) alpha = 1.0f;
sf::RectangleShape segmentShape({SNAKE_SEGMENT_SIZE, SNAKE_SEGMENT_SIZE});
sf::Vector2f headPos = m_body.front().getPosition();
sf::Vector2f nextHeadPos = headPos;
switch (m_dir) {
case Direction::Up: nextHeadPos.y -= SNAKE_SEGMENT_SIZE; break;
case Direction::Down: nextHeadPos.y += SNAKE_SEGMENT_SIZE; break;
case Direction::Left: nextHeadPos.x -= SNAKE_SEGMENT_SIZE; break;
case Direction::Right: nextHeadPos.x += SNAKE_SEGMENT_SIZE; break;
case Direction::None: break;
}
segmentShape.setPosition(lerp(headPos, nextHeadPos, alpha));
segmentShape.setFillColor(sf::Color::Green);
m_window.draw(segmentShape);
for (size_t i = 1; i < m_body.size(); ++i) {
sf::Vector2f currentPos = m_body[i].getPosition();
sf::Vector2f prevPos = m_body[i - 1].getPosition();
segmentShape.setPosition(lerp(currentPos, prevPos, alpha));
segmentShape.setFillColor(sf::Color(0, 150, 0));
m_window.draw(segmentShape);
}
}
const sf::Vector2f& Snake::GetHeadPosition() const {
return m_body.front().getPosition();
}
const std::deque<sf::RectangleShape>& Snake::GetBody() const {
return m_body;
}
bool Snake::checkCollision() const {
const sf::Vector2f& headPos = m_body.front().getPosition();
if (headPos.x < 0 || headPos.x >= WINDOW_WIDTH ||
headPos.y < 0 || headPos.y >= WINDOW_HEIGHT) {
return true;
}
for (auto it = std::next(m_body.begin()); it != m_body.end(); ++it) {
if (headPos == it->getPosition()) {
return true;
}
}
return false;
}