Merge pull request #10 from TriantaTV/Development
Fixed snake food generation
This commit is contained in:
commit
545bc2a41b
@ -1,8 +1,7 @@
|
|||||||
// #include <iostream>
|
// GameState.cpp
|
||||||
#include <SFML\Graphics.hpp>
|
#include <SFML\Graphics.hpp>
|
||||||
#include <SFML\System.hpp>
|
#include <SFML\System.hpp>
|
||||||
#include "Snake.h"
|
#include "Snake.h"
|
||||||
#include "SnakeFood.h"
|
|
||||||
#include "GameState.h"
|
#include "GameState.h"
|
||||||
|
|
||||||
GameState::GameState()
|
GameState::GameState()
|
||||||
@ -24,14 +23,10 @@ GameState::GameState(int newHorizontal, int newVertical)
|
|||||||
void GameState::startNewGame()
|
void GameState::startNewGame()
|
||||||
{
|
{
|
||||||
gameWindow.create(gameVideoMode, "SnakePlusPlus");
|
gameWindow.create(gameVideoMode, "SnakePlusPlus");
|
||||||
sf::Time delay = sf::milliseconds(25);
|
sf::Time delay = sf::milliseconds(100);
|
||||||
int snakeDirection = 0;
|
int snakeDirection = 0;
|
||||||
Snake player(sf::Vector2f(25,25));
|
Snake player(sf::Vector2f(25,25));
|
||||||
SnakeFood playerFood(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);
|
|
||||||
|
|
||||||
while (gameWindow.isOpen())
|
while (gameWindow.isOpen())
|
||||||
{
|
{
|
||||||
@ -41,15 +36,8 @@ void GameState::startNewGame()
|
|||||||
if ((event.type == sf::Event::Closed) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
if ((event.type == sf::Event::Closed) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
||||||
gameWindow.close();
|
gameWindow.close();
|
||||||
}
|
}
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
|
player.CheckDirection();
|
||||||
snakeDirection = 1;
|
player.MoveSnake(playerFood, gameVideoMode);
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
|
|
||||||
snakeDirection = 2;
|
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
|
|
||||||
snakeDirection = 3;
|
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
|
|
||||||
snakeDirection = 4;
|
|
||||||
player.MoveSnake(playerFood.snakeFoodObject);
|
|
||||||
gameWindow.clear();
|
gameWindow.clear();
|
||||||
player.DisplaySnake(gameWindow);
|
player.DisplaySnake(gameWindow);
|
||||||
gameWindow.draw(playerFood.snakeFoodObject);
|
gameWindow.draw(playerFood.snakeFoodObject);
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
#ifndef GAMESTATE_H
|
#ifndef GAMESTATE_H
|
||||||
#define GAMESTATE_H
|
#define GAMESTATE_H
|
||||||
#include <SFML\Graphics.hpp>
|
#include <SFML\Graphics.hpp>
|
||||||
#include <SFML\System.hpp>
|
|
||||||
|
|
||||||
class GameState
|
class GameState
|
||||||
{
|
{
|
||||||
|
@ -1,22 +1,35 @@
|
|||||||
|
// Snake.cpp
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <queue>
|
||||||
#include <SFML\Graphics.hpp>
|
#include <SFML\Graphics.hpp>
|
||||||
#include <SFML\System.hpp>
|
|
||||||
#include "Snake.h"
|
#include "Snake.h"
|
||||||
|
#include "SnakeFood.h"
|
||||||
|
|
||||||
// Test for collision between two objects
|
// Get a new coordinate position based on snake direction
|
||||||
bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2)
|
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)
|
||||||
{
|
{
|
||||||
// Hack for getting a temporary collision
|
|
||||||
// Collision only tested for origin corrordinate
|
|
||||||
sf::Vector2f object1Position = object1.getPosition();
|
|
||||||
sf::Vector2f object2Position = object2.getPosition();
|
|
||||||
if (object1Position.x != object2Position.x)
|
if (object1Position.x != object2Position.x)
|
||||||
return 0;
|
return 0;
|
||||||
if (object1Position.y != object2Position.y)
|
if (object1Position.y != object2Position.y)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check keyboard for new direction of snake
|
// Check keyboard for new direction of snake
|
||||||
@ -48,26 +61,10 @@ bool Snake::CheckBoundaries()
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Move snake based on direction and test for eating food
|
// Move snake based on direction and test for eating food
|
||||||
void Snake::MoveSnake(sf::RectangleShape& snakeFood)
|
void Snake::MoveSnake(SnakeFood& snakeFood, sf::VideoMode gameVideoMode)
|
||||||
{
|
{
|
||||||
CheckDirection();
|
// CheckDirection();
|
||||||
sf::Vector2f newHeadPosition;
|
sf::Vector2f newHeadPosition;
|
||||||
newHeadPosition = GetSnakeHeadPosition();
|
newHeadPosition = GetSnakeHeadPosition();
|
||||||
if (CheckBoundaries())
|
if (CheckBoundaries())
|
||||||
@ -81,15 +78,9 @@ void Snake::MoveSnake(sf::RectangleShape& snakeFood)
|
|||||||
}
|
}
|
||||||
newBodyPart.setFillColor(sf::Color::Green);
|
newBodyPart.setFillColor(sf::Color::Green);
|
||||||
snakeBody.push_front(newBodyPart);
|
snakeBody.push_front(newBodyPart);
|
||||||
if (!SnakeCollision(GetSnakeHead(), snakeFood))
|
if (!GlobalCollision(GetSnakeHead().getPosition(), snakeFood.snakeFoodObject.getPosition()))
|
||||||
snakeBody.pop_back();
|
snakeBody.pop_back();
|
||||||
else if (SnakeCollision(GetSnakeHead(), snakeFood))
|
SnakeFoodCollision(snakeFood, gameVideoMode);
|
||||||
{
|
|
||||||
sf::Vector2f snakeFoodPosition = snakeFood.getPosition();
|
|
||||||
snakeFoodPosition.x += 25;
|
|
||||||
snakeFoodPosition.y += 25;
|
|
||||||
snakeFood.setPosition(snakeFoodPosition.x, snakeFoodPosition.y);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,7 +115,7 @@ bool Snake::IsSelfCollision(sf::RectangleShape testRectangle)
|
|||||||
{
|
{
|
||||||
for (auto it = snakeBody.cbegin(); it != snakeBody.cend(); ++it)
|
for (auto it = snakeBody.cbegin(); it != snakeBody.cend(); ++it)
|
||||||
{
|
{
|
||||||
if (SnakeCollision(testRectangle, *it))
|
if (GlobalCollision(testRectangle.getPosition(), (*it).getPosition()))
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -132,6 +123,16 @@ bool Snake::IsSelfCollision(sf::RectangleShape testRectangle)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If player collides with food then generate until no longer collided with food
|
||||||
|
void Snake::SnakeFoodCollision(SnakeFood& snakeFood, sf::VideoMode gameVideoMode)
|
||||||
|
{
|
||||||
|
while(IsSelfCollision(snakeFood.snakeFoodObject))
|
||||||
|
{
|
||||||
|
snakeFood.GenerateNewLocation(gameVideoMode.width, gameVideoMode.height);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// General constructor for snake class
|
// General constructor for snake class
|
||||||
Snake::Snake()
|
Snake::Snake()
|
||||||
{
|
{
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
// Snake.h
|
// Snake.h
|
||||||
#ifndef SNAKE_H
|
#ifndef SNAKE_H
|
||||||
#define SNAKE_H
|
#define SNAKE_H
|
||||||
#include <queue>
|
|
||||||
|
|
||||||
bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2);
|
|
||||||
int SnakeMovement();
|
|
||||||
sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position);
|
sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position);
|
||||||
|
bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position);
|
||||||
|
|
||||||
class Snake
|
class Snake
|
||||||
{
|
{
|
||||||
@ -18,7 +16,8 @@ public:
|
|||||||
sf::Vector2f GetSnakeHeadPosition();
|
sf::Vector2f GetSnakeHeadPosition();
|
||||||
sf::RectangleShape GetSnakeHead();
|
sf::RectangleShape GetSnakeHead();
|
||||||
void DisplaySnake(sf::RenderWindow& window);
|
void DisplaySnake(sf::RenderWindow& window);
|
||||||
void MoveSnake(sf::RectangleShape& snakeFood);
|
void MoveSnake(SnakeFood& playerFood, sf::VideoMode gameVideoMode);
|
||||||
|
void SnakeFoodCollision(SnakeFood& snakeFood, sf::VideoMode gameVideoMode);
|
||||||
void CheckDirection();
|
void CheckDirection();
|
||||||
bool CheckBoundaries();
|
bool CheckBoundaries();
|
||||||
bool IsSelfCollision(sf::RectangleShape testRectangle);
|
bool IsSelfCollision(sf::RectangleShape testRectangle);
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
#include <random>
|
// SnakeFood.cpp
|
||||||
|
#include <iostream>
|
||||||
|
#include <SFML\Graphics.hpp>
|
||||||
#include "SnakeFood.h"
|
#include "SnakeFood.h"
|
||||||
|
#include "Snake.h"
|
||||||
|
|
||||||
SnakeFood::SnakeFood()
|
SnakeFood::SnakeFood()
|
||||||
{
|
{
|
||||||
@ -13,12 +16,26 @@ SnakeFood::SnakeFood(sf::Vector2f snakeFoodSize)
|
|||||||
snakeFoodObject.setFillColor(sf::Color::Red);
|
snakeFoodObject.setFillColor(sf::Color::Red);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SnakeFood::GenerateNewLocation(int maxLocation)
|
void SnakeFood::GenerateNewLocation(int horizontalLocation, int verticalLocation)
|
||||||
{
|
{
|
||||||
sf::Vector2f newPosition;
|
sf::Vector2f newPosition;
|
||||||
std::default_random_engine generator;
|
newPosition.x = GenerateRandomNumber(horizontalLocation);
|
||||||
std::uniform_int_distribution<int> distribution(0, maxLocation);
|
newPosition.y = GenerateRandomNumber(verticalLocation);
|
||||||
newPosition.x = distribution(generator);
|
if (GlobalCollision(snakeFoodObject.getPosition(), newPosition))
|
||||||
newPosition.y = distribution(generator);
|
{
|
||||||
|
std::cout << "Location error: " << newPosition.x << " " << newPosition.y << '\n';
|
||||||
|
throw std::runtime_error("Error! New generation on same location");
|
||||||
|
}
|
||||||
snakeFoodObject.setPosition(newPosition);
|
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
|
// SnakeFood.h
|
||||||
#ifndef SNAKEFOOD_H
|
#ifndef SNAKEFOOD_H
|
||||||
#define SNAKEFOOD_H
|
#define SNAKEFOOD_H
|
||||||
|
#include <random>
|
||||||
#include <SFML\Graphics.hpp>
|
#include <SFML\Graphics.hpp>
|
||||||
|
|
||||||
class SnakeFood
|
class SnakeFood
|
||||||
@ -8,9 +9,11 @@ class SnakeFood
|
|||||||
private:
|
private:
|
||||||
public:
|
public:
|
||||||
sf::RectangleShape snakeFoodObject;
|
sf::RectangleShape snakeFoodObject;
|
||||||
|
std::default_random_engine generator;
|
||||||
SnakeFood();
|
SnakeFood();
|
||||||
SnakeFood(sf::Vector2f snakeFoodSize);
|
SnakeFood(sf::Vector2f snakeFoodSize);
|
||||||
void GenerateNewLocation(int maxLocation);
|
void GenerateNewLocation(int horizontalLocation, int verticalLocation);
|
||||||
|
int GenerateRandomNumber(int generationLimit);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user