From 69423bf9d0f6c8a05f42432d95fdb9b140ba153e Mon Sep 17 00:00:00 2001 From: TriantaTV <56975502+TriantaTV@users.noreply.github.com> Date: Wed, 27 Jul 2022 15:15:36 -0500 Subject: [PATCH 1/3] Cleaned up unused code and added more comments for clarification --- src/Snake.cpp | 19 ++++++++++--------- src/Snake.h | 12 ------------ src/main.cpp | 1 - 3 files changed, 10 insertions(+), 22 deletions(-) diff --git a/src/Snake.cpp b/src/Snake.cpp index 707c7ee..6a9fd9c 100644 --- a/src/Snake.cpp +++ b/src/Snake.cpp @@ -64,12 +64,7 @@ sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position) return position; } -void Snake::ExtendSnake() -{ - // Create a new deque RectangleShape without popping old - return; -} - +// Move snake based on direction and test for eating food void Snake::MoveSnake(sf::RectangleShape& snakeFood) { // Create a new deque RectangleShape and pop old @@ -94,7 +89,7 @@ void Snake::MoveSnake(sf::RectangleShape& snakeFood) return; } -// Get x and y position of snake head +// Return the Vector2f head of snake sf::Vector2f Snake::GetSnakeHeadPosition() { sf::Vector2f position; @@ -102,6 +97,7 @@ sf::Vector2f Snake::GetSnakeHeadPosition() return position; } +// Return the RectangleShape head of snake sf::RectangleShape Snake::GetSnakeHead() { sf::RectangleShape head; @@ -109,6 +105,7 @@ sf::RectangleShape Snake::GetSnakeHead() return head; } +// Iterate through snake deque and draw to window void Snake::DisplaySnake(sf::RenderWindow& window) { for (auto it = snakeBody.cbegin(); it != snakeBody.cend(); ++it) @@ -118,12 +115,16 @@ void Snake::DisplaySnake(sf::RenderWindow& window) return; } +// General constructor for snake class Snake::Snake() { - // Possibly unnecessary - // The big 3 could be used to create a fresh game state + sf::RectangleShape newBodyPart(sf::Vector2f(25,25)); + newBodyPart.setFillColor(sf::Color::Green); + snakeBody.push_back(newBodyPart); return; } + +// Constructor for snake with position Snake::Snake(sf::Vector2f head) { sf::RectangleShape newBodyPart(head); diff --git a/src/Snake.h b/src/Snake.h index 602029b..5670fc5 100644 --- a/src/Snake.h +++ b/src/Snake.h @@ -7,17 +7,6 @@ bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2); int SnakeMovement(); 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 { private: @@ -29,7 +18,6 @@ public: sf::Vector2f GetSnakeHeadPosition(); sf::RectangleShape GetSnakeHead(); void DisplaySnake(sf::RenderWindow& window); - void ExtendSnake(); void MoveSnake(sf::RectangleShape& snakeFood); void CheckDirection(); bool CheckBoundaries(); diff --git a/src/main.cpp b/src/main.cpp index 72f713f..fd9a693 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -43,7 +43,6 @@ int main() if ((event.type == sf::Event::Closed) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) window.close(); } - // sf::Vector2f snakeFoodPosition = snakeFood.getPosition(); if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) snakeDirection = 1; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) From e8125882003b03a8266d40773da76e8595bc3ab3 Mon Sep 17 00:00:00 2001 From: TriantaTV <56975502+TriantaTV@users.noreply.github.com> Date: Thu, 28 Jul 2022 14:56:54 -0500 Subject: [PATCH 2/3] Added snake collision with itself Added snake collision with itself and also changed color to green. --- src/Snake.cpp | 29 ++++++++++++++++++++++++----- src/Snake.h | 1 + 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Snake.cpp b/src/Snake.cpp index 6a9fd9c..b771736 100644 --- a/src/Snake.cpp +++ b/src/Snake.cpp @@ -37,15 +37,15 @@ void Snake::CheckDirection() bool Snake::CheckBoundaries() { if (snakeBody.front().getPosition().x == 0 && snakeDirection == 1) - return 1; + return true; if (snakeBody.front().getPosition().y == 0 && snakeDirection == 2) - return 1; + return true; // TODO: Change boundaries to not be hard-coded if (snakeBody.front().getPosition().y > 675 && snakeDirection == 3) - return 1; + return true; if (snakeBody.front().getPosition().x > 975 && snakeDirection == 4) - return 1; - return 0; + return true; + return false; } // Get a new coordinate position based on snake direction @@ -76,6 +76,12 @@ void Snake::MoveSnake(sf::RectangleShape& snakeFood) newHeadPosition = CalculateNewPosition(snakeDirection, newHeadPosition); sf::RectangleShape newBodyPart(sf::Vector2f(25,25)); newBodyPart.setPosition(newHeadPosition); + if (IsSelfCollision(newBodyPart)) + { + // Do nothing if self collision + return; + } + newBodyPart.setFillColor(sf::Color::Green); snakeBody.push_front(newBodyPart); if (!SnakeCollision(GetSnakeHead(), snakeFood)) snakeBody.pop_back(); @@ -115,6 +121,19 @@ void Snake::DisplaySnake(sf::RenderWindow& window) 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() { diff --git a/src/Snake.h b/src/Snake.h index 5670fc5..5dc98a8 100644 --- a/src/Snake.h +++ b/src/Snake.h @@ -21,6 +21,7 @@ public: void MoveSnake(sf::RectangleShape& snakeFood); void CheckDirection(); bool CheckBoundaries(); + bool IsSelfCollision(sf::RectangleShape testRectangle); }; From 1a90a4601ad6a092be73dcaa7a55818bfe804033 Mon Sep 17 00:00:00 2001 From: TriantaTV <56975502+TriantaTV@users.noreply.github.com> Date: Thu, 28 Jul 2022 15:18:24 -0500 Subject: [PATCH 3/3] Added GameState and cleaned up unused code --- src/GameState.cpp | 24 ++++++++++++++++++++++++ src/GameState.h | 16 ++++++++++++++++ src/Snake.cpp | 4 ---- src/Snake.h | 2 +- src/main.cpp | 12 ------------ 5 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 src/GameState.cpp create mode 100644 src/GameState.h diff --git a/src/GameState.cpp b/src/GameState.cpp new file mode 100644 index 0000000..24f2310 --- /dev/null +++ b/src/GameState.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +#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; +} diff --git a/src/GameState.h b/src/GameState.h new file mode 100644 index 0000000..5381228 --- /dev/null +++ b/src/GameState.h @@ -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 diff --git a/src/Snake.cpp b/src/Snake.cpp index b771736..ded4b4c 100644 --- a/src/Snake.cpp +++ b/src/Snake.cpp @@ -67,8 +67,6 @@ sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position) // Move snake based on direction and test for eating food void Snake::MoveSnake(sf::RectangleShape& snakeFood) { - // Create a new deque RectangleShape and pop old - // Todo: Depreciate ExtendSnake and just add a collision test CheckDirection(); sf::Vector2f newHeadPosition; newHeadPosition = GetSnakeHeadPosition(); @@ -151,5 +149,3 @@ Snake::Snake(sf::Vector2f head) snakeBody.push_back(newBodyPart); return; } -// SnakeNode::SnakeNode(); -// SnakeNode::SnakeNode(sf::Vector2f addBodyPiece); diff --git a/src/Snake.h b/src/Snake.h index 5dc98a8..27016ed 100644 --- a/src/Snake.h +++ b/src/Snake.h @@ -1,4 +1,4 @@ -//Snake.h +// Snake.h #ifndef SNAKE_H #define SNAKE_H #include diff --git a/src/main.cpp b/src/main.cpp index fd9a693..ba99147 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,18 +3,6 @@ #include #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 videoSizeHorizontal, videoSizeVertical;