返回列表中我将鼠标位置与python中的坐标匹配的部分

2024-10-01 11:31:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个国际象棋程序,因此需要该程序来告诉我我点击了什么棋子。到目前为止,它告诉我是否点击过任何一个工件,方法是将鼠标x和y坐标与当前工件坐标列表匹配。然而,该程序不知道我点击了哪一块,我想知道使用while循环是否可以在找到一块时结束循环,并打印/存储我在列表中找到匹配块时的部分到我的鼠标坐标

目前我已经有了这段代码,这应该是解决我的问题的一个很好的起点

        if event.type == pygame.MOUSEBUTTONDOWN:
            mousepos = pygame.mouse.get_pos()
            #rounddown80 is a function to round down mouse coords to multiple of 80
            roundedmouse1 = roundup80(mousepos[0])
            roundedmouse2 = roundup80(mousepos[1])
            #print(roundedmouse1,roundedmouse2)
            mousecoords = [roundedmouse1,roundedmouse2]
            #print(mousecoords)
            foundpiece = False
            while foundpiece == False:
                for x in piecespositions:
                    if x[0] == mousecoords[0] and x[1] == mousecoords[1]:
                        print("Great job you clicked a piece")
                        foundpiece = True

我的计件清单看起来像

piecespositions = [queenblackpos,kingblackpos,bishop1blackpos,bishop2blackpos,knight1blackpos,knight2blackpos, 

等,例如queenblackpos将为[80160]


Tags: to程序列表if鼠标pygame工件print
2条回答

您可以在for i in range(0,len(piecepositions))中或使用增加的变量跟踪while循环中的哪一部分。 例如,类似这样的事情:

  while foundpiece == False:
    index=0 #Reset index before checking all pieces
    for x in piecespositions:
        if x[0] == mousecoords[0] and x[1] == mousecoords[1]:
                 print("Great job you clicked a piece")
                 foundpiece = True
        
                 found_pos=x #store position of the piece that was found
           index=index+1 #Keep adding to index to know which piece was found
    pygame.display.flip() #Update the window 
  print (f"Piece found in {index}: {found_pos}") #print the position of the piece as well as what is its index in the list.

这将存储在found_pos中找到的工件的位置,并使用index变量跟踪当前正在检查的工件。while循环结束后,它将打印工件的位置和列表中该工件的索引

但请注意,在while循环期间,脚本将被“卡住”,直到while完成,因此在计件检查期间无法更新窗口,因此游戏将冻结。所以要解决这个问题,必须在while中添加pygame.display.flip()

我做了这样的事。希望它能帮助您:

square_s = 160 # square(tile) size - you can pass your value
letters = ["A", "B", "C", "D", "E", "F", "G", "H"] # letters used in pos symbols

# match each tile on board to the right cords symbol and value(x, y)
def matchSqrCords(sqr_s, letters):
    positions = [] # matched positions
    for y in range(8): # for each row on the board
        for x in range(8): # for each column(tile in a row) on the board
            pos = ["", 0, 0] # curr position
            pos[0] += letters[y] # add letter prefix to pos symbol
            pos[0] += str(x + 1) # add number prefix to pos symbol
            pos[1] = sqr_s*(x + 1) # calc max x range of a tile
            pos[2] = sqr_s*(y + 1) # calc max y range of a tile
            positions.append(pos) # add matched pos to list
    return positions # return list with matched positions

def checkPosition(given_cords, cords, sqr_s):
    # check if typed values are on the board
    if given_cords[0] > sqr_s * 8 or given_cords[1] > sqr_s * 8:
    return "Cords not on the board!"
    else:
        for cord in cords: # check all cords
            # check if given cords x value is in range of curr cord
            if given_cords[0] <= cord[1]  and given_cords[0] > cord[1] - sqr_s:
                # check if given cords y value is in range of curr cord
                if given_cords[1] <= cord[2]  and given_cords[1] > cord[2] - sqr_s:
                    return cord[0] # return symbol of the found cord

matched_pos = matchSqrCords(square_s, letters=letters)
print(checkPosition([192, 569], matched_pos, square_s))

相关问题 更多 >