root.after导致Tkinter应用程序停止工作

时间:2011-06-12 18:37:27

标签: python canvas tkinter

    def colision(self):
        if self.coords(self.bola)[1]<50:
            self.boladir=1
        if self.coords(self.bola)[1]>870:
            self.jugando=0
            self.pierde()
#        ladrillos=self.find_withtag("brick")

    def mueve_bola(self):
        if self.jugando:
            if self.boladir==0:
                    self.move(self.bola,0,-10)
            elif self.boladir==1:
                    self.move(self.bola,0,10)
        self.colision()
        root.after(velocidad_bola,self.mueve_bola)

1 个答案:

答案 0 :(得分:1)

colision通过after调用自己,因此当游戏开始时,它将每20毫秒调用一次。 mueve_bola also calls itself every 20ms. However, mueve_bola _also_ calls colision . So, every 20ms, colision creates another unending stream of calls to itself every 20ms. 20 ms later mueve_bola calls colision again, which again starts another stream of calls every 20ms. After just one second colision is being called 50 times every 20ms. After two seconds it will be 100 calls to colision every 20 ms. Do you see the problem? In very little time you will have millions of calls to colision`第二。

你只需要在移动东西时计算碰撞,所以不需要每20ms调用一次colision。每次更新显示时,只需要调用一次。

我建议您创建一个每40ms左右调用一次的方法。在其中,您可以一次调整所有内容的坐标。更新行的坐标,然后是球员的球拍,然后是球,然后检查是否有碰撞。