python 中的 Tkinter - 对象检测(不是碰撞)
Tkinter in python - object detection (not collision)
对编程还很陌生,并试图让我的移动方块检测随机生成在两个盒子之一上的宝藏
from Tkinter import *
import time
import random
window = Tk()
canvas = Canvas(window, width = 1200, height = 800, bg = 'yellow')
canvas.pack()
canvas.pack(padx = 10, pady = 10)
a1 = 120
a2 = 120
a3 = 60
a4 = 60
Lm1 = canvas.create_rectangle(a1, a2, a3, a4, fill = 'green')
b1 = 530
b2 = 530
b3 = 600
b4 = 600
Lm2 = canvas.create_rectangle(b1, b2, b3, b4, fill = 'green')
Tr1 = canvas.create_rectangle(85,85,85+10,85+10, fill = 'blue')
Tr2 = canvas.create_rectangle(560,560,560+10,560+10, fill = 'blue')
list1 = [Tr1, Tr2]
Treasure1 = random.choice(list1)
if Treasure1 == Tr1:
Tr1 = canvas.create_rectangle(85,85,85+10,85+10, fill = 'blue')
canvas.delete(Tr2)
if Treasure1 == Tr2:
Tr2 = canvas.create_rectangle(560,560,560+10,560+10, fill = 'blue')
canvas.delete(Tr1)
vx = 10.0
vy = 10.0
Ro1 = canvas.create_rectangle(20, 20, 20+10, 20+10)
def right():
canvas.coords(Ro1,x1+vx,y1,x2+vx,y2)
canvas.update()
time.sleep(0.1)
def left():
canvas.coords(Ro1, x1-vx,y1,x2-vx,y2)
canvas.update()
time.sleep(0.1)
def down():
canvas.coords(Ro1, x1,y1+vy,x2,y2+vy)
canvas.update()
time.sleep(0.1)
def up():
canvas.coords(Ro1, x1,y1-vy,x2,y2-vy)
canvas.update()
time.sleep(0.1)
for r in range(1, 12):
x1,y1,x2,y2 = canvas.coords(Ro1)
right()
for r in range(1,12):
x1,y1,x2,y2 = canvas.coords(Ro1)
down()
for r in range(1,5):
x1,y1,x2,y2 = canvas.coords(Ro1)
left()
这是我尝试过的方法,但它不起作用,我希望它在知道宝藏在那里并向右移动时将其移除,我不确定该怎么做?
if Treasure1 == Tr1:
canvas.delete(Tr1)
for r in range(1,50):
x1,y1,x2,y2 = canvas.coords(Ro1)
right()
window.mainloop()
没有理由创建两个方块然后在几行之后删除一个。只需创建您想要的任何一个。此外,您还必须以某种方式跟踪坐标,或者在每次移动时获取它们。以下代码有效,但可能不是您想要的。我无法从 post 中分辨出什么是 "move right"。希望这能让您朝着正确的方向开始。另外,看看 move 而不是 coords http://effbot.org/tkinterbook/canvas.htm 并考虑在进一步使用 GUI 编程之前学习如何使用 类。
from Tkinter import *
import random
def right():
canvas.coords(Ro1, ctr_list[1], ctr_list[2],
ctr_list[1]+10, ctr_list[2]+10)
## canvas.update()
ctr_list[0] += 1
ctr_list[1] += 10
ctr_list[2] += 10
print ctr_list
if ctr_list[0] < 12:
window.after(500, right)
## time.sleep(0.1)
window = Tk()
canvas = Canvas(window, width = 1200, height = 800, bg = 'yellow')
canvas.pack()
canvas.pack(padx = 10, pady = 10)
Button(window, text="Quit", command=window.quit).pack(side="bottom")
a1 = 120
a2 = 120
a3 = 60
a4 = 60
Lm1 = canvas.create_rectangle(a1, a2, a3, a4, fill = 'green')
b1 = 530
b2 = 530
b3 = 600
b4 = 600
Lm2 = canvas.create_rectangle(b1, b2, b3, b4, fill = 'green')
##Tr1 = canvas.create_rectangle(85,85,85+10,85+10, fill = 'blue')
##Tr2 = canvas.create_rectangle(560,560,560+10,560+10, fill = 'blue')
list1 = ["Tr1", "Tr2"]
Treasure1 = random.choice(list1)
if Treasure1 == "Tr1":
print "TR1"
ctr_list = [0, a1-30, a3+30]
else:
print "Tr2"
ctr_list = [0, b1+30, b3-30]
Tr = canvas.create_rectangle(ctr_list[1], ctr_list[2],
ctr_list[1]+10, ctr_list[2]+10, fill="blue")
Ro1 = canvas.create_rectangle(20, 20, 20+10, 20+10)
right()
window.mainloop()
对编程还很陌生,并试图让我的移动方块检测随机生成在两个盒子之一上的宝藏
from Tkinter import *
import time
import random
window = Tk()
canvas = Canvas(window, width = 1200, height = 800, bg = 'yellow')
canvas.pack()
canvas.pack(padx = 10, pady = 10)
a1 = 120
a2 = 120
a3 = 60
a4 = 60
Lm1 = canvas.create_rectangle(a1, a2, a3, a4, fill = 'green')
b1 = 530
b2 = 530
b3 = 600
b4 = 600
Lm2 = canvas.create_rectangle(b1, b2, b3, b4, fill = 'green')
Tr1 = canvas.create_rectangle(85,85,85+10,85+10, fill = 'blue')
Tr2 = canvas.create_rectangle(560,560,560+10,560+10, fill = 'blue')
list1 = [Tr1, Tr2]
Treasure1 = random.choice(list1)
if Treasure1 == Tr1:
Tr1 = canvas.create_rectangle(85,85,85+10,85+10, fill = 'blue')
canvas.delete(Tr2)
if Treasure1 == Tr2:
Tr2 = canvas.create_rectangle(560,560,560+10,560+10, fill = 'blue')
canvas.delete(Tr1)
vx = 10.0
vy = 10.0
Ro1 = canvas.create_rectangle(20, 20, 20+10, 20+10)
def right():
canvas.coords(Ro1,x1+vx,y1,x2+vx,y2)
canvas.update()
time.sleep(0.1)
def left():
canvas.coords(Ro1, x1-vx,y1,x2-vx,y2)
canvas.update()
time.sleep(0.1)
def down():
canvas.coords(Ro1, x1,y1+vy,x2,y2+vy)
canvas.update()
time.sleep(0.1)
def up():
canvas.coords(Ro1, x1,y1-vy,x2,y2-vy)
canvas.update()
time.sleep(0.1)
for r in range(1, 12):
x1,y1,x2,y2 = canvas.coords(Ro1)
right()
for r in range(1,12):
x1,y1,x2,y2 = canvas.coords(Ro1)
down()
for r in range(1,5):
x1,y1,x2,y2 = canvas.coords(Ro1)
left()
这是我尝试过的方法,但它不起作用,我希望它在知道宝藏在那里并向右移动时将其移除,我不确定该怎么做?
if Treasure1 == Tr1:
canvas.delete(Tr1)
for r in range(1,50):
x1,y1,x2,y2 = canvas.coords(Ro1)
right()
window.mainloop()
没有理由创建两个方块然后在几行之后删除一个。只需创建您想要的任何一个。此外,您还必须以某种方式跟踪坐标,或者在每次移动时获取它们。以下代码有效,但可能不是您想要的。我无法从 post 中分辨出什么是 "move right"。希望这能让您朝着正确的方向开始。另外,看看 move 而不是 coords http://effbot.org/tkinterbook/canvas.htm 并考虑在进一步使用 GUI 编程之前学习如何使用 类。
from Tkinter import *
import random
def right():
canvas.coords(Ro1, ctr_list[1], ctr_list[2],
ctr_list[1]+10, ctr_list[2]+10)
## canvas.update()
ctr_list[0] += 1
ctr_list[1] += 10
ctr_list[2] += 10
print ctr_list
if ctr_list[0] < 12:
window.after(500, right)
## time.sleep(0.1)
window = Tk()
canvas = Canvas(window, width = 1200, height = 800, bg = 'yellow')
canvas.pack()
canvas.pack(padx = 10, pady = 10)
Button(window, text="Quit", command=window.quit).pack(side="bottom")
a1 = 120
a2 = 120
a3 = 60
a4 = 60
Lm1 = canvas.create_rectangle(a1, a2, a3, a4, fill = 'green')
b1 = 530
b2 = 530
b3 = 600
b4 = 600
Lm2 = canvas.create_rectangle(b1, b2, b3, b4, fill = 'green')
##Tr1 = canvas.create_rectangle(85,85,85+10,85+10, fill = 'blue')
##Tr2 = canvas.create_rectangle(560,560,560+10,560+10, fill = 'blue')
list1 = ["Tr1", "Tr2"]
Treasure1 = random.choice(list1)
if Treasure1 == "Tr1":
print "TR1"
ctr_list = [0, a1-30, a3+30]
else:
print "Tr2"
ctr_list = [0, b1+30, b3-30]
Tr = canvas.create_rectangle(ctr_list[1], ctr_list[2],
ctr_list[1]+10, ctr_list[2]+10, fill="blue")
Ro1 = canvas.create_rectangle(20, 20, 20+10, 20+10)
right()
window.mainloop()