Advertisement
furas

PyGame - Drag Rectangles

Feb 12th, 2016
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 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.  
  9. # ---------------------------------------------------------------------
  10.  
  11. __author__  = 'Bartlomiej "furas" Burek'
  12. __webpage__ = 'http://blog.furas.pl'
  13.  
  14. # ---------------------------------------------------------------------
  15.  
  16. import pygame
  17.  
  18. # === CONSTANS === (UPPER_CASE names)
  19.  
  20. BLACK = (  0,   0,   0)
  21. WHITE = (255, 255, 255)
  22.  
  23. RED   = (255,   0,   0)
  24. GREEN = (  0, 255,   0)
  25. BLUE  = (  0,   0, 255)
  26.  
  27. SCREEN_WIDTH  = 600
  28. SCREEN_HEIGHT = 400
  29.  
  30. BLOCK_SIZE = 50
  31.  
  32. # === CLASSES === (CamelCase names)
  33.  
  34. '''
  35. class Button():
  36. '''
  37.    
  38. # === FUNCTIONS === (lower_case names)
  39.  
  40.     # empty
  41.    
  42. # === MAIN === (lower_case names)
  43.  
  44. # --- (global) variables ---
  45.  
  46. # --- init ---
  47.  
  48. pygame.init()
  49.  
  50. screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  51. screen_rect = screen.get_rect()
  52.  
  53. # --- objects ---
  54.  
  55. '''
  56. button = Button(...)
  57. '''
  58.  
  59. rects = []
  60.  
  61. for x in range(10):
  62.     rects.append( pygame.Rect(x*(BLOCK_SIZE+5), BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE) )
  63.  
  64. selected = None
  65.    
  66. # --- mainloop ---
  67.  
  68. clock = pygame.time.Clock()
  69. is_running = True
  70.  
  71. while is_running:
  72.  
  73.     # --- events ---
  74.    
  75.     for event in pygame.event.get():
  76.  
  77.         # --- global events ---
  78.        
  79.         if event.type == pygame.QUIT:
  80.             is_running = False
  81.  
  82.         elif event.type == pygame.KEYDOWN:
  83.             if event.key == pygame.K_ESCAPE:
  84.                 is_running = False
  85.  
  86.         elif event.type == pygame.MOUSEBUTTONDOWN:
  87.             if event.button == 1:
  88.                 for i, r in enumerate(rects):
  89.                     if r.collidepoint(event.pos):
  90.                         selected = i
  91.                         selected_offset_x = r.x - event.pos[0]
  92.                         selected_offset_y = r.y - event.pos[1]
  93.                
  94.         elif event.type == pygame.MOUSEBUTTONUP:
  95.             if event.button == 1:
  96.                 selected = None
  97.                
  98.         elif event.type == pygame.MOUSEMOTION:
  99.             if selected is not None: # selected can be `0` so `is not None` is required
  100.                 # move object
  101.                 rects[selected].x = event.pos[0] + selected_offset_x
  102.                 rects[selected].y = event.pos[1] + selected_offset_y
  103.                
  104.         # --- objects events ---
  105.  
  106.         '''
  107.        button.handle_event(event)
  108.        '''
  109.        
  110.     # --- updates ---
  111.  
  112.         # empty
  113.        
  114.     # --- draws ---
  115.    
  116.     screen.fill(BLACK)
  117.  
  118.     '''
  119.    button.draw(screen)    
  120.    '''
  121.    
  122.     # draw rect
  123.     for r in rects:
  124.         pygame.draw.rect(screen, RED, r)
  125.        
  126.     pygame.display.update()
  127.  
  128.     # --- FPS ---
  129.  
  130.     clock.tick(25)
  131.  
  132. # --- the end ---
  133.    
  134. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement