"""pygame.Rect: virtual attributes, moving, clamping, inflating and collision helpers.""" import pygame r = pygame.Rect(10, 20, 100, 50) # x, y, width, height print(r.topleft, r.bottomright, r.center, r.size) # (10, 20) (110, 70) (60, 45) (100, 50) r.center = (200, 200) # assigning a virtual attribute moves the rect print(r) # Rect(150, 175, 100, 50) (pygame prints ) moved = r.move(10, 0) # returns a new Rect; r is unchanged r.move_ip(10, 0) # _ip = "in place": changes r itself print(moved == r) # True bigger = r.inflate(20, 20) # grows around the centre print(bigger.size, bigger.center == r.center) screen_rect = pygame.Rect(0, 0, 640, 480) outside = pygame.Rect(600, 450, 100, 100) print(outside.clamp(screen_rect)) # moved back inside: Rect(540, 380, 100, 100) a = pygame.Rect(0, 0, 50, 50) b = pygame.Rect(40, 40, 50, 50) print(a.colliderect(b), a.clip(b)) # True Rect(40, 40, 10, 10) print(a.collidepoint(25, 25), a.collidepoint(50, 50)) # True False (right/bottom edges are outside) walls = [pygame.Rect(100, 0, 10, 10), pygame.Rect(45, 45, 10, 10)] print(a.collidelist(walls), a.collidelistall(walls)) # 1 [1] print(pygame.Rect(0, 0, 10, 10).union(pygame.Rect(20, 20, 10, 10))) # Rect(0, 0, 30, 30)