-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.cpp
More file actions
49 lines (41 loc) · 1.31 KB
/
Snake.cpp
File metadata and controls
49 lines (41 loc) · 1.31 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
#include"Snake.h"
Snake::Snake(const int x, const int y, const int w, const int h) : x(x), y(y), w(w), h(h) {
parts.push_back(new Square(Main::window.getRenderer(), x, y, w, h, SDL_Color{0, 255, 0}));
}
void Snake::move() {
if (direction == Direction::NONE) return;
switch (direction) {
case Direction::UP:
y -= h;
parts.push_front(new Square(Main::window.getRenderer(), x, y, w, h, SDL_Color{ 0, 255, 0 }));
break;
case Direction::DOWN:
y += h;
parts.push_front(new Square(Main::window.getRenderer(), x, y, w, h, SDL_Color{ 0, 255, 0 }));
break;
case Direction::LEFT:
x -= w;
parts.push_front(new Square(Main::window.getRenderer(), x, y, w, h, SDL_Color{ 0, 255, 0 }));
break;
case Direction::RIGHT:
x += w;
parts.push_front(new Square(Main::window.getRenderer(), x, y, w, h, SDL_Color{ 0, 255, 0 }));
break;
}
// delete last element before pop_back
delete parts.back();
parts.pop_back();
}
void Snake::grow() {
parts.push_back(new Square(Main::window.getRenderer(), x, y, w, h, SDL_Color{ 0, 255, 0 }));
}
void Snake::reset(const int newX, const int newY) {
for (auto i = parts.begin(); i != parts.end(); ++i) {
delete* i;
}
parts.clear();
x = newX;
y = newY;
parts.push_front(new Square(Main::window.getRenderer(), x, y, w, h, SDL_Color{ 0, 255, 0 }));
direction = Direction::NONE;
}