2013-10-11 11 views
5

私はpygameが初めてで、画像を10秒ごとに90度だけ回転させるコードを書きたいと思っています。私のコードは次のようになります。画像が回転していない、つまり「回転」が、端末ごとに10秒で印刷されているが、このコードは、動作していないpygameを使用して画像を回転

import pygame 
    import time 
    from pygame.locals import * 
    pygame.init() 
    display_surf = pygame.display.set_mode((1200, 1200)) 
    image_surf = pygame.image.load("/home/tempuser/Pictures/desktop.png").convert() 
    imagerect = image_surf.get_rect() 
    display_surf.blit(image_surf,(640, 480)) 
    pygame.display.flip() 
    start = time.time() 
    new = time.time() 
    while True: 
     end = time.time() 
     if end - start > 30: 
      break 
     elif end - new > 10: 
      print "rotating" 
      new = time.time() 
      pygame.transform.rotate(image_surf,90) 
      pygame.display.flip() 

。誰かが私が間違っていることを教えてもらえますか?

答えて

7

pygame.transform.rotateは、Surfaceを回転させず、新しい回転済みのSurfaceを返します。既存のSurfaceを変更しても、それを再び表示面にblitする必要があります。

あなたがすべきことは、変数内の角度を追跡し、それを90ごとに10秒ごとに増加させ、新しいSurfaceを画面にblitすることです。

angle = 0 
... 
while True: 
    ... 
    elif end - new > 10: 
     ... 
     # increase angle 
     angle += 90 
     # ensure angle does not increase indefinitely 
     angle %= 360 
     # create a new, rotated Surface 
     surf = pygame.transform.rotate(image_surf, angle) 
     # and blit it to the screen 
     display_surf.blit(surf, (640, 480)) 
     ... 
関連する問題