Rect(rectangle)指的是矩形,或者长方形,在 Pygame 中我们使用 Rect() 方法来创建一个指定位置,大小的矩形区域。函数的语法格式如下:
rect =pygame.Rect(left,top,width,height)
Rect 表示的区域必须位于一个 Surface 对象之上,比如游戏的主窗口(screen)。上述方法由四个关键参数值构成,分别是 left、top、width、height,为了方便大家理解这些距离的含义,下面给出了一张示意图:
注意:在 Pygame 中以游戏主窗口的左上角为坐标原点。
下面看一组简单的使用示例,如下所示:
* import pygame
* pygame.init()
* screen = pygame.display.set_mode((500,300))
* pygame.display.set_caption('python教程入门学习')
* image_surface = pygame.image.load("C:/Users/Administrator/Desktop/c-net.png")
* rect1 = pygame.Rect(50,50,100,100)
* # 在原图的基础上创建一个新的子图(surface对象)
* image_child= image_surface.subsurface(rect1)
* rect2 = image_child.get_rect()
* #输出的矩形大小为 100*100
* print(rect2)
* while True:
* for event in pygame.event.get():
* if event.type == pygame.QUIT:
*