import tkinter
import random

cursor_x = 0
cursor_y = 0
mouse_x = 0
mouse_y = 0
mouse_c = 0

def mouse_move(e):
    global mouse_x, mouse_y
    mouse_x = e.x
    mouse_y = e.y

def mouse_press(e):
    global mouse_c
    mouse_c = 1

cat = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
]

def draw_cat():
    for y in range(10):
        for x in range(8):
            if cat[y][x] > 0:
                cvs.create_image(x * 72 + 60, y * 72 + 60, image = img_cat[cat[y][x]], tag = 'CAT')

def drop_cat():
    for y in range(8, -1, -1):
        for x in range(8):
            if cat[y][x] != 0 and cat[y + 1][x] == 0:
                cat[y + 1][x] = cat[y][x]
                cat[y][x] = 0

def game_main():
    global cursor_x, cursor_y, mouse_c
    drop_cat()
    if 24 <= mouse_x and mouse_x < 24 + 72 * 8 and 24 <= mouse_y and mouse_y < 24 + 72 * 10:
        cursor_x = int((mouse_x - 24) / 72)
        cursor_y = int((mouse_y - 24) / 72)
        if mouse_c == 1:
            #mouse_c = 0
            cat[cursor_y][cursor_x] = random.randint(1, 6)
    cvs.delete('CURSOR')
    cvs.create_image(cursor_x * 72 + 60, cursor_y * 72 + 60, image = cursor, tag = 'CURSOR')
    cvs.delete('CAT')
    draw_cat()
    root.after(100, game_main)

root = tkinter.Tk()
root.title('點選後配置貓咪')
root.resizable(False, False)
root.bind('<Motion>', mouse_move)
root.bind('<ButtonPress>', mouse_press)
cvs = tkinter.Canvas(root, width = 912, height = 768)
cvs.pack()

bg = tkinter.PhotoImage(file = 'images/cat_bg.png')
cursor = tkinter.PhotoImage(file = 'images/cat_cursor.png')

img_cat = [
    None,
    tkinter.PhotoImage(file = 'images/cat1.png'),
    tkinter.PhotoImage(file = 'images/cat2.png'),
    tkinter.PhotoImage(file = 'images/cat3.png'),
    tkinter.PhotoImage(file = 'images/cat4.png'),
    tkinter.PhotoImage(file = 'images/cat5.png'),
    tkinter.PhotoImage(file = 'images/cat6.png'),
    tkinter.PhotoImage(file= 'images/cat_paw.png')
]

cvs.create_image(456, 384, image = bg)
game_main()
root.mainloop()