#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Color.hpp>
#include "MainMenu.hpp"
#include "Game.hpp"
#include "Snake.hpp"
#include "Food.hpp"
#include "GameOver.hpp"
#include <ctime>
enum class GameState {
MainMenu,
Game,
GameOver
};
int main() {
srand(time(NULL));
sf::RenderWindow window(sf::VideoMode(800, 600), "Zmija");
sf::Texture texture;
sf::Font font;
sf::Text text;
texture.loadFromFile("assets/grass.png");
font.loadFromFile("assets/Salsa-Regular.ttf");
MainMenu menu(font, "Zmija");
GameOver over(font, "Game Over?");
Game game;
Snake snake;
Food food;
snake.drawSnake();
game.textures();
menu.startSetColor(sf::Color::Yellow);
GameState state = GameState::MainMenu;
texture.setRepeated(true);
game.grass.setTexture(texture);
game.grass.setTextureRect(sf::IntRect(0, 0, 800, 600));
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
switch (state) {
case GameState::MainMenu:
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
menu.startSetColor(sf::Color::Yellow);
menu.exitSetColor(sf::Color::White);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
state = GameState::Game;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
menu.startSetColor(sf::Color::White);
menu.exitSetColor(sf::Color::Yellow);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
window.close();
}
}
case GameState::Game:
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
snake.pos = sf::Vector2f(0, -0.2);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
snake.pos = sf::Vector2f(0, 0.2);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
snake.pos = sf::Vector2f(-0.2, 0);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
snake.pos = sf::Vector2f(0.2, 0);
}
//udaljenost izmedu zmije i hrane
int dist2p = sqrt(pow((snake.head.getPosition().x - food.food.getPosition().x), 2) + pow((snake.head.getPosition().y - food.food.getPosition().y), 2));
if (snake.head.getRadius() + food.food.getRadius() >= dist2p) {
food.food.setPosition(rand() % 770, rand() % 570);
}
//Sudar sa zidom
if (snake.head.getPosition().x < 20.f)
state = GameState::GameOver;
if (snake.head.getPosition().y < 20.f)
state = GameState::GameOver;
if (snake.head.getPosition().x > 755)
state = GameState::GameOver;
if (snake.head.getPosition().y > 555.f)
state = GameState::GameOver;
}
case GameState::GameOver:
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
over.retrySetColor(sf::Color::Yellow);
over.exitSetColor(sf::Color::White);
snake.head.setPosition(400, 300);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
state = GameState::Game;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
over.retrySetColor(sf::Color::White);
over.exitSetColor(sf::Color::Yellow);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
window.close();
}
snake.move();
window.clear();
}
}
switch (state) {
case GameState::MainMenu:
window.draw(menu);
break;
case GameState::Game:
window.draw(game);
window.draw(food);
window.draw(snake);
break;
case GameState::GameOver:
window.draw(over);
break;
}
window.display();
}
return 0;
}
So my problem is, every time I press Up it returns my snake to the original position, but I don't understand why because I am in a state of Game, and not in a state of GameOver. I am new to SFML.