簡體   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