import tkinter as tk import random def create_num(): return 2 if random.randint(0, 6) else 4 def init(n): for _ in range(n): while True: x, y = random.randint(0, 3), random.randint(0, 3) if map[x][y] == 0: map[x][y] = create_num() break def get_empty_num(): return sum(row.count(0) for row in map) def game_over_flag(): new_map = [row[:] for row in map] if move_down() == -1 and move_left() == -1 and move_up() == -1 and move_right() == -1: return -1 else: return 0 def print_board(): for i in range(4): for j in range(4): value = map[i][j] text = str(value) if value > 0 else "" label = tk.Label(root, text=text, width=5, height=2, relief="solid", font=("Helvetica", 16), padx=10, pady=10) if value == 0: label.configure(bg="#cdc1b4") # Empty cell color else: label.configure(bg=get_tile_color(value), fg=get_text_color(value)) label.grid(row=i, column=j) # Display the score score_label.config(text=f"Score: {score}") def get_tile_color(value): colors = { 2: "#eee4da", 4: "#ede0c8", 8: "#f2b179", 16: "#f59563", 32: "#f67c5f", 64: "#f65e3b", 128: "#edcf72", 256: "#edcc61", 512: "#edc850", 1024: "#edc53f", 2048: "#edc22e", } return colors.get(value, "#ff0000") def get_text_color(value): return "#776e65" if value < 16 else "#f9f6f2" def move_up(): global score n = 1 move_flag = -1 while n < 4: for i in range(1, 4): for j in range(4): if map[i - 1][j] == 0: move_flag = 0 map[i - 1][j] = map[i][j] map[i][j] = 0 if map[i - 1][j] == map[i][j]: move_flag = 0 map[i - 1][j] = 2 * map[i][j] score += map[i][j] * 10 map[i][j] = 0 n += 1 init(1) if move_flag == -1: return -1 else: return 0 # Similar functions for move_down, move_right, and move_left # Function to move the tiles down def move_down(): global score n = 1 move_flag = -1 while n < 4: for i in range(2, -1, -1): for j in range(4): if map[i + 1][j] == 0: move_flag = 0 map[i + 1][j] = map[i][j] map[i][j] = 0 if map[i + 1][j] == map[i][j]: move_flag = 0 map[i + 1][j] = 2 * map[i][j] score += map[i][j] * 10 map[i][j] = 0 n += 1 init(1) if move_flag != -1: return 0 else: return -1 # Function to move the tiles right def move_right(): global score n = 1 move_flag = -1 while n < 4: for i in range(4): for j in range(2, -1, -1): if map[i][j + 1] == 0: move_flag = 0 map[i][j + 1] = map[i][j] map[i][j] = 0 if map[i][j + 1] == map[i][j]: move_flag = 0 map[i][j + 1] = 2 * map[i][j] score += map[i][j] * 10 map[i][j] = 0 n += 1 init(1) if move_flag != -1: return 0 else: return -1 # Function to move the tiles left def move_left(): global score n = 1 move_flag = -1 while n < 4: for i in range(4): for j in range(1, 4): if map[i][j - 1] == 0: move_flag = 0 map[i][j - 1] = map[i][j] map[i][j] = 0 if map[i][j - 1] == map[i][j]: move_flag = 0 map[i][j - 1] = 2 * map[i][j] score += map[i][j] * 10 map[i][j] = 0 n += 1 init(1) if move_flag != -1: return 0 else: return -1 def restart_game(): global score global map score = 0 map = [[0] * 4 for _ in range(4)] init(2) score_label.config(text=f"Score: {score}") print_board() def move_key(event): key = event.keysym if key == 'w': move_up() elif key == 'd': move_right() elif key == 's': move_down() elif key == 'a': move_left() print_board() root = tk.Tk() root.title("2048 Game") map = [[0] * 4 for _ in range(4)] score = 0 init(2) # Create a label to display the score score_label = tk.Label(root, text=f"Score: {score}", font=("Helvetica", 16)) score_label.grid(row=4, columnspan=4) restart_button = tk.Button(root, text="Restart", command=restart_game) restart_button.grid(row=5, columnspan=4) print_board() root.bind("<Key>", move_key) root.mainloop()
2048Python实现(带GUI界面)
最新推荐文章于 2024-05-06 22:21:23 发布