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

时间:2019-02-18 01:22:29

标签: python python-3.x pygame

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

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)

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

1 个答案:

答案 0 :(得分: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”更改为相反(如果关闭则为打开,如果开启则为关闭),然后当他们单击任意位置时,它将检查此变量是否打开,以及是否未选中光标在工具栏中,如果同时满足这两个条件,则它将绘制。

这就是您要执行的操作。我不能保证这段代码能正常工作,因为我自己从未使用过pygame,但是除非有某种内置函数,否则我知道这是做这种事情的最佳方法。