Ok.Trying to make a snake game in SFML. Made most of the other stuff, now trying to make sure that after the snake eat the food ,the food will only be generated in coordinates not occupied by the snake body and not having any success.
Some helpful snake class attributes
size_t snakeLength = 3;
const size_t snakeRadius = 12;
const float snakeSpeed = 2 * snakeRadius;
sf::CircleShape snakeBody;
std::deque<sf::Vector2f> snakePosition;
std::pair<sf::Vector2f, sf::Vector2f> snakeVelocity = { {0,0},{0,0} };
const bool snakeFoodFeedback = false;
bool snakeFoodStatus = false;
bool selfCollision = false;
Snake update position function [SNAKE CLASS]
void updatePosition() {
if (snakeVelocity.second != sf::Vector2f(0,0)) {
if (!(snakeVelocity.first.x + snakeVelocity.second.x == 0 && snakeVelocity.first.y + snakeVelocity.second.y == 0)) {
snakeVelocity.first = snakeVelocity.second;
snakeVelocity.second = { 0,0 };
}
}
for (sf::Vector2f& globalBound : snakePosition) {
if (globalBound.x > winWidth) {
globalBound.x = 0;
}
else if (globalBound.x < 0) {
globalBound.x = winWidth;
}
if (globalBound.y > winHeight) {
globalBound.y = 0;
}
else if (globalBound.y < 0) {
globalBound.y = winHeight;
}
}
if (std::find(snakePosition.begin() + 3, snakePosition.end(), snakePosition.front()) != snakePosition.end()) {
selfCollision = true;
}
else {
snakePosition.emplace_front(sf::Vector2f(snakePosition.front() + snakeVelocity.first));
if (!snakeFoodStatus) {
snakePosition.pop_back();
}
else {
snakeLength++;
}
}
}
The food class attributes
const sf::Vector2f foodSize = { 12.5,12.5 };
const float foodRoation = 45.0f;
sf::Vector2f foodPosition;
std::unique_ptr<Snake> snakeHWND = std::make_unique<Snake>();
std::vector<sf::FloatRect> snakeBound;
sf::CircleShape dummyBody;
The function that check if snake ate food [FOOD CLASS]
void ateBySnake() {
if(food.getGlobalBounds().intersects(snakeHWND->snakeBody.getGlobalBounds())){
do {
auto [foodXaxis, foodYaxis] = genetrateRandomCoordinates((foodSize.x + foodSize.y)/2*sqrtf(2));
foodPosition = sf::Vector2f(foodXaxis, foodYaxis);
food.setPosition(foodPosition);
} while (snakeBodyBound(food.getGlobalBounds()));
snakeAteFood = true;
}
}
The function RESPONSIBLE to check if food and snakebody intersect[I think i messed something in this function] [FOOD CLASS]
bool snakeBodyBound(const sf::FloatRect foodBound) {
for (sf::Vector2f bodyPosition : snakeHWND->snakePosition) {
dummyBody.setPosition(bodyPosition);
snakeBound.emplace_back(dummyBody.getGlobalBounds());
}
for (sf::FloatRect bodyBound : snakeBound) {
if (bodyBound.intersects(foodBound)) {
snakeBound.clear();
return true;
}
}
snakeBound.clear();
return false;
}
The main() snippet
internalGameFPSlimiter();
food.snakeAteFood = snake.snakeFoodFeedback;
snake.updatePosition();
food.ateBySnake();
scorecard.update();
snake.snakeFoodStatus = food.snakeAteFood;
window.draw(food);
for (auto reversePosition_it = snake.snakePosition.rbegin(); reversePosition_it != snake.snakePosition.rend(); ++reversePosition_it) {
sf::Vector2f snakeCoordinate = *reversePosition_it;
snake.snakeBody.setPosition(snakeCoordinate);
window.draw(snake);
}
window.draw(scorecard);
window.display();
Aucun commentaire:
Enregistrer un commentaire