![]() direction is now a vector containing an x and y offset, which you can just remove two segments max per update (while queue length > max length and while segments removed < shrink rate).Įdit: Addressing your comment below wondering how to change direction based on an integer (or an enum, which I see you use now - same concept) here's some more pseudocode: Vec2 direction(0, 0) If there's an intersection, a collision occurred.īTW if you want a smoother "shrinking" effect when the maximum snake length decreases, you could e.g. Check the head against the rest of the body segments. To detect collisions, implement this on each update step. If you ever need to change the length of the snake, just change the maximum length - the update step will then adjust the length of the snake.Ī collision, semantically, is when the snakes head runs into any other part of its body.On keyboard input, just change the current direction.While the queue length is greater than maximum length, remove a segment at tail of queue.Add segment at head of queue, in next position. ![]() Next position = head position + current direction.Let's say your snake is a list of Segments, with a maximum length: Rather than pointing out each and every issue, here's a rough outline. Your implementation has a lot of problems. If(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) If(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) If(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) Sf::RenderWindow window(sf::VideoMode(1028,768), "SFML Snake") The problem is that I don't know how I could possibly make the snake bite himself : each snake block takes the position of the last one, resulting in a perfectly straight snake. I'm trying to make a snake game in C++ with SFML.
0 Comments
Leave a Reply. |