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

Author Topic: Turn basesd games and sf::Keyboard  (Read 5068 times)

0 Members and 2 Guests are viewing this topic.

svladd Cjelik

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Turn basesd games and sf::Keyboard
« on: November 03, 2012, 05:00:57 am »
Hey everyone, and thanks in advance.
 
First off, I searched for turn based and no results came up.
 
I know this is a very elementary question, but I need a little help.  I am currently using  (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) in my gameloop for movement ...but I am making a roguelike, and this function is not working for me.
 
What I need is perhaps a boolean function or something for each keypress -I am not sure.
 
In case I have not been clear -I need one "Left" keypress, to move one tile to the left only.  As it stands, pressing the key even for an instant uses up far too many "turns".
 
I hope that makes sense to you,

Kind regards,

Svladd

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Turn basesd games and sf::Keyboard
« Reply #1 on: November 03, 2012, 07:26:17 am »
Just use your own boolean, set it to false, check with sf::Keyboard if it's pressed and your boolean is false, if so set the boolean to true and move the object.
Btw the term turn-based usually does have a different meaning.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

svladd Cjelik

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Turn basesd games and sf::Keyboard
« Reply #2 on: November 03, 2012, 10:10:12 am »
I'm sorry, but for clarification -how is a roguelike game not turn  based?  What is the common usage of the term?  I want to press the left key, and have my sprite move one tile to the left and stay there.  At the moment I press left and the game cycles through 12 or so turns.
 
Thanks :3

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Turn basesd games and sf::Keyboard
« Reply #3 on: November 03, 2012, 10:36:22 am »
Quote
In turn-based games, game flow is partitioned into well-defined and visible parts, called turns. A player of a turn-based game is allowed a period of analysis (sometimes bounded, sometimes unbounded) before committing to a game action, ensuring a separation between the game flow and the thinking process, which in turn presumably leads to better choices. Once every player has taken his or her turn, that round of play is over, and any special shared processing is done. This is followed by the next round of play. In games where the game flow unit is time, turns may represent such things as years, months, weeks or days.

From:
http://en.wikipedia.org/wiki/Turns,_rounds_and_time-keeping_systems_in_games

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Re: Turn basesd games and sf::Keyboard
« Reply #4 on: November 03, 2012, 12:34:28 pm »
in that case just use sf::keyboard::iskeyReleased
thats a basic thing to know
you always take the input from keyboard after releasing the key
this should solve your problem
Ehrm have you ever worked wirh SFML? ;)
Such a function does not exist in sf::Keyboard. But one can use events with relased.

About turn based, I wasn't referring to the game itself, bit your statement made it seem like the mechnism should be called turn based. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

svladd Cjelik

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Turn basesd games and sf::Keyboard
« Reply #5 on: November 03, 2012, 12:59:33 pm »
Quote
In turn-based games, game flow is partitioned into well-defined and visible parts, called turns. A player of a turn-based game is allowed a period of analysis (sometimes bounded, sometimes unbounded) before committing to a game action, ensuring a separation between the game flow and the thinking process, which in turn presumably leads to better choices. Once every player has taken his or her turn, that round of play is over, and any special shared processing is done. This is followed by the next round of play. In games where the game flow unit is time, turns may represent such things as years, months, weeks or days.

From:
http://en.wikipedia.org/wiki/Turns,_rounds_and_time-keeping_systems_in_games

This is exactly what I want.  A roguelike.  Turn based.  The player moves one tile, up down left or right ...with as much time to wait between turns as he or she likes.  I will look into isKeyReleased :3
« Last Edit: November 03, 2012, 01:03:59 pm by svladd Cjelik »

svladd Cjelik

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: AW: Re: Turn basesd games and sf::Keyboard
« Reply #6 on: November 03, 2012, 01:02:13 pm »
Ehrm have you ever worked wirh SFML? ;)
Such a function does not exist in sf::Keyboard. But one can use events with relased.

About turn based, I wasn't referring to the game itself, bit your statement made it seem like the mechnism should be called turn based. ;)

Sorry friend, no.  I only started with SMFL this morning.  Up until now I have been using nCurses.  It was fantastic for ironing out my game code, but I am looking now to migrate away from the console and into the world of graphics.  It is just a matter of learing the librarys and methods available really.
 
Thank you for your help so far though!

upseen

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Turn basesd games and sf::Keyboard
« Reply #7 on: November 04, 2012, 04:56:56 pm »

sf::Keyboard gives the current state of key, regardless of when it was pressed or since how much time.

Assuming your game runs at 60FPS, then your game loop runs at about 60 times per second, so when you press a key for exactly 1 second, this code
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
    moveLeft();
}
is called 60 times and so your character moves 60 steps.

What you actually need is to use sf::Events, a sample code would be :
sf::Event event;
while (mWindow.pollEvent(event)) {
    if (event.type == sf::Event::KeyPressed) {
        sf::Uint32 keyCode = event.key.code;
        if (keyCode == sf::Keyboard::Left) {
            moveLeft();
        }
        else if (keyCode == sf::Keyboard::Right) {
            moveRight();
        }
        else if (keyCode == sf::Keyboard::Up) {
            enterRoom();
        }
    }
    else if (event.type == sf::Event::Closed) {
        mWindow.close();
    }
}

In this code, our functions are only called the moment when the keys are just pressed, so in order to activate them again, one must release the key then press it again. So instead of asking the keyboard : "Hey, is the left key currently pressed?" we ask him "Hey, did the player just press the left key this frame?" ;)

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Turn basesd games and sf::Keyboard
« Reply #8 on: November 04, 2012, 06:27:50 pm »
Quote
In this code, our functions are only called the moment when the keys are just pressed, so in order to activate them again, one must release the key then press it again.

That's not quite true.  By default KeyPressed events are continuously generated for keys which are held down, so processing when (event.type == sf::Event::KeyReleased) is probably closer to what the OP wants.

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Turn basesd games and sf::Keyboard
« Reply #9 on: November 07, 2012, 03:44:56 pm »
What you actually need is to use sf::Events
Indeed, and don't forget:

Quote from: tutorial
If a key is held, multiple KeyPressed events will be generated, at the default OS delay (ie. the same delay that applies when you hold a letter in a text editor). To disable repeated KeyPressed events, you can call window.setKeyRepeatEnabled(false). On the contrary, obviously, KeyReleased events can never be repeated.

SJFrK

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Turn basesd games and sf::Keyboard
« Reply #10 on: November 13, 2012, 03:25:45 pm »
There's also the option of using void sf::Window::setKeyRepeatEnabled(bool enabled) with sf::Event::KeyPressed which would work as wanted.

But generally, game-wise, it's really better to use isKeyPressed and a boolean as eXpl0it3r suggested. It's faster and less laggy in most cases (at least that's my experience with events, I only use them for TextEntered really).

 

anything