美文网首页
2048的Python实现

2048的Python实现

作者: Louis_Duan | 来源:发表于2018-08-20 22:37 被阅读0次

    https://github.com/yangshun/2048-python

    主程序puzzle.py

    from tkinter import *
    from logic import *
    from random import *
    

    [import]https://blog.csdn.net/appleheshuang/article/details/7602499
    [_ init _.py]https://www.cnblogs.com/Lands-ljk/p/5880483.html
    [sys.path]https://blog.csdn.net/wangjianno2/article/details/48783127
    [TK接口]https://www.cnblogs.com/blitheG/p/7844983.html
    [Frame]https://blog.csdn.net/mengxianhua/article/details/40536429

    SIZE = 500
    GRID_LEN = 4
    GRID_PADDING = 10
    
    BACKGROUND_COLOR_GAME = "#92877d"
    BACKGROUND_COLOR_CELL_EMPTY = "#9e948a"
    BACKGROUND_COLOR_DICT = {   2:"#eee4da", 4:"#ede0c8", 8:"#f2b179", 16:"#f59563", \
                                32:"#f67c5f", 64:"#f65e3b", 128:"#edcf72", 256:"#edcc61", \
                                512:"#edc850", 1024:"#edc53f", 2048:"#edc22e" }
    CELL_COLOR_DICT = { 2:"#776e65", 4:"#776e65", 8:"#f9f6f2", 16:"#f9f6f2", \
                        32:"#f9f6f2", 64:"#f9f6f2", 128:"#f9f6f2", 256:"#f9f6f2", \
                        512:"#f9f6f2", 1024:"#f9f6f2", 2048:"#f9f6f2" }
    FONT = ("Verdana", 40, "bold")
    
    KEY_UP_ALT = "\'\\uf700\'"
    KEY_DOWN_ALT = "\'\\uf701\'"
    KEY_LEFT_ALT = "\'\\uf702\'"
    KEY_RIGHT_ALT = "\'\\uf703\'"
    
    KEY_UP = "'w'"
    KEY_DOWN = "'s'"
    KEY_LEFT = "'a'"
    KEY_RIGHT = "'d'"
    
    class GameGrid(Frame):##Frame就是屏幕上的一块矩形区域,多是用来作为容器(container)来布局窗体。
        def __init__(self):
            Frame.__init__(self)
    
            self.grid()#显示窗口并使用grid布局。grid方法是从Frame继承来的。Tkinter中有一个概念叫布局(layout),就是控件的排列方式。除了grid(),还有pack等布局。
            self.master.title('2048')#将窗口的标题设置为‘2048’
            self.master.bind("<Key>", self.key_down)
    
            #self.gamelogic = gamelogic
            self.commands = {   KEY_UP: up, KEY_DOWN: down, KEY_LEFT: left, KEY_RIGHT: right,
                                KEY_UP_ALT: up, KEY_DOWN_ALT: down, KEY_LEFT_ALT: left, KEY_RIGHT_ALT: right }
    
            self.grid_cells = []
            self.init_grid()
            self.init_matrix()
            self.update_grid_cells()
            
            self.mainloop()
    
        def init_grid(self):
            background = Frame(self, bg=BACKGROUND_COLOR_GAME, width=SIZE, height=SIZE)
            background.grid()
            for i in range(GRID_LEN):
                grid_row = []
                for j in range(GRID_LEN):
                    cell = Frame(background, bg=BACKGROUND_COLOR_CELL_EMPTY, width=SIZE/GRID_LEN, height=SIZE/GRID_LEN)
                    cell.grid(row=i, column=j, padx=GRID_PADDING, pady=GRID_PADDING)
                    # font = Font(size=FONT_SIZE, family=FONT_FAMILY, weight=FONT_WEIGHT)
                    t = Label(master=cell, text="", bg=BACKGROUND_COLOR_CELL_EMPTY, justify=CENTER, font=FONT, width=4, height=2)
                    t.grid()
                    grid_row.append(t)
    
                self.grid_cells.append(grid_row)
    
        def gen(self):
            return randint(0, GRID_LEN - 1)
    
        def init_matrix(self):
            self.matrix = new_game(4)
    
            self.matrix=add_two(self.matrix)
            self.matrix=add_two(self.matrix)
    
        def update_grid_cells(self):
            for i in range(GRID_LEN):
                for j in range(GRID_LEN):
                    new_number = self.matrix[i][j]
                    if new_number == 0:
                        self.grid_cells[i][j].configure(text="", bg=BACKGROUND_COLOR_CELL_EMPTY)
                    else:
                        self.grid_cells[i][j].configure(text=str(new_number), bg=BACKGROUND_COLOR_DICT[new_number], fg=CELL_COLOR_DICT[new_number])
            self.update_idletasks()
            
        def key_down(self, event):
            key = repr(event.char)
            if key in self.commands:
                self.matrix,done = self.commands[repr(event.char)](self.matrix)
                if done:
                    self.matrix = add_two(self.matrix)
                    self.update_grid_cells()
                    done=False
                    if game_state(self.matrix)=='win':
                        self.grid_cells[1][1].configure(text="You",bg=BACKGROUND_COLOR_CELL_EMPTY)
                        self.grid_cells[1][2].configure(text="Win!",bg=BACKGROUND_COLOR_CELL_EMPTY)
                    if game_state(self.matrix)=='lose':
                        self.grid_cells[1][1].configure(text="You",bg=BACKGROUND_COLOR_CELL_EMPTY)
                        self.grid_cells[1][2].configure(text="Lose!",bg=BACKGROUND_COLOR_CELL_EMPTY)
    
    
        def generate_next(self):
            index = (self.gen(), self.gen())
            while self.matrix[index[0]][index[1]] != 0:
                index = (self.gen(), self.gen())
            self.matrix[index[0]][index[1]] = 2
    
    gamegrid = GameGrid()
    

    相关文章

      网友评论

          本文标题:2048的Python实现

          本文链接:https://www.haomeiwen.com/subject/igyriftx.html