我最近下载了 Kivy,因为它为我提供了最易理解的教程和文档等。我尝试过 pygame 和 cocos,但从来没有打过基础,而使用 Kivy 很容易。
所以这是我的问题,我做了一个乒乓球游戏,我试图通过停止乒乓球来暂停游戏,然后在它未暂停时重新开始(通过改变它的速度)。
这是我的代码:
class PongGame(Widget):
...
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'escape':
#Why doesnt it work without global?
#If I don't use a global i get "tempBallVelocity referenced before assignment
global tempBallVelocity
tempBallVelocity = self.ball.velocity
self.ball.velocity = 0,0
if keycode[1] == '`':
#Make the ball go again, thus exiting pause
#This is where the error occurs if I don't use global
self.ball.velocity = tempBallVelocity
正如您在评论中看到的,如果我不使用全局,我会在分配错误之前被引用。但这是一个局部变量,我不明白为什么会这样。
有人有想法么?谢谢?
编辑:为了确保每个人都清楚我的意图,我不想使用全局变量,但这是唯一可行的方法。我宁愿不使用全局变量。