繁体   English   中英

使用 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 使用rects_list.append(pygame.Rect())附加到列表中

pygame 有一个名为collidepoint(x, y)的 function 。 最小的例子:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM