From 9f90e6fb2f679b76156875ae61d5180b71dbfd04 Mon Sep 17 00:00:00 2001 From: TriantaTV <56975502+TriantaTV@users.noreply.github.com> Date: Tue, 26 Jul 2022 19:47:08 -0500 Subject: [PATCH] Added boundaries for snake movement Snake is now limited to a hard-coded boundary. Boundary is planned to be changed to no longer be hard-coded. --- src/Snake.cpp | 27 +++++++++++++++++++++------ src/Snake.h | 5 ++++- src/main.cpp | 6 +++--- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/Snake.cpp b/src/Snake.cpp index 612deb7..9d3d0cd 100644 --- a/src/Snake.cpp +++ b/src/Snake.cpp @@ -20,10 +20,8 @@ bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2) } // Move snake head piece -int SnakeMovement() +void Snake::CheckDirection() { - std::cout << "Test text \n"; - int snakeDirection = 0; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) snakeDirection = 1; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) @@ -32,7 +30,22 @@ int SnakeMovement() snakeDirection = 3; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) snakeDirection = 4; - return snakeDirection; + return; +} + +// Check snake head for running into boundaries +bool Snake::CheckBoundaries() +{ + if (snakeBody.front().getPosition().x == 0 && snakeDirection == 1) + return 1; + if (snakeBody.front().getPosition().y == 0 && snakeDirection == 2) + return 1; + // TODO: Change boundaries to not be hard-coded + if (snakeBody.front().getPosition().y > 675 && snakeDirection == 3) + return 1; + if (snakeBody.front().getPosition().x > 975 && snakeDirection == 4) + return 1; + return 0; } sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position) @@ -56,13 +69,15 @@ void Snake::ExtendSnake() return; } -void Snake::MoveSnake(int snakeDirection) +void Snake::MoveSnake() { // Create a new deque RectangleShape and pop old // Todo: Depreciate ExtendSnake and just add a collision test + CheckDirection(); sf::Vector2f newHeadPosition; newHeadPosition = GetSnakeHeadPosition(); - newHeadPosition = CalculateNewPosition(snakeDirection, newHeadPosition); + if (!CheckBoundaries()) + newHeadPosition = CalculateNewPosition(snakeDirection, newHeadPosition); sf::RectangleShape newBodyPart(sf::Vector2f(25,25)); newBodyPart.setPosition(newHeadPosition); snakeBody.push_back(newBodyPart); diff --git a/src/Snake.h b/src/Snake.h index 8e373af..3cf13a6 100644 --- a/src/Snake.h +++ b/src/Snake.h @@ -22,6 +22,7 @@ class Snake { private: std::deque snakeBody; + int snakeDirection = 0; public: Snake(); Snake(sf::Vector2f head); @@ -31,7 +32,9 @@ public: sf::RectangleShape GetSnakeHead(); void DisplaySnake(sf::RenderWindow& window); void ExtendSnake(); - void MoveSnake(int snakeDirection); // Move only head body piece + void MoveSnake(); // Move only head body piece + void CheckDirection(); + bool CheckBoundaries(); }; diff --git a/src/main.cpp b/src/main.cpp index 2799948..10e0505 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,7 +27,7 @@ int main() gameGridVertical = (videoSizeVertical // 25) * 25; */ sf::RenderWindow window(sf::VideoMode(videoSizeHorizontal, videoSizeVertical), "SnakePlusPlus"); - sf::Time delay = sf::milliseconds(5); + sf::Time delay = sf::milliseconds(100); int snakeDirection = 0; Snake Player(sf::Vector2f(25,25)); @@ -59,7 +59,7 @@ int main() snakeDirection = 3; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) snakeDirection = 4; - Player.MoveSnake(snakeDirection); + Player.MoveSnake(); // if (SnakeCollision(snakeHead, snakeFood)) // { // snakeFoodPosition.x += 25; @@ -69,6 +69,6 @@ int main() window.draw(snakeFood); Player.DisplaySnake(window); window.display(); - sf::sleep(delay); + // sf::sleep(delay); } }