Changed movement to control the Player object

Changed movement to control the Player object. Movement not currently working, actively being looked into.
This commit is contained in:
TriantaTV 2022-07-26 19:03:55 -05:00
parent 94abee7e61
commit a8eee1db4d
3 changed files with 77 additions and 32 deletions

View File

@ -20,19 +20,34 @@ bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2)
} }
// Move snake head piece // Move snake head piece
void SnakeMovement(sf::Keyboard keyboard) int SnakeMovement()
{ {
/* std::cout << "Test text \n";
if (keyboard.pressed(Left)) int snakeDirection = 0;
snakeHead.moveleft(); if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
if (keyboard.pressed(Right)) snakeDirection = 1;
snakeHead.moveRight(); if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
if (keyboard.pressed(Down)) snakeDirection = 2;
snakeHead.moveDown(); if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
if (keyboard.pressed(Up)) snakeDirection = 3;
snakeHead.moveUp(); if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
*/ snakeDirection = 4;
return; return snakeDirection;
}
sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position)
{
if (direction == 0)
return position;
if (direction == 1)
position.x -= 25;
if (direction == 2)
position.y -= 25;
if (direction == 3)
position.y += 25;
if (direction == 4)
position.x += 25;
return position;
} }
void Snake::ExtendSnake() void Snake::ExtendSnake()
@ -41,21 +56,43 @@ void Snake::ExtendSnake()
return; return;
} }
void Snake::MoveSnake() void Snake::MoveSnake(int snakeDirection)
{ {
// Create a new deque RectangleShape and pop old // Create a new deque RectangleShape and pop old
// Todo: Depreciate ExtendSnake and just add a collision test // Todo: Depreciate ExtendSnake and just add a collision test
sf::Vector2f newHeadPosition;
newHeadPosition = GetSnakeHeadPosition();
newHeadPosition = CalculateNewPosition(snakeDirection, newHeadPosition);
sf::RectangleShape newBodyPart(newHeadPosition);
snakeBody.push_back(newBodyPart);
// snakeBody.pop_front();
return; return;
} }
// Get x and y position of snake head // Get x and y position of snake head
sf::Vector2f Snake::GetHeadPosition() sf::Vector2f Snake::GetSnakeHeadPosition()
{ {
sf::Vector2f position; sf::Vector2f position;
position = snakeBody.front().getPosition(); position = snakeBody.front().getPosition();
return position; return position;
} }
sf::RectangleShape Snake::GetSnakeHead()
{
sf::RectangleShape head;
head = snakeBody.front();
return head;
}
void Snake::DisplaySnake(sf::RenderWindow& window)
{
for (auto it = snakeBody.cbegin(); it != snakeBody.cend(); ++it)
{
window.draw(*it);
}
return;
}
Snake::Snake() Snake::Snake()
{ {
// Possibly unnecessary // Possibly unnecessary

View File

@ -4,7 +4,8 @@
#include <queue> #include <queue>
bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2); bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2);
void SnakeMovement(sf::Keyboard keyboard); int SnakeMovement();
sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position);
// class SnakeNode // class SnakeNode
// { // {
@ -26,9 +27,11 @@ public:
Snake(sf::Vector2f head); 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(); sf::Vector2f GetSnakeHeadPosition();
sf::RectangleShape GetSnakeHead();
void DisplaySnake(sf::RenderWindow& window);
void ExtendSnake(); void ExtendSnake();
void MoveSnake(); // Move only head body piece void MoveSnake(int snakeDirection); // Move only head body piece
}; };

View File

@ -22,13 +22,18 @@ int main()
int videoSizeHorizontal, videoSizeVertical; int videoSizeHorizontal, videoSizeVertical;
videoSizeHorizontal = 1024; videoSizeHorizontal = 1024;
videoSizeVertical = 725; videoSizeVertical = 725;
/*
gameGridHorizontal = (videoSizeHorizontal // 25) * 25;
gameGridVertical = (videoSizeVertical // 25) * 25;
*/
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);
int snakeDirection = 0;
Snake Player(sf::Vector2f(25,25)); 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);
snakeFood.setFillColor(sf::Color::Red); snakeFood.setFillColor(sf::Color::Red);
snakeFood.setPosition(25,25); snakeFood.setPosition(25,25);
@ -41,30 +46,30 @@ 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 = Player.GetHeadPosition(); sf::Vector2f snakeHeadPosition = Player.GetSnakeHeadPosition();
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
// Add movement until boundaries // Add movement until boundaries
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
snakeHeadPosition.x -= 25; snakeDirection = 1;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
snakeHeadPosition.y -= 25; snakeDirection = 2;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
snakeHeadPosition.y += 25; snakeDirection = 3;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
snakeHeadPosition.x += 25; snakeDirection = 4;
snakeHead.setPosition(snakeHeadPosition.x, snakeHeadPosition.y); Player.MoveSnake(snakeDirection);
if (SnakeCollision(snakeHead, snakeFood)) // if (SnakeCollision(snakeHead, snakeFood))
{ // {
snakeFoodPosition.x += 25; // snakeFoodPosition.x += 25;
snakeFoodPosition.y += 25; // snakeFoodPosition.y += 25;
snakeFood.setPosition(snakeFoodPosition.x, snakeFoodPosition.y); // snakeFood.setPosition(snakeFoodPosition.x, snakeFoodPosition.y);
}
window.clear(); window.clear();
window.draw(snakeFood); window.draw(snakeFood);
window.draw(snakeHead);
Player.DisplaySnake(window);
window.display(); window.display();
sf::sleep(delay); // sf::sleep(delay);
} }
} }