Advertisement
furas

Python - Pygame - butterflies example

Jan 6th, 2016
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.76 KB | None | 0 0
  1. # pygame simple template - by furas
  2.  
  3. import pygame
  4. import random
  5.  
  6. ''' click butterfly to freeze/unfreeze it '''
  7.  
  8. # === CONSTANTS === UPPER_CASE names
  9.  
  10. SCREEN_WIDTH = 800
  11. SCREEN_HEIGHT = 600
  12.  
  13. FPS = 10
  14.  
  15. BLACK = (  0,   0,   0)
  16. WHITE = (255, 255, 255)
  17.  
  18. RED   = (255,   0,   0)
  19. GREEN = (  0, 255,   0)
  20. BLUE  = (  0,   0, 255)
  21.  
  22. CYAN  = (  0, 255, 255)
  23.  
  24. # === CLASSES === CamelCase names
  25.  
  26. class Butterfly:
  27.  
  28.     def __init__(self):
  29.  
  30.         img1 = pygame.Surface( (50,50) )
  31.         img1.fill(RED)
  32.  
  33.         img2 = pygame.Surface( (50,50) )
  34.         img2.fill(GREEN)
  35.        
  36.         img3 = pygame.Surface( (50,50) )
  37.         img3.fill(BLUE)
  38.  
  39.         # animation frames        
  40.         self.images = [
  41.             #pygame.image.load('butterflya.png'),
  42.             #pygame.image.load('butterflyb.png'),
  43.             img1,
  44.             img2,
  45.             img3,
  46.         ]
  47.  
  48.         # numbers of frames
  49.         self.images_count = len(self.images)
  50.        
  51.         # start at random frame
  52.         self.current_image = random.randint(0, self.images_count-1)
  53.  
  54.         # image of freezed butterfly
  55.         self.image_of_freezed_butterfly = pygame.Surface( (50,50) )
  56.         self.image_of_freezed_butterfly.fill(BLACK)
  57.  
  58.         # current image to draw
  59.         self.image = self.images[self.current_image]
  60.        
  61.         # currrent positon
  62.         self.rect = self.images[0].get_rect()
  63.         self.set_random_position()
  64.        
  65.         # start as unfreezed
  66.         self.freezed = False
  67.        
  68.        
  69.     def set_random_position(self):
  70.         self.rect.x = random.randint(0, SCREEN_WIDTH-self.rect.width)
  71.         self.rect.y = random.randint(0, SCREEN_HEIGHT-self.rect.height)
  72.  
  73.  
  74.     def update(self, surface_rect):
  75.         if not self.freezed:
  76.             self.rect.x += random.randint(-10,10)
  77.             self.rect.y += random.randint(-10,10)
  78.             self.rect.clamp_ip(surface_rect)
  79.            
  80.             self.current_image = (self.current_image + 1) % self.images_count
  81.             self.image = self.images[self.current_image]
  82.         else:
  83.             self.image = self.image_of_freezed_butterfly
  84.  
  85.        
  86.     def draw(self, surface):
  87.         surface.blit(self.image, self.rect)
  88.        
  89.  
  90.     def event_handle(self, event):
  91.  
  92.         if event.type == pygame.MOUSEBUTTONDOWN:
  93.             if event.button == 1: # left button
  94.                 if self.rect.collidepoint(event.pos):
  95.                     print("butterfly clicked at", event.pos)
  96.                    
  97.                     # feeze/unfreez on click
  98.                     self.freezed = not self.freezed
  99.  
  100.                
  101. # === FUNCTIONS === lower_case names
  102.  
  103. # empty
  104.  
  105. # === MAIN ===
  106.  
  107. # --- init ---
  108.  
  109. pygame.init()
  110.  
  111. screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  112. screen_rect = screen.get_rect()
  113.  
  114. pygame.display.set_caption('Butterflies')
  115.  
  116. # --- objects ---
  117.  
  118. net = []
  119.  
  120. for _ in range (15):
  121.     net.append( Butterfly() )
  122.  
  123. # --- mainloop
  124.  
  125. clock = pygame.time.Clock()
  126.  
  127. running = True
  128.  
  129. while running:
  130.  
  131.     # --- events ---
  132.    
  133.     for event in pygame.event.get():
  134.         if event.type == pygame.QUIT:
  135.             running = False
  136.            
  137.         if event.type == pygame.KEYDOWN:
  138.             if event.key == pygame.K_ESCAPE:
  139.                 running = False
  140.          
  141.         # --- objects event ---
  142.  
  143.         for butterfly in net:
  144.             butterfly.event_handle(event)
  145.                
  146.     # --- updates (without draws) ---
  147.    
  148.     for butterfly in net:
  149.         butterfly.update(screen_rect)
  150.        
  151.     # --- draws (without updates) ---
  152.    
  153.     screen.fill(CYAN)
  154.    
  155.     for butterfly in net:
  156.         butterfly.draw(screen)
  157.  
  158.     pygame.display.update()
  159.    
  160.     clock.tick(FPS)
  161.    
  162. # --- the end ---
  163.  
  164. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement