'How Do I Make a Circle Green When I Click It

I'm trying to make a Pygame window that has one thousand small circles and when I click one that circle will turn green. I am new to Pygame.

import pygame
import random as r
pygame.init()

screen = pygame.display.set_mode([1700, 900])
pygame.display.set_caption('So many buttons')
from pygame import mixer
mixer.init()
mixer.music.load('looperman-l-2592210-0277509-vein-95bpm-blend-beats.ogg')
mixer.music.play(-1)
run = True
num = 1

def draw_circle(self):
    x = r.randint(1, 1700)
    y = r.randint(1, 900)
    pygame.draw.circle(screen, (255, 0, 0),[x, y], 10, 0)
    pygame.display.update()

while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    if num <= 1000:
        draw_circle()
        num += 1
pygame.quit()


Solution 1:[1]

Create a class that represents a circle. The class should have a method that can draw the circle, method that detects if a point is inside the circle and a method that can change the color:

class Circle:
    def __init__(self):
        self.x = r.randint(1, 1700)
        self.y = r.randint(1, 1700)
        self.color = (255, 0, 0)
        self.r = 10
        self.clicked = False
    def is_point_in(self, p):
        dx = p[0] - self.x
        dy = p[1] - self.y
        return math.hypot(dx, dy) <= self.r * self.r
    def set_color(self, color):
        self.color = color
    def draw(self):
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.r, 0)

Create circles before the application loop and put them in a list:

list_of_circles = []
for i in range(10):
    list_of_circles.append(Circle())

When the mouse is clicked, detect if the mouse is in a circle and change color:

for event in pygame.event.get():
    # [...]
    if event.type == pygame.MOUSEBUTTONDOWN:
        for circle in list_of_circles:
            if circle.is_point_in(event.pos):
                circle.set_color((0, 255, 0))

Draw the circle in a loop in the application loop:

while run:
    # [...]

    for circle in list_of_circles:
        circle.draw()

See alos How do I detect collision in pygame? and Pygame mouse clicking detection.


Minimal example:

import pygame
import random as r
import math

class Circle:
    def __init__(self):
        self.x = r.randint(1, 1700)
        self.y = r.randint(1, 1700)
        self.color = (255, 0, 0)
        self.r = 10
        self.clicked = False
    def is_point_in(self, p):
        dx = p[0] - self.x
        dy = p[1] - self.y
        return math.hypot(dx, dy) <= self.r * self.r
    def set_color(self, color):
        self.color = color
    def draw(self):
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.r, 0)

pygame.init()
screen = pygame.display.set_mode([1700, 900])

list_of_circles = []
for i in range(10):
    list_of_circles.append(Circle())

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            for circle in list_of_circles:
                if circle.is_point_in(event.pos):
                    circle.set_color((0, 255, 0))

    screen.fill(0)
    for circle in list_of_circles:
        circle.draw()
    pygame.display.update()

pygame.quit()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rabbid76