2015-10-04 91 views
0

我想在MOUSEBUTTONCLICK被激活時不斷旋轉此圖像,但是當我運行MOUSEBUTTONCLICK時沒有任何事情發生。這是我嘗試過的代碼。旋轉鼠標按鈕上的圖像單擊pygame

while True: 
    'RotatingImg' 
    image_surf = pygame.image.load('imagefile') 
    image_surf = pygame.transform.scale(image_surf, (810,810)) 
    image_surfpos = image_surf.get_rect() 
    screen.blit(image_surf, image_surfpos) 
    degree = 0 
    pygame.display.update() 
    for event in pygame.event.get(): 
     '''Quit Button''' 
     if event.type == pygame.QUIT: 
      pygame.quit() 
     elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: 
      ## if mouse is pressed get position of cursor ## 
      screen.blit(target1, target1pos) 

      'RotatingImg' 
      image_surf = pygame.image.load('C:\\Users\Leyton\Documents\Coding Stuff\Python\Apps\Fan_blades.png') 
      image_surf = pygame.transform.scale(image_surf, (810,810)) 
      image_surfpos = image_surf.get_rect() 
      screen.blit(image_surf, image_surfpos) 
      degree = 0 
      blittedRect = screen.blit(image_surf, image_surfpos) 
      'Get center of surf for later' 
      oldCenter = blittedRect.center 
      'rotate surf by amount of degrees' 
      rotatedSurf = pygame.transform.rotate(image_surf, degree) 
      'Get the rect of the rotated surf and set its center to the old center' 
      rotRect = rotatedSurf.get_rect() 
      rotRect.center = oldCenter 
      'Draw rotatedSurf with the corrected rect so it gets put in proper spot' 
      screen.blit(rotatedSurf, rotRect) 

      'Change the degree of rotation' 
      degree += 5 

      if degree > 0: 
       degree = 0 

      'Show the screen Surface' 
      pygame.display.flip() 
      'Wait 0 ms for loop to restart' 
      pygame.time.wait(0) 



     elif event.type == pygame.MOUSEBUTTONUP and event.button == 1: 
      screen.blit(target, targetpos) 

如果 == '主要': 遊戲()

回答

0

1:不要在事件循環加載圖像。你會想要保持一個非旋轉的版本,然後根據需要旋轉它。

2:pygame.MOUSEBUTTONDOWN只有當您第一次單擊鼠標按鈕時纔會關閉。按住按鈕不會觸發事件。

3:您正在重設學位兩次;首先,將其設置爲0,然後如果它大於0,則將其設置爲0.

可能還有其他問題。

+0

我可以使用什麼樣的事件,讓圖片繼續旋轉而不是鼠標按鈕 – Leyton

+0

沒有這樣的事件。你可能需要某種「is_pressed」變量,在鼠標向下/向上設置/取消設置,或每幀調用pygame.mouse.get_pressed()。 – user3757614