Python 3 Game Bug:按住回车键

时间:2015-02-13 16:03:23

标签: python

我还有另一个与此游戏相关的问题,这里是: https://stackoverflow.com/questions/28545444/python-3-game-bug-message-repeating

我制作的游戏是Cookie Clicker(仅用于编码练习!)。我遇到了一个问题,用户可以按住回车键,并快速获得硬币。我想阻止这一点。这是我的代码:

coin = 0
keepgoing = True

while keepgoing == True:
        print('You have ' + str(coin) + ' cmd-coins' )
        response = input('Press enter to get a coin!')
        if response == 'q':
                keepgoing = False
                print('Thanks for playing!')
coin = coin + 1

3 个答案:

答案 0 :(得分:2)

我为你的问题写了一个相当“噱头”的解决方案,它在某种程度上工作但绝对不是完美的。根据您的编译器,您可能需要更改start-end的条件值,使其成为一个值,允许您尽可能快地按静态按下而不会中断但不允许用户按住enter键。对于WingIde中的我来说,0.03是最佳值。

如果您要导入像Pygame这样的模块,您会发现这种游戏更容易制作。

import time

coins = 0
keepgoing = True
end = -1

while keepgoing:
    print('You have ' + str(coins) + ' cmd-coins' )
    response = input('Press enter to get a coin!')
    start = time.time()
    if start-end < 0.03:
        print ("Don't hold down the Enter key you cheater!")
        keepgoing = False
    if response == 'q':
        print('Thanks for Playing!')
        keepgoing = False
    coins = coins + 1
    end = time.time()

注意此解决方案适用于Python 3,适用于&lt;在Python 3中,您必须将raw_input()替换为input()。另外,我不建议尝试在在线编译器上使用它。

答案 1 :(得分:0)

G Force dog, 你可以通过做一些改变来改变你的游戏,直到某人或我找到一个好的解决方案(妈妈下令上床睡觉)。直到我们找不到解决方案,请使用这个不完全是解决方案的代码。

coins = 0
real = 0
while real == 0:
    if True:
        print('You have ' + str(coins) + ' cmd-coins' )
        response = input('Press c to get a coin, then press ENTER!')
        if response == 'c' or response == 'C':
            coins = coins + 1
        if response == 'q':
            print('Thanks for playing!')
            real = 1

还有两件事: -

  1. 很好的回答Yeniaul!好好试试!
  2. Elizion,很好的尝试,但你的方法不起作用。但这是个好主意。 我也用过时间,但似乎当按下ENTER键很长一段时间,它就会一起出现在屏幕上。
  3. 我认为我确实有一个想法形成...如果我们在导入'time'之后再创建一个while循环,其条件通过赋值变量'time.clock()每2秒激活一次循环'当它的值是2的倍数时,它会激活! 得到它了?我知道该怎么做,但似乎无法将其放入代码中。看看你是否可以,读者(特别是Elizion和Yeniaul)

答案 2 :(得分:-1)

尝试使用time.sleep(延迟秒数)来减速 所以你可以做到

coin = 0
keepgoing = True

while keepgoing == True:
        time.sleep(0.25)
        print('You have ' + str(coin) + ' cmd-coins' )
        response = input('Press enter to get a coin!')
        if response == 'q':
                keepgoing = False
                print('Thanks for playing!')
coin = coin + 1

这可能会让他们在获得另一枚硬币之前延迟四分之一秒。

相关问题