Merge pull request #3 from TriantaTV/Development
Changed player to be a class with a deque for the body with movement working.
This commit is contained in:
commit
84805e9751
@ -2,7 +2,6 @@
|
||||
#include <SFML\Graphics.hpp>
|
||||
#include <SFML\System.hpp>
|
||||
#include "Snake.h"
|
||||
using namespace std;
|
||||
|
||||
// Test for collision between two objects
|
||||
bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2)
|
||||
@ -21,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()
|
||||
@ -42,10 +56,41 @@ 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(sf::Vector2f(25,25));
|
||||
newBodyPart.setPosition(newHeadPosition);
|
||||
snakeBody.push_back(newBodyPart);
|
||||
snakeBody.pop_front();
|
||||
return;
|
||||
}
|
||||
|
||||
// Get x and y position of snake head
|
||||
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;
|
||||
}
|
||||
|
||||
@ -55,5 +100,12 @@ Snake::Snake()
|
||||
// The big 3 could be used to create a fresh game state
|
||||
return;
|
||||
}
|
||||
Snake::Snake(sf::Vector2f head)
|
||||
{
|
||||
sf::RectangleShape newBodyPart(head);
|
||||
newBodyPart.setFillColor(sf::Color::Green);
|
||||
snakeBody.push_back(newBodyPart);
|
||||
return;
|
||||
}
|
||||
// SnakeNode::SnakeNode();
|
||||
// SnakeNode::SnakeNode(sf::Vector2f addBodyPiece);
|
||||
|
11
src/Snake.h
11
src/Snake.h
@ -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
|
||||
// {
|
||||
@ -21,12 +22,16 @@ class Snake
|
||||
{
|
||||
private:
|
||||
std::deque<sf::RectangleShape> snakeBody;
|
||||
Snake();
|
||||
public:
|
||||
Snake();
|
||||
Snake(sf::Vector2f head);
|
||||
// Instead of popping like in MoveSnake()
|
||||
// Simply add to deque
|
||||
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
|
||||
};
|
||||
|
||||
|
||||
|
36
src/main.cpp
36
src/main.cpp
@ -2,7 +2,6 @@
|
||||
#include <SFML\Graphics.hpp>
|
||||
#include <SFML\System.hpp>
|
||||
#include "Snake.h"
|
||||
using namespace std;
|
||||
|
||||
|
||||
/*
|
||||
@ -23,12 +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);
|
||||
sf::Time delay = sf::milliseconds(5);
|
||||
|
||||
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,29 +46,28 @@ int main()
|
||||
if ((event.type == sf::Event::Closed) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
||||
window.close();
|
||||
}
|
||||
sf::Vector2f snakeHeadPosition = snakeHead.getPosition();
|
||||
// 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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user