Tkinter:每1毫秒更新一次,但仅在某个数字的倍数时调用函数。

huangapple go评论64阅读模式
英文:

Tkinter: calling a function only once while updating every 1 millisecond on a multiple of a certain number

问题

我正在制作一个点击游戏,其中有一项升级,如果发电机 1(最差的发电机)的数量是 5 的倍数,它会每秒增加玩家获得的总数 0.5。例如,如果我有 5、10、15... 台发电机 1 并购买了升级,我将每秒额外获得 0.5/1.0/1.5...。

假设有 5 台发电机 1(每台发电机每秒增加 0.1),购买升级后,它将增加到每秒 2.0。再购买 5 台发电机 1(总共 10 台发电机 1),它应该是每秒 3.0 而不是 3.5。

这是升级的当前代码,有一个注释指出了问题,但我也会在这里提出。现在,如果 generator_1_amount 是 5 的倍数,它将每 1 毫秒继续调用 upgrade_2_adder 函数。我知道有些东西丢失了,以使它在 generator_1_amount 是 5 的倍数时只调用一次,我想不出任何方法来确保在倍数为 5 时不会多次调用该函数,比如停留在 5 上。

英文:

I am making a clicker game and there is this upgrade where if generator 1 (worst generator) is on a multiple of 5, it increases the total amount the player gets per second by 0.5. For example, if I have 5,10,15... of generator 1 and I bought the upgrade, I will get an extra 0.5/1.0/1.5... per second.

Like assume it is 1.5/sec with 5 generator 1 (0.1/sec per generator), after buying the upgrade, it turns to 2.0/sec. After buying 5 more generator 1 (10 generator 1), it should be 3.0/sec instead of 3.5/sec.

def updater2():
    upgrade_2_checker()
    root.after(1, updater2) # updates/checks every 1 millisecond

def upgrade_2_checker(): # for every multiple of 5, call the upgrade_2_adder function ONCE even if it is stuck on the multiple of 5
    global generator_1_amount
    while generator_1_amount % 5 == 0: # right now it will keep calling the function every millisecond if generator_1_amount is on a multiple of 5
        upgrade_2_adder()
    else:
        pass

def upgrade_2_adder(): # for every multiple of 5, this function gets called ONCE even if it is stuck on the multiple of 5
    global total_auto
    global generator_1_amount
    total_auto = total_auto + (int(generator_1_amount / 5) * 0.5)

This is the code right now for the upgrade, there is a comment which indicates the problem but I'm gonna say it here as well. Right now it will keep on calling the upgrade_2_adder function every 1 millisecond if the generator_1_amount is on a multiple of 5. I know something is missing to make it only call once if generator_1_amount is on a multiple of 5 and I couldn't think of anyway to not call the function more than 1 time if the amount is stuck on a multiple of 5, like stuck on 5.

答案1

得分: 2

为了确保在generator_1_amount是5的倍数时upgrade_2_adder函数仅被调用一次,您可以引入一个变量来跟踪升级是否已经应用。

def updater2():
    upgrade_2_checker()
    root.after(1, updater2)  # 每1毫秒更新/检查一次

def upgrade_2_checker():
    global generator_1_amount
    if generator_1_amount % 5 == 0:  # 检查generator_1_amount是否是5的倍数
        upgrade_2_adder()

def upgrade_2_adder():
    global total_auto
    global generator_1_amount
    global upgrade_2_applied

    if not upgrade_2_applied:  # 检查升级是否已经应用
        total_auto += (generator_1_amount // 5) * 0.5
        upgrade_2_applied = True  # 设置升级标志以指示已应用
英文:

To ensure that the upgrade_2_adder function is called only once when generator_1_amount is on a multiple of 5, you can introduce a variable to keep track of whether the upgrade has already been applied.

def updater2():
    upgrade_2_checker()
    root.after(1, updater2)  # updates/checks every 1 millisecond

def upgrade_2_checker():
    global generator_1_amount
    if generator_1_amount % 5 == 0:  # Check if generator_1_amount is a multiple of 5
        upgrade_2_adder()

def upgrade_2_adder():
    global total_auto
    global generator_1_amount
    global upgrade_2_applied

    if not upgrade_2_applied:  # Check if the upgrade has already been applied
        total_auto += (generator_1_amount // 5) * 0.5
        upgrade_2_applied = True  # Set the upgrade flag to indicate it has been applied

huangapple
  • 本文由 发表于 2023年5月28日 17:23:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350791.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定