当前位置: 代码迷 >> python >> python中的滑块拼图没有pygame只有功能
  详细解决方案

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

热度:84   发布时间:2023-07-16 10:42:32.0

如何添加我在此处插入的输入(如向上移动列表等),因此当您单击为输入插入的键时,我的拼图也会移动。

还有关于如何为此代码添加自动求解器选项的任何想法?

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 ')

模块 pynput 可用于此目的

它有一个键盘模块,可以检测按键。 唯一的缺点是,如果您试图按下一个可打印的字符,它会在外壳、空闲或您使用的任何东西上打字。 但是,如果您使用箭头键之类的东西,则不会出现任何内容。 光标只会移动。

这是一个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()