-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.cpp
More file actions
39 lines (34 loc) · 937 Bytes
/
Snake.cpp
File metadata and controls
39 lines (34 loc) · 937 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
36
37
38
39
#include "Snake.h"
using namespace std;
Snake::Snake()
{
tup2 pos0 = {0, 0}, pos1 = {1, 0}, pos2 = {2, 0}, pos3 = {3, 0};
body = {pos3, pos2, pos1, pos0};
direction = {1, 0}; //right
}
void Snake::take_step(tup2 pos)
{
body.erase(body.end()-1);
body.insert(body.begin(), pos);
}
void Snake::extend_body()
{
tup2 ext_pos{};
int last = body.size() - 1;
Snake::body_t myBody = body;
if(myBody[last][0] == myBody[last-1][0])
{
if(!myBody[last-1][1] == myBody[last][1]+1)
ext_pos = {myBody[last][0], myBody[last][1]+1};
else
ext_pos = {myBody[last][0], myBody[last][1]-1};
}
else
{
if(!myBody[last-1][0] == myBody[last][0]+1)
ext_pos = {myBody[last][0]+1, myBody[last][1]};
else
ext_pos = {myBody[last][0]-1, myBody[last][1]};
}
body.push_back(ext_pos);
}