Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- # Window size
- WINDOW_WIDTH = 800
- WINDOW_HEIGHT = 400
- WINDOW_SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
- DARK_BLUE = ( 3, 5, 54 )
- class RotationSprite( pygame.sprite.Sprite ):
- def __init__( self, image, x, y, ):
- pygame.sprite.Sprite.__init__(self)
- self.original = image
- self.image = image
- self.rect = self.image.get_rect()
- self.rect.center = ( x, y )
- # for maintaining trajectory
- self.position = pygame.math.Vector2( ( x, y ) )
- self.velocity = pygame.math.Vector2( ( 0, 0 ) )
- def lookAt( self, co_ordinate ):
- # Rotate image to point in the new direction
- delta_vector = co_ordinate - self.position
- radius, angle = delta_vector.as_polar()
- self.image = pygame.transform.rotozoom( self.original, -angle, 1 )
- # Re-set the bounding rectagle
- current_pos = self.rect.center
- self.rect = self.image.get_rect()
- self.rect.center = current_pos
- ### initialisation
- pygame.init()
- pygame.mixer.init()
- window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), WINDOW_SURFACE )
- pygame.display.set_caption( "Rotation Example" )
- ### Missiles!
- rotation_sprites = pygame.sprite.Group()
- rotation_image1 = pygame.image.load( 'canss.png' )
- rotation_image2 = pygame.image.load( 'canss.png' )
- rotation_sprites.add( RotationSprite( rotation_image1, WINDOW_WIDTH//3, WINDOW_HEIGHT//2 ) )
- rotation_sprites.add( RotationSprite( rotation_image2, 2*( WINDOW_WIDTH//3 ), WINDOW_HEIGHT//2 ) )
- ### Main Loop
- clock = pygame.time.Clock()
- done = False
- while not done:
- # Handle user-input
- for event in pygame.event.get():
- if ( event.type == pygame.QUIT ):
- done = True
- elif ( event.type == pygame.MOUSEBUTTONUP ):
- # On mouse-click
- pass
- # Record mouse movements for positioning the paddle
- mouse_pos = pygame.mouse.get_pos()
- for m in rotation_sprites:
- m.lookAt( mouse_pos )
- rotation_sprites.update()
- # Update the window, but not more than 60 FPS
- window.fill( DARK_BLUE )
- rotation_sprites.draw( window )
- pygame.display.flip()
- # Clamp FPS
- clock.tick_busy_loop(60)
- pygame.quit()
Add Comment
Please, Sign In to add comment