Pygame 简明教程
Pygame - Moving Rectangular Objects
Pygame.Rect 类具有存储和处理矩形区域的功能。Rect 对象可以从 left、top、width 和 height 值构造。Rect 类中的函数允许复制、移动和调整 Rect 对象的大小。
The Pygame.Rect class has functionality to store and manipulate rectangular areas. A Rect object can be constructed from left, top, width and height values. Functions in Rect class enable copying, moving nd resizing the Rect object.
Rect 对象具有以下虚拟属性 −
A Rect object has following virtual attributes −

除运动之外,Rect 类还有方法用于测试矩形之间的碰撞。
In addition to movement, Rect class has methods to test collision between rectangles.
copy() |
Returns a new rectangle having the same position and size as the original. |
move() |
Returns a new rectangle that is moved by the given offset. The x and y arguments can be any integer value, positive or negative. |
move_ip() |
Same as the Rect.move() method, but operates in place. |
inflate(x,y) |
Returns a new rectangle with the size changed by the given offset. Negative values will shrink the rectangle. |
inflate_ip(x, y) |
Same as the Rect.inflate() method, but operates in place. |
clamp(Rect) |
Returns a new rectangle that is moved to be completely inside the argument Rect. |
clip(Rect) |
Returns a new rectangle that is cropped to be completely inside the argument Rect. |
union(Rect) |
Returns a new rectangle that completely covers the area of the two provided rectangles. |
union_ip(Rect) |
Same as the Rect.union() method, but operates in place. |
contains(Rect) |
Returns true when the argument is completely inside the Rect. |
collidepointx,y |
Returns true if the given point is inside the rectangle. |
colliderect(Rect) |
Returns true if any portion of either rectangle overlap |
Example
在以下程序中,以红色轮廓绘制一个 Rect 对象。使用 copy() 方法,为其创建克隆以供移动。移动通过 move_ip() 方法生效。箭头键通过将 x/y 坐标递增/递减 + 或 -5 像素来移动复制的矩形的位置。
In the following program, a Rect object is drawn with red outline. Using copy() method, its clone is created for movement. The movement is effected by move_ip() method. The arrow keys move the position of copied rectangle by incrementing/decrementing x/y coordinate by + or -5 pixels.
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
screen = pygame.display.set_mode((400,300))
rect1 = Rect(50, 60, 200, 80)
rect2=rect1.copy()
running = True
x=0
y=0
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == KEYDOWN:
if event.key==K_LEFT:
x= -5
y=0
if event.key == K_RIGHT:
x=5
y=0
if event.key == K_UP:
x = 0
y = -5
if event.key == K_DOWN:
x = 0
y = 5
rect2.move_ip(x,y)
screen.fill((127,127,127))
pygame.draw.rect(screen, (255,0,0), rect1, 1)
pygame.draw.rect(screen, (0,0,255), rect2, 5)
pygame.display.update()
pygame.quit()
Output
以下输出显示带有红色轮廓的矩形就是原始矩形。其副本不断移动以响应箭头键,并且有蓝色轮廓
The following output shows rectangle with red outline is the original rectangle. Its copy keeps moving responding to arrow keys and has blue outline

Example
将 move_ip() 方法更改为 inflate_ip() 方法可根据按下的箭头来增大/缩小矩形。
Changing the move_ip() method to inflate_ip() method to grow/shrink the rectangle depending upon the arrow pressed.
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == KEYDOWN:
if event.key==K_LEFT:
x= -5
y=0
if event.key == K_RIGHT:
x=5
y=0
if event.key == K_UP:
x = 0
y = -5
if event.key == K_DOWN:
x = 0
y = 5
rect2.inflate_ip(x,y)
screen.fill((127,127,127))
pygame.draw.rect(screen, (255,0,0), rect1, 1)
pygame.draw.rect(screen, (0,0,255), rect2, 5)
pygame.display.update()
Example
要通过检测 MOUSEMOTION 事件使矩形移动,我们需要首先在原始矩形内部按下鼠标。要验证鼠标位置是否在矩形内部,我们使用 Rect 对象的 collidepoint() 方法。当鼠标移动时,矩形对象通过 move_ip() 方法就地移动。当鼠标释放时,移动停止。
To make the rectangle move by detecting MOUSEMOTION event, we need to first press the mouse inside the original rectangle. To verify whether mouse position is inside the rectangle, we use collidepoint() method of the Rect object. While the mouse is in motion, the rectangle object is moved in place by move_ip() method. Movement shall stop when mouse is released.
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
screen = pygame.display.set_mode((400,300))
rect = Rect(50, 60, 200, 80)
moving = False
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == MOUSEBUTTONDOWN:
if rect.collidepoint(event.pos):
moving = True
elif event.type == MOUSEBUTTONUP:
moving = False
elif event.type == MOUSEMOTION and moving:
rect.move_ip(event.rel)
screen.fill((127,127,127))
pygame.draw.rect(screen, (255,0,0), rect)
if moving:
pygame.draw.rect(screen, (0,0,255), rect, 4)
pygame.display.flip()
pygame.quit()
Example
要通过鼠标绘制矩形,请在 MOUSEBUTTONDOWN 和 MOUSEBUTTONUP 事件中捕获鼠标指针坐标,计算左上角坐标、宽度和高度并调用 rect() 函数。
To draw rectangle by mouse, capture the mouse pointer coordinates in MOUSEBUTTONDOWN and MOUSEBUTTONUP events, calculate the topleft coordinates, width and height and call rect() function.
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
screen = pygame.display.set_mode((400,300))
pygame.display.set_caption("Draw Rectangle with Mouse")
screen.fill((127,127,127))
x=0
y=0
w=0
h=0
drawmode=True
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == MOUSEBUTTONDOWN:
x,y = pygame.mouse.get_pos()
drawmode = True
if event.type == MOUSEBUTTONUP:
x1,y1 = pygame.mouse.get_pos()
w=x1-x
h=y1-y
drawmode= False
rect = pygame.Rect(x,y,w,h)
if drawmode == False:
pygame.draw.rect(screen, (255,0,0), rect)
pygame.display.flip()
pygame.quit()