【发布时间】:2020-11-12 13:46:00
【问题描述】:
所以我正在构建一个纸牌游戏程序以允许 2 名玩家互相对战 (MTG),但我对这个问题完全感到困惑。
所以我有 2 个画布的 1 为每个玩家生成的“战场”代码是相同的(只是名称更改)并且将“卡片”添加到每个战场的代码几乎相同(删除了几个菜单对手牌)
当我在玩家卡片上使用 frame.destory() 时,它工作正常并且框架被移除,但是当我在对手卡片框架上使用它时,程序停止并且没有响应。
我正在使用 python 2.7,并且程序在 card_frame.destroy() 上的对手卡删除字段例程中停止
任何想法我做错了什么?
这是我的代码;
对手框架创建代码;
def create_opponent_frame(self):
self.opponent_frame = tk.Frame(self.field, bg='grey11', highlightthickness=0)
self.opponent_battlefield = tk.Frame(self.opponent_frame, bg='grey11', highlightthickness=0)
self.opponent_battlefield_canvas = tk.Canvas(self.opponent_battlefield)
self.opponent_battlefield_canvas.pack(side='top', padx=(2, 2), pady=(2, 2))
self.opponent_battlefield.pack(side='left', fill='both', expand='True', padx=(5, 5), pady=(5, 5))
self.opponent_frame.pack(side='top', fill="both", expand="True")
玩家框架创建;
def create_player_frame(self):
self.player_frame = tk.Frame(self.field, bg='grey11', highlightthickness=0)
self.player_battlefield = tk.Frame(self.player_frame, bg='grey11', highlightthickness=0)
self.player_battlefield_canvas = tk.Canvas(self.player_battlefield)
self.player_battlefield_canvas.pack(side='top', padx=(2, 2), pady=(2, 2))
self.player_battlefield.pack(side='left', fill='both', expand='True', padx=(5, 5), pady=(5, 5))
self.player_frame.pack(side='top', fill="both", expand="True")
玩家卡片创建(图片是从数据库中读取的图片文件);
def create_player_battlefield_card(self, card, status, facing):
details['card'] = card
details["card frame"] = tk.Frame(self.player_battlefield_canvas, bg='midnight blue', highlightthickness=0)
details["card canvas"] = tk.Canvas(details["card frame"], width=75, height=105,
bg='midnight blue', highlightthickness=0)
details["original"] = image
details["img"] = details["original"].resize((75, 105))
details["img"] = ImageTk.PhotoImage(details["img"])
details['list index'] = a
details['count'] = self.player_field
details["object"] = details["card canvas"].create_image(0, 0, anchor='nw', image=details["img"])
self.player_battlefield_image_objects.append(details)
details["card canvas"].pack(side='top')
self.player_battlefield_canvas.create_window((55, 75), window=details["card frame"], tags=details['count'])
创建对手卡(同样图像是来自数据库的图像文件);
def create_opponent_battlefield_card(self, card, status, facing):
details['card'] = card
details["card frame"] = tk.Frame(self.opponent_battlefield_canvas, bg='midnight blue', highlightthickness=0)
details["card canvas"] = tk.Canvas(details["card frame"], width=75, height=105,
bg='midnight blue', highlightthickness=0)
details["original"] = image
details['img'] = image.rotate(180)
details["img"] = details["img"].resize((75, 105))
details["img"] = ImageTk.PhotoImage(details["img"])
details['list index'] = a
details['count'] = self.opponent_field
details["object"] = details["card canvas"].create_image(0, 0, anchor='nw', image=details["img"])
self.opponent_battlefield_image_objects.append(details)
details["card canvas"].pack(side='top')
y = self.opponent_battlefield_canvas.winfo_height() - details["card frame"].winfo_height()
y -= 75
self.opponent_battlefield_canvas.create_window((55, y),
window=details["card frame"], tags=details['count'])
移除玩家卡的代码;
def field_to_hand(self, list_index):
card = self.player_battlefield_image_objects[list_index]['card']
card_frame = self.player_battlefield_image_objects[list_index]['card frame']
status = self.player_battlefield_image_objects[list_index]['status']
if status == 'original':
self.create_player_hand_card(card)
card_frame.destroy()
self.player_battlefield_image_objects.pop(list_index)
for detail in self.player_battlefield_image_objects:
if detail['list index'] > list_index:
self.player_battlefield_image_objects[detail['list index'] - 1]['list index'] -= 1
移除对手牌的代码;
def opponent_card_removed_field(self, list_index):
card = self.opponent_battlefield_image_objects[list_index]['card']
card_frame = self.opponent_battlefield_image_objects[list_index]['card frame']
status = self.opponent_battlefield_image_objects[list_index]['status']
card_frame.destroy()
print self.opponent_battlefield_image_objects
self.opponent_battlefield_image_objects.pop(list_index)
for detail in self.opponent_battlefield_image_objects:
if detail['list index'] > list_index:
self.opponent_battlefield_image_objects[detail['list index'] - 1]['list index'] -= 1
编辑以减少代码
【问题讨论】:
-
更多信息我忘了补充。 “发送消息”例程将用于通过套接字将数据发送到服务器,然后发送给你的对手。现在我将它设置为循环返回,这样就好像对手正在镜像你所做的每一个动作。如果完整的程序代码有帮助,我可以添加它,但它的代码将近 1600 行
-
你发布的代码太多了。请尝试将其压缩为minimal reproducible example。
-
我很抱歉 - 我已经通过删除与上下文菜单和其他对我遇到的问题没有影响的部分有关的内容来减少代码
标签: python-2.7 tkinter frame destroy