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:
TriantaTV 2022-08-15 23:51:18 -05:00
parent 0d3988a7ff
commit cc3a61abb7
5 changed files with 38 additions and 29 deletions

View File

@ -1,4 +1,4 @@
// #include <iostream>
// GameState.cpp
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include "Snake.h"
@ -24,7 +24,7 @@ GameState::GameState(int newHorizontal, int newVertical)
void GameState::startNewGame()
{
gameWindow.create(gameVideoMode, "SnakePlusPlus");
sf::Time delay = sf::milliseconds(25);
sf::Time delay = sf::milliseconds(50);
int snakeDirection = 0;
Snake player(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)))
gameWindow.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
snakeDirection = 1;
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);
player.CheckDirection();
player.MoveSnake(playerFood, gameVideoMode);
gameWindow.clear();
player.DisplaySnake(gameWindow);
gameWindow.draw(playerFood.snakeFoodObject);

View File

@ -1,6 +1,8 @@
// Snake.cpp
#include <iostream>
#include <SFML\Graphics.hpp>
#include "Snake.h"
#include "SnakeFood.h"
// Test for collision between two objects
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
void Snake::MoveSnake(sf::RectangleShape& snakeFood)
void Snake::MoveSnake(SnakeFood& snakeFood, sf::VideoMode gameVideoMode)
{
CheckDirection();
// CheckDirection();
sf::Vector2f newHeadPosition;
newHeadPosition = GetSnakeHeadPosition();
if (CheckBoundaries())
@ -80,15 +82,9 @@ void Snake::MoveSnake(sf::RectangleShape& snakeFood)
}
newBodyPart.setFillColor(sf::Color::Green);
snakeBody.push_front(newBodyPart);
if (!SnakeCollision(GetSnakeHead(), snakeFood))
if (!SnakeCollision(GetSnakeHead(), snakeFood.snakeFoodObject))
snakeBody.pop_back();
else if (SnakeCollision(GetSnakeHead(), snakeFood))
{
sf::Vector2f snakeFoodPosition = snakeFood.getPosition();
snakeFoodPosition.x += 25;
snakeFoodPosition.y += 25;
snakeFood.setPosition(snakeFoodPosition.x, snakeFoodPosition.y);
}
SnakeFoodCollision(snakeFood, gameVideoMode);
return;
}
@ -131,6 +127,16 @@ bool Snake::IsSelfCollision(sf::RectangleShape testRectangle)
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
Snake::Snake()
{

View File

@ -2,6 +2,7 @@
#ifndef SNAKE_H
#define SNAKE_H
#include <queue>
#include "SnakeFood.h"
bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2);
int SnakeMovement();
@ -18,7 +19,8 @@ public:
sf::Vector2f GetSnakeHeadPosition();
sf::RectangleShape GetSnakeHead();
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();
bool CheckBoundaries();
bool IsSelfCollision(sf::RectangleShape testRectangle);

View File

@ -1,3 +1,4 @@
// SnakeFood.cpp
#include <random>
#include "SnakeFood.h"
@ -13,11 +14,18 @@ SnakeFood::SnakeFood(sf::Vector2f snakeFoodSize)
snakeFoodObject.setFillColor(sf::Color::Red);
}
int SnakeFood::GenerateNewLocation(int maxLocation)
void SnakeFood::GenerateNewLocation(int horizontalLocation, int verticalLocation)
{
int newPosition;
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0, maxLocation);
newPosition = distribution(generator);
return newPosition;
sf::Vector2f newPosition;
std::default_random_engine generator(time(NULL));
std::uniform_int_distribution<int> distributionX(0, horizontalLocation);
std::uniform_int_distribution<int> distributionY(0, verticalLocation);
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;
}

View File

@ -10,7 +10,7 @@ public:
sf::RectangleShape snakeFoodObject;
SnakeFood();
SnakeFood(sf::Vector2f snakeFoodSize);
int GenerateNewLocation(int maxLocation);
void GenerateNewLocation(int horizontalLocation, int verticalLocation);
};
#endif