使用 pygame 中的坐标检查矩形列表中的矩形
check for a rect in list of rects using co-ordinates in pygame
如果我想查看矩形是否在给定坐标处,我该怎么做?在伪代码中,这就是我想要实现的...
xpos = 40
ypos = 40
If rect(xpos, ypos) in rects_list:
print("There is a rect at " + xpos + ", " + ypos)
rects_list 看起来像
[<rect(0, 0, 40, 40)>, <rect(40, 0, 40, 40)>, <rect(80, 0, 40, 40)>]
其中使用 rects_list.append(pygame.Rect())
将矩形追加到列表中
pygame 有一个名为 collidepoint(x, y)
的函数。
最小示例:
import pygame
l = [pygame.Rect(0, 0, 41, 41), pygame.Rect(40, 0, 40, 40), pygame.Rect(80, 0, 40, 40)]
xpos = 40
ypos = 40
if any(i.collidepoint((xpos, ypos)) for i in l):
print(f"There is a rect at {xpos},{ypos}")
结果:
There is a rect at 40,40
如果我想查看矩形是否在给定坐标处,我该怎么做?在伪代码中,这就是我想要实现的...
xpos = 40
ypos = 40
If rect(xpos, ypos) in rects_list:
print("There is a rect at " + xpos + ", " + ypos)
rects_list 看起来像
[<rect(0, 0, 40, 40)>, <rect(40, 0, 40, 40)>, <rect(80, 0, 40, 40)>]
其中使用 rects_list.append(pygame.Rect())
pygame 有一个名为 collidepoint(x, y)
的函数。
最小示例:
import pygame
l = [pygame.Rect(0, 0, 41, 41), pygame.Rect(40, 0, 40, 40), pygame.Rect(80, 0, 40, 40)]
xpos = 40
ypos = 40
if any(i.collidepoint((xpos, ypos)) for i in l):
print(f"There is a rect at {xpos},{ypos}")
结果:
There is a rect at 40,40