我知道这可能听起来很愚蠢,但他们网站上的 pygame 文档说它是:
x = pygame.Rect(left,top,width,height)
但是,在我的程序中,我无法弄清楚这是否属实,或者参数是否实际上是两组坐标。我几乎没有经验通过查看 pygame 源代码来找出答案。
类pygame.Rect
用于存储直角坐标的pygame对象
Rect(left, top, width, height) -> Rect
Rect((left, top), (width, height)) -> Rect
Rect(object) -> Rect
因此,如果您有坐标
(x1, y1)
和 (x2, y2)
,则以下两者都可以工作:
pygame.Rect(x1, y1, x2-x1, y2-y1)
pygame.Rect((x1, y1), (x2-x1, y2-y1))
帮助让事情变得更清楚。当您创建 Rect 的实例(也称为 Rect 对象)时。您指定它的左上角位置。然后指定它的宽度,该宽度延伸到该位置的右侧。接下来是从该点向下延伸的高度。
如果您想确认 Rect 对象的 4 个角,您可以调用这些变量。前任。左上矩形、右上矩形、左下矩形、右下矩形。您需要做的就是在打印函数中调用它们,以便在控制台上查看它们。
# Example
import pygame
# First I'll make a Rect whose topleft corner is in the coordinate (0, 0) with a width=10 and height=10
test_rect = pygame.Rect(0, 0, 10, 30)
# Next print the four corners to the console
print(test_rect.topleft)
print(test_rect.topright)
print(test_rect.bottomleft)
print(test_rect.bottomright)
# This should result in the four positions:
# (0, 0)
# (0, 10)
# (30, 0)
# (30, 10)
# Because of how pygame plots on the screen once you get to the graphics portion it important to note that it's x accends from left to right, which is normal.
# However, it y coordinates ascend from top to bottom, which would normally be negative values for standard graph plotting.