from tkinter import*
from random import randint
global a4
a=800
d=800
w=Tk()


g="Я уничтожу тебя! хихихихихихихих"

w.title('агробот')
w.geometry(f'{a}x{d}')
w.resizable(False,False)
label1=Label(w,text='агробот:{}'.format(g),font=('Areal',10))
label1.pack()
canv=Canvas(w,height=a,width=d,bg='black')
canv.pack()





x22=0
y22=0
def ai():
    global x22
    global y22
    r = randint(1, 4)


    if r == 1 and x22 <300:
        x22+=50
        canv.move(id_bot,50, 0)
        print('x',x22)
    elif r == 3 and x22 > -450:
        x22 -= 50
        canv.move(id_bot, -50, 0)
        print('x',x22)


    elif r == 2 and y22<350:
        y22+=50
        canv.move(id_bot, 0, 50)
        print('y',y22)
    elif r==4 and y22 >-350:
        y22-=50
        canv.move(id_bot, 0, -50)
        print('y',y22)
    w.after(1000, ai)




def on_key_press(event):
    if event.keycode == 27:
        exit(0)

    move_up(event)
    move_down(event)
    move_left(event)
    move_right(event)

y11=0
def move_down(event):
    global y11
    if event.keycode == 83 and y11>-400:
        y11-=50
        canv.move(id, 0, 50)
        print("move down",y11)
def move_up(event):
    global y11
    if event.keycode == 87 and y11<300:
        y11+=50
        canv.move(id, 0,-50)
        print("move up",y11)

x11=0
def move_left(event):
    global x11
    if event.keycode == 65 and x11 > -400:
        x11 -= 50
        canv.move(id,-50,0)
        print("move left", x11)

def move_right(event):
    global x11
    if event.keycode ==68 and x11 <350:
        x11+= 50
        canv.move(id, 50, 0)
        print("move right",x11)


id = canv.create_polygon(400,350,400,320,450,320,450,350,  width=3, fill="SlateBlue4", outline="SlateBlue4" )


id_bot = canv.create_polygon(400,400,400,320,500,320,500,400, width=3, fill="red", outline="SlateBlue4" )


ai()
w.bind('<KeyPress>', on_key_press)
w.mainloop()