Cleaned up code for readability
This commit is contained in:
+18
-12
@@ -2,11 +2,12 @@
|
||||
#include <iostream>
|
||||
#include <SFML\Graphics.hpp>
|
||||
#include "SnakeFood.h"
|
||||
#include "Snake.h"
|
||||
|
||||
const int kGridSize = 25;
|
||||
|
||||
SnakeFood::SnakeFood()
|
||||
{
|
||||
snakeFoodObject.setSize(sf::Vector2f(25,25));
|
||||
snakeFoodObject.setSize(sf::Vector2f(kGridSize,kGridSize));
|
||||
snakeFoodObject.setFillColor(sf::Color::Red);
|
||||
}
|
||||
|
||||
@@ -16,26 +17,31 @@ SnakeFood::SnakeFood(sf::Vector2f snakeFoodSize)
|
||||
snakeFoodObject.setFillColor(sf::Color::Red);
|
||||
}
|
||||
|
||||
void SnakeFood::GenerateNewLocation(int horizontalLocation, int verticalLocation)
|
||||
// Returns a new food object for the snakeFood
|
||||
void SnakeFood::GenerateNewFood(sf::Vector2f windowSize)
|
||||
{
|
||||
sf::Vector2f newPosition;
|
||||
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");
|
||||
}
|
||||
newPosition.x = GenerateRandomNumber(windowSize.x);
|
||||
newPosition.y = GenerateRandomNumber(windowSize.y);
|
||||
snakeFoodObject.setPosition(newPosition);
|
||||
return;
|
||||
// if (GlobalCollision(snakeFoodObject.getPosition(), newPosition))
|
||||
// {
|
||||
// std::cout << "Location error: " << newPosition.x << " " << newPosition.y << '\n';
|
||||
// throw std::runtime_error("Error! New generation on same location");
|
||||
// }
|
||||
}
|
||||
|
||||
sf::RectangleShape SnakeFood::GetFoodObject(void)
|
||||
{
|
||||
return snakeFoodObject;
|
||||
}
|
||||
|
||||
// Returns a newly generated number
|
||||
int SnakeFood::GenerateRandomNumber(int generationLimit)
|
||||
{
|
||||
int generatedNumber;
|
||||
std::uniform_int_distribution<> distribution(0, generationLimit);
|
||||
generatedNumber = distribution(generator);
|
||||
generatedNumber -= (generatedNumber % 25);
|
||||
generatedNumber -= (generatedNumber % kGridSize);
|
||||
return generatedNumber;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user