If you have 2 pygame.Rect
objects, you can move the 2nd rectangle on the 1st rectangle by setting the bottom
of the 2nd rectangle with the top
of the 1st rectangle. With the virtual attributes of pygame.Rect
it is easy to do so:
rect2.bottom = rect1.top
Or if you have 2 pygame.sprite.Sprite
objects:
sprite2.rect.bottom = sprote1.rect.top
If you have 2 pygame.Surface
objects (surf1
and surf2
) and the 2 corresponding positions (x1
, y1
) and (x2
, y2
), use get_rect()
to create pygame.Rect
objects from the Surfaces. Move the 2nd rectangle on the 1st rectangle and update the position of the 2nd rectangle:
rect1 = surf1.get_rect(topleft = (x1, y1))
rect2 = surf2.get_rect(topleft = (x2, y2))
rect2.bottom = rect1.top
y2 = rect2.y
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…