我怎样才能在一个循环中等待按钮的按下?[英] How can I wait for a button press in a loop?

本文是小编为大家收集整理的关于我怎样才能在一个循环中等待按钮的按下?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

假设我有一个简单的程序来模拟棋盘游戏,其中有许多玩家轮流掷骰子在棋盘上移动.玩家可以是人类或计算机.
如果这是一个命令行风格的游戏,我可以简单地创建一个循环来迭代那些将为该玩家调用 diceRoll 函数的玩家.

如果玩家是电脑玩家,diceRoll 只是告诉电脑掷骰子.
如果玩家是人类,diceRoll 将等待用户输入掷骰命令然后继续.

我如何将这个想法转化为平面设计?我认为连续检查滚动按钮是否被按下是没有意义的.我正在使用 actionscript 2,但想法可以是您想要的任何语言.我只想就设计这个的最佳方式提出一些意见.我不认为有某种我不知道的"waitForButtonPress"功能?

推荐答案

我想我已经找到了我喜欢的解决方案.主游戏类将有一个 nextTurn 函数,如下所示:

nextTurn() {
   bool guiSet = false
   while (guiSet = false) {
      //Get the next player
      if (next player is human) {
         //enable the gui (ie. the 'Roll Dice' button)
         guiSet = true
      } else {
         //The player is a computer
         //Have the computer roll the dice and make any necessary decisions
      }
   }
}

当人类玩家完成他的回合时,将调用 nextTurn 以继续游戏.当计算机完成一个回合时,流程仍处于while循环中,因此游戏将继续.

其他推荐答案

取决于游戏,如果您有 N 个回合,并且每隔一个回合是人类回合,那么您可以使用 if 从循环内简单地调用 getUserResponse()其他..

for i to N
if(i%2==0)
getUserRoll()
else
getComputerRoll()

如果有任何限制,请在 if 条件中检查它们.

本文地址:https://www.itbaoku.cn/post/627339.html

问题描述

Say I have simple program that emulates a board game with a number of players who take turns rolling dice to move across a board. The players can be human or computer.
If this was a command-line style game, I could simply make a loop to iterate over the players which would call the diceRoll function for that player.

If the player is a computer player, diceRoll simply tells the computer to roll the dice.
If the player is a human, the diceRoll will wait until a user inputs the roll command and then continues.

How can I transfer this idea to a graphic design? I don't think it makes sense to continuously check to see if the roll button has been pressed. I am working with actionscript 2, but ideas can be in whatever language you want. I'd just like some opinions on the best way to design this. I don't suppose there's some sort of 'waitForButtonPress' function that I don't know about?

推荐答案

I think I've found a solution that I like. The main game class will have a nextTurn function like so:

nextTurn() {
   bool guiSet = false
   while (guiSet = false) {
      //Get the next player
      if (next player is human) {
         //enable the gui (ie. the 'Roll Dice' button)
         guiSet = true
      } else {
         //The player is a computer
         //Have the computer roll the dice and make any necessary decisions
      }
   }
}

When a human player is finished his turn, a call will be made to nextTurn to continue the game play. When a computer finishes a turn, the flow is still in the while loop so the game will continue.

其他推荐答案

depending on the game if you have N turns and every other turn is the humans turn then you could simply call a getUserResponse() from within the loop using if elses..

for i to N
if(i%2==0)
getUserRoll()
else
getComputerRoll()

and if there's any contraints check them in the if conditon.