没有pygame仅功能的python中的滑块拼图

问题描述 投票:0回答:1

你好,我试图做一个滑块难题,但是我遇到了问题。

我需要有关如何添加我在此处插入的输入(例如上移列表等)的帮助,所以当您单击为输入插入的键时,我的困惑也将移动。

还有关于如何为此代码添加自动求解器选项的想法?任何指针都会有所帮助。非常感谢!

import random,sys

game_on = True
move = 0
up_move = []
down_move = []
left_move = []
right_move = []
eight_list = [0,1,2,3,4,5,6,7,8]
fifteen_list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
up = input("Enter a key for up: ")
up_move.append(up)
down = input("Enter a key for down: ")
down_move.append(down)
left = input("Enter a key for left: ")
left_move.append(left)
right = input("Enter a key for right: ")
right_move.append(right)
n_o_t = int(input("Do you want to play 8 or 15 puzzle? "))

if n_o_t == 8:
    #------------------ to get shuffled list -----------
    random.shuffle(eight_list)
    print('\n'*2)
    #------------------ to make list of lists -----------
    matrix=[]
    while eight_list !=[]:
        matrix.append(eight_list[:3])
        eight_list = eight_list[3:]
    #------------------- function to find where the zero is ------
    def zero(board):
        global empty_space
        for x in range (len(board)):
            for y in range(len(board[x])):
                if board[x][y] == 0:
                    empty_space = (x,y)
        return empty_space
    #----------------------- function to draw the board -----------
    def draw_board(board):
        print('\n\t+-------+-------+-------|')
        for x in range (len(board)):
            for y in range(len(board[x])):
                if board[x][y] == 0:
                    print('\t|  XX' , end='')
                else:
                    print('\t|  ' + '{:02d}' .format(board[x][y]), end=' ') 
            print('\n\t+-------+-------+-------|')
    # ------------------------ function to ask for the number to move ---------- 
    def ask_number():
        global num , piece 
        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 in range(len(matrix)):
            for j in range(len(matrix[i])):
                if num == matrix[i][j]:
                    piece = (i,j)
        return piece , num
    #---------------------------------------------- game starts here -------------
    zero(matrix)
    while game_on:
        draw_board(matrix)      
        ask_number()         
        if num > 8:
            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 ')
elif n_o_t == 15:
    #------------------ to get shuffled list -----------
    random.shuffle(fifteen_list)
    print('\n'*2)
    #------------------ to make list of lists -----------
    matrix=[]
    while fifteen_list !=[]:
        matrix.append(fifteen_list[:4])
        fifteen_list = fifteen_list[4:]
    #------------------- function to find where the zero is ------
    def zero(board):
        global empty_space
        for x in range (len(board)):
            for y in range(len(board[x])):
                if board[x][y] == 0:
                    empty_space = (x,y)
        return empty_space
    #----------------------- function to draw the board -----------
    def draw_board(board):
        print('\n\t+-------+-------+-------+-------|')
        for x in range (len(board)):
            for y in range(len(board[x])):
                if board[x][y] == 0:
                    print('\t|  XX' , end='')
                else:
                    print('\t|  ' + '{:02d}' .format(board[x][y]), end=' ') 
            print('\n\t+-------+-------+-------+-------|')
    # ------------------------ function to ask for the number to move ---------- 
    def ask_number():
        global num , piece 
        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 in range(len(matrix)):
            for j in range(len(matrix[i])):
                if num == matrix[i][j]:
                    piece = (i,j)
        return piece , num
    #---------------------------------------------- game starts here -------------
    zero(matrix)
    while game_on:
        draw_board(matrix)      
        ask_number()         
        if num > 15:
            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 ')

感谢您的时间

python puzzle
1个回答
0
投票

模块Pynput可以用于此目的

它具有一个键盘模块,可以检测按键。唯一的缺点是,如果您尝试按下可打印字符,它将在外壳,IDLE或您正在使用的任何内容上键入。但是,如果使用箭头键之类的东西,则不会显示任何内容。光标将刚刚移动。

这里是pynput的示例:

from pynput import keyboard

def on_press(key):
    try:
        print("pressed"+key.char)
    except AttributeError: #special character
        print("pressed", key)

def on_release(key):
    try:
        print("released "+key.char)
    except AttributeError: #special character
        print("released", key)

listener = keyboard.Listener(
    on_press=on_press,
    on_release=on_release)
listener.start()
© www.soinside.com 2019 - 2024. All rights reserved.