2022-08-15 23:51:18 -05:00
|
|
|
// SnakeFood.cpp
|
2022-08-17 00:41:29 -05:00
|
|
|
#include <iostream>
|
|
|
|
#include <SFML\Graphics.hpp>
|
2022-08-02 21:58:58 -05:00
|
|
|
#include "SnakeFood.h"
|
2022-08-21 02:51:33 -05:00
|
|
|
#include "Snake.h"
|
2022-08-02 21:58:58 -05:00
|
|
|
|
|
|
|
SnakeFood::SnakeFood()
|
|
|
|
{
|
|
|
|
snakeFoodObject.setSize(sf::Vector2f(25,25));
|
|
|
|
snakeFoodObject.setFillColor(sf::Color::Red);
|
|
|
|
}
|
|
|
|
|
|
|
|
SnakeFood::SnakeFood(sf::Vector2f snakeFoodSize)
|
|
|
|
{
|
|
|
|
snakeFoodObject.setSize(snakeFoodSize);
|
|
|
|
snakeFoodObject.setFillColor(sf::Color::Red);
|
|
|
|
}
|
|
|
|
|
2022-08-15 23:51:18 -05:00
|
|
|
void SnakeFood::GenerateNewLocation(int horizontalLocation, int verticalLocation)
|
2022-08-02 21:58:58 -05:00
|
|
|
{
|
2022-08-15 23:51:18 -05:00
|
|
|
sf::Vector2f newPosition;
|
2022-08-17 00:41:29 -05:00
|
|
|
newPosition.x = GenerateRandomNumber(horizontalLocation);
|
|
|
|
newPosition.y = GenerateRandomNumber(verticalLocation);
|
|
|
|
if (GlobalCollision(snakeFoodObject.getPosition(), newPosition))
|
|
|
|
{
|
|
|
|
std::cout << "Location error: " << newPosition.x << " " << newPosition.y << '\n';
|
|
|
|
throw std::runtime_error("Error! New generation on same location");
|
|
|
|
}
|
2022-08-15 23:51:18 -05:00
|
|
|
snakeFoodObject.setPosition(newPosition);
|
|
|
|
return;
|
2022-08-02 21:58:58 -05:00
|
|
|
}
|
2022-08-21 02:51:33 -05:00
|
|
|
|
|
|
|
|
|
|
|
int SnakeFood::GenerateRandomNumber(int generationLimit)
|
|
|
|
{
|
|
|
|
int generatedNumber;
|
|
|
|
std::uniform_int_distribution<> distribution(0, generationLimit);
|
|
|
|
generatedNumber = distribution(generator);
|
|
|
|
generatedNumber -= (generatedNumber % 25);
|
|
|
|
return generatedNumber;
|
|
|
|
}
|