来 ,开搞!
效果展示
先看看效果
代码展示
直接上代码
1 import random 2 from tkinter import Frame, Label, CENTER 3 4 5 SIZE = 400 6 GRID_LEN = 4 7 GRID_PADDING = 10 8 9 BACKGROUND_COLOR_GAME = "#92877d" 10 BACKGROUND_COLOR_CELL_EMPTY = "#9e948a" 11 12 BACKGROUND_COLOR_DICT = {2: "#eee4da", 4: "#ede0c8", 8: "#f2b179", 13 16: "#f59563", 32: "#f67c5f", 64: "#f65e3b", 14 128: "#edcf72", 256: "#edcc61", 512: "#edc850", 15 1024: "#edc53f", 2048: "#edc22e", 16 17 4096: "#eee4da", 8192: "#edc22e", 16384: "#f2b179", 18 32768: "#f59563", 65536: "#f67c5f", } 19 20 CELL_COLOR_DICT = {2: "#776e65", 4: "#776e65", 8: "#f9f6f2", 16: "#f9f6f2", 21 32: "#f9f6f2", 64: "#f9f6f2", 128: "#f9f6f2", 22 256: "#f9f6f2", 512: "#f9f6f2", 1024: "#f9f6f2", 23 2048: "#f9f6f2", 24 25 4096: "#776e65", 8192: "#f9f6f2", 16384: "#776e65", 26 32768: "#776e65", 65536: "#f9f6f2", } 27 28 FONT = ("Verdana", 40, "bold") 29 30 KEY_UP_ALT = "\'\\uf700\'" 31 KEY_DOWN_ALT = "\'\\uf701\'" 32 KEY_LEFT_ALT = "\'\\uf702\'" 33 KEY_RIGHT_ALT = "\'\\uf703\'" 34 35 KEY_UP = "'w'" 36 KEY_DOWN = "'s'" 37 KEY_LEFT = "'a'" 38 KEY_RIGHT = "'d'" 39 KEY_BACK = "'b'" 40 41 KEY_J = "'j'" 42 KEY_K = "'k'" 43 KEY_L = "'l'" 44 KEY_H = "'h'" 45 46 47 def new_game(n): 48 matrix = [] 49 50 for i in range(n): 51 matrix.append([0] * n) 52 return matrix 53 54 55 def add_two(mat): 56 a = random.randint(0, len(mat)-1) 57 b = random.randint(0, len(mat)-1) 58 while(mat[a][b] != 0): 59 a = random.randint(0, len(mat)-1) 60 b = random.randint(0, len(mat)-1) 61 mat[a][b] = 2 62 return mat 63 64 65 def game_state(mat): 66 for i in range(len(mat)): 67 for j in range(len(mat[0])): 68 if mat[i][j] == 2048: 69 return 'win' 70 for i in range(len(mat)-1): 71 72 for j in range(len(mat[0])-1): 73 if mat[i][j] == mat[i+1][j] or mat[i][j+1] == mat[i][j]: 74 return 'not over' 75 for i in range(len(mat)): 76 for j in range(len(mat[0])): 77 if mat[i][j] == 0: 78 return 'not over' 79 for k in range(len(mat)-1): 80 if mat[len(mat)-1][k] == mat[len(mat)-1][k+1]: 81 return 'not over' 82 for j in range(len(mat)-1): 83 if mat[j][len(mat)-1] == mat[j+1][len(mat)-1]: 84 return 'not over' 85 return 'lose' 86 87 88 89 def reverse(mat): 90 new = [] 91 for i in range(len(mat)): 92 new.append([]) 93 for j in range(len(mat[0])): 94 new[i].append(mat[i][len(mat[0])-j-1]) 95 return new 96 97 98 99 def transpose(mat): 100 new = [] 101 for i in range(len(mat[0])): 102 new.append([]) 103 for j in range(len(mat)): 104 new[i].append(mat[j][i]) 105 return new 106 107 108 109 def cover_up(mat): 110 new = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] 111 done = False 112 for i in range(4): 113 count = 0 114 for j in range(4): 115 if mat[i][j] != 0: 116 new[i][count] = mat[i][j] 117 if j != count: 118 done = True 119 count += 1 120 return (new, done) 121 122 123 def merge(mat,scores): 124 done = False 125 for i in range(4): 126 for j in range(3): 127 if mat[i][j] == mat[i][j+1] and mat[i][j] != 0: 128 mat[i][j] *= 2 129 mat[i][j+1] = 0 130 scores += 1 131 done = True 132 return (mat, done,scores) 133 134 135 def up(game,scores): 136 print("up") 137 # return matrix after shifting up 138 game = transpose(game) 139 game, done = cover_up(game) 140 temp = merge(game,scores) 141 game = temp[0] 142 done = done or temp[1] 143 scores = temp[2] 144 game = cover_up(game)[0] 145 game = transpose(game) 146 return (game, done, scores) 147 148 149 def down(game,scores): 150 print("down") 151 game = reverse(transpose(game)) 152 game, done = cover_up(game) 153 temp = merge(game,scores) 154 game = temp[0] 155 done = done or temp[1] 156 scores = temp[2] 157 game = cover_up(game)[0] 158 game = transpose(reverse(game)) 159 return (game, done, scores) 160 161 162 def left(game,scores): 163 print("left") 164 # return matrix after shifting left 165 game, done = cover_up(game) 166 temp = merge(game,scores) 167 game = temp[0] 168 done = done or temp[1] 169 scores
0 评论