问题描述
我是编程新手,正在尝试创建一个没有类且只有标准 python 模块的滑动拼图程序。我从互联网上找到了一个关于滑动拼图游戏的代码,并试图修改棋盘尺寸和输入来移动数字。移动数字的输入应该在 wasd 中,其中 w = 将数字向上滑动/移动到空白处,s = 向下移动,a = 向左移动,d = 向右移动。
这是来自互联网的代码,我已经将其修改为自定义板尺寸从 3x3 到 10x10。在这个程序中移动的输入是想要移动到空白处的数字。
import random,sys
n = input('Please input the board dimension: ')
n = int(n)
def board():
'''Make matrix board of random numbers'''
list1 = [*range(0,n**2,1)]
random.shuffle(list1)
matrix = []
while list1 !=[]:
matrix.append(list1[:n])
list1 = list1[n:]
return matrix
def zero(board):
'''function to find where the zero is'''
empty_space = None
for x,item in enumerate(board):
for y,item in enumerate(board):
if board[x][y] == 0:
empty_space = (x,y)
return empty_space
def draw_board(board):
'''function to draw the board'''
print('\n\t')
for x,item in enumerate(board):
if board[x][y] == 0:
print('\t| ',end='')
else:
print('\t| ' + '{:02d}' .format(board[x][y]),end=' ')
print('\n\t')
def ask_number(board):
''' function to ask for the number to move'''
num = input('\nplease type the number of the piece to move : ( q ) to quit ')
if num in ['q','Q']:
print('\n\ngame over ')
sys.exit()
num = int(num)
piece = ()
for i,item in enumerate(board):
for j,item in enumerate(board):
if num == board[i][j]:
piece = (i,j)
return piece,num
def game():
'''Run the game logic'''
matrix = board()
empty_space = zero(matrix)
game_on = True
move = 0
while game_on:
draw_board(matrix)
piece,num = ask_number(matrix)
if num > n**2:
print('illegal move,try again ')
else:
if(empty_space==(piece[0]-1,piece[1]))\
or(empty_space==(piece[0]+1,piece[1]))\
or(empty_space==(piece[0],piece[1]-1))\
or(empty_space==(piece[0],piece[1]+1)):
matrix[empty_space[0]][empty_space[1]]=num
matrix[piece[0]][piece[1]]=0
empty_space=(piece[0],piece[1])
move = move +1
print()
print('you have made ',move,'moves so far ')
print(2*'\n')
else:
print('illegal move,try again ')
if __name__ == '__main__':
game()
然而,当我完成输入的编程以移动数字时,我的滑动拼图板不会出现。这是修改输入后的程序:
import random,end=' ')
print('\n\t')
def make_move(board):
''' function to ask for the number to switch'''
switch = input('\nplease type the direction of the piece to switch : ( q ) to quit ')
switch = str(switch)
if switch in ['q','Q']:
print('\n\ngame over ')
sys.exit()
empty_space = zero(board)
piece = ()
for i,item in enumerate(board):
matrix = board()
empty_space = zero(matrix)
empty_space_nearby = ()
return piece,empty_space_nearby,switch
def game():
'''Run the game logic'''
matrix = board()
game_on = True
move = 0
while game_on:
make_move(board)
draw_board(matrix)
piece,switch = make_move(matrix)
if(switch != 'w')\
or(switch != 'a')\
or(switch != 's')\
or(switch != 'd'):
print('illegal move,try again ')
else:
if switch == 'a':
(empty_space==(piece[0]-1,piece[1]))
matrix[empty_space[0]][empty_space[1]]=empty_space_nearby
matrix[piece[0]][piece[1]]=0
empty_space=(piece[0],'moves so far ')
print(2*'\n')
elif switch == 'd':
(empty_space==(piece[0]+1,'moves so far ')
print(2*'\n')
elif switch == 's':
(empty_space==(piece[0],piece[1]-1))
matrix[empty_space[0]][empty_space[1]]=empty_space_nearby
matrix[piece[0]][piece[1]]=0
empty_space=(piece[0],'moves so far ')
print(2*'\n')
elif switch == 'w':
(empty_space==(piece[0],piece[1]+1))
matrix[empty_space[0]][empty_space[1]]=empty_space_nearby
matrix[piece[0]][piece[1]]=0
empty_space=(piece[0],try again ')
if __name__ == '__main__':
game()
有没有办法在不使用 pygames 或 class 的情况下解决这个问题?提前致谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)