矩形pygame.Rect

一个 Rect 对象可以由 left,top,width,height 几个值创建

Rect 对象覆盖的范围并不包含 right 和 bottom 指定的边缘位置

这样的话,如果一个 Rect 对象的 bottom 边框恰好是另一个 Rect 对象的 top 边框(即 rect1.bottom == rect2.top),那么两矩形就恰好没有重叠的显示在屏幕上,rect1.colliderect(rect2) 也将返回 False

  

import pygame

pygame.init()
screen = pygame.display.set_mode((960, 600))
image = pygame.image.load('1.png')
rect2=pygame.Rect(100,50,10,10)  #创建一个矩形对象
rect = image.get_rect()  #获取图像的矩形属性
#<rect(0, 0, 210, 135)>    0,0是左上角坐标  210是图像的宽   135是图像的高
x=rect.centerx  #返回矩形x轴方向的中心坐标
y=rect.centery  #返回矩形y轴方向的中心坐标
rect1 = screen.get_rect()  #获取窗口的矩形属性
#<rect(0, 0, 960, 600)>
aa = rect.bottom   #返回矩形的底坐标
#135
aa=rect.center  #返回矩形的中心点--元组
#(105, 67)

print(aa)

r.left 左边x坐标的整数值
r.right 右边x坐标的整数值
r.top  顶部y坐标的整数值
r.bottom   底部y坐标的整数值
r.centerx 中央x坐标整数值
r.centery 中央y坐标整数值
r.width 宽度
r.height 高度
r.size 即元组(width,height)
r.topleft (left,top)
r.topright (right,top)
r.bottomleft (left,bottom)
r.bottomright (right,bottom)
r.midleft (left,centery)
r.midright (right,centery)
r.midtop (centerx,top)
r.midbottom (centerx,bottom)

import pygame

pygame.init()
screen = pygame.display.set_mode((960, 600))
pygame.display.set_caption("图像")

rect=pygame.Rect(100,50,10,10)  #创建一个矩形对象
rect.center = (200,300)  #设置矩形中心坐标
rect1=pygame.Rect.copy(rect)  #复制矩形
#<rect(195, 295, 10, 10)>
#返回一个新的 Rect 对象,拥有与该 Rect 对象相同的位置和尺寸
rect2=rect1.move(5,5)  #移动 Rect 对象
#<rect(200, 300, 10, 10)>
#返回一个新的 Rect 对象。x 和 y 参数可以是正数或负数,用于指定新对象的偏移地址
rect2.move_ip(10,20)   #移动 Rect 对象
#<rect(210, 320, 10, 10)>
#效果跟 move() 方法一样,区别是这个直接作用于当前 Rect 对象,而不是返回一个新的

rect3=rect2.inflate(100,50)  #增大或缩小矩形大小
#100表示矩形宽的变化量;50表示矩形高的变化量
#返回一个新的Rect对象。新的对象保持与原始 Rect 对象在同一个中心上
#rect(160, 295, 110, 60)
rect3.inflate_ip(-100,-50)  ##增大或缩小矩形大小
#效果跟 inflate() 方法一样,区别是这个直接作用于当前 Rect 对象,而不是返回一个新的对象
#<rect(210, 320, 10, 10)

rect4=pygame.Rect(50,10,30,20)
rect5=rect4.clamp(rect3)  #把rect4的中心移到rect3的中心
#返回一个新的 Rect 对象

rect4=pygame.Rect(50,10,30,20)
rect4.clamp_ip(rect3)  #把rect4的中心移到rect3的中心
#效果跟 clamp() 方法一样,区别是这个直接作用于当前rect4对象,而不是返回一个新的

print(rect4,rect4.center)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    pygame.display.update()
import pygame

pygame.init()
screen = pygame.display.set_mode((960, 600))
pygame.display.set_caption("图像")

rect=pygame.Rect(100,100,100,100)
rect1=pygame.Rect(220,150,120,20)

rect2=rect.clip(rect1)  #获取两个 Rect 对象互相重叠的部分
#返回一个新的 Rect 对象。如果两个 Rect 对象没有任何重叠,则返回一个 (0, 0, 0, 0) 的 Rect 对象

rect3=rect.union(rect1)  #将两个 Rect 对象合并
#返回一个新的Rect对象,范围取两个对象的最大区域
#<rect(100, 100, 240, 100)>

rect.union_ip(rect1)  #将两个 Rect 对象合并
#效果跟 union() 方法一样,区别是这个直接作用于当前 rect 对象,而不是返回一个新的

rect4=pygame.Rect(220,150,120,200)
rect5=rect4.unionall((rect,rect1))  #合并多个矩形
#返回一个新的 Rect 对象,范围取多个对象的最大区域
#<rect(100, 100, 240, 250)>

rect4.unionall_ip((rect,rect1))  #合并多个矩形
#效果跟 unionall() 方法一样,区别是这个直接作用于当前 rect4 对象,而不是返回一个新的
#范围取多个对象的最大区域

print(rect4,rect5)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    pygame.display.update()
import pygame

pygame.init()
screen = pygame.display.set_mode((960, 600))
pygame.display.set_caption("矩形")

rect1 = pygame.Rect(0, 0, 100, 50)
rect2 = pygame.Rect(200, 100, 200, 200)

rect3=rect1.fit(rect2)  #保持rect1的宽高比不变,在rect2范围内产生一个最大的矩形并返回新矩形
#rect3的中心与rect2的一样

print(rect3)
bg = (255, 255, 255)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    screen.fill(bg)
    pygame.draw.rect(screen, (255, 0, 0), rect1)
    pygame.draw.rect(screen, (0, 255, 0), rect2)
    pygame.draw.rect(screen, (0, 0, 255), rect3)
    pygame.display.update()

效果图:

import pygame

pygame.init()
screen = pygame.display.set_mode((960, 600))
pygame.display.set_caption("矩形")

rect1 = pygame.Rect(150, 150, 100, 50)
rect2 = pygame.Rect(150, 150, -100, -50)
rect2.normalize()  #修改起点坐标
#如果 width 或 height 存在负数,都改成正数,把起点改为左上角位置,矩形的位置不变

print(rect2)
bg = (255, 255, 255)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    screen.fill(bg)
    pygame.draw.rect(screen, (255, 0, 0), rect1)
    pygame.draw.rect(screen, (0, 255, 0), rect2)

    pygame.display.update()
import pygame

pygame.init()
screen = pygame.display.set_mode((960, 600))
pygame.display.set_caption("矩形")

rect1 = pygame.Rect(0, 0, 100, 50)
rect2 = pygame.Rect(0, 0, 10, 10)
t=rect1.contains(rect2)  #检测一个 Rect2 对象是否完全包含在该 Rect1 对象内
# 1
rect2 = pygame.Rect(0, 0, 101, 50)
t=rect1.contains(rect2)
#0

print(t)
bg = (255, 255, 255)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    screen.fill(bg)
    pygame.draw.rect(screen, (255, 0, 0), rect1)
    pygame.draw.rect(screen, (0, 255, 0), rect2)

    pygame.display.update()
import pygame

pygame.init()
screen = pygame.display.set_mode((960, 600))
pygame.display.set_caption("矩形")

rect1 = pygame.Rect(0, 0, 100, 50)
point = (10, 10)
t=rect1.collidepoint(point)  #检测一个点是否包含在该Rect对象内
#如果给定的点在该 Rect 对象内,返回 True,否则返回 False
#一个点在 Rect 的 right 或 bottom 边缘上时,并不被认为包含在该矩形内
#1
print(t)
point = (100, 50)
t=rect1.collidepoint(point)
#0
print(t)
point = (1001, 50)
t=rect1.collidepoint(point)
#0
print(t)
bg = (255, 255, 255)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    screen.fill(bg)


    pygame.display.update()
import pygame

pygame.init()
screen = pygame.display.set_mode((960, 600))
pygame.display.set_caption("矩形")

rect1 = pygame.Rect(0, 0, 100, 50)
rect2 = pygame.Rect(50, 50, 100, 50)
t=rect1.colliderect(rect2)  #检测两个Rect对象是否重叠
#如果两个 Rect 对象有任何重叠的地方,返回 True,否则返回 False
#注意:right 和 bottom 指定的边缘位置并不属于对应的矩形
# 0

rect2 = pygame.Rect(50, 49, 100, 50)
t=rect1.colliderect(rect2)
# 1
print(t)
bg = (255, 255, 255)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    screen.fill(bg)
    pygame.draw.rect(screen, (255, 0, 0), rect1)
    pygame.draw.rect(screen, (0, 255, 0), rect2)

    pygame.display.update()
import pygame

pygame.init()
screen = pygame.display.set_mode((960, 600))
pygame.display.set_caption("矩形")

rect1 = pygame.Rect(0, 0, 20, 20)
rect2 = pygame.Rect(50, 50, 20, 20)
rect3 = pygame.Rect(150, 150, 20, 20)

rect4 = pygame.Rect(40, 50, 20, 20)

t=rect4.collidelist([rect1,rect2,rect3]) #检测该Rect对象是否与列表中的任何一个矩形有交集
#返回值是第 1 个有相交的矩形所在列表中的索引号(如果有的话),否则返回 -1

rect4 = pygame.Rect(25, 50, 20, 20)
t=rect4.collidelist([rect1,rect2,rect3])
print(t)
bg = (255, 255, 255)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    screen.fill(bg)
    pygame.draw.rect(screen, (255, 0, 0), rect1)
    pygame.draw.rect(screen, (0, 255, 0), rect2)
    pygame.draw.rect(screen, (0, 0, 255), rect3)
    pygame.draw.rect(screen, (0, 255, 255), rect4)
    pygame.display.update()
import pygame

pygame.init()
screen = pygame.display.set_mode((960, 600))
pygame.display.set_caption("矩形")

rect1 = pygame.Rect(0, 0, 20, 20)
rect2 = pygame.Rect(50, 50, 20, 20)
rect3 = pygame.Rect(150, 150, 20, 20)

rect4 = pygame.Rect(40, 50, 20, 20)

t=rect4.collidelist([rect1,rect2,rect3]) #检测该 Rect 对象是否与列表中的任何一个矩形有交集
#返回值是第 1 个有相交的矩形所在列表中的索引号(如果有的话),否则返回 -1

t=rect4.collidelistall([rect1,rect2,rect3])  #检测该 Rect 对象与列表中的哪些矩形有交集
#返回一个列表,包含所有与该 Rect 对象有交集的元素序号;如果一个都没有,返回一个空列表
print(t)
bg = (255, 255, 255)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    screen.fill(bg)
    pygame.draw.rect(screen, (255, 0, 0), rect1)
    pygame.draw.rect(screen, (0, 255, 0), rect2)
    pygame.draw.rect(screen, (0, 0, 255), rect3)
    pygame.draw.rect(screen, (0, 255, 255), rect4)
    pygame.display.update()

 

原文地址:https://www.cnblogs.com/liming19680104/p/13043889.html