2017-10-08 178 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.