import tkinter
import random

index = 0
timer = 0
score = 0
next_cat = 0

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 = []
check = []

for i in range(10):
    cat.append([0, 0, 0, 0, 0, 0, 0, 0])
    check.append([0, 0, 0, 0, 0, 0, 0, 0])

def draw_cat():
    cvs.delete('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 check_cat():
    for y in range(10):
        for x in range(8):
            check[y][x] = cat[y][x]

    for y in range(1, 9):
        for x in range(8):
            if check[y][x] > 0:
                if check[y - 1][x] == check[y][x] and check[y + 1][x] == check[y][x]:
                    cat[y - 1][x] = 7
                    cat[y][x] = 7
                    cat[y + 1][x] = 7

    for y in range(10):
        for x in range(1, 7):
            if check[y][x] > 0:
                if check[y][x - 1] == check[y][x] and check[y][x + 1] == check[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(1, 7):
            if check[y][x] > 0:
                if check[y - 1][x - 1] == check[y][x] and check[y + 1][x + 1] == check[y][x]:
                    cat[y - 1][x - 1] = 7
                    cat[y][x] = 7
                    cat[y + 1][x + 1] = 7
                if check[y + 1][x - 1] == check[y][x] and check[y - 1][x + 1] == check[y][x]:
                    cat[y + 1][x - 1] = 7
                    cat[y][x] = 7
                    cat[y - 1][x + 1] = 7

def sweep_cat():
    num = 0
    for y in range(10):
        for x in range(8):
            if cat[y][x] == 7:
                cat[y][x] = 0
                num = num + 1
    return num

def drop_cat():
    flg = False
    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
                flg = True
    return flg

def over_cat():
    for x in range(8):
        if cat[0][x] > 0:
            return True
    return False

def set_cat():
    for x in range(8):
        cat[0][x] = random.randint(0, 6)

def draw_txt(txt, x, y, siz, col, tg):
    fnt = ('Times New Roman', siz, 'bold')
    cvs.create_text(x + 2, y + 2, text = txt, fill = 'black', font = fnt, tag = tg)
    cvs.create_text(x, y, text = txt, fill = col, font = fnt, tag = tg)

def game_main():
    global index, timer, score, next_cat
    global cursor_x, cursor_y, mouse_c
    if index == 0:	# 標題的標誌
        draw_txt('貓咪貓咪', 312, 240, 100, 'violet', 'TITLE')
        draw_txt('Click to start.', 312, 560, 50, 'orange', 'TITLE')
        index = 1
        mouse_c = 0
    elif index == 1:	# 標題畫面  等待遊戲開始
        if mouse_c == 1:
            for y in range(10):
                for x in range(8):
                    cat[y][x] = 0
            mouse_c = 0
            score = 0
            next_cat = 0
            cursor_x = 0
            cursor_y = 0
            set_cat()
            draw_cat()
            cvs.delete('TITLE')
            index = 2
    elif index == 2:	# 貓咪落下
        if drop_cat() == False:
            index = 3
        draw_cat()
    elif index == 3:	# 是否連成一線
        check_cat()
        draw_cat()
        index = 4
    elif index == 4:	# 消除連成一線的貓咪
        sc = sweep_cat()
        score = score + sc * 10
        if sc > 0:
            index = 2
        else:
            if over_cat() == False:
                next_cat = random.randint(1, 6)
                index = 5
            else:
                index = 6
                timer = 0
        draw_cat()
    elif index == 5:	# 等待玩家滑鼠輸入
        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
                set_cat()
                cat[cursor_y][cursor_x] = next_cat
                next_cat = 0
                index = 2
        cvs.delete('CURSOR')
        cvs.create_image(cursor_x * 72 + 60, cursor_y * 72 + 60, image = cursor, tag = 'CURSOR')
        draw_cat()
    elif index == 6:	# 遊戲結束
        timer = timer + 1
        if timer == 1:
            draw_txt('GAME OVER', 312, 348, 60, 'red', 'OVER')
        if timer == 50:
            cvs.delete('OVER')
            index = 0
    cvs.delete('INFO')
    draw_txt('SCORE ' + str(score), 160, 60, 32, 'blue', 'INFO')
    if next_cat > 0:
        cvs.create_image(752, 128, image = img_cat[next_cat], tag = 'INFO')
    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()
