如何使对象跟随tkinter中的鼠标(How to make object follow mouse in tkinter)
我正在尝试制作一个agar.io克隆并且我有鼠标的坐标,但我不知道如何让玩家向鼠标移动而不是直接转到鼠标。 到目前为止,我有这个来获取鼠标的坐标:
def mouseCoords(self): rawMouseX, rawMouseY = tk.winfo_pointerx(), tk.winfo_pointery() self.mousecoords = rawMouseX - tk.winfo_rootx(), rawMouseY - tk.winfo_rooty() return self.mousecoords编辑:
我试图用这个代码使点朝着鼠标移动,但它只在8个不同的方向上移动,而不是总是直接朝向鼠标移动。
这是完整的(未完成的)代码:
from tkinter import * import time, random, numpy class PlayerSprite: def __init__(self, canvas): self.canvas = canvas self.endgame = False self.id = self.canvas.create_oval(350, 350, 400, 400, tag='User', fill=random.choice(colors)) self.id2 = self.canvas.create_text(375, 375, text=nick, font=('Helvetica', 15), tag='User') def coords(self): print(self.canvas.coords('User')) return self.canvas.coords('User') def mouseCoords(self): rawMouseX, rawMouseY = tk.winfo_pointerx(), tk.winfo_pointery() self.mousecoords = rawMouseX - tk.winfo_rootx(), rawMouseY - tk.winfo_rooty() return self.mousecoords def moveTowardMouse(self): #This is the function that I don't know how to complete selfx, selfy = self.coords() mousex, mousey = self.mousecoords movex = (mousex-selfx) movey = (mousey-selfy) if movex > 0 and movey > 0: self.canvas.move('User', 2, 2) elif movex > 0 and movey < 0: self.canvas.move('User', 2, -2) elif movex < 0 and movey > 0: self.canvas.move('User', -2, 2) elif movex < 0 and movey < 0: self.canvas.move('User', -2, -2) elif movex == 0 and movey > 0: self.canvas.move('User', 0, 2) elif movex == 0 and movey < 0: self.canvas.move('User', 0, -2) elif movex > 0 and movey == 0: self.canvas.move('User', 2, 0) elif movex < 0 and movey == 0: self.canvas.move('User', -2, 0) elif movex == 0 and movey == 0: pass else: pass tk = Tk() nick = simpledialog.askstring('nickname', 'Nickname') tk.title("My Agar.io Clone") tk.wm_attributes('-topmost', 1) tk.resizable(0, 0) canvas = Canvas(tk, width=750, height=750) center = (canvas.winfo_reqwidth()/2), (canvas.winfo_reqheight()/2) colors = ['red', 'blue', 'green', 'yellow'] canvas.pack() player = PlayerSprite(canvas) player.mouseCoords() while player.endgame == False: try: player.moveTowardMouse() player.mouseCoords() tk.update_idletasks() tk.update() time.sleep(.01) except KeyboardInterrupt: print('CRL-C recieved, quitting') tk.quit() breakI am trying to make an agar.io clone and I have the coordinates of the mouse, but I don't know how to make the player move toward the mouse not just go directly to the mouse. So far I have this to get the coordinates of the mouse:
def mouseCoords(self): rawMouseX, rawMouseY = tk.winfo_pointerx(), tk.winfo_pointery() self.mousecoords = rawMouseX - tk.winfo_rootx(), rawMouseY - tk.winfo_rooty() return self.mousecoordsEdit:
I'm trying to make the dot go toward the mouse with this code, but it only moves in 8 different directions, not always directly toward the mouse.
Here is the full (unfinished) code:
from tkinter import * import time, random, numpy class PlayerSprite: def __init__(self, canvas): self.canvas = canvas self.endgame = False self.id = self.canvas.create_oval(350, 350, 400, 400, tag='User', fill=random.choice(colors)) self.id2 = self.canvas.create_text(375, 375, text=nick, font=('Helvetica', 15), tag='User') def coords(self): print(self.canvas.coords('User')) return self.canvas.coords('User') def mouseCoords(self): rawMouseX, rawMouseY = tk.winfo_pointerx(), tk.winfo_pointery() self.mousecoords = rawMouseX - tk.winfo_rootx(), rawMouseY - tk.winfo_rooty() return self.mousecoords def moveTowardMouse(self): #This is the function that I don't know how to complete selfx, selfy = self.coords() mousex, mousey = self.mousecoords movex = (mousex-selfx) movey = (mousey-selfy) if movex > 0 and movey > 0: self.canvas.move('User', 2, 2) elif movex > 0 and movey < 0: self.canvas.move('User', 2, -2) elif movex < 0 and movey > 0: self.canvas.move('User', -2, 2) elif movex < 0 and movey < 0: self.canvas.move('User', -2, -2) elif movex == 0 and movey > 0: self.canvas.move('User', 0, 2) elif movex == 0 and movey < 0: self.canvas.move('User', 0, -2) elif movex > 0 and movey == 0: self.canvas.move('User', 2, 0) elif movex < 0 and movey == 0: self.canvas.move('User', -2, 0) elif movex == 0 and movey == 0: pass else: pass tk = Tk() nick = simpledialog.askstring('nickname', 'Nickname') tk.title("My Agar.io Clone") tk.wm_attributes('-topmost', 1) tk.resizable(0, 0) canvas = Canvas(tk, width=750, height=750) center = (canvas.winfo_reqwidth()/2), (canvas.winfo_reqheight()/2) colors = ['red', 'blue', 'green', 'yellow'] canvas.pack() player = PlayerSprite(canvas) player.mouseCoords() while player.endgame == False: try: player.moveTowardMouse() player.mouseCoords() tk.update_idletasks() tk.update() time.sleep(.01) except KeyboardInterrupt: print('CRL-C recieved, quitting') tk.quit() break最满意答案
要让玩家向各个方向移动,您需要使用玩家和鼠标坐标与参考轴的角度。 然后使用该角度找到移动玩家的x和y距离。
我使用数学模块中的atan2方法来计算角度。
修改代码
## imports for Python 2.7, change as appropriate from Tkinter import * import tkSimpleDialog as simpledialog import time, random, numpy, math class PlayerSprite: def __init__(self, canvas): self.canvas = canvas self.endgame = False self.id = self.canvas.create_oval(350, 350, 400, 400, tag='User', fill=random.choice(colors)) self.id2 = self.canvas.create_text(375, 375, text=nick, font=('Helvetica', 15), tag='User') def coords(self): print(self.canvas.coords('User')) return self.canvas.coords('User') def mouseCoords(self): rawMouseX, rawMouseY = tk.winfo_pointerx(), tk.winfo_pointery() self.mousecoords = rawMouseX - tk.winfo_rootx(), rawMouseY - tk.winfo_rooty() return self.mousecoords def moveTowardMouse(self): # Problem function? ## Use center of the of the oval/player as selfx, selfy selfx, selfy = (self.coords()[0]+self.coords()[2])/2, (self.coords()[1]+self.coords()[3])/2 mousex, mousey = self.mousecoords movex = (mousex-selfx) movey = (mousey-selfy) speed = 2 ## Player speed theta = math.atan2(movey, movex) ## angle between player and mouse position, relative to positive x ## Player speed in terms of x and y coordinates x = speed*math.cos(theta) y = speed*math.sin(theta) self.canvas.move('User', x, y) tk = Tk() nick = simpledialog.askstring('nickname', 'Nickname') tk.title("My Agar.io Clone") tk.wm_attributes('-topmost', 1) tk.resizable(0, 0) canvas = Canvas(tk, width=750, height=750) center = (canvas.winfo_reqwidth()/2), (canvas.winfo_reqheight()/2) colors = ['red', 'blue', 'green', 'yellow'] canvas.pack() player = PlayerSprite(canvas) player.mouseCoords() while player.endgame == False: try: player.moveTowardMouse() player.mouseCoords() tk.update_idletasks() tk.update() time.sleep(.005) except:# KeyboardInterrupt: print('CRL-C recieved, quitting') tk.quit() breakI figured it out:
def moveTowardMouse(self): selfx, selfy = self.coords() mousex, mousey = self.mousecoords directDist = math.sqrt(((mousex-selfx) ** 2) + ((mousey-selfy) ** 2)) self.speed = 4 movex = (mousex-selfx) / directDist movey = (mousey-selfy) / directDist self.canvas.move('User', movex*self.speed, movey*self.speed)更多推荐
发布评论