gpt4 book ai didi

python - Tkinter - Canvas.move() 不会 move 列表中的最后一项

转载 作者:太空宇宙 更新时间:2023-11-03 19:07:08 24 4
gpt4 key购买 nike

我正在尝试使用 Tkinter 编写贪吃蛇游戏程序我的蛇由下面定义的圆列表组成。但是,当我调用 move 方法时,最后一个方法不会 move 。有什么问题吗?

    class Circle:

def __init__(self,canv,x,y,index,direc):
self._x = x
self._y = y
self._index = index
self._direction = direc
self._canvas = canv
coordinat = (self._x)-5,(self._y)-5,(self._x)+5,(self._y)+5

if index==0:
color = "gold"
else:
color = "green"

self._circle = canv.create_oval(coordinat ,fill=color, tags=str(self._index))

def _move(self):
if self._direction == "up":
delta = (0,-5)
elif self._direction == "down":
delta = (0,5)
elif self._direction == "left":
delta = (-5,0)
elif self._direction == "right":
delta = (5,0)

tag = str(self._index)
self._canvas.move(tag, delta[0],delta[1])

我就是这么称呼她的

self._canvas = Canvas(self._playArea,width=750,height=450,bg="#1C1C1C")
x = int(self._parent.winfo_width() / 4.)
y = int(self._parent.winfo_height()/ 4.)
circle = Circle(self._canvas,x,y,0,"right")
self._circleList.append(circle)
self._addCircle()
self._addCircle()
self._addCircle()
self._addCircle()
self._addCircle()

self._canvas.place(x=0, y=0)

for i in range(0,500):
for x in self._circleList:
x._move()
root.update()
root.after(10)

这是 addCircle 方法

length = len(self._circleList)
if self._circleList[length-1]._direction == "right":
x = (self._circleList[length-1]._x)-10
y = self._circleList[length-1]._y
d = "right"
elif self._circleList[length-1]._direction == "left":
x = (self._circleList[length-1]._x) + 10
y = self._circleList[length-1]._y
d = "left"
elif self._circleList[length-1]._direction == "up":
x = self._circleList[length-1]._x
y = (self._circleList[length-1]._y)+10
d = "up"
elif self._circleList[length-1]._direction == "down":
x = self._circleList[length-1]._x
y = (self._circleList[length-1]._y)-10
d = "down"

newCircle = Circle(self._canvas,x,y,length,d)
self._circleList.append(newCircle)

最佳答案

问题是您正在使用 index=0 创建第一个项目,然后使用 index作为识别该项目的标签。你不应该使用整数作为标签,它是为项目的 id 保留的,但是,你毕竟可以使用它。除此之外0被 Tcl 评估为 false,因此您没有有效地为其定义任何标签。事实证明,当您调用canvas.move(0, ...)时您不 move 任何创建的圆圈。但是您创建的下一个项目被分配了标签“1”,并且当您调用 canvas.move(1, ...) 时您实际上正在 move 之前创建的项目(具有“金色”颜色的项目),因为 Tcl 自动为其分配了 id“1”。对创建的所有其他圈子重复此操作。

解决这个问题的快速方法是更改​​代码以使用 index + 1对于您正在通过的所有这些指数。但是您包含的代码中有几个问题,因此这里是一个修改后的代码,可以执行您所追求的操作:

import Tkinter

UP, RIGHT, DOWN, LEFT = range(4)

class Circle:
def __init__(self, canv, x, y, direc, color):
self.x, self.y = x, y
self.direction = direc

self._canvas = canv
coord = (self.x)-5, (self.y)-5, (self.x)+5, (self.y)+5
self._index = canv.create_oval(coord, fill=color)

def move(self):
y_sign = 1 if self.direction == DOWN else -1
x_sign = 1 if self.direction == RIGHT else -1
if self.direction in (UP, DOWN):
delta = (0, y_sign * 5)
else:
delta = (x_sign * 5, 0)
self._canvas.move(self._index, delta[0], delta[1])


def add_circle(canvas, l):
d = l[-1].direction
if d in (UP, DOWN):
x = l[-1].x
y = (1 if d == UP else -1) * 10 + l[-1].y
else:
x = (1 if d == LEFT else -1) * 10 + l[-1].x
y = l[-1].y

circle = Circle(canvas, x, y, d, "green")
l.append(circle)


def move_circles(circle_l, root):
for c in circle_l:
c.move()
root.after(50, lambda: move_circles(circle_l, root))

root = Tkinter.Tk()
width, height = 750, 450

canvas = Tkinter.Canvas(width=width, height=height, bg="#1C1C1C")
canvas.pack(fill=None, expand=False)

circle_l = []
circle = Circle(canvas, width / 4, height / 4, RIGHT, "gold")
circle_l.append(circle)
for _ in range(5):
add_circle(canvas, circle_l)

move_circles(circle_l, root)

root.mainloop()

关于python - Tkinter - Canvas.move() 不会 move 列表中的最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14429134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com