python 2048游戏源码

import random
import curses
# 一开始生成两个方块
STARTUP_TILES = 2
# 随机生成的方块中出现4的概率
FOUR_POSSIBILITY = 0.1
# 游戏板
BOARD = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
# 游戏分数
SCORE = 0
# 是否有2048
HAS_2048 = False
# 是否已经不能动了
IS_STUCK = False
# 是否已经问过是否需要继续游戏了
ASKED_FOR_CONTINUE = False
# 方向
LEFT, RIGHT, UP, DOWN = 0, 1, 2, 3
# 用来代表是否可以结束游戏的常量
CAN_END = True
VECTOR = [[-1, 0], [1, 0], [0, -1], [0, 1]]
SUCCESS = True
FAILED = False
# curses相关
SCREEN = None
def clip(num, lowerbound, upperbound):
if num < lowerbound:
return lowerbound
elif num > upperbound:
return upperbound
else:
return num
def print_score():
global SCREEN
SCREEN.addstr(9, 0, ''.join(['本场游戏结束,得分:', str(SCORE), ' 。']))
def print_prompt(prompt):
global SCREEN
SCREEN.addstr(10, 0, prompt)
def get_user_input(prompt, requested_input):
global SCREEN
error_prompt_str = ''.join(
['请输入', ','.join([str(x) for x in requested_input]), '的其中之一 。'])
print_prompt(prompt)
user_input = SCREEN.getkey()
while user_input not in requested_input:
print_prompt(error_prompt_str)
user_input = SCREEN.getkey()
return user_input
def get_random_tile_number():
return 4 if random.random() <= FOUR_POSSIBILITY else 2
def get_empty_pos():
result = []
for y, row in enumerate(BOARD):
for x, _ in enumerate(row):
if BOARD[y][x] == 0:
result.Append((x, y))
return result
def get_random_empty_pos():
# 因为get_empty_pos返回的是(x, y),对应横坐标(也就是列数)和横坐标(行数)
try:
col, row = random.choice(get_empty_pos())
return row, col
except IndexError:
return None
def gen_tile():
pos = get_random_empty_pos()
if pos is None:
return FAILED
row, col = pos
number = get_random_tile_number()
BOARD[row][col] = number
return SUCCESS
# 开始新游戏
def new_game():
global BOARD, SCORE, HAS_2048, IS_STUCK, ASKED_FOR_CONTINUE
# 将板和分数清空
BOARD = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
SCORE = 0
HAS_2048 = False
IS_STUCK = False
ASKED_FOR_CONTINUE = False
# 随机生成起始方块
for _ in range(STARTUP_TILES):
gen_tile()
while True:
print_board()
check_board()
if HAS_2048 and not ASKED_FOR_CONTINUE:
user_choice = get_user_input(
'你合并出了2048!还要继续吗?(输入Y继续,输入Q结束这盘游戏)[Y/Q]:',
['Y', 'Q', 'y', 'q']
)
if user_choice in 'yY':
print_prompt('好的,继续游戏……')
ASKED_FOR_CONTINUE = True
elif user_choice in 'qQ':
print_prompt('好的,结束游戏……')
break
elif IS_STUCK:
break
# 取用户输入
direction = get_user_input(
'请按方向键移动方块 。(按Q放弃本盘游戏)',
['KEY_UP', 'KEY_DOWN', 'KEY_LEFT', 'KEY_RIGHT', 'Q', 'q']
)
if direction in 'qQ':
break
elif direction == 'KEY_LEFT':
direction = LEFT
elif direction == 'KEY_RIGHT':
direction = RIGHT
elif direction == 'KEY_UP':
direction = UP
elif direction == 'KEY_DOWN':
direction = DOWN
【python 2048游戏源码】moved_result = move_tile(direction)
if moved_result:
gen_tile()
print_score()
# 这里是这样:整块板分为(1)顶部的边框和(2)数字和数字下面的边框 。
# 横向同理,分为(1)左边的边框和(2)数字和数字右边的边框 。
def print_board():
# 顶部边框
SCREEN.addstr(0, 0, '+----+----+----+----+')
for y, row in enumerate(BOARD):
# 左边边框
SCREEN.addstr(y * 2 + 1, 0, '|')
# the number
for x, num in enumerate(row):
# 我们用0表示当前位置没有方块
numstr = str(num) if num != 0 else ''
SCREEN.addstr(y * 2 + 1, x * 5 + 1, numstr +
(' ' * (4 - len(numstr))) + '|')
# 数字下面的边框
SCREEN.addstr(y * 2 + 2, 0, '+----+----+----+----+')
def move_tile(direction):
global SCORE
def get_line(offset: int, direction: int):
'''


推荐阅读