【问题标题】:How to move a rect together with its associated drawing (Pygame)?如何移动矩形及其相关绘图(Pygame)?
【发布时间】:2023-03-16 07:20:02
【问题描述】:

最小的工作示例:

x = pygame.draw.circle(screen, color, (x, y), radius, width)
x.center = (my_wanted_center_x, my_wanted_center_y)

然后在更新显示后,它始终显示在其原始位置。也尝试了其他数字。 x.move() 也不起作用。如何以最简单的方式移动任何绘制的图形?

【问题讨论】:

    标签: python pygame drawing rectangles


    【解决方案1】:

    您可以使用pygame.Rect 来存储对象的位置,在while 循环中更新矩形的属性(例如.x.center),然后在@987654325 处绘制您的圆圈或图像/表面@位置。

    import pygame as pg
    
    pg.init()
    
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()
    BG_COLOR = pg.Color('gray12')
    SIENNA = pg.Color('sienna1')
    radius = 20
    # This rect serves as the position of the circle and
    # can be used for collision detection.
    rect = pg.Rect(50, 200, radius, radius)
    
    done = False
    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
    
        rect.x += 1  # Update the position of the rect.
    
        screen.fill(BG_COLOR)
        # Now draw the circle at the center of the rect.
        pg.draw.circle(screen, SIENNA, rect.center, radius)
        pg.display.flip()
        clock.tick(30)
    
    pg.quit()
    

    【讨论】:

      猜你喜欢
      • 2014-12-02
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2021-02-06
      • 2016-06-19
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多