habib257

Untitled

Jun 24th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. # Window size
  5. WINDOW_WIDTH    = 800
  6. WINDOW_HEIGHT   = 400
  7. WINDOW_SURFACE  = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
  8.  
  9. DARK_BLUE = (   3,   5,  54 )
  10.  
  11. class RotationSprite( pygame.sprite.Sprite ):
  12.     def __init__( self, image, x, y, ):
  13.         pygame.sprite.Sprite.__init__(self)
  14.         self.original = image
  15.         self.image    = image
  16.         self.rect     = self.image.get_rect()
  17.         self.rect.center = ( x, y )
  18.         # for maintaining trajectory
  19.         self.position    = pygame.math.Vector2( ( x, y ) )
  20.         self.velocity    = pygame.math.Vector2( ( 0, 0 ) )
  21.  
  22.     def lookAt( self, co_ordinate ):
  23.         # Rotate image to point in the new direction
  24.         delta_vector  = co_ordinate - self.position
  25.         radius, angle = delta_vector.as_polar()
  26.         self.image    = pygame.transform.rotozoom( self.original, -angle, 1 )
  27.         # Re-set the bounding rectagle
  28.         current_pos      = self.rect.center
  29.         self.rect        = self.image.get_rect()
  30.         self.rect.center = current_pos
  31.  
  32.  
  33.  
  34. ### initialisation
  35. pygame.init()
  36. pygame.mixer.init()
  37. window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), WINDOW_SURFACE )
  38. pygame.display.set_caption( "Rotation Example" )
  39.  
  40. ### Missiles!
  41. rotation_sprites = pygame.sprite.Group()
  42. rotation_image1 = pygame.image.load( 'canss.png' )
  43. rotation_image2 = pygame.image.load( 'canss.png' )
  44. rotation_sprites.add( RotationSprite( rotation_image1, WINDOW_WIDTH//3, WINDOW_HEIGHT//2 ) )
  45. rotation_sprites.add( RotationSprite( rotation_image2, 2*( WINDOW_WIDTH//3 ), WINDOW_HEIGHT//2 ) )
  46.  
  47.  
  48. ### Main Loop
  49. clock = pygame.time.Clock()
  50. done = False
  51. while not done:
  52.  
  53.     # Handle user-input
  54.     for event in pygame.event.get():
  55.         if ( event.type == pygame.QUIT ):
  56.             done = True
  57.         elif ( event.type == pygame.MOUSEBUTTONUP ):
  58.             # On mouse-click
  59.             pass
  60.  
  61.     # Record mouse movements for positioning the paddle
  62.     mouse_pos = pygame.mouse.get_pos()
  63.     for m in rotation_sprites:
  64.         m.lookAt( mouse_pos )
  65.     rotation_sprites.update()
  66.  
  67.     # Update the window, but not more than 60 FPS
  68.     window.fill( DARK_BLUE )
  69.     rotation_sprites.draw( window )
  70.     pygame.display.flip()
  71.  
  72.     # Clamp FPS
  73.     clock.tick_busy_loop(60)
  74.  
  75. pygame.quit()
Add Comment
Please, Sign In to add comment