Added ability to get snake head position

Added ability to get snake head position and created a snake constructor with a position.
This commit is contained in:
TriantaTV 2022-07-26 16:56:26 -05:00
parent c03b16f0e9
commit 94abee7e61
3 changed files with 19 additions and 4 deletions

View File

@ -2,7 +2,6 @@
#include <SFML\Graphics.hpp> #include <SFML\Graphics.hpp>
#include <SFML\System.hpp> #include <SFML\System.hpp>
#include "Snake.h" #include "Snake.h"
using namespace std;
// Test for collision between two objects // Test for collision between two objects
bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2) bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2)
@ -49,11 +48,25 @@ void Snake::MoveSnake()
return; return;
} }
// Get x and y position of snake head
sf::Vector2f Snake::GetHeadPosition()
{
sf::Vector2f position;
position = snakeBody.front().getPosition();
return position;
}
Snake::Snake() Snake::Snake()
{ {
// Possibly unnecessary // Possibly unnecessary
// The big 3 could be used to create a fresh game state // The big 3 could be used to create a fresh game state
return; return;
} }
Snake::Snake(sf::Vector2f head)
{
sf::RectangleShape newBodyPart(head);
snakeBody.push_back(newBodyPart);
return;
}
// SnakeNode::SnakeNode(); // SnakeNode::SnakeNode();
// SnakeNode::SnakeNode(sf::Vector2f addBodyPiece); // SnakeNode::SnakeNode(sf::Vector2f addBodyPiece);

View File

@ -21,10 +21,12 @@ class Snake
{ {
private: private:
std::deque<sf::RectangleShape> snakeBody; std::deque<sf::RectangleShape> snakeBody;
Snake();
public: public:
Snake();
Snake(sf::Vector2f head);
// Instead of popping like in MoveSnake() // Instead of popping like in MoveSnake()
// Simply add to deque // Simply add to deque
sf::Vector2f GetHeadPosition();
void ExtendSnake(); void ExtendSnake();
void MoveSnake(); // Move only head body piece void MoveSnake(); // Move only head body piece
}; };

View File

@ -2,7 +2,6 @@
#include <SFML\Graphics.hpp> #include <SFML\Graphics.hpp>
#include <SFML\System.hpp> #include <SFML\System.hpp>
#include "Snake.h" #include "Snake.h"
using namespace std;
/* /*
@ -26,6 +25,7 @@ int main()
sf::RenderWindow window(sf::VideoMode(videoSizeHorizontal, videoSizeVertical), "SnakePlusPlus"); sf::RenderWindow window(sf::VideoMode(videoSizeHorizontal, videoSizeVertical), "SnakePlusPlus");
sf::Time delay = sf::milliseconds(100); sf::Time delay = sf::milliseconds(100);
Snake Player(sf::Vector2f(25,25));
sf::RectangleShape snakeHead(sf::Vector2f(25,25)); sf::RectangleShape snakeHead(sf::Vector2f(25,25));
sf::RectangleShape snakeFood(sf::Vector2f(25,25)); sf::RectangleShape snakeFood(sf::Vector2f(25,25));
snakeHead.setFillColor(sf::Color::Green); snakeHead.setFillColor(sf::Color::Green);
@ -41,7 +41,7 @@ int main()
if ((event.type == sf::Event::Closed) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) if ((event.type == sf::Event::Closed) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
window.close(); window.close();
} }
sf::Vector2f snakeHeadPosition = snakeHead.getPosition(); sf::Vector2f snakeHeadPosition = Player.GetHeadPosition();
sf::Vector2f snakeFoodPosition = snakeFood.getPosition(); sf::Vector2f snakeFoodPosition = snakeFood.getPosition();
// TODO: Split Movement into separate function // TODO: Split Movement into separate function
// Add boundaries // Add boundaries