【问题标题】:How do I draw a rectangle at the center of another rectangle in pygame?如何在pygame中另一个矩形的中心绘制一个矩形?
【发布时间】:2018-08-01 14:35:00
【问题描述】:

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

【问题讨论】:

    标签: python pygame rect


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      相关资源
      最近更新 更多