【Python】2048——初学者也能写的小游戏
2048这个游戏相信大家都有玩过吧,光玩有什么意思,咱们要用代码写出来才有意思~
来 ,开搞!
先看看效果
直接上代码
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 = temp[2] 170 game = cover_up(game)[0] 171 return (game, done, scores) 172 173 174 def right(game, scores): 175 print("right") 176 # return matrix after shifting right 177 game = reverse(game) 178 game, done = cover_up(game) 179 temp = merge(game, scores) 180 game = temp[0] 181 done = done or temp[1] 182 scores = temp[2] 183 game = cover_up(game)[0] 184 game = reverse(game) 185 return (game, done, scores) 186 187 188 class GameGrid(Frame): 189 def __init__(self): 190 Frame.__init__(self) 191 192 self.grid() 193 self.master.title('2048') 194 self.master.bind("<Key>", self.key_down) 195 self.scores = 0 196 # self.gamelogic = gamelogic 197 self.commands = {KEY_UP: up, KEY_DOWN: down, 198 KEY_LEFT: left, KEY_RIGHT: right, 199 KEY_UP_ALT: up, KEY_DOWN_ALT: down, 200 KEY_LEFT_ALT: left, KEY_RIGHT_ALT: right, 201 KEY_H: left, KEY_L: right, 202 KEY_K: up, KEY_J: down} 203 204 self.grid_cells = [] 205 self.init_grid() 206 self.init_matrix() 207 self.update_grid_cells() 208 209 self.mainloop() 210 211 def init_grid(self): 212 background = Frame(self, bg=BACKGROUND_COLOR_GAME, 213 width=SIZE, height=SIZE) 214 background.grid() 215 216 for i in range(GRID_LEN): 217 grid_row = [] 218 for j in range(GRID_LEN): 219 cell = Frame(background, bg=BACKGROUND_COLOR_CELL_EMPTY, 220 width=SIZE / GRID_LEN, 221 height=SIZE / GRID_LEN) 222 cell.grid(row=i, column=j, padx=GRID_PADDING, 223 pady=GRID_PADDING) 224 t = Label(master=cell, text="", 225 bg=BACKGROUND_COLOR_CELL_EMPTY, 226 justify=CENTER, font=FONT, width=5, height=2) 227 t.grid() 228 grid_row.append(t) 229 230 self.grid_cells.append(grid_row) 231 232 def gen(self): 233 return random.randint(0, GRID_LEN - 1) 234 235 def init_matrix(self): 236 self.matrix = new_game(4) 237 self.history_matrixs = list() 238 self.matrix = add_two(self.matrix) 239 self.matrix = add_two(self.matrix) 240 241 def update_grid_cells(self): 242 for i in range(GRID_LEN): 243 for j in range(GRID_LEN): 244 new_number = self.matrix[i][j] 245 if new_number == 0: 246 self.grid_cells[i][j].configure( 247 text="", bg=BACKGROUND_COLOR_CELL_EMPTY) 248 else: 249 self.grid_cells[i][j].configure(text=str( 250 new_number), bg=BACKGROUND_COLOR_DICT[new_number], 251 fg=CELL_COLOR_DICT[new_number]) 252 self.update_idletasks() 253 254 def key_down(self, event): 255 key = repr(event.char) 256 if key == KEY_BACK and len(self.history_matrixs) > 1: 257 self.matrix = self.history_matrixs.pop() 258 self.update_grid_cells() 259 print('back on step total step:', len(self.history_matrixs)) 260 elif key in self.commands: 261 self.matrix, done,self.scores = self.commands[repr(event.char)](self.matrix,self.scores) 262 if done: 263 self.matrix = add_two(self.matrix) 264 # record last move 265 self.history_matrixs.append(self.matrix) 266 self.update_grid_cells() 267 done = False 268 if game_state(self.matrix) == 'win': 269 self.grid_cells[1][1].configure( 270 text="You", bg=BACKGROUND_COLOR_CELL_EMPTY) 271 self.grid_cells[1][2].configure( 272 text="Win!", bg=BACKGROUND_COLOR_CELL_EMPTY) 273 self.grid_cells[2][1].configure( 274 text="scores", bg=BACKGROUND_COLOR_CELL_EMPTY) 275 self.grid_cells[2][2].configure( 276 text=self.scores, bg=BACKGROUND_COLOR_CELL_EMPTY) 277 if game_state(self.matrix) == 'lose': 278 self.grid_cells[1][1].configure( 279 text="You", bg=BACKGROUND_COLOR_CELL_EMPTY) 280 self.grid_cells[1][2].configure( 281 text="Lose!", bg=BACKGROUND_COLOR_CELL_EMPTY) 282 self.grid_cells[2][1].configure( 283 text="scores", bg=BACKGROUND_COLOR_CELL_EMPTY) 284 self.grid_cells[2][2].configure( 285 text=self.scores, bg=BACKGROUND_COLOR_CELL_EMPTY) 286 287 def generate_next(self): 288 index = (self.gen(), self.gen()) 289 while self.matrix[index[0]][index[1]] != 0: 290 index = (self.gen(), self.gen()) 291 self.matrix[index[0]][index[1]] = 2 292 self.scores += 1 293 294 295 296 297 gamegrid = GameGrid()
# 兄弟们学习python,有时候不知道怎么学,从哪里开始学。掌握了基本的一些语法或者做了两个案例后,不知道下一步怎么走,不知道如何去学习更加高深的知识。 # 那么对于这些大兄弟们,我准备了大量的免费视频教程,PDF电子书籍,以及视频源的源代码! # 还会有大佬解答! # 都在这个群里了 924040232 # 欢迎加入,一起讨论 一起学习!