Merge pull request #6 from TriantaTV/Development

Cleaned up unused code and added GameState
This commit is contained in:
TriantaTV 2022-07-28 15:45:15 -05:00 committed by GitHub
commit 962d2a006b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 44 deletions

24
src/GameState.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <iostream>
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include "GameState.h"
GameState::GameState()
{
videoSizeHorizontal = 1024;
videoSizeVertical = 725;
sf::Vector2u newVideoSize(videoSizeHorizontal, videoSizeVertical);
window.setSize(newVideoSize);
window.setTitle("SnakePlusPlus");
return;
}
GameState::GameState(int newHorizontal, int newVertical)
{
videoSizeHorizontal = newHorizontal;
videoSizeVertical = newVertical;
sf::Vector2u newVideoSize(videoSizeHorizontal, videoSizeVertical);
window.setSize(newVideoSize);
window.setTitle("SnakePlusPlus");
return;
}

16
src/GameState.h Normal file
View File

@ -0,0 +1,16 @@
// GameState.h
#ifndef GAMESTATE_H
#define GAMESTATE_H
class GameState
{
private:
int videoSizeHorizontal;
int videoSizeVertical;
sf::RenderWindow window;
public:
GameState();
GameState(int newHorizontal, int newVertical);
};
#endif

View File

@ -37,15 +37,15 @@ void Snake::CheckDirection()
bool Snake::CheckBoundaries() bool Snake::CheckBoundaries()
{ {
if (snakeBody.front().getPosition().x == 0 && snakeDirection == 1) if (snakeBody.front().getPosition().x == 0 && snakeDirection == 1)
return 1; return true;
if (snakeBody.front().getPosition().y == 0 && snakeDirection == 2) if (snakeBody.front().getPosition().y == 0 && snakeDirection == 2)
return 1; return true;
// TODO: Change boundaries to not be hard-coded // TODO: Change boundaries to not be hard-coded
if (snakeBody.front().getPosition().y > 675 && snakeDirection == 3) if (snakeBody.front().getPosition().y > 675 && snakeDirection == 3)
return 1; return true;
if (snakeBody.front().getPosition().x > 975 && snakeDirection == 4) if (snakeBody.front().getPosition().x > 975 && snakeDirection == 4)
return 1; return true;
return 0; return false;
} }
// Get a new coordinate position based on snake direction // Get a new coordinate position based on snake direction
@ -64,16 +64,9 @@ sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position)
return position; return position;
} }
void Snake::ExtendSnake() // Move snake based on direction and test for eating food
{
// Create a new deque RectangleShape without popping old
return;
}
void Snake::MoveSnake(sf::RectangleShape& snakeFood) void Snake::MoveSnake(sf::RectangleShape& snakeFood)
{ {
// Create a new deque RectangleShape and pop old
// Todo: Depreciate ExtendSnake and just add a collision test
CheckDirection(); CheckDirection();
sf::Vector2f newHeadPosition; sf::Vector2f newHeadPosition;
newHeadPosition = GetSnakeHeadPosition(); newHeadPosition = GetSnakeHeadPosition();
@ -81,6 +74,12 @@ void Snake::MoveSnake(sf::RectangleShape& snakeFood)
newHeadPosition = CalculateNewPosition(snakeDirection, newHeadPosition); newHeadPosition = CalculateNewPosition(snakeDirection, newHeadPosition);
sf::RectangleShape newBodyPart(sf::Vector2f(25,25)); sf::RectangleShape newBodyPart(sf::Vector2f(25,25));
newBodyPart.setPosition(newHeadPosition); newBodyPart.setPosition(newHeadPosition);
if (IsSelfCollision(newBodyPart))
{
// Do nothing if self collision
return;
}
newBodyPart.setFillColor(sf::Color::Green);
snakeBody.push_front(newBodyPart); snakeBody.push_front(newBodyPart);
if (!SnakeCollision(GetSnakeHead(), snakeFood)) if (!SnakeCollision(GetSnakeHead(), snakeFood))
snakeBody.pop_back(); snakeBody.pop_back();
@ -94,7 +93,7 @@ void Snake::MoveSnake(sf::RectangleShape& snakeFood)
return; return;
} }
// Get x and y position of snake head // Return the Vector2f head of snake
sf::Vector2f Snake::GetSnakeHeadPosition() sf::Vector2f Snake::GetSnakeHeadPosition()
{ {
sf::Vector2f position; sf::Vector2f position;
@ -102,6 +101,7 @@ sf::Vector2f Snake::GetSnakeHeadPosition()
return position; return position;
} }
// Return the RectangleShape head of snake
sf::RectangleShape Snake::GetSnakeHead() sf::RectangleShape Snake::GetSnakeHead()
{ {
sf::RectangleShape head; sf::RectangleShape head;
@ -109,6 +109,7 @@ sf::RectangleShape Snake::GetSnakeHead()
return head; return head;
} }
// Iterate through snake deque and draw to window
void Snake::DisplaySnake(sf::RenderWindow& window) void Snake::DisplaySnake(sf::RenderWindow& window)
{ {
for (auto it = snakeBody.cbegin(); it != snakeBody.cend(); ++it) for (auto it = snakeBody.cbegin(); it != snakeBody.cend(); ++it)
@ -118,12 +119,29 @@ void Snake::DisplaySnake(sf::RenderWindow& window)
return; return;
} }
// Test for snake self collision
bool Snake::IsSelfCollision(sf::RectangleShape testRectangle)
{
for (auto it = snakeBody.cbegin(); it != snakeBody.cend(); ++it)
{
if (SnakeCollision(testRectangle, *it))
{
return true;
}
}
return false;
}
// General constructor for snake class
Snake::Snake() Snake::Snake()
{ {
// Possibly unnecessary sf::RectangleShape newBodyPart(sf::Vector2f(25,25));
// The big 3 could be used to create a fresh game state newBodyPart.setFillColor(sf::Color::Green);
snakeBody.push_back(newBodyPart);
return; return;
} }
// Constructor for snake with position
Snake::Snake(sf::Vector2f head) Snake::Snake(sf::Vector2f head)
{ {
sf::RectangleShape newBodyPart(head); sf::RectangleShape newBodyPart(head);
@ -131,5 +149,3 @@ Snake::Snake(sf::Vector2f head)
snakeBody.push_back(newBodyPart); snakeBody.push_back(newBodyPart);
return; return;
} }
// SnakeNode::SnakeNode();
// SnakeNode::SnakeNode(sf::Vector2f addBodyPiece);

View File

@ -7,17 +7,6 @@ bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2);
int SnakeMovement(); int SnakeMovement();
sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position); sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position);
// class SnakeNode
// {
// private:
// // sf::RectangleShape snakeBody(sf::Vector2f(25,25));
// sf::Vector2f snakeBodyLocation;
// SnakeNode* next;
// public:
// SnakeNode();
// SnakeNode(sf::Vector2f addBodyPiece);
// };
class Snake class Snake
{ {
private: private:
@ -29,10 +18,10 @@ public:
sf::Vector2f GetSnakeHeadPosition(); sf::Vector2f GetSnakeHeadPosition();
sf::RectangleShape GetSnakeHead(); sf::RectangleShape GetSnakeHead();
void DisplaySnake(sf::RenderWindow& window); void DisplaySnake(sf::RenderWindow& window);
void ExtendSnake();
void MoveSnake(sf::RectangleShape& snakeFood); void MoveSnake(sf::RectangleShape& snakeFood);
void CheckDirection(); void CheckDirection();
bool CheckBoundaries(); bool CheckBoundaries();
bool IsSelfCollision(sf::RectangleShape testRectangle);
}; };

View File

@ -3,18 +3,6 @@
#include <SFML\System.hpp> #include <SFML\System.hpp>
#include "Snake.h" #include "Snake.h"
/*
TODO:
Add ability for body to extend when eating food
Each piece of queue has coordinates
If head touches food, just add to queue, don't pop
*/
int main() int main()
{ {
int videoSizeHorizontal, videoSizeVertical; int videoSizeHorizontal, videoSizeVertical;
@ -43,7 +31,6 @@ 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 snakeFoodPosition = snakeFood.getPosition();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
snakeDirection = 1; snakeDirection = 1;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))