Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Game.h"
- void Game::setGameDiff(const difficulty newDiff)
- {
- if ( gameState != INGAME ) // to avoid changing game difficulty in-game
- gameDiff = newDiff;
- }
- void Game::setGameState(const state newState)
- {
- gameState = newState;
- }
- Game::difficulty Game::getGameDiff()
- {
- return gameDiff;
- }
- Game::state Game::getGameState()
- {
- return gameState;
- }
- Game::state Game::gameState = INMENU;
- Game::difficulty Game::gameDiff = NONEDIFF;
- Game::Game()
- : gameWindow(sf::VideoMode(WINDOW_WIDTH,WINDOW_HEIGHT),WINDOW_TITLE), gameMenu(),
- gameWorm(sf::Vector2f(WINDOW_HEIGHT/GAMEOBJECT_HEIGHT,WINDOW_WIDTH/GAMEOBJECT_WIDTH)),
- gameFood(gameWorm.getSnakePosition()), resume("resume",GAME_FONT(),DEFAULT_FONT_SIZE),// this is where the problems comes in
- pause("pause",GAME_FONT(),DEFAULT_FONT_SIZE) // this is where the problems comes in
- {
- gameWindow.setFramerateLimit(FPS);
- }
- Game::~Game()
- {
- }
- void Game::run()
- {
- while(gameWindow.isOpen())
- {
- sf::Event e;
- while(gameWindow.pollEvent(e))
- {
- eventHandler(e);
- }
- update();
- render();
- }
- }
- void Game::init()
- {
- }
- void Game::eventHandler(sf::Event e)
- {
- if ( sf::Event::Closed == e.type)
- {
- gameWindow.close();
- }
- if ( gameState == INMENU )
- {
- if ( sf::Event::MouseMoved == e.type )
- {
- gameMenu.changeColorWhenHovered(sf::Mouse::getPosition(gameWindow));
- }
- if ( sf::Event::MouseButtonPressed == e.type )
- {
- if ( e.mouseButton.button == sf::Mouse::Left )
- {
- gameMenu.changeStateWhenClicked(gameWindow);
- }
- }
- if ( sf::Event::KeyPressed == e.type && Menu::getMenuState() == Menu::START )
- {
- switch(e.key.code)
- {
- case sf::Keyboard::Escape:
- gameState = INMENU;
- Menu::setMenuState(Menu::DEFAULT);
- break;
- default:
- gameState = INGAME;
- break;
- }
- }
- }
- else if ( gameState == INGAME )
- {
- if ( sf::Event::KeyPressed == e.type )
- {
- switch(e.key.code)
- {
- case sf::Keyboard::Left:
- gameWorm.setNewDir(Worm::LEFT);
- break;
- case sf::Keyboard::Right:
- gameWorm.setNewDir(Worm::RIGHT);
- break;
- case sf::Keyboard::Up:
- gameWorm.setNewDir(Worm::UP);
- break;
- case sf::Keyboard::Down:
- gameWorm.setNewDir(Worm::DOWN);
- break;
- case sf::Keyboard::P:
- gameState = PAUSEDINGAME;
- break;
- default:
- break;
- }
- }
- }
- else if ( gameState == PAUSEDINGAME )
- {
- }
- }
- void Game::update()
- {
- if ( gameState == INGAME )
- {
- gameWorm.update();
- }
- else
- {
- if ( Menu::getMenuState() == Menu::NONESTATE )
- {
- gameState = INGAME;
- }
- }
- }
- void Game::render()
- {
- gameWindow.clear();
- if ( gameState == INGAME )
- {
- gameWorm.draw(gameWindow); // when i changed the gameState as INGAME the gameworm and gamefood renders fine.
- gameFood.draw(gameWindow);
- }
- else if ( gameState == INMENU )
- {
- gameMenu.draw(gameWindow); // when i changed the gameState as INMENU this one doesnt work, i think it has to do with the font.
- }
- gameWindow.display();
- }
- //////////////MENU.CPP
- #include "Menu.h"
- #include <string>
- #include <iostream>
- #include "Game.h"
- void Menu::setMenuState(const state newState)
- {
- if ( Game::getGameState() == Game::INMENU )
- {
- menuState = newState;
- }
- }
- Menu::state Menu::getMenuState()
- {
- return menuState;
- }
- Menu::state Menu::menuState = DEFAULT;
- bool Menu::isHovered(sf::Text& txt)
- {
- sf::Vector2f txtPos = txt.getPosition();
- return ( mousePosition.x > txtPos.x && mousePosition.y > txtPos.y && mousePosition.x < txtPos.x + txt.getGlobalBounds().width && mousePosition.y < txtPos.y + txt.getGlobalBounds().height );
- }
- bool Menu::processColorOfTextHovered(sf::Text& txt)
- {
- if ( isHovered(txt) )
- {
- txt.setColor(sf::Color::Red);
- return true;
- }
- else
- {
- txt.setColor(sf::Color::White);
- return false;
- }
- }
- sf::Vector2i Menu::mousePosition = sf::Vector2i(0,0);
- Menu::Menu()
- : start("Start",GAME_FONT(),DEFAULT_FONT_SIZE),
- pressanykeytostart("Press any key to start or ESC to back",GAME_FONT(),DEFAULT_FONT_SIZE),
- difficulty("Difficulty",GAME_FONT(),DEFAULT_FONT_SIZE),
- easy("Easy",GAME_FONT(),DEFAULT_FONT_SIZE),
- hard("Hard",GAME_FONT(),DEFAULT_FONT_SIZE),
- exit("Exit",GAME_FONT(),DEFAULT_FONT_SIZE),
- areyousureyouwanttotexit("Are you sure you want to exit?",GAME_FONT(),DEFAULT_FONT_SIZE),
- yes("Yes",GAME_FONT(),DEFAULT_FONT_SIZE),
- no("No",GAME_FONT(),DEFAULT_FONT_SIZE)
- {
- defaultFont.loadFromFile("arial.ttf");
- const float verticalOffset = 32;
- //default
- start.setPosition((WINDOW_WIDTH/2)-(start.getGlobalBounds().width/2),((WINDOW_HEIGHT/2)-((start.getGlobalBounds().height*3)/2)));
- difficulty.setPosition((WINDOW_WIDTH/2)-(difficulty.getGlobalBounds().width/2),start.getPosition().y+verticalOffset);
- exit.setPosition((WINDOW_WIDTH/2)-(exit.getGlobalBounds().width/2),difficulty.getPosition().y+verticalOffset);
- //start
- pressanykeytostart.setPosition((WINDOW_WIDTH/2)-(pressanykeytostart.getGlobalBounds().width/2),((WINDOW_HEIGHT/2)/2)-(pressanykeytostart.getGlobalBounds().height/2));
- //difficulty
- easy.setPosition((WINDOW_WIDTH/2)-(easy.getGlobalBounds().width/2),(WINDOW_HEIGHT/2)-(easy.getGlobalBounds().height/2));
- hard.setPosition((WINDOW_WIDTH/2)-(hard.getGlobalBounds().width/2),easy.getPosition().y+verticalOffset);
- std::cout << easy.getPosition().y;
- //exit
- areyousureyouwanttotexit.setPosition((WINDOW_WIDTH/2)-(areyousureyouwanttotexit.getGlobalBounds().width/2),(WINDOW_HEIGHT/2)-(areyousureyouwanttotexit.getGlobalBounds().height/2));
- yes.setPosition((WINDOW_WIDTH/2)-(yes.getGlobalBounds().width/2),areyousureyouwanttotexit.getPosition().y+verticalOffset);
- no.setPosition((WINDOW_WIDTH/2)-(no.getGlobalBounds().width/2),yes.getPosition().y+verticalOffset);
- }
- Menu::~Menu()
- {
- }
- void Menu::draw(sf::RenderWindow& window)
- {
- if ( menuState == DEFAULT )
- {
- window.draw(start);
- window.draw(difficulty);
- window.draw(exit);
- }
- else if ( menuState == START )
- {
- window.draw(pressanykeytostart);
- }
- else if ( menuState == DIFFICULTY )
- {
- window.draw(easy);
- window.draw(hard);
- }
- else if ( menuState == EXIT )
- {
- window.draw(areyousureyouwanttotexit);
- window.draw(yes);
- window.draw(no);
- }
- }
- void Menu::changeColorWhenHovered(const sf::Vector2i& mousePos)
- {
- mousePosition = mousePos;
- sf::Color red = sf::Color::Red;
- sf::Color white = sf::Color::White;
- if ( menuState == DEFAULT )
- {
- if ( processColorOfTextHovered(start) ) {}
- else if ( processColorOfTextHovered(difficulty) ) {}
- else if ( processColorOfTextHovered(exit) ) {}
- }
- else if ( menuState == START )
- {
- // nothing to put here just press any key to continue
- // just want to include this else if :D
- }
- else if ( menuState == DIFFICULTY )
- {
- if ( processColorOfTextHovered(easy) ) {}
- else if ( processColorOfTextHovered(hard) ) {}
- }
- else if ( menuState == EXIT )
- {
- if ( processColorOfTextHovered(yes) ) {}
- else if ( processColorOfTextHovered(no) ) {}
- }
- }
- void Menu::changeStateWhenClicked(sf::RenderWindow& window)
- {
- mousePosition = sf::Mouse::getPosition(window);
- if ( menuState == DEFAULT )
- {
- if ( isHovered(start) )
- {
- menuState = START;
- }
- else if ( isHovered(difficulty) )
- {
- menuState = DIFFICULTY;
- }
- else if ( isHovered(exit) )
- {
- menuState = EXIT;
- }
- }
- else if ( menuState == START )
- {
- menuState = NONESTATE;
- }
- else if ( menuState == DIFFICULTY )
- {
- if ( isHovered(easy) )
- {
- Game::setGameDiff(Game::EASY);
- menuState = DEFAULT;
- }
- else if ( isHovered(hard) )
- {
- Game::setGameDiff(Game::HARD);
- menuState = DEFAULT;
- }
- }
- else if ( menuState == EXIT )
- {
- if ( isHovered(yes) )
- {
- window.close();
- }
- else if ( isHovered(no) )
- {
- menuState = DEFAULT;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment