3

I am writing a code for a paint app, and I want to have multiple brushes. Only issue right now is that with this code here, the brush works fine, but it only works when the cursor is above the actual icon. Here is the code:

def paintScreen():
    intro = True
    gameDisplay.fill(cyan)
    message_to_screen('Welcome to PyPaint', black, -300, 'large')
    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        pygame.draw.rect(gameDisplay, white, (50, 120, displayWidth - 100, displayHeight - 240))

        button('X', 20, 20, 50, 50, red, lightRed, action = 'quit')
        icon(airbrushIcon, white, 50, displayHeight - 101, 51, 51, white, grey, 'airbrush')
        pygame.display.update()

def icon(icon, colour, x, y, width, height, inactiveColour, activeColour, action = None):
        cur = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if x + width > cur[0] > x and y + height > cur[1] > y:#if the cursor is over the button
            pygame.draw.rect(gameDisplay, activeColour, (x, y, width, height))
            gameDisplay.blit(icon, (x, y))
            if click[0] == 1 and action != None:
                if action == 'quit':
                    pygame.quit()
                    quit()
                if action == 'pencil':
                    pencil()
                if action == 'airbrush':
                    airbrush()
                if action == 'calligraphy':
                    calligraphy()
                if action == 'erase':
                    pencil()
        else:
            pygame.draw.rect(gameDisplay, inactiveColour, (x, y, width, height))
            gameDisplay.blit(icon, (x, y))

def airbrush(brushSize = 3):
    airbrush = True
    cur = pygame.mouse.get_pos() #cur[0] is x location, cur[1] is y location
    click = pygame.mouse.get_pressed()
    if click[0] == True:
        if cur[0] > 50 < displayWidth - 50 and cur[1] > 120 < displayHeight - 120:
            #the area of the canvas is x(50, width-50) y(120, width-120)
            pygame.draw.circle(gameDisplay, black, (cur[0] + random.randrange(brushSize), cur[1] + random.randrange(brushSize)), random.randrange(1, 5))
        clock.tick(60)

I recognise that the issue is with the function only being called when the cursor is above the icon, but I don't know where to move the action statements, or how to change them.

1 Answer 1

1

You want to make it so when the user clicks on the paint icon a variable is set. So instead of:

def icon(icon, colour, x, y, width, height, inactiveColour, activeColour, action = None):
        cur = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if x + width > cur[0] > x and y + height > cur[1] > y:#if the cursor is over the button
            pygame.draw.rect(gameDisplay, activeColour, (x, y, width, height))
            gameDisplay.blit(icon, (x, y))
            if click[0] == 1 and action != None:
                if action == 'quit':
                    pygame.quit()
                    quit()
                if action == 'pencil':
                    pencil()
                if action == 'airbrush':
                    airbrush()
                if action == 'calligraphy':
                    calligraphy()
                if action == 'erase':
                    pencil()
        else:
            pygame.draw.rect(gameDisplay, inactiveColour, (x, y, width, height))
            gameDisplay.blit(icon, (x, y))

You can change this code to simply toggle paint on and off for:

def icon(icon, colour, x, y, width, height, inactiveColour, activeColour, paint_on, action = None):
        cur = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if x + width > cur[0] > x and y + height > cur[1] > y and click[0] == 1: # if the cursor is over the button and they clicked
            if paint_on == True:
                paint_on = False
            else:
                paint_on = True
            return paint_on

Obviously in your case since you have multiple tools you would have to create different toggles for each tool inside this function, but I'm trying to keep it simple and show an example for just a single paint tool.

Now that you have a toggle that will change the variable "paint_on" upon clicking the icon, you can check for just a regular mouse click

def regular_click(colour, x, y, width, height, inactiveColour, activeColour, action = None):
    cur = pygame.mouse.get_pos()
    click = pygame.get_pressed()
    if cur[1] > y and click[0] == 1 and paint_on == True: # if cursor is beneath the tool bar (I'm assuming your tool bar is at the top)
        pygame.draw.rect(gameDisplay, activeColour, (x, y, width, height))

Then adding this function to your main while True loop:

def paintScreen():
    intro = True
    gameDisplay.fill(cyan)
    message_to_screen('Welcome to PyPaint', black, -300, 'large')
    paint_on = False
    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        pygame.draw.rect(gameDisplay, white, (50, 120, displayWidth - 100, displayHeight - 240))

        button('X', 20, 20, 50, 50, red, lightRed, action = 'quit')
        paint_on = icon(airbrushIcon, white, 50, displayHeight - 101, 51, 51, white, grey, paint_on, 'airbrush')
        regular_click(paint_on)
        pygame.display.update()

So all up this code works as follows:

Upon user clicking the icon, it changes the variable "paint_on" to it's opposite (so on if off or off if on), then when they click anywhere, it checks whether this variable is on, and if the curso is not in the tool bar, and if both of these are met, then it draws.

This is how you would do this. I can not guarantee that this code works as I have never used pygame myself, but I know this is the best way of doing this kind of thing, unless there is some sort of in built function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.