如何在 pygame 中的另一个矩形的中心绘制一个矩形?

How do I draw a rectangle at the center of another rectangle in pygame?

我有一个矩形,比如 rect1 。我有另一个名为 rect2 的矩形。我想 blit 'rect2' 使得 rect2 的中心与 rect1 的中心相同?

只需将rect1的center坐标赋给rect2的center即可。

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')

rect1 = pg.Rect(200, 100, 161, 100)
rect2 = pg.Rect(0, 0, 120, 74)
rect2.center = rect1.center

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True

    screen.fill(BG_COLOR)
    pg.draw.rect(screen, (0, 100, 255), rect1, 2)
    pg.draw.rect(screen, (255, 128, 0), rect2, 2)
    pg.display.flip()
    clock.tick(60)