引言
2048 是一个非常受欢迎的数字拼图游戏,玩家通过移动方块使相同的数字合并,目标是得到 2048 或更高的数字。这个游戏简单而有趣,适合用来练习编程技能。
代码实现
1import random
2import curses
3
4def get_empty_board():
5 return [[0] * 4 for _ in range(4)]
6
7def add_new_tile(board):
8 empty_tiles = [(i, j) for i in range(4) for j in range(4) if board[i][j] == 0]
9 if not empty_tiles:
10 return False
11 i, j = random.choice(empty_tiles)
12 board[i][j] = 2
13 return True
14
15def move_left(board):
16 new_board = [row[:] for row in board]
17 for i in range(4):
18 row = new_board[i]
19 merged = [False] * 4
20 for j in range(1, 4):
21 if row[j] != 0:
22 k = j
23 while k > 0 and (row[k - 1] == 0 or (row[k - 1] == row[k] and not merged[k - 1])):
24 if row[k - 1] == 0:
25 row[k - 1], row[k] = row[k], row[k - 1]
26 elif row[k - 1] == row[k]:
27 row[k - 1] *= 2
28 row[k] = 0
29 merged[k - 1] = True
30 k -= 1
31 return new_board
32
33def move_right(board):
34 new_board = [row[:] for row in board]
35 for i in range(4):
36 row = new_board[i]
37 merged = [False] * 4
38 for j in range(2, -1, -1):
39 if row[j] != 0:
40 k = j
41 while k < 3 and (row[k + 1] == 0 or (row[k + 1] == row[k] and not merged[k + 1])):
42 if row[k + 1] == 0:
43 row[k + 1], row[k] = row[k], row[k + 1]
44 elif row[k + 1] == row[k]:
45 row[k + 1] *= 2
46 row[k] = 0
47 merged[k + 1] = True
48 k += 1
49 return new_board
50
51def move_up(board):
52 new_board = [row[:] for row in board]
53 for j in range(4):
54 col = [new_board[i][j] for i in range(4)]
55 merged = [False] * 4
56 for i in range(1, 4):
57 if col[i] != 0:
58 k = i
59 while k > 0 and (col[k - 1] == 0 or (col[k - 1] == col[k] and not merged[k - 1])):
60 if col[k - 1] == 0:
61 col[k - 1], col[k] = col[k], col[k - 1]
62 elif col[k - 1] == col[k]:
63 col[k - 1] *= 2
64 col[k] = 0
65 merged[k - 1] = True
66 k -= 1
67 for i in range(4):
68 new_board[i][j] = col[i]
69 return new_board
70
71def move_down(board):
72 new_board = [row[:] for row in board]
73 for j in range(4):
74 col = [new_board[i][j] for i in range(4)]
75 merged = [False] * 4
76 for i in range(2, -1, -1):
77 if col[i] != 0:
78 k = i
79 while k < 3 and (col[k + 1] == 0 or (col[k + 1] == col[k] and not merged[k + 1])):
80 if col[k + 1] == 0:
81 col[k + 1], col[k] = col[k], col[k + 1]
82 elif col[k + 1] == col[k]:
83 col[k + 1] *= 2
84 col[k] = 0
85 merged[k + 1] = True
86 k += 1
87 for i in range(4):
88 new_board[i][j] = col[i]
89 return new_board
90
91def is_game_over(board):
92 for i in range(4):
93 for j in range(4):
94 if board[i][j] == 0:
95 return False
96 if i < 3 and board[i][j] == board[i + 1][j]:
97 return False
98 if j < 3 and board[i][j] == board[i][j + 1]:
99 return False
100 return True
101
102def draw_board(stdscr, board):
103 stdscr.clear()
104 for i in range(4):
105 for j in range(4):
106 if board[i][j] == 0:
107 stdscr.addstr(i, j * 5, ' ')
108 else:
109 stdscr.addstr(i, j * 5, f'{board[i][j]:3}')
110 stdscr.refresh()
111
112def main(stdscr):
113 curses.curs_set(0)
114 board = get_empty_board()
115 add_new_tile(board)
116 add_new_tile(board)
117
118 while True:
119 draw_board(stdscr, board)
120 if is_game_over(board):
121 stdscr.addstr(5, 0, "Game Over! Press any key to exit.")
122 stdscr.getch()
123 break
124
125 key = stdscr.getch()
126 if key == ord('q'):
127 break
128 elif key == curses.KEY_LEFT:
129 board = move_left(board)
130 elif key == curses.KEY_RIGHT:
131 board = move_right(board)
132 elif key == curses.KEY_UP:
133 board = move_up(board)
134 elif key == curses.KEY_DOWN:
135 board = move_down(board)
136
137 if add_new_tile(board):
138 continue
139 else:
140 break
141
142 curses.endwin()
143
144if __name__ == "__main__":
145 curses.wrapper(main)
运行说明
- 安装依赖:确保你的系统上安装了
curses
库。大多数 Linux 和 macOS 系统默认已安装此库。如果你使用的是 Windows,可以考虑使用windows-curses
库。 - 保存代码:将上述代码保存为一个 Python 文件,例如
2048.py
。 - 运行游戏:在终端或命令提示符中运行
python 2048.py
。
控制方式
- 左箭头键:向左移动方块
- 右箭头键:向右移动方块
- 上箭头键:向上移动方块
- 下箭头键:向下移动方块
- q 键:退出游戏
总结
这个简单的 2048 游戏实现展示了如何使用 Python 和 curses
库来创建一个基本的命令行游戏。你可以在此基础上进行扩展,添加更多的功能和优化,例如分数显示、更复杂的动画效果等。希望你喜欢这个小游戏!