我怎样才能阻止它消失?

2024-04-25 23:53:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在这里做一个pygame代码,但它没有按照我的想法工作,我想修复屏幕上的这个圆圈,你们能帮我吗

while True:
    clock.tick(60)

    window.blit(bg, (0,0))

    # circulo #

    for event in pg.event.get():
        if event.type == pg.KEYDOWN:
            if event.key == pg.K_q:
                clock.tick(1)
                pg.draw.circle(window, (5,5,5), [120, 120], 60, 1)
                continue

Tags: 代码eventtrueif屏幕windowpygamebg
1条回答
网友
1楼 · 发布于 2024-04-25 23:53:00

问题是您(可能)正在用window.blit(bg, (0,0))清除屏幕上的每一帧

然后,如果用户按q,则仅针对该帧绘制一个圆。再过几毫秒(可能小于1)后,重新绘制背景。因此,根据您的系统,人类可能永远不会注意到正在绘制的圆

解决这一问题的一种方法是设置一个布尔“标志”,以便重新绘制圆,直到标志再次更改

draw_circle = False   # Should the circle be drawn?

while True:
    clock.tick(60)

    # re-draw the scene
    window.blit(bg, (0,0))

    # circulo #
    if draw_circle:
        pg.draw.circle(window, (5,5,5), [120, 120], 60, 1)

    for event in pg.event.get():
        if event.type == pg.KEYDOWN:
            if event.key == pg.K_q:
                draw_circle = not draw_circle  # toggle circle on/off

相关问题 更多 >