1 #include <Game/Player.hpp> 2 #include <Game/Board.hpp> 4 Player::Player(
int x,
int y):
6 currentDirection(
Player::RIGHT),
7 nextDirection(
Player::RIGHT)
10 this->body.push_back(
Body(x, y));
14 this->body.push_back(
Body(x - 1, y));
15 this->body.push_back(
Body(x - 1, y));
17 bool Player::isAlive()
23 return (
int)(this->body.size());
27 return (this->body.front().x);
31 return (this->body.front().y);
33 void Player::moveTo(
int x,
int y)
35 this->body.front().x = x;
36 this->body.front().y = y;
38 void Player::move(Direction dir)
40 this->nextDirection = dir;
46 void Player::update(
Board* board)
50 switch(this->nextDirection)
53 if (this->currentDirection != Player::LEFT)
54 this->currentDirection = this->nextDirection;
58 if (this->currentDirection != Player::RIGHT)
59 this->currentDirection = this->nextDirection;
63 if (this->currentDirection != Player::DOWN)
64 this->currentDirection = this->nextDirection;
68 if (this->currentDirection != Player::UP)
69 this->currentDirection = this->nextDirection;
74 for (
unsigned int i = (this->body.size() - 1); i > 0; i--)
76 this->body[i].x = this->body[i - 1].x;
77 this->body[i].y = this->body[i - 1].y;
81 switch(this->currentDirection)
84 this->body.front().x++;
88 this->body.front().x--;
91 this->body.front().y--;
95 this->body.front().y++;
102 if (board->isBorder(this->body.front().x,
103 this->body.front().y))
105 if (board->
style == Board::TELEPORT)
111 int headx = this->body.front().x;
112 int heady = this->body.front().y;
115 if (this->bodyHit(headx, heady,
true))
119 if (board->
isWall(headx, heady))
122 void Player::draw(
Window* win)
125 for (
unsigned int i = 1; i < (this->body.size()); i++)
129 Colors::pair(COLOR_GREEN, COLOR_DEFAULT,
true));
135 this->body.front().x,
136 this->body.front().y,
137 Colors::pair(((this->alive) ?
139 COLOR_RED), COLOR_DEFAULT,
true));
141 bool Player::headHit(
int x,
int y)
143 return ((this->body.front().x == x) &&
144 (this->body.front().y == y));
149 if (isCheckingHead) initial = 3;
151 for (
unsigned int i = initial; i < (this->body.size()); i++)
152 if ((this->body[i].x == x) &&
153 (this->body[i].y == y))
158 void Player::increase()
160 int lastx = this->body.back().x;
161 int lasty = this->body.back().y;
163 this->body.push_back(
Body(lastx, lasty));
A segment of the terminal screen (2D char matrix).
Style style
Tells if the player will teleport when reaching the Board's limits or not.
A level where the snake runs and eats fruits.
bool isWall(int x, int y)
Tells if there's a wall at #x #y.
void teleport(Player *player)
Makes the Player teleport if it's on a border.
void printChar(int c, int x, int y, ColorPair pair=0)
Shows #c at #x #y with color #pair.
int getX()
Returns the head's x position.
bool bodyHit(int x, int y, bool isCheckingHead=false)
Tells if something at #x and #y collides with any part of the snake.
int getY()
Returns the head's y position.