【问题标题】:Circle isn't being drawn upon mouse click (pygame)鼠标单击时未绘制圆圈(pygame)
【发布时间】:2017-10-08 14:12:40
【问题描述】:

我想在单击鼠标时在鼠标位置画一个圆圈,但它不起作用。它在 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()

【问题讨论】:

    标签: python pygame mouseevent draw geometry


    【解决方案1】:

    你写错字了

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

    我想你的意思是输入:

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

    经常检查错误日志:它应该会告诉你哪里出错了

    【讨论】:

      【解决方案2】:

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

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

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

      planet_colorplanet_radius 可以在 while 循环之外定义。

      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.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-02
        • 2018-11-12
        • 1970-01-01
        • 1970-01-01
        • 2018-08-12
        • 1970-01-01
        相关资源
        最近更新 更多