-
Notifications
You must be signed in to change notification settings - Fork 1
/
Snake.cpp
214 lines (184 loc) · 5.2 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "Snake.hpp"
#include <iostream>
namespace rg {
#pragma region Unit functions
Unit::Unit(Unit* previous_unit, int X, int Y, int size) : Unit(previous_unit, X, Y) {
grap.setSize(sf::Vector2f(size, size));
grap.setFillColor(sf::Color::Cyan);
}
Unit::Unit(Unit* previous_unit, int X, int Y) {
u1 = previous_unit;
this->setPos({ X, Y });
}
void Unit::draw(sf::RenderWindow& window) {
window.draw(grap);
}
void Unit::setPos(Pos pos) {
x = pos.x;
y = pos.y;
grap.setPosition(x, y);
}
void Unit::setNext(Unit* next_unit) {
u2 = next_unit;
}
HeadUnit::HeadUnit(Unit* previous_unit, int X, int Y, int size) : Unit(previous_unit, X, Y) {
u1 = previous_unit;
this->setPos({ X, Y });
m_size = size;
c_adjust_x = 0;
c_adjust_y = 0;
m_grap_rect.setSize(sf::Vector2f(m_size / 2, m_size));
m_grap_rect.setFillColor(sf::Color::Color(0, 139, 139));
m_grap_circle.setRadius(m_size / 2);
m_grap_circle.setFillColor(sf::Color::Color(0, 139, 139));
first_draw = true;
}
void HeadUnit::draw(sf::RenderWindow& window) {
if (!first_draw)
window.draw(m_grap_rect);
else
first_draw = false;
window.draw(m_grap_circle);
}
void HeadUnit::setPos(Pos pos) {
x = pos.x;
y = pos.y;
m_grap_rect.setPosition(x + c_adjust_x, y + c_adjust_y);
m_grap_circle.setPosition(x, y);
}
void HeadUnit::onWayChanged(Snake::Ways new_direction) {
switch (new_direction)
{
case Snake::Ways::UP:
m_grap_rect.setSize(sf::Vector2f(m_size, m_size / 2));
c_adjust_x = 0;
c_adjust_y = m_size / 2;
break;
case Snake::Ways::DOWN:
m_grap_rect.setSize(sf::Vector2f(m_size, m_size / 2));
c_adjust_x = 0;
c_adjust_y = 0;
break;
case Snake::Ways::R:
m_grap_rect.setSize(sf::Vector2f(m_size / 2, m_size));
c_adjust_x = 0;
c_adjust_y = 0;
break;
case Snake::Ways::L:
m_grap_rect.setSize(sf::Vector2f(m_size / 2, m_size));
c_adjust_x = m_size / 2;
c_adjust_y = 0;
break;
default:
break;
}
}
#pragma endregion
Snake::Snake(BaseData data) {
m_outgame_size = data.outgame_size;
m_ingame_width = data.ingame_width;
m_ingame_height = data.ingame_height;
x = m_outgame_size + m_ingame_width / 4;
y = m_outgame_size + m_ingame_height / 2;
m_tail_x = x;
m_tail_y = y;
size = data.snake_size;
head = new HeadUnit(nullptr, x, y, size);//head's prev is nullptr
tail = new Unit(head, x - size, y, size);//start with one body
head->setNext(tail);
now_direction = Ways::R;
last_move_direction = Ways::R;
}
void Snake::setDirection(Ways new_direction) {
if (static_cast<int>(new_direction) + static_cast<int>(last_move_direction))
now_direction = new_direction;
}
bool Snake::UpkeyDown(sf::Keyboard::Key keycode) {
return keycode == sf::Keyboard::Up || keycode == sf::Keyboard::W;
}
bool Snake::DownkeyDown(sf::Keyboard::Key keycode) {
return keycode == sf::Keyboard::Down || keycode == sf::Keyboard::S;
}
bool Snake::RightkeyDown(sf::Keyboard::Key keycode) {
return keycode == sf::Keyboard::Right || keycode == sf::Keyboard::D;
}
bool Snake::LeftkeyDown(sf::Keyboard::Key keycode) {
return keycode == sf::Keyboard::Left || keycode == sf::Keyboard::A;
}
void Snake::detectWayKeys(sf::Keyboard::Key keycode) {
if (UpkeyDown(keycode))
this->setDirection(Snake::Ways::UP);
if (DownkeyDown(keycode))
this->setDirection(Snake::Ways::DOWN);
if (RightkeyDown(keycode))
this->setDirection(Snake::Ways::R);
if (LeftkeyDown(keycode))
this->setDirection(Snake::Ways::L);
}
bool Snake::move() {
switch (now_direction) {
case Ways::UP:
y -= size;
break;
case Ways::DOWN:
y += size;
break;
case Ways::L:
x -= size;
break;
case Ways::R:
x += size;
break;
}
if (now_direction != last_move_direction)
dynamic_cast<HeadUnit*>(head)->onWayChanged(now_direction);
last_move_direction = now_direction;
if (illegalPos())
return false;
Unit* moving = tail;
Pos last_position = tail->getPos();
m_tail_x = last_position.x;
m_tail_y = last_position.y;
while (moving->prev()) {
moving->setPos(moving->prev()->getPos());
moving = moving->prev();
}
head->setPos({ x, y });
return !this->posInBody({ x,y });
}
bool Snake::illegalPos() {
return (x < m_outgame_size || y < m_outgame_size || x > m_outgame_size + m_ingame_width - size || y > m_outgame_size + m_ingame_height - size);
}
bool Snake::posInBody(Pos position) {
Unit* testing = tail;
while (testing->prev()) {
if (testing->getPos() == position)
return true;
testing = testing->prev();
}
return false;
}
void Snake::draw(sf::RenderWindow& window) {
Unit* drawing = tail;
while (drawing->prev()) {
drawing->draw(window);
drawing = drawing->prev();
}
head->draw(window);
}
void Snake::gainFood() {
Unit* prev_tail = tail;
tail = new Unit(tail, this->getTailX_prev(), this->getTailY_prev(), size);
prev_tail->setNext(tail);
}
bool Snake::gameover_delete_tail() {
Unit* new_tail = tail->prev();
delete tail;
tail = new_tail;
return tail;
}
Unit::~Unit() {
}
Snake::~Snake() {
}
}