[英]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.