Advertisement
furas

Python - PyGame - keep ciicked (Stackoverflow)

May 29th, 2025
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # date: 2025.05.29
  4. # stackoverflow: [python - PYGAME Circle changes colour on clicking but changes back on mouse up - Stack Overflow](https://stackoverflow.com/questions/79639247/pygame-circle-changes-colour-on-clicking-but-changes-back-on-mouse-up)
  5.  
  6. import pygame
  7. import random
  8.  
  9. # --- constants ---  # PEP8: constants after imports
  10.  
  11. # display window
  12. SCREEN_HEIGHT = 800
  13. SCREEN_WIDTH = 1400
  14.  
  15. # colours
  16. BLUE = (0, 0, 255)
  17. LIGHTBLUE = (72, 243, 235)
  18. BLUEBLACK = (20, 21, 77)
  19. IODINE = (243, 200, 99)
  20. REDYELLOW = (221, 78, 20)
  21. BLACK = (0, 0, 0)
  22.  
  23. # other
  24. X = 500
  25. Y = 400
  26.  
  27. SIZE = 40
  28. X_SPACING = SIZE*2.3
  29. Y_SPACING = SIZE*2.3
  30.  
  31. # --- classes ---  # PEP8: classes after constants
  32.  
  33. class Experiment():
  34.     def __init__ (self, colour, x, y, size, number):
  35.         self.x = x
  36.         self.y = y
  37.         self.size = size
  38.         self.number = number
  39.         self.colour = colour
  40.  
  41.         self.rect = pygame.Rect(self.x-self.size, self.y-self.size, self.size*2, self.size*2)
  42.  
  43.         self.clicked = False
  44.  
  45.     def draw(self, screen):
  46.         #pygame.draw.rect(screen, LIGHTBLUE, self.rect)
  47.  
  48.         if self.clicked:
  49.             pygame.draw.circle(screen, BLACK, (self.x, self.y), self.size )
  50.         else:
  51.             pygame.draw.circle(screen, self.colour, (self.x, self.y), self.size)
  52.             pygame.draw.circle(screen, BLACK, (self.x, self.y), self.size, 4)
  53.  
  54.     def handle_event(self, event):
  55.         if event.type == pygame.MOUSEBUTTONDOWN:
  56.             if self.rect.collidepoint(event.pos):
  57.                 self.clicked = True
  58.  
  59. # --- functions ---  # PEP8: functions after classes
  60.  
  61. # --- main ---  # # PEP8: main code after functions
  62.  
  63. # - variables -
  64.  
  65. wellcolour = IODINE
  66. colour = IODINE
  67.  
  68. # - init -
  69.  
  70. pygame.init()
  71.  
  72. screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  73. pygame.display.set_caption('Amylase')
  74.  
  75. # - create all instances only once, using for-loop, and keep them on list
  76.  
  77. items = []
  78.  
  79. for row in range(4):
  80.     wellcolour = (243, row*40+100, 99)
  81.     for col in range(5):
  82.         x = X + (col * X_SPACING)
  83.         y = Y + (row * Y_SPACING)
  84.         number = col + (row * 5) + 1
  85.  
  86.         item = Experiment(wellcolour, x, y, SIZE, number)
  87.  
  88.         items.append(item)
  89.  
  90. # - main loop -
  91.  
  92. run = True
  93.  
  94. while run:
  95.  
  96.     # - events -
  97.  
  98.     for event in pygame.event.get():
  99.         if event.type == pygame.QUIT:
  100.             run = False
  101.         # send every event to all items
  102.         for item in items:
  103.             item.handle_event(event)
  104.  
  105.     # - updates (without drawing) -
  106.  
  107.     # update all items
  108.     # for item in items:
  109.     #     pass  # empty function
  110.  
  111.     # - draws (without updates) -
  112.  
  113.     screen.fill(LIGHTBLUE)
  114.  
  115.     # draw all items
  116.     for item in items:
  117.         item.draw(screen)
  118.  
  119.     pygame.display.flip()
  120.  
  121. # - end -
  122.  
  123. pygame.quit()
  124.  
Tags: python pygame
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement