Snake food works with vertical movement
Snake food works with vertical movement, however horizontal movement is unchanging. Will look into getting a fix for it soon.
This commit is contained in:
parent
0d3988a7ff
commit
cc3a61abb7
@ -1,4 +1,4 @@
|
|||||||
// #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"
|
||||||
@ -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(25);
|
sf::Time delay = sf::milliseconds(50);
|
||||||
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));
|
||||||
@ -37,15 +37,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);
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
|
// Snake.cpp
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <SFML\Graphics.hpp>
|
#include <SFML\Graphics.hpp>
|
||||||
#include "Snake.h"
|
#include "Snake.h"
|
||||||
|
#include "SnakeFood.h"
|
||||||
|
|
||||||
// Test for collision between two objects
|
// Test for collision between two objects
|
||||||
bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2)
|
bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2)
|
||||||
@ -64,9 +66,9 @@ sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f 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())
|
||||||
@ -80,15 +82,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 (!SnakeCollision(GetSnakeHead(), snakeFood.snakeFoodObject))
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,6 +127,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()
|
||||||
{
|
{
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#ifndef SNAKE_H
|
#ifndef SNAKE_H
|
||||||
#define SNAKE_H
|
#define SNAKE_H
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
#include "SnakeFood.h"
|
||||||
|
|
||||||
bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2);
|
bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2);
|
||||||
int SnakeMovement();
|
int SnakeMovement();
|
||||||
@ -18,7 +19,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,3 +1,4 @@
|
|||||||
|
// SnakeFood.cpp
|
||||||
#include <random>
|
#include <random>
|
||||||
#include "SnakeFood.h"
|
#include "SnakeFood.h"
|
||||||
|
|
||||||
@ -13,11 +14,18 @@ SnakeFood::SnakeFood(sf::Vector2f snakeFoodSize)
|
|||||||
snakeFoodObject.setFillColor(sf::Color::Red);
|
snakeFoodObject.setFillColor(sf::Color::Red);
|
||||||
}
|
}
|
||||||
|
|
||||||
int SnakeFood::GenerateNewLocation(int maxLocation)
|
void SnakeFood::GenerateNewLocation(int horizontalLocation, int verticalLocation)
|
||||||
{
|
{
|
||||||
int newPosition;
|
sf::Vector2f newPosition;
|
||||||
std::default_random_engine generator;
|
std::default_random_engine generator(time(NULL));
|
||||||
std::uniform_int_distribution<int> distribution(0, maxLocation);
|
std::uniform_int_distribution<int> distributionX(0, horizontalLocation);
|
||||||
newPosition = distribution(generator);
|
std::uniform_int_distribution<int> distributionY(0, verticalLocation);
|
||||||
return newPosition;
|
int newX = distributionX(generator);
|
||||||
|
int newY = distributionY(generator);
|
||||||
|
newX = newX - (newX % 25) - 25;
|
||||||
|
newY = newY - (newY % 25) - 25;
|
||||||
|
newPosition.x = newX;
|
||||||
|
newPosition.y = newY;
|
||||||
|
snakeFoodObject.setPosition(newPosition);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ public:
|
|||||||
sf::RectangleShape snakeFoodObject;
|
sf::RectangleShape snakeFoodObject;
|
||||||
SnakeFood();
|
SnakeFood();
|
||||||
SnakeFood(sf::Vector2f snakeFoodSize);
|
SnakeFood(sf::Vector2f snakeFoodSize);
|
||||||
int GenerateNewLocation(int maxLocation);
|
void GenerateNewLocation(int horizontalLocation, int verticalLocation);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user