简体   繁体   English

如何在 pygame 中更新接收函数即 lambda 的 Sprite?

[英]How to update a Sprite in pygame which takes in a function i.e. lambda?

Say I have a Button sprite class like假设我有一个 Button 精灵类

class Button(pygame.sprite.Sprite): 
     def __init__(self, callback, ...):
         self.callback = callback

Now, callback is a function, but takes say an array and does something with it.现在,回调是一个函数,但需要说一个数组并用它做一些事情。 Once I click the button, the array is changed, but now I want to apply the same button but on the new array?单击按钮后,数组会更改,但现在我想应用相同的按钮但在新数组上?

Note that the button works the first iteration but now I want the function to be updated with the new array.请注意,该按钮在第一次迭代时起作用,但现在我希望使用新数组更新该函数。

If you are passing a lambda (or function containing the array as a closure) to callback , then all you need to do to change that is reassign self.callback with a new one.如果您将一个 lambda(或包含数组作为闭包的函数)传递给callback ,那么您需要做的就是用一个新的重新分配self.callback来改变它。 I would suggest doing it with a method.我建议用一种方法来做。

    def set_callback(self, callback):
        self.callback = callback

You can just set the callback wherever it is that you want it changed, including inside the previous callback function.你可以在任何你想要改变的地方设置回调,包括在之前的回调函数中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM