Python GUI 2048的圖形界面實現(三)

       上一篇博文中我們實現了button觸發對應的lebal值,並動態顯示出來。

       這一次我們加入對應的上下左右按鍵觸發相應的計算結果,當檢測到button觸發後沒有可合併的小方塊,也即二維數組沒有變時,對數組更新,隨機在空白處插入一個數字2或者4。

       這裏的move函數跟update函數copy自博友yaolongdeng的一篇博文:http://blog.csdn.net/dengyaolongacmblog/article/details/28123105

       不多說,上代碼:

  

from Tkinter import *
#!/usr/bin/env python  
# coding=utf-8
# -*- coding=utf-8 -*-
import random  
import copy

def set_mtr(mtr):
    mt00.set(mtr[0][0])
    mt01.set(mtr[0][1])
    mt02.set(mtr[0][2])
    mt03.set(mtr[0][3])
    mt10.set(mtr[1][0])
    mt11.set(mtr[1][1])
    mt12.set(mtr[1][2])
    mt13.set(mtr[1][3])
    mt20.set(mtr[2][0])
    mt21.set(mtr[2][1])
    mt22.set(mtr[2][2])
    mt23.set(mtr[2][3])
    mt30.set(mtr[3][0])
    mt31.set(mtr[3][1])
    mt32.set(mtr[3][2])
    mt33.set(mtr[3][3]) 

def move(mtr,dirct):
    score = 0  
    visit = []  
    if dirct == 0:  # left  
        for i in range(4):  
            for j in range(1, 4):  
                for k in range(j,0,-1):  
                    if mtr[i][k - 1] == 0 :  
                        mtr[i][k - 1] = mtr[i][k]  
                        mtr[i][k] = 0  
                    elif mtr[i][k - 1] == mtr[i][k] and 4 * i + k - 1 not in visit and 4 * i + k not in visit:  
                        mtr[i][k - 1] *= 2  
                        mtr[i][k] = 0  
                        score += mtr[i][k - 1]  
                        visit.append(4 * i + k)  
                        visit.append(4 * i + k - 1)  
        # for i in range(4):  
        #    for j in range(3):  
      
    elif dirct == 1:  # down  
        for j in range(4):  
            for i in range(3, 0, -1):  
                for k in range(0,i):  
                    if mtr[k+1][j] == 0:  
                        mtr[k+1][j] = mtr[k][j]  
                        mtr[k][j]=0  
                    elif mtr[k+1][j]==mtr[k][j] and (4 *(k+1)+j) not in visit and (4*k+j) not in visit:  
                        mtr[k+1][j]*=2  
                        mtr[k][j]=0  
                        score=mtr[k+1][j]  
                        visit.append(4*(k)+j)  
                        visit.append(4*(k+1)+j)  
      
      
    elif dirct == 2:  # up  
        for j in range(4):  
            for i in range(1,4):  
                for k in range(i,0,-1):  
                    if mtr[k-1][j]==0:  
                        mtr[k-1][j]=mtr[k][j]  
                        mtr[k][j]=0  
                    elif mtr[k-1][j]==mtr[k][j] and (4 *(k-1)+j) not in visit and (4*k+j) not in visit:  
                        mtr[k-1][j]*=2  
                        mtr[k][j]=0  
                        score += mtr[k-1][j]  
                        visit.append(4*(k)+j)  
                        visit.append(4*(k-1)+j)  
      
    elif dirct == 3:  # right  
        for i in range(4):  
            for j in range(3, 0, -1):  
                for k in range(j):  
                    if mtr[i][k+1]  == 0:  
                        mtr[i][k+1] = mtr[i][k]  
                        mtr[i][k]=0  
                    elif mtr[i][k] ==mtr[i][k+1] and 4 * i + k + 1 not in visit and 4 * i + k not in visit:  
                        mtr[i][k+1]*=2  
                        mtr[i][k]=0  
                        score+=mtr[i][k+1]  
                        visit.append(4*i+k+1)  
                        visit.append(4*i+k)  
      
      
    return score

def update(mtr):  
    ran_pos=[]  
    ran_num=[2,4]  
      
    for i in range(4):  
        for j in range(4):  
            if mtr[i][j]==0:  
                ran_pos.append(4*i+j)  
    if len(ran_pos)>0:  
        k=random.choice(ran_pos)  
        n=random.choice(ran_num)  
        mtr[k/4][k%4]=n
      
    
def click_back():  
    mtr[0][1]=3
    mt01.set(mtr[0][1]) 
def click_quit():  
    mtr[0][2]=3
    mt02.set(mtr[0][2])
def click_up():
    tmp = copy.deepcopy(mtr)
    move(mtr,2)
    if tmp == mtr:   
        update(mtr) #
    set_mtr(mtr)
def click_left():
    tmp = copy.deepcopy(mtr)
    move(mtr,0)
    if tmp == mtr:   
        update(mtr) #  
    set_mtr(mtr)
def click_down():
    tmp = copy.deepcopy(mtr)
    move(mtr,1)
    if tmp == mtr:   
        update(mtr) #
    set_mtr(mtr)
def click_right():
    tmp = copy.deepcopy(mtr)
    move(mtr,3)
    if tmp == mtr:   
        update(mtr) #
    set_mtr(mtr) 
root = Tk()

win = Frame(root, height=600, width=600)
win.grid_propagate(False)
win.grid()


mtr = [[0 for i in range(4)] for j in range(4)]
ran_pos = random.sample(range(16), 2)  
mtr[ran_pos[0]/4][ran_pos[0]%4] = mtr[ran_pos[1]/4][ran_pos[1]%4] = 2

mt00=IntVar()  
mt00.set(mtr[0][0])
mt01=IntVar()  
mt01.set(mtr[0][1])
mt02=IntVar()  
mt02.set(mtr[0][2])
mt03=IntVar()  
mt03.set(mtr[0][3])
mt10=IntVar()  
mt10.set(mtr[1][0])
mt11=IntVar()  
mt11.set(mtr[1][1])
mt12=IntVar()  
mt12.set(mtr[1][2])
mt13=IntVar()  
mt13.set(mtr[1][3])
mt20=IntVar()  
mt20.set(mtr[2][0])
mt21=IntVar()  
mt21.set(mtr[2][1])
mt22=IntVar()  
mt22.set(mtr[2][2])
mt23=IntVar()  
mt23.set(mtr[2][3])
mt30=IntVar()  
mt30.set(mtr[3][0])
mt31=IntVar()  
mt31.set(mtr[3][1])
mt32=IntVar()  
mt32.set(mtr[3][2])
mt33=IntVar()  
mt33.set(mtr[3][3])

helloLabel = Label(win, textvariable=mt00, height=2, width=10)
helloLabel.grid(row=0, column=0)
helloLabe2 = Label(win, textvariable=mt01, height=2, width=10,bg='blue',fg='white')
helloLabe2.grid(row=0, column=1)
helloLabe3 = Label(win, textvariable=mt02, height=2, width=10)
helloLabe3.grid(row=0, column=2)
helloLabe4 = Label(win, textvariable=mt03, height=2, width=10)
helloLabe4.grid(row=0, column=3)
helloLabe5 = Label(win, textvariable=mt10, height=2, width=10,bg='black',fg='white')
helloLabe5.grid(row=1, column=0)
helloLabe6 = Label(win, textvariable=mt11, height=2, width=10)
helloLabe6.grid(row=1, column=1)
helloLabe7 = Label(win, textvariable=mt12, height=2, width=10)
helloLabe7.grid(row=1, column=2)
helloLabe8 = Label(win, textvariable=mt13, height=2, width=10,bg='red')
helloLabe8.grid(row=1, column=3)
helloLabe9 = Label(win, textvariable=mt20, height=2, width=10)
helloLabe9.grid(row=2, column=0)
helloLabe10 = Label(win, textvariable=mt21, height=2, width=10)
helloLabe10.grid(row=2, column=1)
helloLabe11 = Label(win, textvariable=mt22, height=2, width=10,bg='red')
helloLabe11.grid(row=2, column=2)
helloLabe12 = Label(win, textvariable=mt23, height=2, width=10)
helloLabe12.grid(row=2, column=3)
helloLabe13 = Label(win, textvariable=mt30, height=2, width=10)
helloLabe13.grid(row=3, column=0)
helloLabe14 = Label(win, textvariable=mt31, height=2, width=10)
helloLabe14.grid(row=3, column=1)
helloLabe15 = Label(win, textvariable=mt32, height=2, width=10)
helloLabe15.grid(row=3, column=2)
helloLabe16 = Label(win, textvariable=mt33, height=2, width=10,bg='green')
helloLabe16.grid(row=3, column=3)

helloLabel_step = Label(win, text='0', height=2, width=10,bg='red')
helloLabel_step.grid(row=7, column=1)
helloLabel_score = Label(win, text='0', height=2, width=10,bg='red')
helloLabel_score.grid(row=7, column=3)
button_back = Button(win, text='BACK', height=2, width=10,command=click_back)

button_back.grid(row=4, column=0)
button_quit = Button(win, text='QUIT', height=2, width=10,command=click_quit)
button_quit.grid(row=4, column=2)
button_up = Button(win, text='UP', height=2, width=10,command=click_up)
button_up.grid(row=5, column=1)
button_left = Button(win, text='LEFT', height=2, width=10,command=click_left)
button_left.grid(row=6, column=0)
button_down = Button(win, text='DOWN', height=2, width=10,command=click_down)
button_down.grid(row=6, column=1)
button_right = Button(win, text='RIGTH', height=2, width=10,command=click_right)
button_right.grid(row=6, column=2)
_step = Label(win, text='STEP:', height=2, width=10)
_step.grid(row=7, column=0)
_score = Label(win, text='SCORE:', height=2, width=10)
_score.grid(row=7, column=2)
root.mainloop()


發佈了39 篇原創文章 · 獲贊 5 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章