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 = []

for i in range(10):
    cat.append([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 linein_cat():
    for y in range(10):
        for x in range(1, 7):
            if cat[y][x] > 0:
                if cat[y][x - 1] == cat[y][x] and cat[y][x + 1] == cat[y][x]:
                    cat[y][x - 1] = 7
                    cat[y][x] = 7
                    cat[y][x + 1] = 7

    for y in range(1, 9):
        for x in range(8):
            if cat[y][x] > 0:
                if cat[y - 1][x] == cat[y][x] and cat[y + 1][x] == cat[y][x]:
                    cat[y - 1][x] = 7
                    cat[y][x] = 7
                    cat[y + 1][x] = 7  

def game_main():
    global cursor_x, cursor_y, mouse_c
    if 660 <= mouse_x and mouse_x < 840 and 100 <= mouse_y and mouse_y < 160 and mouse_c == 1:
        mouse_c = 0
        linein_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, 2)

    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)
cvs.create_rectangle(660, 100, 840, 160, fill = 'white')
cvs.create_text(750, 130, text = '測試', fill = 'red', font = ('Times New Roman', 30))
game_main()
root.mainloop()