Tkinter画布碰撞检测

时间:2016-11-18 18:06:11

标签: python canvas tkinter

我有一些tkinter代码,我试图让碰撞工作但是x碰撞(overlap_e和overlap_w)不起作用,当你和过去的物体在同一个x上时,检测就会消失y是不同的。

from tkinter import *
import time

canvas_height = 500
canvas_width = 600
canvas_colour = 'Grey'

col = 0
p1_x = canvas_width/2
p1_y = canvas_height
p1_colour = 'Red'
line_width = 1
line_length = line_width
colours = ['Red', 'Green', 'Blue', 'Black']

x = 0


def colorc(event):
    global colours
    global col
    global p1_colour
    global x
    global a
    if col < int(len(colours)-1):
        col = col+1
    else:
        col = 0
    p1_colour = colours[col]
    canvas.itemconfig(a, text=p1_colour, fill=p1_colour)
    x = 1

def p1_move_N(event):
    global p1_y
    canvas.create_line(p1_x, p1_y, p1_x, (p1_y-line_length), width=line_width, fill=p1_colour)
    p1_y = p1_y - line_length
    overlap_n()

def p1_move_S(event):
    global p1_y
    canvas.create_line(p1_x, p1_y, p1_x, (p1_y+line_length), width=line_width, fill=p1_colour)
    p1_y = p1_y + line_length
    overlap_s()


def p1_move_E(event):
    global p1_x
    canvas.create_line(p1_x, p1_y, (p1_x+line_length), p1_y, width=line_width, fill=p1_colour)
    p1_x = p1_x + line_length
    overlap_e()

def p1_move_W(event):
    global p1_x
    canvas.create_line(p1_x, p1_y, (p1_x-line_length), p1_y, width=line_width, fill=p1_colour)
    p1_x = p1_x - line_length
    overlap_w()

def overlap_n():
    global p1_x
    global p1_y
    global past_x
    global past_y
    if (p1_x in past_x) and (p1_y in past_y):
        print('Collision Detected')
        print(past_x)
        print(past_y)
        print(p1_x)
        print(p1_y)

    else:
        past_x.append(p1_x)
        past_y.append(p1_y+1)


def overlap_s():
    global p1_x
    global p1_y
    global past_x
    global past_y
    if (p1_x in past_x) and (p1_y in past_y):
        print('Collision Detected')
        print(past_x)
        print(past_y)
        print(p1_x)
        print(p1_y)
    else:
        past_x.append(p1_x)
        past_y.append(p1_y-1)

def overlap_w():
    global p1_x
    global p1_y
    global past_x
    global past_y
    if (p1_x in past_x) and (p1_y in past_y):
        print('Collision Detected')
        print(past_x)
        print(past_y)
        print(p1_x)
        print(p1_y)

    else:
        past_x.append(p1_x+1)
        past_y.append(p1_y)
def overlap_e():
    global p1_x
    global p1_y
    global past_x
    global past_y
    if (p1_x in past_x) and (p1_y in past_y):
        print('Collision Detected')
        print(past_x)
        print(past_y)
        print(p1_x-1)
        print(p1_y)

    else:
        past_x.append(p1_x)
        past_y.append(p1_y)

past_x = list()
past_y = list()

window = Tk()
window.title('Draw')
canvas = Canvas(bg=canvas_colour, height=canvas_height, width=canvas_width)
canvas.pack()

a = canvas.create_text(canvas_width/2, 20, text=p1_colour, fill=p1_colour, font='50')

window.bind('<Up>', p1_move_N)
window.bind('<Down>', p1_move_S)
window.bind('<Right>', p1_move_E)
window.bind('<Left>', p1_move_W)
window.bind('<space>', colorc)


window.mainloop()

0 个答案:

没有答案