gpt4 book ai didi

python - 如何移动一个矩形及其关联的绘图(Pygame)?

转载 作者:行者123 更新时间:2023-11-28 21:38:02 24 4
gpt4 key购买 nike

最小工作示例:

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

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

最佳答案

您可以使用 pygame.Rect要存储对象的位置,请在 while 循环中更新矩形的属性(例如 .x.center),然后在rect.center 位置。

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()

关于python - 如何移动一个矩形及其关联的绘图(Pygame)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48733823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com