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:
parent
94abee7e61
commit
a8eee1db4d
@ -20,19 +20,34 @@ bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2)
|
||||
}
|
||||
|
||||
// Move snake head piece
|
||||
void SnakeMovement(sf::Keyboard keyboard)
|
||||
int SnakeMovement()
|
||||
{
|
||||
/*
|
||||
if (keyboard.pressed(Left))
|
||||
snakeHead.moveleft();
|
||||
if (keyboard.pressed(Right))
|
||||
snakeHead.moveRight();
|
||||
if (keyboard.pressed(Down))
|
||||
snakeHead.moveDown();
|
||||
if (keyboard.pressed(Up))
|
||||
snakeHead.moveUp();
|
||||
*/
|
||||
return;
|
||||
std::cout << "Test text \n";
|
||||
int snakeDirection = 0;
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
|
||||
snakeDirection = 1;
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
|
||||
snakeDirection = 2;
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
|
||||
snakeDirection = 3;
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
|
||||
snakeDirection = 4;
|
||||
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()
|
||||
@ -41,21 +56,43 @@ void Snake::ExtendSnake()
|
||||
return;
|
||||
}
|
||||
|
||||
void Snake::MoveSnake()
|
||||
void Snake::MoveSnake(int snakeDirection)
|
||||
{
|
||||
// Create a new deque RectangleShape and pop old
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Get x and y position of snake head
|
||||
sf::Vector2f Snake::GetHeadPosition()
|
||||
sf::Vector2f Snake::GetSnakeHeadPosition()
|
||||
{
|
||||
sf::Vector2f position;
|
||||
position = snakeBody.front().getPosition();
|
||||
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()
|
||||
{
|
||||
// Possibly unnecessary
|
||||
|
@ -4,7 +4,8 @@
|
||||
#include <queue>
|
||||
|
||||
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
|
||||
// {
|
||||
@ -26,9 +27,11 @@ public:
|
||||
Snake(sf::Vector2f head);
|
||||
// Instead of popping like in MoveSnake()
|
||||
// Simply add to deque
|
||||
sf::Vector2f GetHeadPosition();
|
||||
sf::Vector2f GetSnakeHeadPosition();
|
||||
sf::RectangleShape GetSnakeHead();
|
||||
void DisplaySnake(sf::RenderWindow& window);
|
||||
void ExtendSnake();
|
||||
void MoveSnake(); // Move only head body piece
|
||||
void MoveSnake(int snakeDirection); // Move only head body piece
|
||||
};
|
||||
|
||||
|
||||
|
35
src/main.cpp
35
src/main.cpp
@ -22,13 +22,18 @@ int main()
|
||||
int videoSizeHorizontal, videoSizeVertical;
|
||||
videoSizeHorizontal = 1024;
|
||||
videoSizeVertical = 725;
|
||||
/*
|
||||
gameGridHorizontal = (videoSizeHorizontal // 25) * 25;
|
||||
gameGridVertical = (videoSizeVertical // 25) * 25;
|
||||
*/
|
||||
sf::RenderWindow window(sf::VideoMode(videoSizeHorizontal, videoSizeVertical), "SnakePlusPlus");
|
||||
sf::Time delay = sf::milliseconds(100);
|
||||
|
||||
int snakeDirection = 0;
|
||||
Snake Player(sf::Vector2f(25,25));
|
||||
sf::RectangleShape snakeHead(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.setPosition(25,25);
|
||||
@ -41,30 +46,30 @@ int main()
|
||||
if ((event.type == sf::Event::Closed) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
||||
window.close();
|
||||
}
|
||||
sf::Vector2f snakeHeadPosition = Player.GetHeadPosition();
|
||||
sf::Vector2f snakeHeadPosition = Player.GetSnakeHeadPosition();
|
||||
sf::Vector2f snakeFoodPosition = snakeFood.getPosition();
|
||||
// TODO: Split Movement into separate function
|
||||
// Add boundaries
|
||||
// Add movement until boundaries
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
|
||||
snakeHeadPosition.x -= 25;
|
||||
snakeDirection = 1;
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
|
||||
snakeHeadPosition.y -= 25;
|
||||
snakeDirection = 2;
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
|
||||
snakeHeadPosition.y += 25;
|
||||
snakeDirection = 3;
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
|
||||
snakeHeadPosition.x += 25;
|
||||
snakeHead.setPosition(snakeHeadPosition.x, snakeHeadPosition.y);
|
||||
if (SnakeCollision(snakeHead, snakeFood))
|
||||
{
|
||||
snakeFoodPosition.x += 25;
|
||||
snakeFoodPosition.y += 25;
|
||||
snakeFood.setPosition(snakeFoodPosition.x, snakeFoodPosition.y);
|
||||
}
|
||||
snakeDirection = 4;
|
||||
Player.MoveSnake(snakeDirection);
|
||||
// if (SnakeCollision(snakeHead, snakeFood))
|
||||
// {
|
||||
// snakeFoodPosition.x += 25;
|
||||
// snakeFoodPosition.y += 25;
|
||||
// snakeFood.setPosition(snakeFoodPosition.x, snakeFoodPosition.y);
|
||||
window.clear();
|
||||
window.draw(snakeFood);
|
||||
window.draw(snakeHead);
|
||||
|
||||
Player.DisplaySnake(window);
|
||||
window.display();
|
||||
sf::sleep(delay);
|
||||
// sf::sleep(delay);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user