两个弹跳球总是在 tkinter 中连接起来
two bouncing balls always linking up in tkinter
我正在尝试创建几个弹跳球,但出于某种原因,即使它们具有不同的速度和起始坐标,它们仍然会在相同的坐标中相遇。如果你 运行 下面的代码,你会看到,过了一会儿,其中一个球会卡在角落里,然后另一个球会像一个球一样跟随它,直到动画的其余部分他们都有不同的速度..
import tkinter as tk
from time import *
from random import randint, choice
myInterface = tk.Tk()
s = tk.Canvas(myInterface, width=800, height=800, background="black")
s.pack()
d = 75
spdx = 8
spdy = 4.5 * -1
spdxb = 10 * -1
spdyb = 5
xblock1 = 0
yblock1 = 0
xblock2 = 800
yblock2 = 800
x1 = randint(475,725)
y1 = randint(475,725)
x1b = randint(75,375)
y1b = randint(75,375)
color = ["yellow", "hot pink","deep skyblue","dark green"]
c = choice(color)
while True:
##c = choice(color) activate for disco mode!!
x1 = x1 + spdx
y1 = y1 - spdy
x2 = x1 + d
y2 = y1 + d
x1b = x1b + spdx
y1b = y1b - spdy
x2b = x1b + d
y2b = y1b + d
firstball = s.create_oval(x1,y1,x2,y2, fill=c)
secondball = s.create_oval(x1b,y1b, x2b,y2b, fill =c)
#A1
if x1 <= xblock1:
x1 = xblock1
x2 = x1 + d
spdx = spdx * -1
#B1
if x1b <= xblock1:
x1b = xblock1
x2b = x1b + d
spdxb = spdxb * -1
#A2
if x2 >= xblock2:
x2 = xblock2
x1 = x2 - d
spdx = spdx * -1
#B2
if x2b >= xblock2:
x2b = xblock2
x1b = x2b - d
spdxb = spdxb * -1
#A3
if y1 <= yblock1:
y1 = yblock1
y2 = y1 + d
spdy = spdy * -1
#B3
if y1b <= yblock1:
y1b = yblock1
y2b = y1b + d
spdyb = spdyb * -1
#A4
if y2 >= yblock2:
y2 = yblock2
y1 = y2 - d
spdy = spdy * -1
#B4
if y2b >= yblock2:
y2b = yblock2
y1b = y2b - d
spdyb = spdyb * -1
s.update()
sleep(0.03)
s.delete(firstball,secondball)
问题是您使用 spdx
和 spdy
来更新球 2,而不是 spdxb
和 spdyb
。
因此,当球 2 击中左边缘时,它每帧只来回翻转 spdxb
一次。这没有任何作用,因为您没有使用 spdxb
做任何事情。同时,您用于两个球的 spdx
在球 1 也击中左边缘之前不会翻转。
顺便说一句,这是一个很好的例子,说明为什么 DRY(不要重复自己)是一条如此重要的规则。因为您已经多次复制并粘贴了相同的代码并进行了细微的更改,所以很容易将这些细微更改之一弄错而看不到它。尤其是在您盯着看几个小时的代码中。
我正在尝试创建几个弹跳球,但出于某种原因,即使它们具有不同的速度和起始坐标,它们仍然会在相同的坐标中相遇。如果你 运行 下面的代码,你会看到,过了一会儿,其中一个球会卡在角落里,然后另一个球会像一个球一样跟随它,直到动画的其余部分他们都有不同的速度..
import tkinter as tk
from time import *
from random import randint, choice
myInterface = tk.Tk()
s = tk.Canvas(myInterface, width=800, height=800, background="black")
s.pack()
d = 75
spdx = 8
spdy = 4.5 * -1
spdxb = 10 * -1
spdyb = 5
xblock1 = 0
yblock1 = 0
xblock2 = 800
yblock2 = 800
x1 = randint(475,725)
y1 = randint(475,725)
x1b = randint(75,375)
y1b = randint(75,375)
color = ["yellow", "hot pink","deep skyblue","dark green"]
c = choice(color)
while True:
##c = choice(color) activate for disco mode!!
x1 = x1 + spdx
y1 = y1 - spdy
x2 = x1 + d
y2 = y1 + d
x1b = x1b + spdx
y1b = y1b - spdy
x2b = x1b + d
y2b = y1b + d
firstball = s.create_oval(x1,y1,x2,y2, fill=c)
secondball = s.create_oval(x1b,y1b, x2b,y2b, fill =c)
#A1
if x1 <= xblock1:
x1 = xblock1
x2 = x1 + d
spdx = spdx * -1
#B1
if x1b <= xblock1:
x1b = xblock1
x2b = x1b + d
spdxb = spdxb * -1
#A2
if x2 >= xblock2:
x2 = xblock2
x1 = x2 - d
spdx = spdx * -1
#B2
if x2b >= xblock2:
x2b = xblock2
x1b = x2b - d
spdxb = spdxb * -1
#A3
if y1 <= yblock1:
y1 = yblock1
y2 = y1 + d
spdy = spdy * -1
#B3
if y1b <= yblock1:
y1b = yblock1
y2b = y1b + d
spdyb = spdyb * -1
#A4
if y2 >= yblock2:
y2 = yblock2
y1 = y2 - d
spdy = spdy * -1
#B4
if y2b >= yblock2:
y2b = yblock2
y1b = y2b - d
spdyb = spdyb * -1
s.update()
sleep(0.03)
s.delete(firstball,secondball)
问题是您使用 spdx
和 spdy
来更新球 2,而不是 spdxb
和 spdyb
。
因此,当球 2 击中左边缘时,它每帧只来回翻转 spdxb
一次。这没有任何作用,因为您没有使用 spdxb
做任何事情。同时,您用于两个球的 spdx
在球 1 也击中左边缘之前不会翻转。
顺便说一句,这是一个很好的例子,说明为什么 DRY(不要重复自己)是一条如此重要的规则。因为您已经多次复制并粘贴了相同的代码并进行了细微的更改,所以很容易将这些细微更改之一弄错而看不到它。尤其是在您盯着看几个小时的代码中。