我正在尝试用Python tkinter
制作2D shooter。
以下是我的进展:
from tkinter import *
root = Tk()
c = Canvas(root, height=500, width=500, bg='blue')
c.pack()
circle1x = 250
circle1y = 250
circle2x = 250
circle2y = 250
circle1 = c.create_oval(circle1x, circle1y, circle1x + 10, circle1y + 10, outline='white')
circle2 = c.create_rectangle(circle2x, circle2y,circle2x + 10, circle2y + 10)
pos1 = c.coords(circle1)
pos2 = c.coords(circle2)
c.move(circle1, 250 - pos1[0], 250 - pos1[2])
c.move(circle2, 250 - pos1[0], 250 - pos1[2])
beginWall = c.create_rectangle(0, 200, 500, 210, outline='white')
def move_circle(event):
pass
c.bind('<Motion>', move_circle)
root.mainloop()
但是我试图让函数move_circle
makecircle1
和circle2
跟随鼠标指针。像这样的c.goto(circle1, x, y)
。你知道吗
可以通过修改
move_circle()
事件处理程序函数中两个“圆”的坐标来实现。一个简单的计算是这样做的,这两个对象的中心定位在鼠标指针的“尖端”(见下图)。你知道吗注意,我还修改了您的代码,以便更紧密地遵循PEP 8 - Style Guide for Python Code编码准则。你知道吗
下面是它在我的Windows电脑上运行的截图:
相关问题 更多 >
编程相关推荐