Not logged inGosu Forums
Forum back to Help Search Register Login
Up Topic Gosu / Gosu Exchange / Turn Based Game
- - By noahc Date 2016-02-04 01:29
I'm not sure how best to approach this or even how to get started. I have two player objects. And they are both drawn on the board. When I press the arrow keys one player moves.

However, I want to do two things:

1. Limit input to only 'one move' at time. What I mean by that is sometimes I can't get off the key press fast enough the the player jumps more than one space in whatever direction I'm pushing down the arrow key in.
2. This is probably pretty easy depending on what the above looks like, but I'd like to switch players after receiving the first key input to the other player. Maybe creating an 'active_player' to receive the method call on.

Any thoughts on how best to approach this or how to do it?
Parent - - By lol_o2 Date 2016-02-04 23:19
1. Use Window.button_down(id). This method is called before update and draw each frame and registers single presses of keys. If you want to use it anywhere, you can store the pressed key in some variable and make sure it resets after update/draw is done.

2. You can make your game state manage the input. Make a method inside player that handles input and when key is pressed, it sends signal to game state that it was pressed. The game state should then change input to another payer. To make it work, players should be able to check whose turn is it now and ignore input if it's not their turn.
Parent - - By noahc Date 2016-02-05 00:35
I should have been clear. I'm already doing what you suggest. However, when I press the key and let go the first player is moving (left for example) the game is switching state and the next player is moving left. So the two robots are essentially following each other around because it is happening too fast.

How can I make sure that the button is released before a new input button is allowed is what I'm really asking or what I really need, right?
Parent - By nietzschette Date 2016-02-05 06:17
try using Window.button_up(id) instead of Window.button_down(id)?

if this does not work, you probably need to re-think your game logic.  It's hard to help without seeing any code, but i could give some suggestions.

I like to put my game entities in a single array,

@ents = []

and keep track of turns with a variable

@selection = 0

you can increment through your list of ents by incrementing @selection:

#put somewhere in your Gosu::Window subclass:
def button_down(id)
case id
  when this

def button_up(id)
case id
  when this
   @selection += 1
@selection %= @ents.size            #<- to ensure @ents[@selection] never returns a nil value.

there are probably better ways of doing this, but testing different methods is half the fun!
Parent - By lol_o2 Date 2016-02-05 09:42
Oh, yeah. I'd just make a flag that prevents players from moving and resets at the end of frame. When a player moves, it sets the flag to true, so no other player will move this frame. Flag is then set to false when all players are updated. Then you have to release the button before it triggers again.
Parent - By JosepM Date 2016-02-08 19:38
Maybe you can use action points for each player. In function of the action the player consume their action points for that turn. One time is 0 no move or whatever. Then you can pass the turn to the other player. At the begin of each turn the action points are reset and available.
The flag option is nice but with the action points you can offer many actions.
For example you have 3AP each turn.
Move 1 space 1AP
Attack 2AP
Refuel 1AP

Then the player can choice a strategy and combime actions. When AP is 0 turn end.

Josep M
Up Topic Gosu / Gosu Exchange / Turn Based Game

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill