gpt4 book ai didi

python - Pygame:国际象棋中的网格/棋盘对齐

转载 作者:行者123 更新时间:2023-11-30 23:09:49 25 4
gpt4 key购买 nike

pygame 中有没有办法让 Sprite 在拖动时捕捉到不可见(或可见)网格?有点像拖放?如果是这样,怎么办?我一直在尝试做一些 Sprite 重叠,但是为每条网格线制作 if 语句太乏味了。那么如何制作一个对齐网格的拖放 Sprite 呢?这是一个国际象棋程序。我将感谢您的帮助。

最佳答案

使用 for 循环创建棋盘角的数组。

corners = []
for x in range(edgeRight, edgeLeft, interval):
for y in range(edgeTop, edgeBottom, interval):
corners.append((x,y))

然后,创建一个事件监听器。当片段被拖动时,将此代码插入到您拥有的任何 while 语句中:

px, py = Piece.Rect.topleft //using tuples to assign multiple vars
for cx, cy in corners:
if math.hypot(cx-px, cy-py) < distToSnap:
Piece.setPos((cx,cy))
break

我不知道你的实际代码是什么,但这应该会给你一个想法。同样,pygame 没有对齐网格功能。

关于python - Pygame:国际象棋中的网格/棋盘对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30966223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com