【问题标题】:How to make a function activated when over a region, stay active outside the limiting region如何在超过区域时激活功能,在限制区域外保持活动状态
【发布时间】:2019-02-18 01:22:28
【问题描述】:

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

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

    您希望在用户单击绘图图标时设置一个变量。所以而不是:

    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,但我知道这是做这种事情的最佳方式,除非有某种内置函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 2016-03-23
      • 2012-03-09
      相关资源
      最近更新 更多