Python Tkinter-如何使某些代码使用更少的内存

Python Tkinter-如何使某些代码使用更少的内存,python,memory,tkinter,processing,Python,Memory,Tkinter,Processing,我正在用GUI编程一个奥赛罗游戏。我已经开始制作翻转某些瓷砖的动画,这里就是。当板上的瓷砖数量变大时,我的计算机将开始阻塞。我不知道如何简化此代码或使用更少的内存。(我可以使用我启用的自动移动键以光速完成游戏,而不需要动画。这很好。)下面是我用来翻转瓷砖的定义 def animateFlips(self, i): self._canvas.delete(tkinter.ALL) column_width = self._canvas.winfo_width()/Othel

我正在用GUI编程一个奥赛罗游戏。我已经开始制作翻转某些瓷砖的动画,这里就是。当板上的瓷砖数量变大时,我的计算机将开始阻塞。我不知道如何简化此代码或使用更少的内存。(我可以使用我启用的自动移动键以光速完成游戏,而不需要动画。这很好。)下面是我用来翻转瓷砖的定义

    def animateFlips(self, i):
    self._canvas.delete(tkinter.ALL)
    column_width = self._canvas.winfo_width()/Othello.COLUMNS
    row_height = (self._canvas.winfo_height()-self._scoreBoard)/Othello.ROWS
    newBoard = self._game.board
    animatedTileCoords = []
    color = ''
    oppcolor = ''
    if self._game.turn == 2:
        color = 'black'
        oppcolor = 'white'
    elif self._game.turn == 1:
        color = 'white'
        oppcolor = 'black'
    for x in range(Othello.COLUMNS):
        for y in range(Othello.ROWS):
            x1 = x * column_width
            y1 = y * row_height
            x2 = x1 + column_width
            y2 = y1 + row_height
            self._canvas.create_rectangle(x1,y1,x2,y2)
            if self._game.board[x][y] == 1:
                self._canvas.create_oval(x1,y1,x2,y2,fill='black')
            elif self._game.board[x][y] == 2:
                self._canvas.create_oval(x1,y1,x2,y2,fill='white')
            elif self._game.board[x][y] == 3 or self._game.board[x][y] == 4:
                animatedTileCoords.append([x,y])
    for x, y in animatedTileCoords:
        x1 = x * column_width
        y1 = y * row_height
        x2 = x1 + column_width
        y2 = y1 + row_height
        if i == 1:
            self._canvas.create_oval(x1,y1,x2,y2,fill=oppcolor)
            self._canvas.after(50, lambda: self.animateFlips(2))
        elif i == 2:
            self._canvas.create_oval(x1 + (column_width/4),y1,x1 + (3*column_width/4),y2,fill=oppcolor)
            self._canvas.after(50, lambda: self.animateFlips(3))
        elif i == 3:
            self._canvas.create_oval(x1 + (column_width/2),y1,x1 + (column_width/2),y2,fill=oppcolor)
            self._canvas.after(50, lambda: self.animateFlips(4))
        elif i == 4:
            self._canvas.create_oval(x1 + (column_width/4),y1,x1 + (3*column_width/4),y2,fill=color)
            self._canvas.after(50, lambda: self.animateFlips(5))
        elif i == 5:
            self._canvas.create_oval(x1,y1,x2,y2,fill=color)
            newBoard[x][y] = Othello.nextTurn(self._game.turn)
    self._game = GameState(board = newBoard, turn = self._game.turn, skipped = 0)
    self.displayMoves()
    self.displayButtons()
    self.displayInfo()
    self.displayWinner(self.getWinner())
    self._canvas.bind('<Configure>',self.draw_handler)
def animateFlips(self,i):
self.\u canvas.delete(tkinter.ALL)
column\u width=self.\u canvas.winfo\u width()/Othello.COLUMNS
行高度=(self.\u canvas.winfo\u height()-self.\u记分板)/Othello.ROWS
新板=自板。\游戏板
animatedTileCoords=[]
颜色=“”
oppcolor=''
如果self.\u game.turn==2:
颜色=‘黑色’
oppcolor='白色'
elif self.\u game.turn==1:
颜色=‘白色’
oppcolor=‘黑色’
对于范围内的x(奥赛罗列):
对于范围内的y(奥赛罗行):
x1=x*列的宽度
y1=y*行高
x2=x1+列宽度
y2=y1+行高
自画布。创建矩形(x1、y1、x2、y2)
如果self._game.board[x][y]==1:
自我。画布。创建椭圆形(x1,y1,x2,y2,fill='black')
elif self._game.board[x][y]==2:
自我。画布。创建椭圆形(x1,y1,x2,y2,填充为白色)
elif self.\u game.board[x][y]==3或self.\u game.board[x][y]==4:
animatedTileCoords.append([x,y])
对于动画文件坐标中的x、y:
x1=x*列的宽度
y1=y*行高
x2=x1+列宽度
y2=y1+行高
如果i==1:
创建椭圆形(x1,y1,x2,y2,fill=oppcolor)
self._canvas.after(50,lambda:self.animateFlips(2))
elif i==2:
self._canvas.create_oval(x1+(列宽/4)、y1、x1+(3*列宽/4)、y2、fill=oppcolor)
self._canvas.after(50,lambda:self.animateFlips(3))
elif i==3:
创建椭圆(x1+(列宽度/2)、y1、x1+(列宽度/2)、y2、填充=颜色)
self._canvas.after(50,lambda:self.animateFlips(4))
elif i==4:
创建椭圆(x1+(列宽/4)、y1、x1+(3*列宽/4)、y2、填充=颜色)
self._canvas.after(50,lambda:self.animateFlips(5))
elif i==5:
自我。画布。创建椭圆形(x1,y1,x2,y2,填充=颜色)
新棋盘[x][y]=奥赛罗下一回合(自选.\u游戏回合)
self.\u game=GameState(棋盘=newBoard,回合=self.\u game.turn,跳过=0)
self.displayMoves()
self.displayButtons()
self.displayInfo()
self.displayWinner(self.getWinner())
self.\u canvas.bind(“”,self.draw\u处理程序)

最简单的解决方案是不要在每次迭代中删除和重新创建游戏块。绘制一次,然后使用
itemconfig
方法更改游戏块的外观


更好的做法是创建一个
游戏片
类,这样每个游戏片都由这个类表示。然后,在类内移动动画函数。这将使您的主要逻辑更容易理解

这和语言有关吗?