[Python] PYGAME Circle changes colour on clicking but changes back on mouse up

Discussão em 'Python' iniciado por Stack, Maio 26, 2025.

  1. Stack

    Stack Membro Participativo

    Now I know that this is due to how I have blocked my code. Clicking on any of the circles changes them to black, on release they go back to yellow. I want them to stay black. I can see why they go back to yellow - makes perfect sense, but how can I stop it doing it?

    I am quite happy for the answer to be "no hope from here..."! I'm very much a beginner I cannot be patronised, lol!

    import pygame
    import random

    pygame.init()

    #Display Window
    SCREEN_HEIGHT = 800
    SCREEN_WIDTH = 1400

    #Colours
    BLUE = (0, 0, 255)
    LIGHTBLUE = (72, 243, 235)
    BLUEBLACK = (20, 21, 77)
    IODINE = (243, 200, 99)
    REDYELLOW = (221, 78, 20)
    BLACK = (0, 0, 0)

    #Location of top left of well plate, diameter of wells, default colour of well, horizontal and vertical spacing
    # between wells.
    x = 500
    y = 400
    size = 40
    wellcolour = IODINE
    colour = IODINE
    spacing = size*2.3
    yspacing = size*2.3
    start = True
    clicked = 0


    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption('Amylase')
    #icon = pygame.image.load("plant.png")
    #pygame.display.set_icon(icon)

    class Experiment():
    def __init__ (self, colour, x, y, size, number):


    self.rect = pygame.Rect(x-size, y-size, size*2, size*2)
    pygame.draw.rect(screen, LIGHTBLUE, self.rect)
    pygame.draw.circle(screen, colour, (x, y), size )
    pygame.draw.circle(screen, BLACK, (x, y), size, 4)
    if self.rect.collidepoint(mouse_pos) and event.type == pygame.MOUSEBUTTONDOWN:
    colour = BLACK
    pygame.draw.circle(screen, colour, (x, y), size )





    #!!!!!!!GAME LOOP!!!!!
    run =True



    while run:



    mouse_pos = pygame.mouse.get_pos()
    #print(mouse_pos)


    for event in pygame.event.get():


    if event.type == pygame.QUIT:
    run = False



    screen.fill(LIGHTBLUE)



    #Row one
    Experiment(wellcolour, x, y, size, 1)
    Experiment(wellcolour, x+spacing, y, size, 2)
    Experiment(wellcolour, x+2*spacing, y, size, 3)
    Experiment(wellcolour, x+3*spacing, y, size, 4)
    Experiment(wellcolour, x+4*spacing, y, size, 5)

    #Row two
    Experiment(wellcolour, x, y+yspacing, size, 6)
    Experiment(wellcolour, x+spacing, y+yspacing, size, 7)
    Experiment(wellcolour, x+2*spacing, y+yspacing, size, 8)
    Experiment(wellcolour, x+3*spacing, y+yspacing, size, 9)
    Experiment(wellcolour, x+4*spacing, y+yspacing, size, 10)

    #Row three
    Experiment(wellcolour, x, y+2*yspacing, size, 11)
    Experiment(wellcolour, x+spacing, y+2*yspacing, size, 12)
    Experiment(wellcolour, x+2*spacing, y+2*yspacing, size, 13)
    Experiment(wellcolour, x+3*spacing, y+2*yspacing, size, 14)
    Experiment(wellcolour, x+4*spacing, y+2*yspacing, size, 15)

    #Row four
    Experiment(wellcolour, x, y+3*yspacing, size, 16)
    Experiment(wellcolour, x+spacing, y+3*yspacing, size, 17)
    Experiment(wellcolour, x+2*spacing, y+3*yspacing, size, 18)
    Experiment(wellcolour, x+3*spacing, y+3*yspacing, size, 19)
    Experiment(wellcolour, x+4*spacing, y+3*yspacing, size, 20)


    pygame.display.flip()

    pygame.quit()

    Continue reading...

Compartilhe esta Página