Fixed generation issue with snakeFood
Fixed generation issue with snakeFood and game should be fully playable without an official end screen implemented. So when the snake gets stuck or the game finishes, there is nothing to close the game automatically.
This commit is contained in:
parent
88902fed6a
commit
2c4537eda7
@ -2,7 +2,6 @@
|
||||
#include <SFML\Graphics.hpp>
|
||||
#include <SFML\System.hpp>
|
||||
#include "Snake.h"
|
||||
#include "SnakeFood.h"
|
||||
#include "GameState.h"
|
||||
|
||||
GameState::GameState()
|
||||
|
@ -1,41 +0,0 @@
|
||||
#include <random>
|
||||
#include <SFML\Graphics.hpp>
|
||||
#include "GeneralFunctions.h"
|
||||
|
||||
|
||||
// Get a new coordinate position based on snake direction
|
||||
sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position)
|
||||
{
|
||||
if (direction == 0)
|
||||
return position;
|
||||
if (direction == 1)
|
||||
position.x -= 25;
|
||||
if (direction == 2)
|
||||
position.y -= 25;
|
||||
if (direction == 3)
|
||||
position.y += 25;
|
||||
if (direction == 4)
|
||||
position.x += 25;
|
||||
return position;
|
||||
}
|
||||
|
||||
// Test for collision between two object positions
|
||||
bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position)
|
||||
{
|
||||
if (object1Position.x != object2Position.x)
|
||||
return 0;
|
||||
if (object1Position.y != object2Position.y)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int GenerateRandomNumber(int generationLimit)
|
||||
{
|
||||
int generatedNumber;
|
||||
std::default_random_engine generator;
|
||||
std::uniform_int_distribution<> distribution(0, generationLimit);
|
||||
generatedNumber = distribution(generator);
|
||||
generatedNumber -= (generatedNumber % 25);
|
||||
return generatedNumber;
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
#ifndef GENERALFUNCTIONS_H
|
||||
#define GENERALFUNCTIONS_H
|
||||
#include <SFML\Graphics.hpp>
|
||||
|
||||
sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position);
|
||||
bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position);
|
||||
int GenerateRandomNumber(int generationLimit);
|
||||
|
||||
|
||||
#endif
|
@ -1,10 +1,37 @@
|
||||
// Snake.cpp
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <SFML\Graphics.hpp>
|
||||
#include "GeneralFunctions.h"
|
||||
#include "Snake.h"
|
||||
#include "SnakeFood.h"
|
||||
|
||||
// Get a new coordinate position based on snake direction
|
||||
sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position)
|
||||
{
|
||||
if (direction == 0)
|
||||
return position;
|
||||
if (direction == 1)
|
||||
position.x -= 25;
|
||||
if (direction == 2)
|
||||
position.y -= 25;
|
||||
if (direction == 3)
|
||||
position.y += 25;
|
||||
if (direction == 4)
|
||||
position.x += 25;
|
||||
return position;
|
||||
}
|
||||
|
||||
// Test for collision between two object positions
|
||||
bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position)
|
||||
{
|
||||
if (object1Position.x != object2Position.x)
|
||||
return 0;
|
||||
if (object1Position.y != object2Position.y)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check keyboard for new direction of snake
|
||||
void Snake::CheckDirection()
|
||||
{
|
||||
|
@ -1,9 +1,9 @@
|
||||
// Snake.h
|
||||
#ifndef SNAKE_H
|
||||
#define SNAKE_H
|
||||
#include <queue>
|
||||
#include "SnakeFood.h"
|
||||
|
||||
sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position);
|
||||
bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position);
|
||||
|
||||
class Snake
|
||||
{
|
||||
|
@ -1,8 +1,8 @@
|
||||
// SnakeFood.cpp
|
||||
#include <iostream>
|
||||
#include <SFML\Graphics.hpp>
|
||||
#include "GeneralFunctions.h"
|
||||
#include "SnakeFood.h"
|
||||
#include "Snake.h"
|
||||
|
||||
SnakeFood::SnakeFood()
|
||||
{
|
||||
@ -29,3 +29,13 @@ void SnakeFood::GenerateNewLocation(int horizontalLocation, int verticalLocation
|
||||
snakeFoodObject.setPosition(newPosition);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int SnakeFood::GenerateRandomNumber(int generationLimit)
|
||||
{
|
||||
int generatedNumber;
|
||||
std::uniform_int_distribution<> distribution(0, generationLimit);
|
||||
generatedNumber = distribution(generator);
|
||||
generatedNumber -= (generatedNumber % 25);
|
||||
return generatedNumber;
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
// SnakeFood.h
|
||||
#ifndef SNAKEFOOD_H
|
||||
#define SNAKEFOOD_H
|
||||
#include <random>
|
||||
#include <SFML\Graphics.hpp>
|
||||
|
||||
class SnakeFood
|
||||
@ -8,9 +9,11 @@ class SnakeFood
|
||||
private:
|
||||
public:
|
||||
sf::RectangleShape snakeFoodObject;
|
||||
std::default_random_engine generator;
|
||||
SnakeFood();
|
||||
SnakeFood(sf::Vector2f snakeFoodSize);
|
||||
void GenerateNewLocation(int horizontalLocation, int verticalLocation);
|
||||
int GenerateRandomNumber(int generationLimit);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user