【问题标题】:image or class appended to list附加到列表的图像或类
【发布时间】:2020-12-13 18:47:16
【问题描述】:

我一直试图通过让消息框出现来检测获胜者,我正在考虑将所有被破坏的部分附加到列表中,然后如果检测到国王在那里获胜,我正在努力是否是 GUIKing 类或附加到列表中的作品的图像。

def destroy_piece(self, piece):
        """ Removes piece from the canvans and click-handler
            automatily called by move_piece
        """
        img1=piece.get_img_int(0)
        img2=piece.get_img_int(1)
        
        del self._on_clicks[str(img1)]
        del self._on_clicks[str(img2)]
        
        self.delete(piece.get_img_int(0))
        self.delete(piece.get_img_int(1))
        
        self.destroyed_pieces = [] 
        self.destroyed_pieces.append(piece)
        
        

【问题讨论】:

    标签: python list chess


    【解决方案1】:

    列表应该包含一个对象,所以 GUIKing 类实例应该是要走的路。但这取决于您在整个程序中如何做到这一点。您必须保持一致并始终使用对象/类。这样更好。

    但是为了快速修复,你可以使用这个:

    def destroy_piece(self, piece):
            """ Removes piece from the canvans and click-handler
                automatily called by move_piece
            """
            img1=piece.get_img_int(0)
            img2=piece.get_img_int(1)
            
            del self._on_clicks[str(img1)]
            del self._on_clicks[str(img2)]
            
            self.delete(piece.get_img_int(0))
            self.delete(piece.get_img_int(1))
            
            ## self.destroyed_pieces = [] ## move this in the class __init__
            self.destroyed_pieces.append(piece)
            
            ##for elem in self.destroyed_pieces: ## remove this unneccessary line
            if "bk.png" or "wk.png" in self.destroyed_pieces:
                messagebox.showinfo("WINNER")
    

    【讨论】:

    • 好的,你有解决办法吗?对于 self.destroyed_pieces 中的元素:如果 self.destroyed_pieces 中的 GUIKing:messagebox.showinfo("WINNER")
    • 视情况而定。你如何存储电路板状态?和现有的作品?代码不够完整,无法给出答案,因为它可能会影响代码的其他部分。
    • 是不是已经按照你的方式工作了?如果是的话……这可能是一项将所有对象更改为使用对象的漫长任务。无论如何,您只使用图像名称,而不是列表中的完整尺寸图像......所以开销不是那么大......只是编码风格的问题,我想
    • 我测试了它,但它实际上并没有工作,所以我实际上不会再进一步​​了
    • 存储是指棋子在游戏板上的排列方式?
    猜你喜欢
    • 2013-09-26
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    • 2012-12-20
    • 2021-12-10
    • 1970-01-01
    相关资源
    最近更新 更多