Moved snakeFood to be a separate class

Moved snakeFood to be a separate class and a temporary unused function was added for randomly generating the new location.
This commit is contained in:
TriantaTV 2022-08-02 21:58:58 -05:00
parent eae6b4c70d
commit c3342a14c7
3 changed files with 48 additions and 6 deletions

View File

@ -1,7 +1,8 @@
#include <iostream>
// #include <iostream>
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include "Snake.h"
#include "SnakeFood.h"
#include "GameState.h"
GameState::GameState()
@ -25,8 +26,9 @@ void GameState::startNewGame()
gameWindow.create(gameVideoMode, "SnakePlusPlus");
sf::Time delay = sf::milliseconds(25);
int snakeDirection = 0;
Snake Player(sf::Vector2f(25,25));
sf::RectangleShape snakeHead(sf::Vector2f(25,25));
Snake player(sf::Vector2f(25,25));
SnakeFood playerFood(sf::Vector2f(25,25));
// sf::RectangleShape snakeHead(sf::Vector2f(25,25));
sf::RectangleShape snakeFood(sf::Vector2f(25,25));
snakeFood.setFillColor(sf::Color::Red);
snakeFood.setPosition(25,25);
@ -47,10 +49,10 @@ void GameState::startNewGame()
snakeDirection = 3;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
snakeDirection = 4;
Player.MoveSnake(snakeFood);
player.MoveSnake(playerFood.snakeFoodObject);
gameWindow.clear();
gameWindow.draw(snakeFood);
Player.DisplaySnake(gameWindow);
player.DisplaySnake(gameWindow);
gameWindow.draw(playerFood.snakeFoodObject);
gameWindow.display();
sf::sleep(delay);
}

24
src/SnakeFood.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <random>
#include "SnakeFood.h"
SnakeFood::SnakeFood()
{
snakeFoodObject.setSize(sf::Vector2f(25,25));
snakeFoodObject.setFillColor(sf::Color::Red);
}
SnakeFood::SnakeFood(sf::Vector2f snakeFoodSize)
{
snakeFoodObject.setSize(snakeFoodSize);
snakeFoodObject.setFillColor(sf::Color::Red);
}
void SnakeFood::GenerateNewLocation(int maxLocation)
{
sf::Vector2f newPosition;
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0, maxLocation);
newPosition.x = distribution(generator);
newPosition.y = distribution(generator);
snakeFoodObject.setPosition(newPosition);
}

16
src/SnakeFood.h Normal file
View File

@ -0,0 +1,16 @@
// SnakeFood.h
#ifndef SNAKEFOOD_H
#define SNAKEFOOD_H
#include <SFML\Graphics.hpp>
class SnakeFood
{
private:
public:
sf::RectangleShape snakeFoodObject;
SnakeFood();
SnakeFood(sf::Vector2f snakeFoodSize);
void GenerateNewLocation(int maxLocation);
};
#endif