python – 移动椭圆形的棋盘代码
发布时间:2020-12-20 13:05:40 所属栏目:Python 来源:网络整理
导读:我正在为大学做一个 python跳棋游戏.我使用tk绘制了电路板,但我似乎无法为这些部件实现移动功能.如果有人在我的代码中看到任何错误,或者可以提供帮助,我将不胜感激.这是完整的来源.提前致谢. 我知道这会吸引棋子.我不知道如何重新绘制碎片,而不删除其他碎片.
我正在为大学做一个
python跳棋游戏.我使用tk绘制了电路板,但我似乎无法为这些部件实现移动功能.如果有人在我的代码中看到任何错误,或者可以提供帮助,我将不胜感激.这是完整的来源.提前致谢.
我知道这会吸引棋子.我不知道如何重新绘制碎片,而不删除其他碎片.我已经在线查看了移动功能,并尝试了简单的测试,但我无法在我的代码中使用它. 我知道递归,但是,在实现更多功能之前,我需要基本功能才能工作,即实际在屏幕上移动一块. lst2 = [] #counter variable i=0 #board variable is what stores the X/O/- values. # It's a 2D list. We iterate over it,looking to see # if there is a value that is X or O. If so,we draw # text to the screen in the appropriate spot (based on # i and j. while i < len(board): j=0 while j < len(board[i]): if board[i][j] == 2: lst2.append(canvas.create_oval((i+1)*width + width/2 + 15,(j+1)*height + height/2 +15,(i+1)*width + width/2 - 15,(j+1)*width + width/2 - 15,fill="Red",outline='Black')) elif board[i][j] == 4: lst2.append(canvas.create_oval((i+1)*width + width/2 + 15,outline='Black')) elif board[i][j] == 1: lst2.append(canvas.create_oval((i+1)*width + width/2 + 15,fill="Black",outline='Black')) elif board[i][j] == 3: lst2.append(canvas.create_oval((i+1)*width + width/2 + 15,outline='Black')) j+=1 i+=1 解决方法
您可以使用
coords和/或
move方法在画布上移动项目,以将坐标从它们更改为您希望的坐标.
这是一个简单的示例,展示如何在画布上创建和移动项目: import Tkinter as tk class Example(tk.Frame): '''Illustrate how to drag items on a Tkinter canvas''' def __init__(self,parent): tk.Frame.__init__(self,parent) # create a canvas self.canvas = tk.Canvas(width=400,height=400) self.canvas.pack(fill="both",expand=True) # this data is used to keep track of an # item being dragged self._drag_data = {"x": 0,"y": 0,"item": None} # create a couple of movable objects self._create_token((100,100),"white") self._create_token((200,"black") # add bindings for clicking,dragging and releasing over # any object with the "token" tag self.canvas.tag_bind("token","<ButtonPress-1>",self.on_token_press) self.canvas.tag_bind("token","<ButtonRelease-1>",self.on_token_release) self.canvas.tag_bind("token","<B1-Motion>",self.on_token_motion) def _create_token(self,coord,color): '''Create a token at the given coordinate in the given color''' (x,y) = coord self.canvas.create_oval(x-25,y-25,x+25,y+25,outline=color,fill=color,tags="token") def on_token_press(self,event): '''Begining drag of an object''' # record the item and its location self._drag_data["item"] = self.canvas.find_closest(event.x,event.y)[0] self._drag_data["x"] = event.x self._drag_data["y"] = event.y def on_token_release(self,event): '''End drag of an object''' # reset the drag information self._drag_data["item"] = None self._drag_data["x"] = 0 self._drag_data["y"] = 0 def on_token_motion(self,event): '''Handle dragging of an object''' # compute how much the mouse has moved delta_x = event.x - self._drag_data["x"] delta_y = event.y - self._drag_data["y"] # move the object the appropriate amount self.canvas.move(self._drag_data["item"],delta_x,delta_y) # record the new position self._drag_data["x"] = event.x self._drag_data["y"] = event.y if __name__ == "__main__": root = tk.Tk() Example(root).pack(fill="both",expand=True) root.mainloop() (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |