Advertisement
zeroblitzt

Pygame Rotation... Need help!

Feb 18th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. #pygame Test by KNovak
  2. #need some help... can't seem to reconcile the sprite rotation with the sprite group update
  3. #vs the rect blit function!
  4.  
  5. import pygame, sys, os
  6. from pygame.locals import *
  7. pygame.init()
  8. pygame.key.set_repeat(1)
  9. fullscreen = False;
  10.  
  11. #startup settings
  12. for arg in sys.argv:
  13.     if arg == "-f":
  14.         fullscreen = True
  15.  
  16.  
  17. #Video Display information
  18. vidinfo = pygame.display.Info()
  19.  
  20. if fullscreen == True:
  21.     vidsize = (vidinfo.current_w, vidinfo.current_h)
  22.     window = pygame.display.set_mode(vidsize, pygame.FULLSCREEN)
  23. else:
  24.     window = pygame.display.set_mode((640,480))
  25. pygame.display.set_caption('KNova Test Window')
  26. screen = pygame.display.get_surface()
  27. background = pygame.Surface((screen.get_width(), screen.get_height()))
  28.  
  29. #colors
  30. WHITE = (255,255,255)
  31. BLUE = (0,0,150)
  32. GREEN = (0,150,0)
  33.  
  34. #text
  35. def drawText (text, color, xloc, yloc, size):
  36.     fontObj = pygame.font.Font('freesansbold.ttf', size)
  37.     textSurface = fontObj.render(text, True, color)
  38.     textRectObj = textSurface.get_rect()
  39.     textRectObj.center = (xloc, yloc)
  40.     return textSurface, textRectObj
  41.  
  42. #Sprite Class for the Ship Image
  43. class Ship(pygame.sprite.Sprite):
  44.     image = pygame.image.load("./resources/ship.png")
  45.     image= image.convert()
  46.    
  47.     #initialization function
  48.     def __init__(self, position=(100,100)):
  49.         pygame.sprite.Sprite.__init__(self,self.groups)
  50.         self.pos = position
  51.         self.image = Ship.image
  52.         self.rect = self.image.get_rect()
  53.         #initial drawing
  54.         self.rect.center = self.pos
  55.         self.angle = 0
  56.    
  57.     #move function
  58.     def move(self, dx, dy):
  59.         oldx, oldy = self.pos
  60.         self.pos = (oldx + dx, oldy - dy)
  61.         self.rect.center = self.pos
  62.        
  63.     #rotate - it's counterclockwise by default
  64.     def rotate(self):
  65.         if self.angle >= 360:
  66.             self.angle = 0
  67.         if self.angle <= -360:
  68.             self.angle = 0
  69.         loc = self.rect.center
  70.         rotimage = pygame.transform.rotate(self.image, self.angle)
  71.         rotrect = rotimage.get_rect()
  72.         rotrect.center = loc
  73.         screen.blit(rotimage,rotrect)
  74.         print rotrect
  75.        
  76.        
  77. spriteGroup = pygame.sprite.Group()
  78. Ship.groups = spriteGroup
  79. newShip = Ship()
  80.  
  81. #sprite group update
  82. def spriteUpdate(group, screen, background):
  83.     group.clear(screen, background)
  84.     group.update()
  85.     group.draw(screen)
  86.    
  87.  
  88.    
  89.  
  90. #get keyboard events
  91. def pyin(events):
  92.     for event in events:
  93.         if event.type == QUIT:
  94.             sys.exit(0)
  95.         if event.type == KEYDOWN:
  96.             key = pygame.key.get_pressed()
  97.             if (key[pygame.K_ESCAPE]):
  98.                 sys.exit(0)
  99.             if (key[K_UP]):
  100.                 newShip.move(0,3)
  101.             if (key[K_DOWN]):
  102.                 newShip.move(0,-3)
  103.             if (key[K_LEFT]):
  104.                 newShip.angle += 3
  105.                 newShip.rotate()
  106.             if (key[K_RIGHT]):
  107.                 newShip.angle -= 3
  108.                 newShip.rotate()               
  109.  
  110.  
  111. while True:
  112.     pyin(pygame.event.get())
  113. #   pykey(pygame.key.get_pressed())
  114.     pygame.display.update()
  115.     spriteUpdate(spriteGroup, screen, background)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement