Moved general functions to a general file
Moved general functions to a general file and added a statement to catch if the newly generated location is the same. The snake food is not currently generating to a new random location like expected and is broken.
This commit is contained in:
parent
cc3a61abb7
commit
88902fed6a
@ -24,7 +24,7 @@ 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(50);
|
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));
|
||||||
|
41
src/GeneralFunctions.cpp
Normal file
41
src/GeneralFunctions.cpp
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#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;
|
||||||
|
}
|
10
src/GeneralFunctions.h
Normal file
10
src/GeneralFunctions.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#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,25 +1,10 @@
|
|||||||
// Snake.cpp
|
// Snake.cpp
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <SFML\Graphics.hpp>
|
#include <SFML\Graphics.hpp>
|
||||||
|
#include "GeneralFunctions.h"
|
||||||
#include "Snake.h"
|
#include "Snake.h"
|
||||||
#include "SnakeFood.h"
|
#include "SnakeFood.h"
|
||||||
|
|
||||||
// Test for collision between two objects
|
|
||||||
bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2)
|
|
||||||
{
|
|
||||||
// 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)
|
|
||||||
return 0;
|
|
||||||
if (object1Position.y != object2Position.y)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check keyboard for new direction of snake
|
// Check keyboard for new direction of snake
|
||||||
void Snake::CheckDirection()
|
void Snake::CheckDirection()
|
||||||
{
|
{
|
||||||
@ -49,22 +34,6 @@ 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(SnakeFood& snakeFood, sf::VideoMode gameVideoMode)
|
void Snake::MoveSnake(SnakeFood& snakeFood, sf::VideoMode gameVideoMode)
|
||||||
{
|
{
|
||||||
@ -82,7 +51,7 @@ void Snake::MoveSnake(SnakeFood& snakeFood, sf::VideoMode gameVideoMode)
|
|||||||
}
|
}
|
||||||
newBodyPart.setFillColor(sf::Color::Green);
|
newBodyPart.setFillColor(sf::Color::Green);
|
||||||
snakeBody.push_front(newBodyPart);
|
snakeBody.push_front(newBodyPart);
|
||||||
if (!SnakeCollision(GetSnakeHead(), snakeFood.snakeFoodObject))
|
if (!GlobalCollision(GetSnakeHead().getPosition(), snakeFood.snakeFoodObject.getPosition()))
|
||||||
snakeBody.pop_back();
|
snakeBody.pop_back();
|
||||||
SnakeFoodCollision(snakeFood, gameVideoMode);
|
SnakeFoodCollision(snakeFood, gameVideoMode);
|
||||||
return;
|
return;
|
||||||
@ -119,7 +88,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;
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,6 @@
|
|||||||
#include <queue>
|
#include <queue>
|
||||||
#include "SnakeFood.h"
|
#include "SnakeFood.h"
|
||||||
|
|
||||||
bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2);
|
|
||||||
int SnakeMovement();
|
|
||||||
sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position);
|
|
||||||
|
|
||||||
class Snake
|
class Snake
|
||||||
{
|
{
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
// SnakeFood.cpp
|
// SnakeFood.cpp
|
||||||
#include <random>
|
#include <iostream>
|
||||||
|
#include <SFML\Graphics.hpp>
|
||||||
|
#include "GeneralFunctions.h"
|
||||||
#include "SnakeFood.h"
|
#include "SnakeFood.h"
|
||||||
|
|
||||||
SnakeFood::SnakeFood()
|
SnakeFood::SnakeFood()
|
||||||
@ -17,15 +19,13 @@ SnakeFood::SnakeFood(sf::Vector2f snakeFoodSize)
|
|||||||
void SnakeFood::GenerateNewLocation(int horizontalLocation, int verticalLocation)
|
void SnakeFood::GenerateNewLocation(int horizontalLocation, int verticalLocation)
|
||||||
{
|
{
|
||||||
sf::Vector2f newPosition;
|
sf::Vector2f newPosition;
|
||||||
std::default_random_engine generator(time(NULL));
|
newPosition.x = GenerateRandomNumber(horizontalLocation);
|
||||||
std::uniform_int_distribution<int> distributionX(0, horizontalLocation);
|
newPosition.y = GenerateRandomNumber(verticalLocation);
|
||||||
std::uniform_int_distribution<int> distributionY(0, verticalLocation);
|
if (GlobalCollision(snakeFoodObject.getPosition(), newPosition))
|
||||||
int newX = distributionX(generator);
|
{
|
||||||
int newY = distributionY(generator);
|
std::cout << "Location error: " << newPosition.x << " " << newPosition.y << '\n';
|
||||||
newX = newX - (newX % 25) - 25;
|
throw std::runtime_error("Error! New generation on same location");
|
||||||
newY = newY - (newY % 25) - 25;
|
}
|
||||||
newPosition.x = newX;
|
|
||||||
newPosition.y = newY;
|
|
||||||
snakeFoodObject.setPosition(newPosition);
|
snakeFoodObject.setPosition(newPosition);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user