用python代码写2048游戏
时间: 2024-02-18 07:12:07 浏览: 134
200 行python 代码实现 2048 游戏
5星 · 资源好评率100%
好的,以下是一个简单的Python代码示例,可以用来实现2048游戏:
```python
import random
import tkinter as tk
# 创建游戏窗口
root = tk.Tk()
root.title("2048 Game")
# 配置游戏界面
board = tk.Frame(root, bg="gray", width=400, height=400)
board.pack()
# 初始化游戏数据
board_data = [[0] * 4 for i in range(4)]
score = 0
# 根据游戏数据更新界面
def update_board():
for i in range(4):
for j in range(4):
cell_value = board_data[i][j]
cell_text = str(cell_value) if cell_value > 0 else ""
cell_color = "white" if cell_value < 8 else "black"
tk.Label(board, text=cell_text, bg="gray", fg=cell_color, font=("Helvetica", 20), width=4, height=2).grid(row=i, column=j)
# 随机生成一个新方块
def generate_block():
block_value = 2 if random.randint(0, 1) == 0 else 4
block_x, block_y = random.randint(0, 3), random.randint(0, 3)
while board_data[block_x][block_y] != 0:
block_x, block_y = random.randint(0, 3), random.randint(0, 3)
board_data[block_x][block_y] = block_value
# 处理向左移动
def move_left():
global score
for i in range(4):
for j in range(1, 4):
if board_data[i][j] != 0:
for k in range(j, 0, -1):
if board_data[i][k-1] == 0:
board_data[i][k-1], board_data[i][k] = board_data[i][k], board_data[i][k-1]
elif board_data[i][k-1] == board_data[i][k]:
board_data[i][k-1], board_data[i][k] = board_data[i][k-1] * 2, 0
score += board_data[i][k-1]
break
# 处理向右移动
def move_right():
global score
for i in range(4):
for j in range(2, -1, -1):
if board_data[i][j] != 0:
for k in range(j, 3):
if board_data[i][k+1] == 0:
board_data[i][k+1], board_data[i][k] = board_data[i][k], board_data[i][k+1]
elif board_data[i][k+1] == board_data[i][k]:
board_data[i][k+1], board_data[i][k] = board_data[i][k+1] * 2, 0
score += board_data[i][k+1]
break
# 处理向上移动
def move_up():
global score
for j in range(4):
for i in range(1, 4):
if board_data[i][j] != 0:
for k in range(i, 0, -1):
if board_data[k-1][j] == 0:
board_data[k-1][j], board_data[k][j] = board_data[k][j], board_data[k-1][j]
elif board_data[k-1][j] == board_data[k][j]:
board_data[k-1][j], board_data[k][j] = board_data[k-1][j] * 2, 0
score += board_data[k-1][j]
break
# 处理向下移动
def move_down():
global score
for j in range(4):
for i in range(2, -1, -1):
if board_data[i][j] != 0:
for k in range(i, 3):
if board_data[k+1][j] == 0:
board_data[k+1][j], board_data[k][j] = board_data[k][j], board_data[k+1][j]
elif board_data[k+1][j] == board_data[k][j]:
board_data[k+1][j], board_data[k][j] = board_data[k+1][j] * 2, 0
score += board_data[k+1][j]
break
# 处理游戏结束
def game_over():
for i in range(4):
for j in range(4):
if board_data[i][j] == 0:
return False
if i < 3 and board_data[i][j] == board_data[i+1][j]:
return False
if j < 3 and board_data[i][j] == board_data[i][j+1]:
return False
return True
# 处理按键事件
def key_pressed(event):
if game_over():
return
if event.keysym == "Left":
move_left()
elif event.keysym == "Right":
move_right()
elif event.keysym == "Up":
move_up()
elif event.keysym == "Down":
move_down()
else:
return
generate_block()
update_board()
if game_over():
tk.messagebox.showinfo("Game Over", "Your score is: " + str(score))
# 初始化游戏界面和数据
generate_block()
generate_block()
update_board()
# 监听按键事件
root.bind("<Key>", key_pressed)
# 进入主循环
root.mainloop()
```
在上面的代码中,我们使用了Tkinter模块来创建2048游戏窗口,并使用Frame组件来创建游戏界面。我们使用一个二维列表来表示游戏数据,使用Label组件来显示游戏方块,使用bind()方法来监听按键事件。我们还定义了一些处理游戏逻辑的函数,例如生成新方块、移动方块、判断游戏结束等。在主循环中,我们首先初始化游戏界面和数据,然后进入事件监听循环。当游戏结束时,我们使用messagebox组件来提示用户,并显示游戏得分。
阅读全文