简体   繁体   中英

Tkinter in python - object detection (not collision)

Quite new to programming and trying to get my moving square to detect a treasure which randomly spawns on one of two boxes

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

This is what I tried but it doesn't work, I want it to remove the treasure if it knows it's there and move right, I'm not sure how else to do it?

if Treasure1 == Tr1:
    canvas.delete(Tr1)
    for r in range(1,50):
        x1,y1,x2,y2 = canvas.coords(Ro1)
        right()


window.mainloop()

There is no reason to create two squares and then delete one a few lines later. Just create whichever one you want. Also you have to keep track of the coordinates in some way, or get them each time you move. The following code works but is probably not what you want. I can not tell from the post what is supposed to "move right". Hopefully this gets you started in the right direction. Also, take a look at move instead of coords http://effbot.org/tkinterbook/canvas.htm and consider learning how to use classes before going any further with GUI programming.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM