python 之 160行代码实现2048游戏
在这里,我们自制一个2048游戏!!
在如下代码中,除了注释以外的代码行数只有160行,但加了注释就变成了200多一点~~~
因此标题为160行实现2048游戏~~~
游戏玩法:
"""
please continuously input you operation:
(
using the words :
u for up
d for down
r for right
and
l for left
)
"""
至于其他的规则呢,自然大家一定都十分清楚了,此处就不再赘述了~~~
代码实现如下(定义了很多函数来模块化,使得代码可以得到一定程度的简化):
# 2048 by me
# 导入库
import random, time, turtle
# 设置初始状态
def __set__initial__state__(list_0, list_1):
"""
设置初始状态
:param list_0: [2, 4]
:param list_1: 游戏信息列表
:return: 游戏信息列表
"""
while True:
x, y = random.sample(range(len(list_1)), 2)
if x != y:
break
list_1[x] = random.choice(list_0)
list_1[y] = random.choice(list_0)
return list_1
# 绘制表格显示游戏的状态
def __draw__the__picture__of__the__game__(list_1):
"""
绘制表格显示游戏的状态
:param list_1: 游戏信息列表
:return: 空
"""
list_1_1 = ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
for __number__ in range(16):
for number1 in range((6 - len(f'{list_1[__number__]}')) // 2):
list_1_1[__number__] += ' '
list_1_1[__number__] += f'{list_1[__number__]}'
for number2 in range(6 - (6 - len(f'{list_1[__number__]}')) // 2 - len(f'{list_1[__number__]}')):
list_1_1[__number__] += ' '
print(f'┍ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ┑\n'f'┃{list_1_1[0]}┃{list_1_1[1]}┃{list_1_1[2]}┃{list_1_1[3]}┃\n'f'━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━\n'f'┃{list_1_1[4]}┃{list_1_1[5]}┃{list_1_1[6]}┃{list_1_1[7]}┃\n'f'━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━\n'f'┃{list_1_1[8]}┃{list_1_1[9]}┃{list_1_1[10]}┃{list_1_1[11]}┃\n'f'━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━\n'f'┃{list_1_1[12]}┃{list_1_1[13]}┃{list_1_1[14]}┃{list_1_1[15]}┃\n'f'┕ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ┙\n')
return 0
# 移动操作
def __move__to__remove__zero__(a_list, one__number):
"""
移动操作
:param a_list: 游戏信息列表
:param one__number: 在__do__up__operation__(list_1, list_0)函数中,实现第one__number列的操作
:return:
"""
for tf_number in range(3): # 3 times !!
for n_n in [0, 4, 8]: # cycle !!
if a_list[n_n + one__number] == 0:
t = a_list[n_n + 4 + one__number]
a_list[n_n + 4 + one__number] = a_list[n_n + one__number]
a_list[n_n + one__number] = t
return a_list
# 结束游戏的条件以及结束显示
def __end__the__game(s):
"""
结束游戏的条件以及结束显示
:param s: 将要在图形中输出的信息
:return: 空
"""
t = turtle.Turtle()
screen = turtle.Screen()
screen.bgcolor('blue')
t.hideturtle()
t.pencolor('red')
t.up()
t.back(325)
t.write(s, font=('', 50, '')) # , font=('font-name', 100, 'font_type'))
time.sleep(5)
exit('bye bye ! ') # exit !!
# 向上移动的操作
def __do__up__operation__(list_1, list_0): # list_1 : list_of_game list_0 : random_list
"""
向上移动的操作
:param list_1: 游戏信息列表
:param list_0: [2, 4]
:return: 游戏信息列表
"""
for one_number in [0, 1, 2, 3]:
list_1 = __move__to__remove__zero__(a_list=list_1, one__number=one_number)
if list_1[0 + one_number] != 0:
if list_1[4 + one_number] != 0:
if list_1[0 + one_number] == list_1[4 + one_number]:
list_1[0 + one_number] = 2 * list_1[4 + one_number]
list_1[4 + one_number] = 0
if list_1[8 + one_number] != 0:
if list_1[12 + one_number] != 0:
if list_1[8 + one_number] == list_1[12 + one_number]:
list_1[8 + one_number] = 2 * list_1[12 + one_number]
list_1[12 + one_number] = 0
else:
if list_1[8 + one_number] != 0:
if list_1[4 + one_number] == list_1[8 + one_number]:
list_1[4 + one_number] = 2 * list_1[8 + one_number]
list_1[8 + one_number] = 0
else:
if list_1[12 + one_number] != 0:
if list_1[8 + one_number] == list_1[12 + one_number]:
list_1[8 + one_number] = 2 * list_1[12 + one_number]
list_1[12 + one_number] = 0
list_1 = __move__to__remove__zero__(a_list=list_1, one__number=one_number)
void_number = 0
void_list = []
for tf_number in range(16):
if list_1[tf_number] == 2048:
void_number += 1
if list_1[tf_number] == 0:
void_list.append(tf_number)
if void_list == []:
__end__the__game(s='You Lost The Game ! ')
elif void_number != 0:
__end__the__game(s='You Win The Game ! ')
else: # choice !!! choice !!!
list_1[random.choice(void_list)] = random.choice(list_0)
return list_1
# 将其他方向的移动转变为向上移动的操作
def __turn__matrix__(l1, l2, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16):
"""
将其他方向的移动转变为向上移动的操作
:return: 游戏信息列表
"""
l1[0] = l2[x1]
l1[4] = l2[x2]
l1[8] = l2[x3]
l1[12] = l2[x4]
l1[1] = l2[x5]
l1[5] = l2[x6]
l1[9] = l2[x7]
l1[13] = l2[x8]
l1[2] = l2[x9]
l1[6] = l2[x10]
l1[10] = l2[x11]
l1[14] = l2[x12]
l1[3] = l2[x13]
l1[7] = l2[x14]
l1[11] = l2[x15]
l1[15] = l2[x16]
return l1
# 向下移动的操作
def __do__down__operation__(list_2, list_0): # here we use an interesting method:
"""
向下移动的操作
:param list_2: 游戏信息列表
:param list_0: [2, 4]
:return: 游戏信息列表
"""
list_1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
list_1 = __turn__matrix__(list_1, list_2, 12, 8, 4, 0, 13, 9, 5, 1, 14, 10, 6, 2, 15, 11, 7, 3)
list_3 = __do__up__operation__(list_1, list_0)
list_4 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
list_4 = __turn__matrix__(list_4, list_3, 12, 8, 4, 0, 13, 9, 5, 1, 14, 10, 6, 2, 15, 11, 7, 3)
return list_4
# 向左移动的操作
def __do__left__operation__(list_2, list_0):
"""
向左移动的操作
:param list_2: 游戏信息列表
:param list_0: [2, 4]
:return: 游戏信息列表
"""
list_1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
list_1 = __turn__matrix__(list_1, list_2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
list_3 = __do__up__operation__(list_1, list_0)
list_4 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
list_4 = __turn__matrix__(list_4, list_3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
return list_4
# 向右移动的操作
def __do__right__operation__(list_2, list_0):
"""
向右移动的操作
param list_2: 游戏信息列表
:param list_0: [2, 4]
:return: 游戏信息列表
"""
list_1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
list_1 = __turn__matrix__(list_1, list_2, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
list_3 = __do__up__operation__(list_1, list_0)
list_4 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
list_4 = __turn__matrix__(list_4, list_3, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
return list_4
# main函数
if __name__ == '__main__':
"""
测试信息!!!
"""
list_of_game = __set__initial__state__(list_0=[2, 4], list_1=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
__draw__the__picture__of__the__game__(list_1=list_of_game)
while True:
while True:
operation = input('please input you operation ! (using the words : u for up,d for down,r for right and l for left) \nplease input here : ')
if operation in ['u', 'd', 'r', 'l']:
break
if operation == 'u':
list_of_game = __do__up__operation__(list_of_game, [2, 4])
__draw__the__picture__of__the__game__(list_of_game)
if operation == 'd':
list_of_game = __do__down__operation__(list_of_game, [2, 4])
__draw__the__picture__of__the__game__(list_of_game)
if operation == 'r':
list_of_game = __do__right__operation__(list_of_game, [2, 4])
__draw__the__picture__of__the__game__(list_of_game)
if operation == 'l':
list_of_game = __do__left__operation__(list_of_game, [2, 4])
__draw__the__picture__of__the__game__(list_of_game)