-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.cpp
81 lines (65 loc) · 1.45 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
#include "snake.hpp"
Snake::Snake()
{
BodyPart snakeHead;
snakeHead.setPosition(sf::Vector2f(WIN_WIDTH/2.0f,WIN_HEIGHT/2.0f));
snakeHead.setColor(sf::Color(50,200,50));
snakeBody.push_back(snakeHead);
}
void Snake::move(Direction dir)
{
switch(dir)
{
case Up:
snakeBody.front().move(Up);
break;
case Down:
snakeBody.front().move(Down);
break;
case Left:
snakeBody.front().move(Left);
break;
case Right:
snakeBody.front().move(Right);
break;
default:
break;
}
//for each frame,set the new bodypart position
//to the last position of the bodypart ahead
//i = 1 to exclude the head
for(int i = 1;i<snakeBody.size();i++)
snakeBody[i].setPosition(snakeBody[i-1].getLastPosition());
}
void Snake::draw(sf::RenderWindow &win)
{
for(BodyPart &bp : snakeBody)
bp.draw(&win);
}
sf::Vector2f Snake::getPosition()
{
return snakeBody[0].getPosition();
}
void Snake::setPosition(sf::Vector2f newPos)
{
snakeBody[0].setPosition(newPos);
}
bool Snake::isSelfColliding()
{
//i = 1 to exclude the head
for(int i = 1;i < snakeBody.size();i++ )
if(snakeBody[i].getPosition() == snakeBody[0].getPosition())
return true;
return false;
}
void Snake::grow()
{
BodyPart newBodyPart(snakeBody.back().getLastPosition());
snakeBody.push_back(newBodyPart);
}
void Snake::reset()
{
this->setPosition(sf::Vector2f(WIN_WIDTH/2.0f,WIN_HEIGHT/2.0f));
//only keep the head and tail
snakeBody.erase(snakeBody.begin()+2,snakeBody.end());
}