Advertisement
EdmundC

paint_app

Sep 13th, 2024 (edited)
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1. import pygame
  2. import sys
  3.  
  4. # Initialize Pygame
  5. pygame.init()
  6.  
  7. # Set up display
  8. WIDTH, HEIGHT = 640, 480
  9. window = pygame.display.set_mode((WIDTH, HEIGHT))
  10. pygame.display.set_caption("Simple Paint App")
  11.  
  12. # Set up colors
  13. BLACK = (0, 0, 0)
  14. WHITE = (255, 255, 255)
  15. current_color = BLACK
  16.  
  17. # Fill background with white
  18. window.fill(WHITE)
  19.  
  20. # Set up variables
  21. drawing = False
  22. eraser = False
  23. brush_size = 5
  24.  
  25. def draw_circle(surface, color, position, radius):
  26.     pygame.draw.circle(surface, color, position, radius)
  27.  
  28. def save_screenshot():
  29.     pygame.image.save(window, "drawing.bmp")
  30.     print("Drawing saved as 'drawing.bmp'")
  31.  
  32. # Main loop
  33. while True:
  34.     for event in pygame.event.get():
  35.         if event.type == pygame.QUIT:
  36.             pygame.quit()
  37.             sys.exit()
  38.        
  39.         # Start drawing or erasing when mouse is pressed
  40.         if event.type == pygame.MOUSEBUTTONDOWN:
  41.             if event.button == 1:  # Left mouse button (draw)
  42.                 drawing = True
  43.                 eraser = False
  44.             elif event.button == 3:  # Right mouse button (erase)
  45.                 drawing = True
  46.                 eraser = True
  47.        
  48.         # Stop drawing when mouse is released
  49.         if event.type == pygame.MOUSEBUTTONUP:
  50.             if event.button in [1, 3]:
  51.                 drawing = False
  52.        
  53.         # Save drawing with 'S' key
  54.         if event.type == pygame.KEYDOWN:
  55.             if event.key == pygame.K_s:
  56.                 save_screenshot()
  57.  
  58.     # Drawing/Erasing logic
  59.     if drawing:
  60.         mouse_position = pygame.mouse.get_pos()
  61.         if eraser:
  62.             draw_circle(window, WHITE, mouse_position, brush_size)  # Erase (draw white)
  63.         else:
  64.             draw_circle(window, current_color, mouse_position, brush_size)  # Draw black
  65.  
  66.     pygame.display.update()
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement