Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - C_Worm

Pages: 1 [2]
16
General / turn-based countdown
« on: April 02, 2019, 10:25:21 pm »
#ifndef SFML_STATIC
#define SFML_STATIC

#include <iostream>
#include <string>
#include <SFML/Graphics.hpp>

int main()
{


   sf::RenderWindow window;

   window.create(sf::VideoMode(800, 600), "TITLE", sf::Style::Close | sf::Style::Resize);

   window.setFramerateLimit(60);
   sf::Font font;

   
   if (!font.loadFromFile("Enchanted Land_0.otf"))
   {

   }

   sf::Clock clock;
   sf::Time time;
   int seconds{ 10 };
   std::string counter = std::to_string(seconds);


   sf::Text text;
   text.setFillColor(sf::Color::Red);
   text.setString(counter);
   text.setFont(font);
   sf::Event events;

   clock.restart();
   while (window.isOpen())
   {
      time = clock.getElapsedTime();

      std::cout << (int)time.asSeconds() << std::endl;

      if ((int)time.asSeconds() == 1)
      {
         counter = std::to_string(seconds);
         text.setString(counter);
         --seconds;
         clock.restart();

         if (seconds < 0)
         {
            text.setString("GAME OVER");
            text.setPosition(window.getSize().x / 2, window.getSize().y / 2);
            
         }
      }

         
      

      while (window.pollEvent(events))
      {
         if (events.type == sf::Event::Closed)
         {
            window.close();
         }

         if (events.type == sf::Event::KeyPressed)
         {
            if (events.key.code == sf::Keyboard::A)
            {
               window.close();
            }
         }
         
      }


      window.clear(sf::Color::Blue);

      window.draw(text);

      window.display();
   }


}

#endif





Does anyone know a better/more efficient way to make a countdown implementation?
Or am i on the right track with this?


The thing is that i need a countdown for a turn-based game state.

each player has 10 seconds at her disposal before the the turn changes to the second player.

the countdown should be visible somewhere on the screen like it is here.

17
Window / Re: Menu-Bug invloving sf::Event::keyPressed
« on: March 26, 2019, 07:42:25 am »
Oh thanks, it worked! :D

18
Window / Menu-Bug invloving sf::Event::keyPressed
« on: March 25, 2019, 11:51:33 pm »
if (this->mainMenuActive)
{
   mainMenuEvents();
}

if (this->startMenuActive)
{
   this->startMenuEvents();
}

if (this->resourceMenuActive)
{
   this->resourceMenuEvents();
}

void Game::mainMenuEvents()
{

   switch (this->events.key.code)
   {
      case sf::Keyboard::Down:
      {

         ++this->mainMenuHighlight;
         if (this->mainMenuHighlight > MAIN_COMMAND)
         {
            this->mainMenuHighlight = MAIN_RESOURCE;
         }

      }break;


      case sf::Keyboard::Up:
      {

         --this->mainMenuHighlight;
         if (this->mainMenuHighlight < MAIN_RESOURCE)
         {
            this->mainMenuHighlight = MAIN_COMMAND;
         }

      }break;


      case sf::Keyboard::Enter:
      {
         this->mainMenuActive = false;

         if (this->mainMenuHighlight == MAIN_RESOURCE)
         {            
            this->resourceMenuActive = true;
            std::cout << "Resource: " << resourceMenuActive << std::endl;
         }         

      }break;

      case sf::Keyboard::Escape:
      {

         this->mainMenuActive = false;
         this->startMenuActive = true;

      }break;
   }
}


void Game::startMenuEvents()
{
   switch (this->events.key.code)
   {
   case sf::Keyboard::Down:
   {
      ++this->startMenuHighlight;
      if (this->startMenuHighlight > START_EXIT_GAME)
      {
         this->startMenuHighlight = START_NEW_GAME;
      }

      std::cout << "start: " << this->startMenuHighlight << std::endl;

   } break;

   case sf::Keyboard::Up:
   {
      --this->startMenuHighlight;
      if (this->startMenuHighlight < START_NEW_GAME)
      {
         this->startMenuHighlight = START_EXIT_GAME;
      }

      std::cout << "start: " << this->startMenuHighlight << std::endl;

   } break;

   case sf::Keyboard::Enter:
   {
      if (this->startMenuHighlight == START_NEW_GAME)
      {
         this->startMenuActive = false;
         this->mainMenuActive = true;
      }

      if (this->startMenuHighlight == START_EXIT_GAME)
      {
         this->gameIsRunning = false;
      }
   }

   }
}


void Game::resourceMenuEvents()
{
   
   switch (this->events.key.code)
   {
      case sf::Keyboard::Down:
      {

         ++this->resourceMenuHighlight;
         if (this->resourceMenuHighlight > CHEMIST)
         {
            this->resourceMenuHighlight = BANKMAN;
         }

      }break;


      case sf::Keyboard::Up:
      {

         --this->resourceMenuHighlight;
         if (this->resourceMenuHighlight < BANKMAN)
         {
            this->resourceMenuHighlight = CHEMIST;
         }

      }break;

      case sf::Keyboard::Enter:
      {
         if (resourceMenuHighlight == BANKMAN)
         {
            ++this->overviewInfoValue;
         }

      } break;

      case sf::Keyboard::Escape:
      {
         this->resourceMenuActive = false;
         this->mainMenuActive = true;
      }break;
   }
}


----(THIS IS JUST A LITTLE BIT OF CODE FROM THE PROGRAM)----

My program renders 3 menus  at the moment: startMenu -> mainMenu -> resourceMenu.

all menus handle sf::Event::KeyPressed: (Enter/Up/Down).

in resourceMenu when i press enter a number gets updated on the screen.

THE PROBLEM is that when i press enter from the mainMenu-Screen, the resourceMenu also registers that Enter was pressed and thus the resource number gets updated as soon as the resourceMenu is rendered to the screen.



What i want to achieve is this:

mainMenu (if Enter is pressed) -> resourceMenu displayed (if Enter is pressed) -> update resourceValue.



what i have:

mainMenu (if Enter is pressed) -> resourceMenu displayed (and since you pressed Enter in mainMenu resourceValue will be updated from that keyPress)


i guess i want to separate the sf::keyboard::Enter from each menu and make it so that the menus don't respond to eachothers keypress-Events.



Thanks in advance!



Pages: 1 [2]
anything