2017-10-08 177 views
1

我想在鼠标位置绘制一个圆,当我单击鼠标但它不工作。它在我被告知要通过互联网进行的while循环中,但它仍然无法工作。有人可以请帮助。谢谢。当鼠标点击鼠标时(pygame)没有绘制圆圈

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

回答

1

您编码

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

时出现了拼写错误,我认为你的意思是输入:

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

经常检查错误日志:它应该告诉你,你犯了一个错误

+0

我刚刚修好了,但它仍然无法正常工作 –

0

您必须致电pygame.display.flip()更新显示屏,然后修复circ/circa错字。

一些建议:增加一个pygame.time.Clock来限制帧速率。

鼠标事件具有pos属性,因此您可以用event.pos替换circ变量。可以在while循环之外定义和planet_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.