python中的Tkinter - 对象检测(不是碰撞)

时间:2015-01-01 19:26:34

标签: python python-2.7 tkinter

非常新的编程并试图让我的移动方块来检测随机在两个盒子中产生的宝藏

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()

1 个答案:

答案 0 :(得分:0)

没有理由创建两个正方形,然后稍后删除一行。只需创建您想要的任何一个。此外,您必须以某种方式跟踪坐标,或在每次移动时获取坐标。以下代码有效,但可能不是您想要的。我无法从帖子中看出应该“向右移动”。希望这能让你开始朝着正确的方向前进。另外,看看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()