📅  最后修改于: 2023-12-03 15:18:53.313000             🧑  作者: Mango
Python 2048 游戏是基于 2048 游戏的 Python 版本实现。该游戏通过键盘控制方向键上下左右,合并相同数字进而得到更高的数字。游戏目标是通过操作最终使得得到一个 2048 的方块。
import random
import curses # 用于控制终端窗口
# 初始化棋盘
def initialize():
# 生成一个空的棋盘
board = [[0 for i in range(4)] for j in range(4)]
# 随机生成两个数字
init_nums(board)
init_nums(board)
return board
# 绘制棋盘
def draw(board):
for i in range(4):
for j in range(4):
# 绘制数字
stdscr.addstr(i*2+1, j*6+3, str(board[i][j]))
stdscr.refresh()
# 处理用户操作
def handle_user_input(board):
# 获取用户键盘事件
action = stdscr.getch()
# 处理方向键事件
if action == curses.KEY_UP:
move_up(board)
elif action == curses.KEY_DOWN:
move_down(board)
elif action == curses.KEY_LEFT:
move_left(board)
elif action == curses.KEY_RIGHT:
move_right(board)
# 合并相同数值的数字,返回操作是否成功
def merge(board, i, j, di, dj):
if board[i][j] == board[i + di][j + dj]:
board[i][j] *= 2
board[i + di][j + dj] = 0
return True
return False
# 移动棋盘
def move_up(board):
moved = False
for j in range(4):
for i in range(1, 4):
if board[i][j] == 0:
continue
for k in range(i, 0, -1):
if board[k - 1][j] == 0:
board[k - 1][j], board[k][j] = board[k][j], board[k - 1][j]
moved = True
else:
if merge(board, k - 1, j, 1, 0):
moved = True
break
return moved
# 同左边
def move_left(board):
moved = False
for i in range(4):
for j in range(1, 4):
if board[i][j] == 0:
continue
for k in range(j, 0, -1):
if board[i][k - 1] == 0:
board[i][k - 1], board[i][k] = board[i][k], board[i][k - 1]
moved = True
else:
if merge(board, i, k - 1, 0, 1):
moved = True
break
return moved
# 同左边
def move_right(board):
moved = False
for i in range(4):
for j in range(2, -1, -1):
if board[i][j] == 0:
continue
for k in range(j, 3):
if board[i][k + 1] == 0:
board[i][k + 1], board[i][k] = board[i][k], board[i][k + 1]
moved = True
else:
if merge(board, i, k + 1, 0, -1):
moved = True
break
return moved
# 同左边
def move_down(board):
moved = False
for j in range(4):
for i in range(2, -1, -1):
if board[i][j] == 0:
continue
for k in range(i, 3):
if board[k + 1][j] == 0:
board[k + 1][j], board[k][j] = board[k][j], board[k + 1][j]
moved = True
else:
if merge(board, k + 1, j, -1, 0):
moved = True
break
return moved
# 随机生成一个数字
def init_nums(board):
i, j = random.randrange(0, 4), random.randrange(0, 4)
while board[i][j] != 0:
i, j = random.randrange(0, 4), random.randrange(0, 4)
board[i][j] = 2 if random.random() < 0.9 else 4
# 返回所有数字之和
def get_score(board):
return sum(map(sum, board))
# 判断游戏是否结束
def game_over(board):
for i in range(4):
for j in range(4):
if board[i][j] == 0:
return False
if i < 3 and board[i][j] == board[i+1][j]:
return False
if j < 3 and board[i][j] == board[i][j+1]:
return False
return True
# 运行游戏
def main(stdscr):
# 初始化
board = initialize()
# 游戏循环
while True:
# 绘制界面
draw(board)
# 处理用户输入
handle_user_input(board)
# 如果已经无法移动,游戏结束
if game_over(board):
stdscr.addstr(9, 3, "game over!")
stdscr.refresh()
break
# 随机生成新数字
init_nums(board)
Python 2048 游戏是一个简单有趣的小游戏,游戏的代码实现也非常简单,同时对 Python 的语法和库函数的应用进行了练习。如果你也想玩玩 2048,可以试试本地运行这个 Python 版本的游戏,也可以在网页上找到类似的在线游戏。