我正在制作一个国际象棋程序,因此需要该程序来告诉我我点击了什么棋子。到目前为止,它告诉我是否点击过任何一个工件,方法是将鼠标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]
您可以在
for i in range(0,len(piecepositions))
中或使用增加的变量跟踪while循环中的哪一部分。 例如,类似这样的事情:这将存储在
found_pos
中找到的工件的位置,并使用index
变量跟踪当前正在检查的工件。while循环结束后,它将打印工件的位置和列表中该工件的索引但请注意,在while循环期间,脚本将被“卡住”,直到while完成,因此在计件检查期间无法更新窗口,因此游戏将冻结。所以要解决这个问题,必须在while中添加
pygame.display.flip()
我做了这样的事。希望它能帮助您:
相关问题 更多 >
编程相关推荐