#定义2048初始数值
game_map=[
[2,0,0,2],
[4,4,2,2],
[2,4,0,4],
[0,0,2,2]
]
def end_zero(list01):
for iin range(len(list01)-1,-1,-1):
if list01[i] ==0:
del list01[i]
list01.append(0)
#合并操作
def merge(list01):
end_zero(list01)
for iin range(len(list01)-1):
if list01[i]==list01[i+1]:
list01[i]+=list01[i+1]
del list01[i+1]
list01.append(0)
#左移
def move_left():
for linein game_map:
merge(line)
#右移
def move_right():
for linein game_map:
list01=line[::-1]
merge(list01)
line[::-1]=list01
# move_right()
# print(game_map)
#矩阵转换原理
# game_map=[
# [2,0,0,2],
# [4,4,2,2],
# [2,4,0,4],
# [0,0,2,2]
# ]
# game_map[0][1] game_map[1][0]
# game_map[0][2] game_map[2][0]
# game_map[0][3] game_map[3][0]
# for i in range(1,4):
# game_map[0][i]
# game_map[1][2] game_map[2][1]
# game_map[1][3] game_map[3][1]
# for i in range(2,4)
# game_map[1][i]
# game_map[2][3] game_map[3][2]
# for i in range(3,4)
# game_map[2][i]
def matrix(list01):
for cin range(1,len(list01)):
for iin range(c,len(list01)):
list01[c-1][i],list01[i][c-1]=list01[i][c-1],list01[c-1][i]
matrix(game_map[0])
print(game_map)
#上移
def move_up():
matrix(game_map)
move_left()
matrix(game_map)
move_up()
print(game_map)
#下移
def move_down():
matrix(game_map)
move_right()
matrix(game_map)
move_down()
print(game_map)
先更新到这里,剩余功能待续