Advertisement
furas

Python - Pygame - put two pixels using mouse click

Jan 5th, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. import pygame
  2.  
  3. # === CONSTANTS ===
  4.  
  5. BLACK = (  0,   0,   0)
  6. WHITE = (255, 255, 255)
  7.  
  8. PIXEL_SIZE = 10
  9.  
  10. # === CLASSES ===
  11.  
  12. # empty
  13.  
  14. # === FUNCTIONS ===
  15.  
  16. # empty
  17.  
  18. # === DATA ===
  19.  
  20. colors = {
  21.     '#': BLACK,
  22.     ' ': WHITE,
  23. }
  24.  
  25. data = [
  26.     "      ########   ########      ",
  27.     " ##   #      #   #      #   ## ",
  28.     "#  #  #      #   #      #  #  #",
  29.     "#   # #      #   #      # #   #",
  30.     "     ##      #####      ##     ",
  31.     "      #      #####      #      ",
  32.     "      #      #   #      #      ",
  33.     "      #      #   #      #      ",
  34.     "      #      #   #      #      ",
  35.     "      #      #   #      #      ",
  36.     "      #      #   #      #      ",
  37.     "      #      #   #      #      ",
  38.     "      ########   ########      ",
  39. ]
  40.  
  41. # === MAIN ===
  42.  
  43. # --- init ---
  44.  
  45. pygame.init()
  46.  
  47. screen = pygame.display.set_mode([400,400])
  48. pygame.display.set_caption('okulary')
  49.  
  50. # --- draws ---
  51.  
  52. screen.fill(WHITE)
  53.  
  54. # create only once
  55. rect = pygame.Rect(0, 0, PIXEL_SIZE, PIXEL_SIZE)
  56.  
  57. for y, row in enumerate(data):
  58.     for x, char in enumerate(row):
  59.         rect.x = x*PIXEL_SIZE
  60.         rect.y = y*PIXEL_SIZE
  61.         screen.fill(colors[char], rect=rect)
  62.  
  63. pygame.display.update()
  64.  
  65. # --- mainloop ---
  66.  
  67. running = True
  68.  
  69. while running:
  70.    
  71.     for event in pygame.event.get():
  72.         if event.type == pygame.QUIT:
  73.            running = False
  74.            
  75.         elif event.type == pygame.MOUSEBUTTONDOWN:
  76.            
  77.             if event.button == 1: # left button
  78.  
  79.                 # Python 2: rounding to integer using /
  80.                 # Python 3: rounding to integer using //
  81.                 col = event.pos[0] // PIXEL_SIZE
  82.                 row = event.pos[1] // PIXEL_SIZE
  83.                 # first pixel
  84.                 rect.x = col*PIXEL_SIZE
  85.                 rect.y = row*PIXEL_SIZE
  86.                 screen.fill(BLACK, rect=rect) # BLACK
  87.                 # pixel on the left
  88.                 rect.x -= PIXEL_SIZE
  89.                 screen.fill(BLACK, rect=rect) # BLACK
  90.  
  91.                 # send on monitor        
  92.                 pygame.display.update()
  93.                
  94.             elif event.button == 3: # right button
  95.                
  96.                 # Python 2: rounding to integer using /
  97.                 # Python 3: rounding to integer using //
  98.                 col = event.pos[0] // PIXEL_SIZE
  99.                 row = event.pos[1] // PIXEL_SIZE
  100.                 # first pixel
  101.                 rect.x = col*PIXEL_SIZE
  102.                 rect.y = row*PIXEL_SIZE
  103.                 screen.fill(WHITE, rect=rect) # WHITE
  104.                 # pixel on the left
  105.                 rect.x -= PIXEL_SIZE
  106.                 screen.fill(WHITE, rect=rect) # WHITE
  107.    
  108.                 # send on monitor        
  109.                 pygame.display.update()
  110.                            
  111. # --- the end ---
  112.  
  113. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement