Okay, thank you SO MUCH really, I'll go back to trying to finish the code now ^_^ Wish me luck ;D
What's wrong with this snake game code?
1. I understand what you mean, what I'm trying to do is make the next element of the existing vector appear after the first.
2. I just want to increase the length of the snake upon colliding with the fruit,
Okay, but those are different things. :)
My understanding of what you mean here:
1. You have { first, second, third, fourth}. When you add a snake element, you want { first, first, second, third, fourth}.
2. You have { first, second, third, fourth}. When you add a snake element, you want { first, second, third, fourth, fifth }.
What your code is attempting is the first one; what you are intending seems to be the second. Is that correct?
Now, I want you to think about what actually happens when snake.push_back(addsnake()) is called. Step through it line by line. What do the elements of the snake vector look like at each line?
when it is called it adds 1 to t which is 0 in the beginning and returns second,then adds and returns third,then fourth , that's what I thought, since you mentioned using just a vector with one element, the very first code I had written attempting to make snake consisted of one vector with an element in it (I thought the rest of the code would add the elements), and a rectangle shape which I tried using push_back to add it to the vector, it looked something like this:-
#include <vector>
#include <limits>
#include <algorithm>
#include <SFML/Graphics.hpp>
#include <iostream>
sf::RectangleShape snakefirst;
std::vector<sf::RectangleShape>snake{ snakefirst };
sf::RectangleShape snakez;
bool intersects(const sf::RectangleShape & r1, const sf::RectangleShape & r2){
sf::FloatRect snake = r1.getGlobalBounds();
sf::FloatRect spawnedFruit = r2.getGlobalBounds();
return snake.intersects(spawnedFruit);
}
sf::RectangleShape generateFruit() {
sf::RectangleShape fruit;
fruit.setFillColor(sf::Color::Yellow);
int fruitx = rand() % 400;
int fruity = rand() % 400;
fruit.setPosition(fruitx, fruity);
fruit.setSize(sf::Vector2f(5, 5));
return fruit;
}
int main()
{
snake[0].setFillColor(sf::Color::Red);
snake[0].setPosition(100, 100);
snake[0].setSize(sf::Vector2f(20, 20));
snake[0].setFillColor(sf::Color::Red);
snake[0].setSize(sf::Vector2f(20, 20));
snakez.setFillColor(sf::Color::Red);
snakez.setSize(sf::Vector2f(20, 20));
srand(time(NULL));
int width = 400;
int height = 400;
sf::VideoMode videomode(width, height);
sf::RenderWindow window(videomode, "Snake");
sf::Clock clock;
sf::Time t1 = sf::seconds(20);
sf::RectangleShape spawnedFruit;
while (window.isOpen()) {
window.clear();
window.draw(snake[0]);
sf::Time elapsed1 = clock.getElapsedTime();
if (elapsed1 >= t1) {
spawnedFruit = generateFruit();
clock.restart();
}
window.draw(spawnedFruit);
window.display();
sf::Event event;
while (window.pollEvent(event))
{
if ((event.type == sf::Event::Closed) ||
((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)))
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
snake[0].move(0, -0.1);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
snake[0].move(0, 0.1);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
snake[0].move(-0.1, 0);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
snake[0].move(0.1, 0);
if (intersects(snake[0], spawnedFruit)){
snake.push_back(snakez);
}
//tried snake[t]
}
}
But I had the same problem of the collision of the snake and the fruit not doing anything :s
Dream in the shadows
But where is that new rectangle shape going to appear? If you never set the position of a rectangle shape, where is it displayed? ;)
Now I set the position of snakez 80,80, but still nothing changed :/ In the beginning I thought push_back would also set its position after snake[0]
Dream in the shadows
Now I set the position of snakez 80,80, but still nothing changed :/
Did you try setting a breakpoint on the push_back call and then running the game in the debugger to see what actually happens?
In the beginning I thought push_back would also set its position after snake[0]
It sets its position within the vector to be after snake[0] (or rather, after whatever the last element in the vector is). But the rectangle's screen position itself has nothing to do with it - remember, the vector itself doesn't actually know what a RectangleShape is, either! All the vector knows is that it stores RectangleShapes.
Consequently, simply being in the vector will not have snake[1...n] change their positions whenever snake[0] moves. You will need to move them individually.
So, do I have to draw snakez too and make it move (whenever snake[0] moves) to be in the last position snake[0] was?
Dream in the shadows
Wow, I think if I do that it will solve the next problem I would have to deal with which is moving the snake in a way it can bite itself and turn and stuff, right?
Do I do it by getting the position of snake[0] (.getPosition) and modifying snakez position to it whenever snake[0] moves? Is that right?
Dream in the shadows