如何在区域上方激活功能,在限制区域外保持活动状态

问题描述 投票:3回答:1

我正在为一个绘图应用程序编写代码,我想要多个画笔。现在唯一的问题是,使用此代码,刷子工作正常,但只有当光标位于实际图标上方时才有效。这是代码:

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)

我认识到问题是当光标位于图标上方时仅调用函数,但我不知道在何处移动动作语句,或者如何更改它们。

python python-3.x pygame
1个回答
1
投票

当用户点击绘图图标时,您希望这样设置变量。所以代替:

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 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

显然,在你的情况下,因为你有多个工具,你必须为这个函数中的每个工具创建不同的切换,但我试图保持简单,只显示一个绘图工具的示例。

现在您有一个切换,在单击图标后会更改变量“paint_on”,您可以检查是否只是常规鼠标单击

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))

然后将此函数添加到主while True循环:

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()

所以这段代码的工作原理如下:

当用户单击该图标时,它会将变量“paint_on”更改为相反(如果打开则关闭或关闭),然后当他们单击任意位置时,它会检查此变量是否已打开,以及该工具中是否存在curso吧,如果这两个都满足,那么就画了。

这就是你如何做到这一点。我无法保证这段代码能够正常工作,因为我自己从未使用过pygame,但我知道这是做这种事情的最好方法,除非有某种内置函数。

热门问题
推荐问题
最新问题