2013-10-11 21 views
5

Sono nuovo in Pygame e voglio scrivere del codice che ruota semplicemente un'immagine di 90 gradi ogni 10 secondi. Mio codice è simile:Ruota immagine usando 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() 

Questo codice non funziona cioè l'immagine non ruota, anche se "rotazione" viene stampato nel terminale ogni 10 secondi. Qualcuno può dirmi cosa sto facendo di sbagliato?

risposta

7

pygame.transform.rotate non ruota il Surface in posizione, ma restituisce un nuovo, ruotato Surface. Anche se modificasse lo esistente, dovresti di nuovo blenderarlo sulla superficie del display.

Quello che dovresti fare è tenere traccia dell'angolo in una variabile, aumentarlo di 90 ogni 10 secondi e blittare il nuovo Surface sullo schermo, ad es.

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