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

标签 python memory tkinter processing

我正在 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)

最佳答案

最简单的解决方案是不删除并在每次迭代时重新创建游戏组件。将它们绘制一次,然后使用 itemconfig 方法更改游戏 block 的外观。

更好的方法是创建一个 GamePiece 类,这样每个片段都由该类表示。然后,将动画函数移到类中。这将使您的主要逻辑很多更容易理解。

关于Python Tkinter - 如何使某些代码使用更少的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35825750/

相关文章:

列表理解中的 Python if 语句

Python MyHashTable 类 : search method with linear probing

c# - 需要会占用系统内存的 C# 代码。

python - 如何将整个 sqlite3 查询放到 tkinter 页面上

python - 确定排序的 numpy 整数数组中的 block

python - SimpleBlobDetector - 隔离与周围不同的 Blob

c - 具有离散建模的图形 : faster?

c++ - 在彼此非常接近的范围内混合二进制数据和指令是否有缓存惩罚?

python-3.x - 按下按钮后尝试在 tkinter 中隐藏标签

python - 导入错误 : No module named tktable