简体   繁体   English

如何在每次变量增加时调用方法?

[英]How to have method called everytime variable increases?

I'm trying to add some difficulty in "game".我试图在“游戏”中增加一些难度。 Basically I want to call method to increase speed of a sprite everytime players score is multiply of 100 (it should happen on 100, 200, etc.).基本上,每次玩家得分乘以 100 时,我都想调用方法来提高精灵的速度(它应该发生在 100、200 等上)。

I'm using pygame and livewires packages.我正在使用 pygame 和 livewires 包。

How I handled it was by using range like:我如何处理它是通过使用范围,如:

if self.score.value in range(0, 10000+1, 100):
                pizza.update_speed()

and update method just increases speed:和更新方法只是提高速度:

def update_speed(self):
        Pizza.speed += 0.25

So... it works but I am sure that it is not elegant and there is a better way of doing it.所以......它有效,但我确信它并不优雅,并且有更好的方法来做到这一点。
How should I code it so I can check the score "infinitely" and in a proper way?我应该如何编码,以便我可以“无限地”以适当的方式检查分数?

You can check if the remainder ( % ) of the division of self.score.value and 100 is 0:您可以检查self.score.value与 100 self.score.value的余数 ( % ) 是否为 0:

if self.score.value % 100 == 0:
    pizza.update_speed() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传递一个随调用而增加的变量? - Pass a variable that increases as it is called? 每次在python中调用__init__时更新自变量 - Update self variable within __init__ everytime it is called in python 在python中,是否每次都有一个变量是不同的随机数? - In python, is there anyway to have a variable be a different random number everytime? 如何让变量的值成为方法,并让方法在Python 2.7中使用该变量? - How can I have a variable's value be a method, and have a method use that variable in Python 2.7? 有没有办法在不同的 class 中设置一个等于变量的值,并让它保持第一次运行的值而不必每次都运行? - Is there a way to set a value equal to a variable in a different class and have it keep the value from the first run and not have to run everytime? 不会每次都调用类装饰器 - Class decorator not being called everytime 如何停止PDB每次打印最后一个变量 - How to stop PDB from printing the last variable everytime 这个方法怎么称呼? (Pyglet) - How is this method called? (Pyglet) 如何不使用f5每次都在IDLE中调用输入变量 - How to call input variable in IDLE without using f5 everytime 可能在APIView上有一个从URL调用的方法 - Possible to have a method on an APIView called from a url
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM