-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.cpp
More file actions
35 lines (29 loc) · 777 Bytes
/
Snake.cpp
File metadata and controls
35 lines (29 loc) · 777 Bytes
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
#include "Snake.hpp"
Snake::Snake(size_t& field_height, size_t& field_width){
direction = RIGHT;
Body.push_back({static_cast<int>(field_height)/2, static_cast<int>(field_width)/2});
}
void Snake::move() {
QPoint head = Body.front();
switch (direction) {
case UP: head.ry() -= 1; break;
case DOWN: head.ry() += 1; break;
case LEFT: head.rx() -= 1; break;
case RIGHT: head.rx() += 1; break;
}
Body.push_front(head);
if (!grow) Body.pop_back();
else grow = false;
}
void Snake::setDirection(Direction dir) {
direction = dir;
}
void Snake::growSnake() {
grow = true;
}
const std::vector<QPoint>& Snake::getBody() const {
return Body;
}
QPoint Snake::getHead() const {
return Body.front();
}