Okay thanks a lot for your advice already. Right now my snake grows when he eats one pickup.
But only the first one, I do not know why.
Here are my modified piece of code (only work once, after that the snake don't grow if he eats one pick up :
Alright, I had only given a piece of the code because my program is all made in one file. :/
No, there is only one key object because I only want them to respawn one at a time, so I didn't made a list/vector/deque for the pick up.
I will give you the functions of snake so you can understand:
Block::Block(int x, int y)
{
ax = x;
ay = y;
}
float Block::getX()
{
return ax;
}
float Block::getY()
{
return ay;
}
void Block::setX(float ZX)
{
ax = ZX;
}
void Block::setY(float ZY)
{
ay = ZY;
}
void Snake::Move(int NewDirection)
{
shrink();
if(NewDirection == Up)
{
if(adirection != Down)
{
asnake.push_front(Block(asnake.front().getX(), asnake.front().getY() - 15));
adirection = Up;
}
else
{
asnake.push_front(Block(asnake.front().getX(), asnake.front().getY() + 15));
}
}
else if(NewDirection == Right)
{
if(adirection != Left)
{
asnake.push_front(Block(asnake.front().getX() + 15, asnake.front().getY()));
adirection = Right;
}
else
{
asnake.push_front(Block(asnake.front().getX() - 15, asnake.front().getY()));
}
}
else if(NewDirection == Down)
{
if(adirection != Up)
{
asnake.push_front(Block(asnake.front().getX(), asnake.front().getY() + 15));
adirection = Down;
}
else
{
asnake.push_front(Block(asnake.front().getX(), asnake.front().getY() - 15));
}
}
else if(NewDirection == Left)
{
if(adirection != Right)
{
asnake.push_front(Block(asnake.front().getX() - 15, asnake.front().getY()));
adirection = Left;
}
else
{
asnake.push_front(Block(asnake.front().getX() + 15, asnake.front().getY()));
}
}
}
void Snake::grow()
{
asnake.push_back(Block(asnake.back().getX(), asnake.back().getY()));
}
void Snake::shrink()
{
asnake.pop_back();
}
std::deque<Block> Snake::getBlocks()
{
return asnake;
}
int Snake::getX()
{
return asnake.front().getX();
}
int Snake::getY()
{
return asnake.front().getY();
}
Snake::Snake()
{
asnake.push_front(Block(BORDERX/2,BORDERY/2));
asnake.push_front(Block(BORDERX/2+15,BORDERY/2+15));
asnake.push_front(Block(BORDERX/2+15,BORDERY/2+15));
}
void Snake::setdirection()
{
adirection = Left;
}
bool Snake::checkForCollisionBorder()
{
for(unsigned int i = 0; i < getBlocks().size(); i++)
{
int cx = (getBlocks())[i].getX();
int cy = (getBlocks())[i].getY();
if((cx > BORDERX - 50) || (cx < 50) || (cy > BORDERY - 50) || (cy < 50))
{
return true;
}
}
return false;
}
bool Snake::checkForBite()
{
for(unsigned int s = 1; s < getBlocks().size(); s++)
{
if((getBlocks())[s].getX() == snake.getX() && (getBlocks())[s].getY() == snake.getY())
{
return true;
}
}
return false;
}
void Key::SpawnKey() // spawn a key every 5 seconds
{
if(clockKey.getElapsedTime().asSeconds() > 3)
{
spawn = true;
keysprite.setPosition(350, 200);
clockKey.restart();
}
}
bool Key::isKeyPickedUp()
{
if((keysprite.getPosition().x == snake.getX() && keysprite.getPosition().y == snake.getY()) && spawn)
{
spawn = false;
return true;
}
return false;
}
sf::Sprite Key::getKeySprite()
{
return keysprite;
}
void Key::growOrDespawn()
{
if(isKeyPickedUp())
{
maximalSize++;
snake.grow();// if key has been picked up, move the key out of the map (I do not know how to destroy it :p
SpawnKey(); // and spawn a new key after 5 seconds.
}
}
My snake is made of a deque of Blocks (which contains 2 coordinates), and in my main function in the window.isOpen() loop, I put this to draw each segment of the snake based on the deque of Blocks:
window.draw(backgroundsprite);
for(unsigned int i = 1; i < (snake.getBlocks()).size(); i++)
{
float xb = (snake.getBlocks())[i].getX();
float yb = (snake.getBlocks())[i].getY();
sf::RectangleShape block(sf::Vector2f(15,15));
block.setFillColor(sf::Color::Green);
block.setOutlineThickness(2);
block.setOutlineColor(sf::Color::Yellow);
block.setPosition(xb, yb);
window.draw(block);
}
So you can understand now how my code works.
Right now the only issue is that it only work once (once the snake takes the pick up, he will grow once, but if he takes again the pick up (which is put elsewhere on the screen), he doesn't grow.
Thanks for your help.