Advertisement
furas

PyGame - Drag Circles & Click Buttons

Feb 12th, 2016
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.89 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. #
  4. # pygame (simple) template - by furas
  5. #
  6. # https://github.com/furas/my-python-codes/tree/master/pygame/__template__/
  7. #
  8. # http://pastebin.com/9VdUEPXi
  9. #
  10.  
  11. # ---------------------------------------------------------------------
  12.  
  13. __author__  = 'Bartlomiej "furas" Burek'
  14. __webpage__ = 'http://blog.furas.pl'
  15.  
  16. # ---------------------------------------------------------------------
  17.  
  18. import pygame
  19.  
  20. # === CONSTANS === (UPPER_CASE names)
  21.  
  22. BLACK = (  0,   0,   0)
  23. WHITE = (255, 255, 255)
  24.  
  25. RED   = (255,   0,   0)
  26. GREEN = (  0, 255,   0)
  27. BLUE  = (  0,   0, 255)
  28.  
  29. SCREEN_WIDTH  = 600
  30. SCREEN_HEIGHT = 400
  31.  
  32. BLOCK_SIZE = 50
  33. CIRCLE_RADIUS = int(BLOCK_SIZE/2)
  34.  
  35. # === CLASSES === (CamelCase names)
  36.  
  37. class Button():
  38.  
  39.     def __init__(self, text='OK', pos=(0,0), size=(100,50), command=None):
  40.         font = pygame.font.SysFont(None, 35)
  41.    
  42.         self.text = text
  43.         self.rect = pygame.Rect((0,0), size)
  44.  
  45.         self.image_normal = pygame.Surface(size)
  46.         self.image_normal.fill(WHITE)
  47.         txt_image = font.render(self.text, True, RED)
  48.         txt_rect = txt_image.get_rect(center=self.rect.center)
  49.         self.image_normal.blit(txt_image, txt_rect)
  50.        
  51.         self.image_hover = pygame.Surface(size)
  52.         self.image_hover.fill(RED)
  53.         txt_image = font.render(self.text, True, WHITE)
  54.         txt_rect = txt_image.get_rect(center=self.rect.center)
  55.         self.image_hover.blit(txt_image, txt_rect)
  56.  
  57.         self.rect.topleft = pos
  58.        
  59.         self.hover = False
  60.  
  61.         if command:
  62.             self.command = command
  63.  
  64.     def draw(self, screen):
  65.         if self.hover:
  66.             screen.blit(self.image_hover, self.rect)
  67.         else:
  68.             screen.blit(self.image_normal, self.rect)
  69.  
  70.     def handle_event(self, event):
  71.         if event.type == pygame.MOUSEMOTION:
  72.             self.hover = self.rect.collidepoint(event.pos)
  73.  
  74.         if self.hover and self.command:
  75.             if event.type == pygame.MOUSEBUTTONDOWN:
  76.                 if event.button == 1:
  77.                     self.command()
  78.                    
  79.     def command(self):
  80.         print("Click")
  81.  
  82. # === FUNCTIONS === (lower_case names)
  83.  
  84. def print_hello():
  85.     print("Click HELLO")
  86.  
  87. def print_world():
  88.     print("Click WORLD")
  89.  
  90. # === MAIN === (lower_case names)
  91.  
  92. # --- (global) variables ---
  93.  
  94. # --- init ---
  95.  
  96. pygame.init()
  97.  
  98. screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  99. screen_rect = screen.get_rect()
  100.  
  101. # --- objects ---
  102.  
  103. # - buttons -
  104.  
  105. button1 = Button(text="HELLO") # create button
  106. button1.command = print_hello # assign function to button
  107.  
  108. button2 = Button(text="WORLD", pos=(110,0), command=print_world) # create button and assign function
  109.  
  110. # - circles -
  111.  
  112. circles = []
  113.  
  114. for x in range(10):
  115.     circles.append( pygame.Rect(x*(BLOCK_SIZE+5), BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE) )
  116.  
  117. # - drag -
  118.  
  119. selected = None
  120.    
  121. # --- mainloop ---
  122.  
  123. clock = pygame.time.Clock()
  124. is_running = True
  125.  
  126. while is_running:
  127.  
  128.     # --- events ---
  129.    
  130.     for event in pygame.event.get():
  131.  
  132.         # --- global events ---
  133.        
  134.         if event.type == pygame.QUIT:
  135.             is_running = False
  136.  
  137.         elif event.type == pygame.KEYDOWN:
  138.             if event.key == pygame.K_ESCAPE:
  139.                 is_running = False
  140.  
  141.         elif event.type == pygame.MOUSEBUTTONDOWN:
  142.             if event.button == 1:
  143.                 for i, c in enumerate(circles):
  144.                     # Pythagoras A^2 + B^2 = C^2
  145.                     dx = c.centerx - event.pos[0] # A
  146.                     dy = c.centery - event.pos[1] # B
  147.                     distance_square = dx**2 + dy**2 # C^2
  148.  
  149.                     if distance_square <= CIRCLE_RADIUS**2: # C^2 <= RADIUS^2
  150.                         selected = i
  151.                         selected_offset_x = c.x - event.pos[0]
  152.                         selected_offset_y = c.y - event.pos[1]
  153.                
  154.         elif event.type == pygame.MOUSEBUTTONUP:
  155.             if event.button == 1:
  156.                 selected = None
  157.                
  158.         elif event.type == pygame.MOUSEMOTION:
  159.             if selected is not None: # selected can be `0` so `is not None` is required
  160.                 # move object
  161.                 circles[selected].x = event.pos[0] + selected_offset_x
  162.                 circles[selected].y = event.pos[1] + selected_offset_y
  163.                
  164.         # --- objects events ---
  165.  
  166.         button1.handle_event(event)
  167.         button2.handle_event(event)
  168.        
  169.     # --- updates ---
  170.  
  171.         # empty
  172.        
  173.     # --- draws ---
  174.    
  175.     screen.fill(BLACK)
  176.  
  177.     button1.draw(screen)    
  178.     button2.draw(screen)    
  179.    
  180.     # draw rect
  181.     for c in circles:
  182.         pygame.draw.circle(screen, RED, c.center, CIRCLE_RADIUS)
  183.        
  184.     pygame.display.update()
  185.  
  186.     # --- FPS ---
  187.  
  188.     clock.tick(25)
  189.  
  190. # --- the end ---
  191.  
  192. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement