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 "common.hpp"
|
|
|
|
#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
|
|
|
// General constructor for snake class
|
|
|
|
Snake::Snake(void)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-03-12 08:50:50 -05:00
|
|
|
|
2023-04-06 19:27:10 -05:00
|
|
|
PlayerDirection Snake::GetDirection(void)
|
|
|
|
{
|
|
|
|
return direction;
|
|
|
|
}
|
2022-07-26 19:47:08 -05:00
|
|
|
|
2023-04-06 19:27:10 -05:00
|
|
|
sf::Vector2f Snake::GetHeadLocation(void)
|
|
|
|
{
|
|
|
|
return headLocation;
|
|
|
|
}
|
2022-07-25 16:12:11 -05:00
|
|
|
|
2023-04-06 19:27:10 -05:00
|
|
|
// Update snake with location information
|
|
|
|
sf::Vector2f Snake::Move(void)
|
|
|
|
{
|
|
|
|
if (direction == kLeft) MoveLeft();
|
|
|
|
if (direction == kUp) MoveUp();
|
|
|
|
if (direction == kDown) MoveDown();
|
|
|
|
if (direction == kRight) MoveRight();
|
|
|
|
return headLocation;
|
|
|
|
}
|
2022-07-26 16:56:26 -05:00
|
|
|
|
2023-04-06 19:27:10 -05:00
|
|
|
// Removes tail of snake
|
|
|
|
void Snake::Pop(void)
|
|
|
|
{
|
|
|
|
*(snakeBody.front().get()) = ' ';
|
|
|
|
snakeBody.pop();
|
|
|
|
return;
|
|
|
|
}
|
2023-03-13 08:24:56 -05:00
|
|
|
|
2023-04-06 19:27:10 -05:00
|
|
|
sf::Vector2f Snake::Reset(void)
|
|
|
|
{
|
|
|
|
return sf::Vector2f(4,5);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Snake::UpdateDirection(PlayerDirection newDirection)
|
|
|
|
{
|
|
|
|
direction = newDirection;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Snake::MoveLeft(void)
|
|
|
|
{
|
|
|
|
headLocation.x -= 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Snake::MoveUp(void)
|
|
|
|
{
|
|
|
|
headLocation.y -= 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Snake::MoveDown(void)
|
|
|
|
{
|
|
|
|
headLocation.y += 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Snake::MoveRight(void)
|
|
|
|
{
|
|
|
|
headLocation.x += 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Food::Food()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a new food object for the snakeFood
|
|
|
|
void Food::GenerateNewFood(sf::Vector2f boundaries)
|
|
|
|
{
|
|
|
|
location.x = GenerateRandomNumber(boundaries.x);
|
|
|
|
location.y = GenerateRandomNumber(boundaries.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
sf::Vector2f Food::GetFoodLocation(void)
|
|
|
|
{
|
|
|
|
return location;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a newly generated number
|
|
|
|
int Food::GenerateRandomNumber(int generationLimit)
|
|
|
|
{
|
|
|
|
int generatedNumber;
|
|
|
|
std::uniform_int_distribution<> distribution(0, generationLimit);
|
|
|
|
generatedNumber = distribution(generator);
|
|
|
|
return generatedNumber;
|
|
|
|
}
|
2022-07-26 16:56:26 -05:00
|
|
|
}
|