1
Graphics / Vector in SFML
« on: December 31, 2018, 02:41:45 pm »
Hello there!
Im starting to learn SFML and are playing with some basic drawables atm.
Im trying to programm a simple Snake game, with sf::Rectangles as the body segments.
Everything is working fine (Moving the Head-Segment, border and segment collision check and creating new Segments...)
BUT im not able to reset the position of existing segments to follow the Head.. They just dont save the given positions...
Normally, the actual position should become the "old" position and then the actual position gets updated by the old position of the segment in front of it...
A hint why this doesnt work?
Im starting to learn SFML and are playing with some basic drawables atm.
Im trying to programm a simple Snake game, with sf::Rectangles as the body segments.
Everything is working fine (Moving the Head-Segment, border and segment collision check and creating new Segments...)
BUT im not able to reset the position of existing segments to follow the Head.. They just dont save the given positions...
Normally, the actual position should become the "old" position and then the actual position gets updated by the old position of the segment in front of it...
void Game::update_game()
{
// Neues Item zum aufsammeln generieren
if (ptrItem == NULL)
{
ptrItem = new Food;
}
// Spieler um increment bewegen
Head.set_last_position(); // Bisherige Position speichern
increment_Spieler = window.get_increment_Spieler();
Head.set_new_position(sf::Vector2f(Head.get_position().x + increment_Spieler.x, Head.get_position().y + increment_Spieler.y));
if (Body.size() == 1)
{
Body[0].set_last_position();
Body[0].set_new_position(Head.get_last_position());
}
if (Body.size() >= 2)
{
for(int i = 1; i < Body.size(); i++)
{
Body[i].set_last_position();
Body[i].set_new_position(Body[i-1].get_last_position());
}
}
// Testen ob Item eingesammelt, wenn ja Items neue Position zuweisen und Schlangenkörper verlängern
if (Head.segment.getGlobalBounds().intersects(ptrItem->target.getGlobalBounds()))
{
ptrItem->set_new_target();
if (Body.size() == 0)
{
Body.push_back(Snakesegment(Head.get_last_position()));
}
if (Body.size() >= 1) // Wenn mindestens ein Element vorhanden
{
Body.push_back(Snakesegment(Body[Body.size()-1].get_last_position())); // Neues Element hinten an vorherige Position des bis dahin letzten Elements hängen
}
}
{
// Neues Item zum aufsammeln generieren
if (ptrItem == NULL)
{
ptrItem = new Food;
}
// Spieler um increment bewegen
Head.set_last_position(); // Bisherige Position speichern
increment_Spieler = window.get_increment_Spieler();
Head.set_new_position(sf::Vector2f(Head.get_position().x + increment_Spieler.x, Head.get_position().y + increment_Spieler.y));
if (Body.size() == 1)
{
Body[0].set_last_position();
Body[0].set_new_position(Head.get_last_position());
}
if (Body.size() >= 2)
{
for(int i = 1; i < Body.size(); i++)
{
Body[i].set_last_position();
Body[i].set_new_position(Body[i-1].get_last_position());
}
}
// Testen ob Item eingesammelt, wenn ja Items neue Position zuweisen und Schlangenkörper verlängern
if (Head.segment.getGlobalBounds().intersects(ptrItem->target.getGlobalBounds()))
{
ptrItem->set_new_target();
if (Body.size() == 0)
{
Body.push_back(Snakesegment(Head.get_last_position()));
}
if (Body.size() >= 1) // Wenn mindestens ein Element vorhanden
{
Body.push_back(Snakesegment(Body[Body.size()-1].get_last_position())); // Neues Element hinten an vorherige Position des bis dahin letzten Elements hängen
}
}
A hint why this doesnt work?