矩形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是圖像的高

rect = image.get_rect(center=(w//2,h//2))  #獲取圖像的矩形屬性
#獲取矩形屬性后,把矩形的中心設置為參數指定的點

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()

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM