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

时间:2018-08-01 14:35:01

标签: python pygame rect

我有一个矩形,例如rect1。我有另一个矩形叫rect2。我想对“ rect2”进行对齐,以使rect2的中心与rect1的中心相同?

1 个答案:

答案 0 :(得分:1)

只需将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)