from tkinter import Tk,Canvas,PhotoImage,NW,Label,Button
from random import randint,randrange
w=Tk()
canv=Canvas(w,width=830,height=830,bg='black')
w.title('поймай призраков')
w.resizable(False,False)
h=0
t = 0

def ddd():
    global h
    lab['text']=f'очки: {h} , собрано коробок с очками: {t}'
lab = Button(w, text=f'очки: {h} , собрано коробок с очками: {t}',command=ddd,
             bg='black',activebackground='SlateBlue4',
             fg='white',width=118, height=0)
lab.pack()
canv.pack()
image=PhotoImage(file='brick.png')
image1=PhotoImage(file='ground.png')
image2=PhotoImage(file='water.png')
image3=PhotoImage(file='wall.png')
W=2
B=1
G=0
WA=3
imeges=[image,image1,image2,image3]

map1=[[G,G,G,G,G,G,G,G,G,G,G,G,G],
     [G,B,B,B,B,B,B,B,B,B,B,B,G],
     [G,B,B,G,B,W,B,B,B,B,B,B,G],
     [G,B,B,B,B,B,B,WA,WA,WA,B,B,G],
     [G,B,B,B,B,B,B,WA,WA,WA,B,B,G],
     [G,B,B,WA,WA,B,B,WA,WA,WA,B,B,G],
     [G,B,B,B,B,B,B,B,B,W,B,B,G],
     [G,B,B,WA,WA,WA,B,B,B,B,B,B,G],
     [G,B,B,WA,WA,WA,B,B,B,B,B,B,G],
     [G,B,B,WA,WA,WA,B,B,B,G,G,B,G],
     [G,B,B,B,B,B,B,B,B,G,G,B,G],
     [G,B,W,B,B,B,B,B,B,W,B,B,G],
     [G,G,G,G,G,G,G,G,G,G,G,G,G],
     ]

def n_(event):
    if event.keycode == 81 and h>=500:
        canv.delete(id_bot)
def n_1(event):
    if event.keycode == 81 and h>=1000:
        canv.delete(id_bot1)
def n_2(event):
    if event.keycode == 81 and h>=2000:
        canv.delete(id_bot2)

for i1 in range(0,len(map1)):
    for j1 in range(0,len(map1[i1])):
        block=map1[i1][j1]
        canv.create_image(i1*64,j1*64,anchor=NW,image=imeges[block])

def on_key_press(event):
    if event.keycode == 27:
        exit()

    move_press2(event)
    move_press1(event)
    move_press(event)
    n_2(event)
    n_1(event)
    n_(event)
    move_up(event)
    move_down(event)
    move_left(event)
    move_right(event)

y11=10
def move_down(event):
    global h
    global y11
    if event.keycode == 83 and y11>-325 and ((y11+15<0 or y11+15>0) or (x11+190<0 or x11-10>0))\
    and ((y11-235<0 or y11-235>0) or (x11-60<0 or x11-260>0))\
    and ((y11+140<0 or y11+140>0) or (x11-185<0 or x11-335>0))\
    and ((y11 -235 < 0 or y11 -235 > 0) or (x11 +65< 0 or x11 -10 > 0))\
    and ((y11 -235 < 0 or y11 -235 > 0) or (x11 +265<0 or x11 +190 > 0)):

        h+=1
        y11-=25
        canv.move(id, 0, 25)
        print("move down",y11)
def move_up(event):
    global h
    global y11
    if event.keycode == 87 and y11<300  and ((y11+265<0 or y11+265>0) or (x11+190<0 or x11-10>0))\
    and ((y11+15<0 or y11+15>0) or (x11-60<0 or x11-260>0)) \
    and ((y11 + 340 < 0 or y11 + 340 > 0) or (x11 - 185 < 0 or x11 - 335 > 0))\
    and ((y11 -60 < 0 or y11 -60 > 0) or (x11 +65 < 0 or x11 -10 > 0))\
    and ((y11 -110 < 0 or y11 -110 > 0) or (x11 +265< 0 or x11 +190 > 0)):
        h += 1
        y11+=25
        canv.move(id, 0,-25)
        print("move up",h,y11)

h=0
x11=10

def move_left(event):
    global h
    global x11
    if event.keycode == 65 and x11 > -275 and ((y11+240<0 or y11+40>0) or (x11-35<0 or x11-35>0))\
    and ((y11-10<0 or y11-210>0) or (x11-285<0 or x11-285>0)) \
    and ((y11 - 85 < 0 or y11 - 210 > 0) or (x11 - 35 < 0 or x11 - 35 > 0))\
    and ((y11 +315 < 0 or y11 +165 > 0) or (x11 - 360 < 0 or x11 - 360 > 0))\
    and ((y11 -135 < 0 or y11 -210 > 0) or (x11 +165 < 0 or x11 +165  > 0)):
        h += 1
        x11 -= 25
        canv.move(id,-25,0)
        print("move left", x11)

def move_right(event):
    global h
    global x11
    if event.keycode ==68 and x11 <350 and ((y11+240<0 or y11+40>0) or (x11+215<0 or x11+215>0))\
    and ((y11-10<0 or y11-210>0) or (x11-35<0 or x11-35>0)) \
    and ((y11 - 85 < 0 or y11 - 210 > 0) or (x11+90 < 0 or x11+90 > 0))\
    and ((y11 + 315 < 0 or y11 +165 > 0) or (x11-160 < 0 or x11-160 > 0))\
    and ((y11 -135 < 0 or y11 -210 > 0) or (x11 +290< 0 or x11 +290 > 0)):
        h+=1
        x11+= 25
        canv.move(id, 25, 0)
        print("move right",x11)


gt=['green']
def move_press(event):
    global h
    global y11
    global t
    if event.keycode == 83 and h<500 and not(y11>-325 and ((y11+90<0 or y11+90>0) or (x11+265<0 or x11+215>0)))\
    or event.keycode == 68 and h<500 and not (y11 > -325 and ((x11 + 290 < 0 or x11 + 290 > 0) or (y11 + 190 < 0 or y11 + 115 > 0)))\
    or event.keycode == 87 and h<500 and not (y11 > -325 and ((y11 + 215 < 0 or y11 + 215 > 0) or (x11 + 265 < 0 or x11 + 215 > 0))):
        h+=500
        t+=1
        print(h)

def move_press1(event):
    global h
    global y11
    global t
    if event.keycode == 83 and h > 500 and h < 1000 and not (y11 > -325 and ((y11 - 310 < 0 or y11 - 310 > 0) or (x11 - 185 < 0 or x11 - 260 > 0)))\
    or event.keycode == 68 and h > 500 and h < 1000 and not (y11 > -325 and ((x11 - 160 < 0 or x11 - 160 > 0) or (y11 - 235 < 0 or y11 - 285 > 0)))\
    or event.keycode == 65 and h > 500 and h < 1000 and not (y11 > -325 and ((x11 - 285 < 0 or x11 - 285 > 0) or (y11 - 235 < 0 or y11 - 285 > 0))):
        h += 500
        t += 1
        print(h)

def move_press2(event):
    global h
    global y11
    global t
    if event.keycode == 83 and h > 1000 and h < 2000 and not (y11 > -325 and ((y11 + 90 < 0 or y11 + 90 > 0) or (x11 - 185 < 0 or x11 - 260 > 0)))\
    or event.keycode == 68 and h > 1000 and h < 2000 and not (y11 > -325 and ((x11 - 160 < 0 or x11 - 160 > 0) or (y11 + 140 < 0 or y11 + 115 > 0))) \
    or event.keycode == 65 and h > 1000 and h < 2000 and not (y11 > -325 and ((x11 - 285 < 0 or x11 - 285 > 0) or (y11 + 140 < 0 or y11 +  115 > 0))):
        h += 1000
        t += 1
        print(h)


x22=20
y22=20

id =canv.create_rectangle(37*x11,37*x11,41*y11,41*y11,width=3,fill=gt[0],outline="SlateBlue4" )
id_bot =canv.create_rectangle(37*(x22/2),37*(x22/2),42*(y22/2),42*(y22/2),width=3, fill="white", outline="SlateBlue4" )
l=350
l1=-250

def ai():
    global x22
    global y22
    r = randint(1, 4)

    if r == 1 and x22 < l:
        x22 += 50
        canv.move(id_bot, 50, 0)
        print('x', x22)
    elif r == 3 and x22 > l1:
        x22 -= 50
        canv.move(id_bot, -50, 0)
        print('x,left', x22)


    elif r == 2 and y22 < l:
        y22 += 50
        canv.move(id_bot, 0, 50)
        print('y', y22)
    elif r == 4 and y22 > l1:
        y22 -= 50
        canv.move(id_bot, 0, -50)
        print('y,up', y22)
    w.after(500, ai)

x222=20
y222=20


id_bot1 =canv.create_rectangle(37*(x222/2),37*(x222/2),41*(y222/2),41*(y222/2),width=3, fill="white", outline="SlateBlue4" )

def ai1():
    global x222
    global y222
    r1 = randint(1, 4)

    if r1 == 1 and x222 <l:
        x222+=50
        canv.move(id_bot1,50, 0)
        print('x',x222)
    elif r1 == 3 and x222 >l1:
        x222 -= 50
        canv.move(id_bot1, -50, 0)
        print('x,left',x222)


    elif r1 == 2 and y222<l:
        y222+=50
        canv.move(id_bot1, 0, 50)
        print('y',y222)
    elif r1==4 and y222 >l1:
        y222-=50
        canv.move(id_bot1, 0, -50)
        print('y,up',y222)
    w.after(500, ai1)

x3=20
y3=20

id_bot2 =canv.create_rectangle(37*(x3/2),37*(x3/2),41*(y3/2),41*(y3/2),width=3, fill="white", outline="SlateBlue4" )


def ai2():
    global x3
    global y3
    r2 = randint(1, 4)

    if r2 == 1 and x3 <l:
        x3+=50
        canv.move(id_bot2,50, 0)
        print('x',x3)
    elif r2 == 3 and x3 >l1:
        x3 -= 50
        canv.move(id_bot2, -50, 0)
        print('x,left',x3)


    elif r2 == 2 and y3<l:
        y3+=50
        canv.move(id_bot2, 0, 50)
        print('y',y3)
    elif r2==4 and y3 >l1:
        y3-=50
        canv.move(id_bot2, 0, -50)
        print('y,up',y3)
    w.after(500, ai2)

ai2()
ai1()
ai()
w.bind('<KeyPress>', on_key_press,)
w.mainloop()
