Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # pygame simple template - by furas
- import pygame
- import random
- ''' click butterfly to freeze/unfreeze it '''
- # === CONSTANTS === UPPER_CASE names
- SCREEN_WIDTH = 800
- SCREEN_HEIGHT = 600
- FPS = 10
- BLACK = ( 0, 0, 0)
- WHITE = (255, 255, 255)
- RED = (255, 0, 0)
- GREEN = ( 0, 255, 0)
- BLUE = ( 0, 0, 255)
- CYAN = ( 0, 255, 255)
- # === CLASSES === CamelCase names
- class Butterfly:
- def __init__(self):
- img1 = pygame.Surface( (50,50) )
- img1.fill(RED)
- img2 = pygame.Surface( (50,50) )
- img2.fill(GREEN)
- img3 = pygame.Surface( (50,50) )
- img3.fill(BLUE)
- # animation frames
- self.images = [
- #pygame.image.load('butterflya.png'),
- #pygame.image.load('butterflyb.png'),
- img1,
- img2,
- img3,
- ]
- # numbers of frames
- self.images_count = len(self.images)
- # start at random frame
- self.current_image = random.randint(0, self.images_count-1)
- # image of freezed butterfly
- self.image_of_freezed_butterfly = pygame.Surface( (50,50) )
- self.image_of_freezed_butterfly.fill(BLACK)
- # current image to draw
- self.image = self.images[self.current_image]
- # currrent positon
- self.rect = self.images[0].get_rect()
- self.set_random_position()
- # start as unfreezed
- self.freezed = False
- def set_random_position(self):
- self.rect.x = random.randint(0, SCREEN_WIDTH-self.rect.width)
- self.rect.y = random.randint(0, SCREEN_HEIGHT-self.rect.height)
- def update(self, surface_rect):
- if not self.freezed:
- self.rect.x += random.randint(-10,10)
- self.rect.y += random.randint(-10,10)
- self.rect.clamp_ip(surface_rect)
- self.current_image = (self.current_image + 1) % self.images_count
- self.image = self.images[self.current_image]
- else:
- self.image = self.image_of_freezed_butterfly
- def draw(self, surface):
- surface.blit(self.image, self.rect)
- def event_handle(self, event):
- if event.type == pygame.MOUSEBUTTONDOWN:
- if event.button == 1: # left button
- if self.rect.collidepoint(event.pos):
- print("butterfly clicked at", event.pos)
- # feeze/unfreez on click
- self.freezed = not self.freezed
- # === FUNCTIONS === lower_case names
- # empty
- # === MAIN ===
- # --- init ---
- pygame.init()
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- screen_rect = screen.get_rect()
- pygame.display.set_caption('Butterflies')
- # --- objects ---
- net = []
- for _ in range (15):
- net.append( Butterfly() )
- # --- mainloop
- clock = pygame.time.Clock()
- running = True
- while running:
- # --- events ---
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- running = False
- # --- objects event ---
- for butterfly in net:
- butterfly.event_handle(event)
- # --- updates (without draws) ---
- for butterfly in net:
- butterfly.update(screen_rect)
- # --- draws (without updates) ---
- screen.fill(CYAN)
- for butterfly in net:
- butterfly.draw(screen)
- pygame.display.update()
- clock.tick(FPS)
- # --- the end ---
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement