Sorry for my many snake game questions guys,
#include "stdafx.h"
#include <SFML\Graphics.hpp>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include<vector>
#include <algorithm>
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()
{
srand(time(NULL));
int width = 400;
int height = 400;
sf::VideoMode videomode(width, height);
sf::RenderWindow window(videomode, "Snake");
sf::RectangleShape snake;
snake.setFillColor(sf::Color::Red);
snake.setSize(sf::Vector2f(20, 20));
snake.setPosition(100, 100);
sf::Clock clock;
sf::Time t1 = sf::seconds(20);
sf::RectangleShape spawnedFruit;
while (window.isOpen()) {
window.clear();
window.draw(snake);
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.move(0, -0.1);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
snake.move(0, 0.1);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
snake.move(-0.1, 0);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
snake.move(0.1, 0);
if (intersects(snake, spawnedFruit))
std::cout << "Intersects";
}
}
This gives me the Errors:
Error 7 error LNK2001: unresolved external symbol "public: bool __thiscall sf::Rect::intersects(class sf::Rect const &)const " (?intersects@?$Rect@M@sf@@QBE_NABV12@@Z)
Error 8 error LNK1120: 1 unresolved externals
The errors appear only when I add the bool intersects function.
Any idea why is that or how to fix it? Thank you.