【问题标题】:Simple Ticker (Idle Game) Concept简单的股票(空闲游戏)概念
【发布时间】:2020-02-15 14:00:59
【问题描述】:

我正在尝试制作一个简单的自动收报机游戏。一个代码以每秒 1 个 clix 的速率滴答作响,如下面的代码所示。当有足够的 clix 时(在这种情况下,40 用于 Apple)我希望能够购买它,并且 clix 应该重置 -40 的数量并在其上保持每秒计数 1 个。

购买那 1 个 Apple 后,我还希望将 clix 速率从之前的 1/秒增加到 2/秒,依此类推。

当代码遵循自上而下的顺序时,这如何在 Python 中实现?在这种情况下,函数clicker() 永远不会到达它下面的行。

我将使用类来使这一切变得更简单,但在此之前有人可以帮助我解决这个障碍吗?(我可以让点击在后台运行,而代码的其他部分可以运行,或者有其他方法吗?周围?)

import time

def clicker():
    global clix
    clix = 0
    endgame = False
    while not endgame:

        clix += 1
        time.sleep(1)

    return clix

c = clicker()

print('1. Apple : 40 clix | 2. Orange : 75 clix')

ch = int(input('\nPurchase ID: '))
if c == 1 and c >= 40:
    print('Got an Apple!')
    clix += 2                  # Just to get the point across, I know this line will throw an error.

elif ch == 2 and c >= 75:
    print('Got an Orange!')
    clix += 4

else:
    print('Need more Clix')

【问题讨论】:

    标签: python 2d-games ticker idle-timer


    【解决方案1】:

    您的代码中还有其他一些错误。但要回答你的具体问题,是的,你可以在后台运行一些东西,而其他部分运行。

    您正在寻找的是线程。

    查看此答案了解更多详情:Creating Threads in python

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-10
      • 2012-12-24
      • 2010-09-28
      • 2011-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多