forked from ntxtung/SnakeVer2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.cpp
111 lines (94 loc) · 2.39 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
#include "Classes.h" //included <vector>
#include "Declaration.h"
#include <stdlib.h>
using namespace std;
Snake::Snake(int _x, int _y){
this->setPosition(_x, _y);
}
Snake::Snake(point _pos){
this->setPosition(_pos);
}
Snake::~Snake() {}
void Snake::setPosition(int _x, int _y){
this->pos.x = _x;
this->pos.y = _y;
}
void Snake::setPosition(point _pos){
this->pos = _pos;
}
point Snake::getPosition(){
return this->pos;
}
void Snake::setTailLen(int _len){
this->tailLen = _len;
}
int Snake::getTailLen(){
return this->tailLen;
}
void Snake::setDirection(SnakeDirection _direction, bool _antiReverse)
{
SnakeDirection oldDir = this->getDirection();
if (_antiReverse)
{
if ((oldDir == LEFT && _direction != RIGHT) ||
(oldDir == RIGHT && _direction != LEFT) ||
(oldDir == UP && _direction != DOWN) ||
(oldDir == DOWN && _direction != UP))
this->direction = _direction;
}
else
this->direction = _direction;
}
SnakeDirection Snake::getDirection(){
return this->direction;
}
void Snake::insertTails(point _tailPos)
{
this->tails.insert(tails.begin(), _tailPos);
}
void Snake::move()
{
point oldPos = this->getPosition(); // get old postion
point newPos;
SnakeDirection newDirection = this->getDirection(); // get direction
switch (newDirection)
{
case UP:
newPos.x = oldPos.x;
newPos.y = oldPos.y - 1;
break;
case DOWN:
newPos.x = oldPos.x;
newPos.y = oldPos.y + 1;
break;
case LEFT:
newPos.x = oldPos.x - 1;
newPos.y = oldPos.y;
break;
case RIGHT:
newPos.x = oldPos.x + 1;
newPos.y = oldPos.y;
break;
}
if (newPos.x == 0)
newPos.x = playZoneW - 1;
if (newPos.y == 0)
newPos.y = playZoneH - 1;
if (newPos.x == playZoneW)
newPos.x = 1;
if (newPos.y == playZoneH)
newPos.y = 1;
this->setPosition(newPos);
}
vector<point> Snake::getTails(){
return this->tails;
}
void Snake::popTails(){
this->tails.pop_back();
}
void Snake::saveTails()
{
this->insertTails(getPosition()); //insert current position into tails
while (this->getTailLen() != this->getTails().size())
this->popTails();
}