Split into separate files and added planning

Snake is now planned to use a deque for location of body. Split snake into separate file outside of main.
This commit is contained in:
TriantaTV 2022-07-25 15:40:45 -05:00
parent 8e4d53ea2a
commit 165e531205
3 changed files with 102 additions and 14 deletions

55
src/Snake.cpp Normal file
View File

@ -0,0 +1,55 @@
#include <iostream>
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include "Snake.h"
using namespace std;
bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2)
{
sf::Vector2f object1Position = object1.getPosition();
sf::Vector2f object2Position = object2.getPosition();
if (object1Position.x != object2Position.x)
return 0;
if (object1Position.y != object2Position.y)
return 0;
return 1;
}
void SnakeMovement(sf::Keyboard keyboard)
{
/*
if (keyboard.pressed(Left))
snakeHead.moveleft();
if (keyboard.pressed(Right))
snakeHead.moveRight();
if (keyboard.pressed(Down))
snakeHead.moveDown();
if (keyboard.pressed(Up))
snakeHead.moveUp();
if (!snakeHead.isTouchingFood())
snakeQueue.pop();
*/
return;
}
void Snake::ExtendSnake(sf::Vector2f newLocation)
{
/*
snakeBody.push_back(newLocation);
*/
return;
}
void Snake::MoveSnake()
{
/*
head.snakeBody()
*/
return;
}
SnakeNode::SnakeNode();
SnakeNode::SnakeNode(sf::Vector2f addBodyPiece);

30
src/Snake.h Normal file
View File

@ -0,0 +1,30 @@
//Snake.h
#ifndef SNAKE_H
#define SNAKE_H
#include <queue>
bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2);
void SnakeMovement(sf::Keyboard keyboard);
class SnakeNode
{
private:
// sf::RectangleShape snakeBody(sf::Vector2f(25,25));
sf::Vector2f snakeBodyLocation;
SnakeNode* next;
public:
SnakeNode();
SnakeNode(sf::Vector2f addBodyPiece);
};
class Snake
{
private:
std::deque<sf::Vector2f> snakeBody;
public:
void ExtendSnake(sf::Vector2f newLocation);
void MoveSnake();
};
#endif

View File

@ -1,9 +1,22 @@
#include <iostream> #include <iostream>
#include <SFML\Graphics.hpp> #include <SFML\Graphics.hpp>
#include <SFML\System.hpp> #include <SFML\System.hpp>
#include "Snake.h"
using namespace std; using namespace std;
bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2);
/*
TODO: Work on snake body
Add ability for body to extend when eating food
Turn snake body into queue
Each piece of queue has coordinates
If head touches food, just add to queue, don't pop
*/
int main() int main()
{ {
@ -30,6 +43,9 @@ int main()
} }
sf::Vector2f snakeHeadPosition = snakeHead.getPosition(); sf::Vector2f snakeHeadPosition = snakeHead.getPosition();
sf::Vector2f snakeFoodPosition = snakeFood.getPosition(); sf::Vector2f snakeFoodPosition = snakeFood.getPosition();
// TODO: Split Movement into separate function
// Add boundaries
// Add movement until boundaries
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
snakeHeadPosition.x -= 25; snakeHeadPosition.x -= 25;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
@ -52,16 +68,3 @@ int main()
sf::sleep(delay); sf::sleep(delay);
} }
} }
bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2)
{
sf::Vector2f object1Position = object1.getPosition();
sf::Vector2f object2Position = object2.getPosition();
if (object1Position.x != object2Position.x)
return 0;
if (object1Position.y != object2Position.y)
return 0;
return 1;
}