单击鼠标时不会绘制圆圈(pygame)

时间:2017-10-08 14:12:40

标签: python pygame mouseevent draw geometry

我想在单击鼠标时在鼠标位置绘制一个圆圈,但它不起作用。它就像我被告知通过互联网做的那样在while循环中,但它仍然无法正常工作。有人可以请帮助。感谢。

def run_game():
    screen_height = 670
    screen_width = 1270
    pygame.init()
    screen = pygame.display.set_mode((screen_width, screen_height))
    screen.fill((10,10,30))
    running = True

    pygame.display.flip()
    while running:
        planet_color = (255,0,0)
        planet_radius = 100
        circ = pygame.mouse.get_pos()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                pygame.draw.circle(screen, planet_color, (circa), planet_radius, 0)
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    running = False


run_game()

2 个答案:

答案 0 :(得分:1)

您在编码

时输入了拼写错误
pygame.draw.circle(screen, planet_color, (circa), planet_radius, 0)

我认为你打算输入:

pygame.draw.circle(screen, planet_color, (circ), planet_radius, 0)

始终检查错误日志:它应该告诉您错误的位置

答案 1 :(得分:0)

您必须致电pygame.display.flip()更新显示内容,当然还要修复circ / circa错字。

一些建议:添加pygame.time.Clock以限制帧速率。

鼠标事件具有pos属性,因此您可以将circ变量替换为event.pos

可以在while循环之外定义planet_colorplanet_radius

planet_color = (255,0,0)
planet_radius = 100
clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            pygame.draw.circle(screen, planet_color, event.pos, planet_radius)
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
                running = False

    pygame.display.flip()  # Call flip() each frame.
    clock.tick(60)  # Limit the game to 60 fps.