ydwb 发表于 2025-1-10 14:24:39

python五子棋悔棋时删除最后两子不成功

python五子棋按悔棋按钮删除最后两子不成功,显示:
    GameLayer.remove(bo)
TypeError: remove() missing 1 required positional argument: 'obj'
import cocos
from cocos.scene import Scene
from cocos.director import director
from cocos.menu import *
from pyglet.gl import *
import random
import pyglet
list1=[(-1,-1, 0)]
qizi= []
class OptionMenu(Menu):
    def __init__(self):
      super().__init__()
      self.font_item["font_name"] = "kaiti"
      self.font_item["font_size"] = 32
      self.font_item["color"] = (160, 16, 0, 255)

      self.font_item_selected["font_name"] = "kaiti"
      self.font_item_selected["font_size"] = 40
      self.font_item_selected["color"] = (0, 128, 0, 255)
      items = []
      items.append(MenuItem("悔棋", self.on_huiqi))
      items.append(MenuItem("帮助", self.on_help))
      self.create_menu(items, shake(), shake_back(),
                         layout_strategy=fixedPositionMenuLayout([(760, 240), (760, 300)]))

    def on_huiqi(self):
      bo = qizi[-1]
      GameLayer.remove(bo)
      del list1[-1]
      bo = qizi[-1]
      GameLayer.remove(bo)
      del list1[-1]

    def on_help(self):
      pass
class GameLayer(cocos.layer.ColorLayer):
    globallist1, qizi
    def __init__(self):
      super().__init__(22, 142, 202, 128)
      cocos.layer.Layer.is_event_handler = True
      bg = cocos.sprite.Sprite('bc55.jpg')
      bg.position = (350, 350)
      self.add(bg)

    def on_mouse_press(self, x, y, button, modifiers):
      if button == pyglet.window.mouse.LEFT:
            x = round((x - 35) / 45)
            y = round((y - 35) / 45)
            if (x, y, 1) in list1:
                return None
            elif (x, y, 2) in list1:
                return None
            elif x < 0 or y < 0 or x > 14 or y > 14:
                return None

            blk0 = cocos.sprite.Sprite('3.png')
            blk0.position = (x * 45 + 35, y * 45 + 35)
            self.add(blk0)
            qizi.append(blk0)
            list1.append((x, y, 1))

            self.computer_move()

    def computer_move(self):
      x = random.randint(0,14)
      y = random.randint(0, 14)
      if (x, y, 1) in list1:
            return None
      elif (x, y, 2) in list1:
            return None

      wht0 = cocos.sprite.Sprite('4.png')
      wht0.position = (x * 45 + 35, y * 45 + 35)
      self.add(wht0)
      qizi.append(wht0)
      list1.append((x, y, 2))
      wht0.do(cocos.actions.Blink(2, 2))

def create_game_scene():
    scene = Scene()
    scene.add(GameLayer(), z=1)
    scene.add(OptionMenu(), z=2)
    return scene

def main():
    director.init(850, 700,resizable=True,caption="五子棋")
    director.run(create_game_scene())

if __name__ == "__main__":
    main()

FishC 发表于 2025-1-10 14:24:51

你好,根据你提供的代码问题,`GameLayer.remove()`方法的调用方式错误。这里我们需要从`qizi`列表中获取最后两个棋子并移除,而不是直接调用`remove()`方法。此外,代码中使用`del list1`是错误的,因为会导致以后无法访问`list1`。你应该修改`on_huiqi`方法如下:

def on_huiqi(self):
    if len(qizi) >= 2:# 确保存在至少两个棋子可以移除
      # 移除最后一个玩家的棋子
      last_player_piece = qizi.pop()
      self.parent.remove(last_player_piece)# 使用self.parent来获取GameLayer的实例
      list1.pop()# 移除相应的坐标信息

      # 移除最后一个计算机的棋子
      last_computer_piece = qizi.pop()
      self.parent.remove(last_computer_piece)# 使用self.parent来获取GameLayer的实例
      list1.pop()# 移除相应的坐标信息

请注意,上述代码中的`self.parent`假设`OptionMenu`和`GameLayer`存在于同一场景中,并且`OptionMenu`作为事件的一部分,能够访问`GameLayer`实例。在调用`remove`时,我们从`GameLayer`实例中移除棋子。

确保`qizi`和`list1`在你的代码中是正确初始化的列表对象,并且在棋子移动时,它们按照出现的顺序正确记录每一步的状态。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

ydwb 发表于 2025-1-10 14:30:07

    raise Exception("Child not found: %s" % str(child))
Exception: Child not found: <cocos.sprite.Sprite object at 0x0000000007773D30>

FishC 发表于 2025-1-10 14:30:23

ydwb 发表于 2025-01-10 14:30
raise Exception("Child not found: %s" % str(child) ...

出现这个错误通常是因为要移除的子对象(棋子)在父层(`GameLayer`)中找不到。你提供的代码中有一些问题需要确认和修正:

1. 确保`qizi`列表中存放的是实际已经添加到`GameLayer`中的棋子对象。

2. 在`OptionMenu`类中的`on_huiqi`方法中,`self.parent`是否正确指向包含`GameLayer`的父级对象。这里我们假定`OptionMenu`和`GameLayer`在同一场景中。

请参考以下调整代码的建议:

class OptionMenu(Menu):
    def __init__(self):
      super().__init__()
      # 其他初始化代码...

    def on_huiqi(self):
      # 确保qizi和list1在本地已经被正确初始化
      global qizi, list1
      if len(qizi) >= 2:
            # 移除玩家的最后一步棋子
            last_player_piece = qizi.pop()
            self.parent.get_children().remove(last_player_piece)
            list1.pop()

            # 移除计算机的最后一步棋子
            last_computer_piece = qizi.pop()
            self.parent.get_children().remove(last_computer_piece)
            list1.pop()

在这个代码中,`self.parent.get_children()`假设`GameLayer`是场景中的第一个子节点。如果有多个节点,请确认`GameLayer`节点的索引正确。

注意:确保在`GameLayer`类中正确维护棋子对象的添加到`qizi`列表时,它们也被添加到了`GameLayer`中。

另外,确保在程序启动时,`list1`和`qizi`是在游戏的全局上下文中被初始化的,例如可以在文件的最上面:

list1 = []
qizi = []

通过上述检查和调整,希望能够解决你遇到的问题。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页: [1]
查看完整版本: python五子棋悔棋时删除最后两子不成功