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\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)
|
||||||
@ -21,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()
|
||||||
@ -42,10 +56,41 @@ 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(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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,5 +100,12 @@ Snake::Snake()
|
|||||||
// 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);
|
||||||
|
newBodyPart.setFillColor(sf::Color::Green);
|
||||||
|
snakeBody.push_back(newBodyPart);
|
||||||
|
return;
|
||||||
|
}
|
||||||
// SnakeNode::SnakeNode();
|
// SnakeNode::SnakeNode();
|
||||||
// SnakeNode::SnakeNode(sf::Vector2f addBodyPiece);
|
// SnakeNode::SnakeNode(sf::Vector2f addBodyPiece);
|
||||||
|
11
src/Snake.h
11
src/Snake.h
@ -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
|
||||||
// {
|
// {
|
||||||
@ -21,12 +22,16 @@ 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 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
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
36
src/main.cpp
36
src/main.cpp
@ -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;
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -23,12 +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(5);
|
||||||
|
|
||||||
|
int snakeDirection = 0;
|
||||||
|
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,29 +46,28 @@ 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.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);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user