Fixed snake

This commit is contained in:
TriantaTV 2023-04-15 05:15:11 -05:00
parent 498ac65dbc
commit a9d194c90c
6 changed files with 62 additions and 62 deletions

View File

@ -15,8 +15,6 @@ namespace snakeplusplus
GameEngine(); GameEngine();
void StartGame(void); void StartGame(void);
sf::Vector2f GetGameBoundaries(void); sf::Vector2f GetGameBoundaries(void);
protected:
;
private: private:
std::vector< std::vector<char> > gameBoard; std::vector< std::vector<char> > gameBoard;
PlayerInput controls; PlayerInput controls;

View File

@ -13,8 +13,6 @@ namespace snakeplusplus
public: public:
PlayerInput(void); PlayerInput(void);
PlayerDirection GetPlayerInput(void); PlayerDirection GetPlayerInput(void);
protected:
;
private: private:
PlayerDirection lastPlayerInput; PlayerDirection lastPlayerInput;
}; };
@ -26,10 +24,8 @@ namespace snakeplusplus
PlayerOutput(void); PlayerOutput(void);
bool IsOpen(void); bool IsOpen(void);
void CheckContinue(void); void CheckContinue(void);
void DisplayGameState(std::vector< std::vector<char> >* gameBoard); void DisplayGameState(std::vector< std::vector<char> >& gameBoard);
void StartGameWindow(void); void StartGameWindow(void);
protected:
;
private: private:
void CheckWindowEvents(void); void CheckWindowEvents(void);
void DisplayEndScreen(void); void DisplayEndScreen(void);
@ -40,7 +36,7 @@ namespace snakeplusplus
sf::VideoMode gameVideoSettings; sf::VideoMode gameVideoSettings;
sf::RectangleShape drawObject; sf::RectangleShape drawObject;
bool isWindowAlive; bool isWindowAlive;
sf::Time delay = sf::milliseconds(120); sf::Time delay = sf::milliseconds(60);
}; };
} }

View File

@ -13,15 +13,10 @@ namespace snakeplusplus
struct Snake struct Snake
{ {
public: public:
Snake(void);
sf::Vector2f headLocation; sf::Vector2f headLocation;
sf::Vector2f speed; sf::Vector2f speed;
std::queue< std::shared_ptr<char> > body; std::queue<char*> body;
void Pop(void); void Pop(void);
protected:
;
private:
;
}; };
struct Food struct Food
@ -29,10 +24,8 @@ namespace snakeplusplus
public: public:
Food(void); Food(void);
sf::Vector2f location; sf::Vector2f location;
std::shared_ptr<char> food; char* food;
void GenerateNewFood(sf::Vector2f boundaries); void GenerateNewFood(sf::Vector2f boundaries);
protected:
;
private: private:
std::default_random_engine generator; std::default_random_engine generator;
int GenerateRandomNumber(int generationLimit); int GenerateRandomNumber(int generationLimit);

View File

@ -1,5 +1,5 @@
// GameState.cpp // GameState.cpp
#include <SFML/System/Vector2.hpp> #include <iostream>
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
@ -31,7 +31,8 @@ namespace snakeplusplus
{ {
UpdatePlayerSpeed(); UpdatePlayerSpeed();
PlaceNewSnakePart(MovePlayer()); PlaceNewSnakePart(MovePlayer());
graphics.DisplayGameState(&gameBoard); RegenerateFood();
graphics.DisplayGameState(gameBoard);
} }
return; return;
} }
@ -50,20 +51,21 @@ namespace snakeplusplus
void GameEngine::PlaceNewSnakePart(sf::Vector2f location) void GameEngine::PlaceNewSnakePart(sf::Vector2f location)
{ {
char locationState;
try try
{ {
locationState = gameBoard.at(location.y).at(location.x); char* locationState;
if (locationState == 'O') locationState = &gameBoard.at(location.y).at(location.x);
if (*locationState == 'O' && player.body.size() > 1)
isGameOver = true; // Game should end (Snake touching snake) isGameOver = true; // Game should end (Snake touching snake)
*locationState = 'O';
player.body.push(locationState);
player.headLocation = location;
if (playerFood.location != location) if (playerFood.location != location)
player.Pop(); player.Pop();
gameBoard.at(location.y).at(location.x) = 'O';
} catch (const std::out_of_range& error) { } catch (const std::out_of_range& error) {
isGameOver = true; // Snake ran into edge isGameOver = true; // Snake ran into edge
return; exit(0);
} }
return;
} }
// Generates new food until not colliding with player // Generates new food until not colliding with player
@ -87,15 +89,16 @@ namespace snakeplusplus
{ {
gameBoard.clear(); gameBoard.clear();
sf::Vector2f boardDimensions = GetGameBoundaries(); sf::Vector2f boardDimensions = GetGameBoundaries();
std::vector<char> tempBoard; gameBoard.resize(boardDimensions.y, std::vector<char> (boardDimensions.x, ' '));
tempBoard.resize(boardDimensions.x, ' '); player.headLocation.x = 4;
gameBoard.resize(boardDimensions.y, tempBoard); player.headLocation.y = 5;
sf::Vector2f playerLocation(4,5); char* locationState = &gameBoard.at(player.headLocation.y).at(player.headLocation.x);
char* headLocation = &gameBoard.at(playerLocation.y).at(playerLocation.x); player.body.push(locationState);
*headLocation = 'O'; *player.body.front() = 'O';
player.body.push(std::shared_ptr<char>(headLocation)); playerFood.location.x = 2;
sf::Vector2f foodLocation(2,2); playerFood.location.y = 2;
gameBoard.at(foodLocation.y).at(foodLocation.x) = 'X'; playerFood.food = &gameBoard.at(2).at(2);
*playerFood.food = 'X';
return; return;
} }
@ -105,16 +108,20 @@ namespace snakeplusplus
switch (input) { switch (input) {
case kUp: case kUp:
player.speed.x = 0; player.speed.x = 0;
player.speed.y = 1; player.speed.y = -1;
break;
case kLeft: case kLeft:
player.speed.x = -1; player.speed.x = -1;
player.speed.y = 0; player.speed.y = 0;
break;
case kRight: case kRight:
player.speed.x = 1; player.speed.x = 1;
player.speed.y = 0; player.speed.y = 0;
break;
case kDown: case kDown:
player.speed.x = 0; player.speed.x = 0;
player.speed.y = -1; player.speed.y = 1;
break;
default: default:
break; break;
} }

View File

@ -1,4 +1,5 @@
#include "playerinterface.hpp" #include "playerinterface.hpp"
#include <SFML/System/Vector2.hpp>
#include <iostream> #include <iostream>
namespace snakeplusplus namespace snakeplusplus
@ -23,15 +24,17 @@ namespace snakeplusplus
bool PlayerOutput::IsOpen(void) bool PlayerOutput::IsOpen(void)
{ {
return isWindowAlive; return gameWindow.isOpen();
} }
PlayerOutput::PlayerOutput(void) PlayerOutput::PlayerOutput(void)
{ {
float kWidth = 1025 / kGridSize; float kWidth = 1025;
float kHeight = 725 / kGridSize; float kHeight = 725;
gameBoundaries = sf::Vector2f(kWidth, kHeight); float kBoardWidth = kWidth / kGridSize;
gameVideoSettings = sf::VideoMode(1025, 725); float kBoardHeight = kHeight / kGridSize;
gameBoundaries = sf::Vector2f(kBoardWidth, kBoardHeight);
gameVideoSettings = sf::VideoMode(kWidth, kHeight);
drawObject.setSize(sf::Vector2f(kGridSize, kGridSize)); drawObject.setSize(sf::Vector2f(kGridSize, kGridSize));
return; return;
} }
@ -74,22 +77,30 @@ namespace snakeplusplus
return; return;
} }
void PlayerOutput::DisplayGameState(std::vector< std::vector<char> >* gameBoard) void PlayerOutput::DisplayGameState(std::vector< std::vector<char> >& gameBoard)
{ {
CheckWindowEvents(); CheckWindowEvents();
sf::Vector2i location(0,0); sf::Vector2f location;
char letterOnBoard; char* letterOnBoard;
for (; location.y < gameBoundaries.y; location.y++) for (float y = 0; y < gameBoundaries.y; y++)
{ {
for (; location.x < gameBoundaries.x; location.x++) for (float x = 0; x < gameBoundaries.x; x++)
{ {
letterOnBoard = gameBoard->at(location.y).at(location.y); location.x = x;
if (letterOnBoard == 'o') location.y = y;
DrawSnake(static_cast<sf::Vector2f>(location)); letterOnBoard = &gameBoard.at(location.y).at(location.x);
else if (letterOnBoard == 'x') switch (*letterOnBoard)
DrawFood(static_cast<sf::Vector2f>(location)); {
else case 'O':
DrawEmpty(static_cast<sf::Vector2f>(location)); DrawSnake(location);
break;
case 'X':
DrawFood(location);
break;
default:
DrawEmpty(location);
break;
}
} }
} }
gameWindow.display(); gameWindow.display();

View File

@ -1,4 +1,5 @@
// Snake.cpp // Snake.cpp
#include <algorithm>
#include <memory> #include <memory>
#include <queue> #include <queue>
#include <random> #include <random>
@ -7,21 +8,15 @@
namespace snakeplusplus namespace snakeplusplus
{ {
// General constructor for snake class
Snake::Snake(void)
{
return;
}
void Snake::Pop(void) void Snake::Pop(void)
{ {
*(body.front().get()) = ' '; *(body.front()) = ' ';
body.pop(); body.pop();
} }
Food::Food() Food::Food(void)
{ {
return; generator.seed(std::random_device{}());
} }
// Returns a new food object for the snakeFood // Returns a new food object for the snakeFood