Rect介绍

Rect用于做矩阵处理,常见的应用为矩阵移动和碰撞检测

获取对象

Rect类提供了顶点坐标和长宽、顶点位置元组和长款元组、rect对象构造这三类方法实例化对象。下面为代码为:

1
2
3
rec1=Rect(left, top, width, height) 
rec2=Rect((left, top), (width, height))
rec3=Rect(object)

除了直接构造外,也可以通过复制已有的rect对象获取新的对象,代码为:

1
new_rect=rect.copy()

或者使用surface的方法获取rect对象,代码为:

1
2
3
surf=pygame.Surface((50,100))
rect=surf.get_rect(midbottom=(x,y))

定位

Rect对象提供了x,y,top, left, bottom, right,topleft, bottomleft, topright, bottomright,midtop, midleft, midbottom, midright,center, centerx, centery,size,width, height,w,h属性设置其大小和位置。以下是这些属性的一览:

  1. 基本属性(位置和尺寸):

    • x:矩形左上角的 x 坐标。
    • y:矩形左上角的 y 坐标。
    • widthw:矩形的宽度。
    • heighth:矩形的高度。
    • size:元组(w,h)
  2. 边缘坐标属性:

    • top:矩形上边缘的 y 坐标。
    • bottom:矩形下边缘的 y 坐标。
    • left:矩形左边缘的 x 坐标。
    • right:矩形右边缘的 x 坐标。
  3. 角坐标属性:

    • topleft:矩形左上角的坐标。
    • bottomleft:矩形左下角的坐标。
    • topright:矩形右上角的坐标。
    • bottomright:矩形右下角的坐标。
  4. 中点坐标属性:

    • center:矩形的中心点坐标。
    • centerx:矩形中心点的 x 坐标。
    • centery:矩形中心点的 y 坐标。
  5. 边缘中点坐标属性:

    • midtop:矩形上边缘的中点坐标。
    • midleft:矩形左边缘的中点坐标。
    • midbottom:矩形下边缘的中点坐标。
    • midright:矩形右边缘的中点坐标。

以下图形辅助理解

图片来源:https://stackoverflow.com/questions/29584561/rectangles-in-pygame

对于surface的位置变换,为了方便我们一般操作其对于的rect,最终显示时第二参数直接传rect对象,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import pygame
import sys

# 初始化 Pygame
pygame.init()

# 创建窗体
screen = pygame.display.set_mode((400, 300))

snail_surf=pygame.image.load('graphics/snail.png').convert_alpha()
snail_rect=snail_surf.get_rect(topleft=(150,130))

screen.blit(snail_surf,snail_rect)

pygame.display.update()
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

展示效果如图

碰撞检测

碰撞可以简单分为两类:矩形-矩形、矩形-点。
矩形-矩形碰撞可以使用colliderect 方法检测,签名为:

1
colliderect(Rect) -> bool

签名表示是 Rect 类的一个成员方法,接受一个 Rect 对象作为参数,返回一个布尔值,表示当前的 Rect 对象是否与传入的 Rect 对象相交(碰撞)。如果相交,则返回 True,否则返回 False

矩形-点的碰撞使用collidepoint方法,签名为:

1
2
collidepoint(x, y) -> bool
collidepoint((x,y)) -> bool

如果给定的点(x,y)在矩形内部,则返回true。沿右边或底边的点不被认为在矩形内。
演示代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import pygame
import sys

# 初始化 Pygame
pygame.init()

# 创建窗体
screen = pygame.display.set_mode((400, 300))

# 设置字体
t_font = pygame.font.Font('SourceHanSansSC-VF.ttf', 40)
coll_t = " 碰 撞!"

# 载入蜗牛和苍蝇图像
snail_surf = pygame.image.load('graphics/snail.png').convert_alpha()
snail_rect = snail_surf.get_rect(topright=(150, 130))

fly_surf = pygame.image.load('graphics/fly.png').convert_alpha()
fly_rect = fly_surf.get_rect(topleft=(0, 130))

# 设置碰撞文字
coll_surf = t_font.render(coll_t, False, (244, 255, 244))
coll_rect = coll_surf.get_rect(topleft=(160, snail_rect.y))

while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

# 清屏
screen.fill((0, 0, 0))

# 绘制蜗牛和苍蝇图像
screen.blit(snail_surf, snail_rect)
screen.blit(fly_surf, fly_rect)

# 移动蜗牛和苍蝇
if snail_rect.right > 0:snail_rect.move_ip(-3, 0)
else:snail_rect.left = 400
if fly_rect.left < 400:fly_rect.move_ip(3, 0)
else:fly_rect.right = 0

# 碰撞检测
if snail_rect.colliderect(fly_rect):
screen.blit(coll_surf, coll_rect)

# 更新显示
pygame.display.update()

# 控制帧率
pygame.time.Clock().tick(60)

展示效果如图