-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.cpp
78 lines (63 loc) · 1.35 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
#include"snake.h"
vector<pair<int,int>> body;
pair<int,int> last_tail_location;
void init_snake()
{
body.clear();
body.push_back({10,10});
body.push_back({10,11});
body.push_back({10,12});
}
void paint_snake()
{
attron(COLOR_PAIR(SNAKE_COLOR_PAIR));
for(int i=0;i<body.size();i++){
auto location= body[i];
move(location.first, location.second);
addstr("\u2592");
}
attroff(COLOR_PAIR(SNAKE_COLOR_PAIR));
}
pair<int, int> move_snake(int direction)
{
last_tail_location=body.back();
body.pop_back();
pair<int,int> head= body[0];
pair<int,int> new_head= {head.first, head.second};
if(direction== LEFT){
new_head.second --;
}
else if(direction== RIGHT){
new_head.second ++;
}
else if(direction== DOWN){
new_head.first ++;
}
else if(direction== UP){
new_head.first --;
}
body.insert(body.begin(),new_head);
return new_head;
}
void grow_snake()
{
body.push_back(last_tail_location);
}
void reset_snake()
{
init_snake();
}
bool has_collision()
{
pair<int,int> head = body[0];
int x=head.first;
int y=head.second;
if(x==0 or x == LINES-1 or y==0 or y==COLS -1){
return true;
}
for(int i= 1; i<body.size(); i++){
if(head == body[i])
return true;
}
return false;
}