2022-08-15 23:51:18 -05:00
|
|
|
// Snake.cpp
|
2022-08-21 02:51:33 -05:00
|
|
|
#include <queue>
|
2023-04-06 19:27:10 -05:00
|
|
|
#include <random>
|
2023-03-17 17:27:02 -05:00
|
|
|
#include <SFML/Graphics.hpp>
|
2023-04-06 19:27:10 -05:00
|
|
|
#include "snake.hpp"
|
2022-07-25 15:40:45 -05:00
|
|
|
|
2023-04-06 19:27:10 -05:00
|
|
|
namespace snakeplusplus
|
2023-03-12 08:50:50 -05:00
|
|
|
{
|
2023-04-06 19:27:10 -05:00
|
|
|
void Snake::Pop(void)
|
|
|
|
{
|
2023-04-15 05:15:11 -05:00
|
|
|
*(body.front()) = ' ';
|
2023-04-06 22:22:06 -05:00
|
|
|
body.pop();
|
2023-04-06 19:27:10 -05:00
|
|
|
}
|
|
|
|
|
2023-08-10 18:47:40 -05:00
|
|
|
void Snake::Reset(void)
|
|
|
|
{
|
|
|
|
while (!body.empty()) Pop();
|
2023-08-14 17:39:04 -05:00
|
|
|
speed.x = 0;
|
|
|
|
speed.y = 0;
|
2023-08-10 18:47:40 -05:00
|
|
|
}
|
|
|
|
|
2023-04-15 05:15:11 -05:00
|
|
|
Food::Food(void)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
2023-04-15 05:15:11 -05:00
|
|
|
generator.seed(std::random_device{}());
|
2023-04-06 19:27:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a new food object for the snakeFood
|
|
|
|
void Food::GenerateNewFood(sf::Vector2f boundaries)
|
|
|
|
{
|
|
|
|
location.x = GenerateRandomNumber(boundaries.x);
|
|
|
|
location.y = GenerateRandomNumber(boundaries.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a newly generated number
|
|
|
|
int Food::GenerateRandomNumber(int generationLimit)
|
|
|
|
{
|
|
|
|
int generatedNumber;
|
2023-05-17 20:20:01 -05:00
|
|
|
std::uniform_int_distribution<> distribution(0, generationLimit - 1);
|
2023-04-06 19:27:10 -05:00
|
|
|
generatedNumber = distribution(generator);
|
|
|
|
return generatedNumber;
|
|
|
|
}
|
2022-07-26 16:56:26 -05:00
|
|
|
}
|