python - 如何在区域上方激活功能,在限制区域外保持事件状态

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

我知道问题出在仅当光标位于图标上方时才调用该函数,但我不知道将操作语句移至何处,或如何更改它们。

最佳答案

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

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,但我知道这是执行此类操作的最佳方法,除非有某种内置函数。

关于python - 如何在区域上方激活功能,在限制区域外保持事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54739298/

相关文章:

python - pygame.init() 什么时候会返回 (6,0) 以外的东西?

python - 无法为python安装pygame

python - 复制python的pygame表面对象

python - 在 Python 中从存储在 HDFS 上的文件中读取行的最有效方法是什么?

linux - opencv导入问题和双重安装

python - ('HY000' , 'The SQL contains 21 parameter markers, but 1 parameters were supplied' )

python - 尝试在 python 中安装库时,如何修复构建环境中不可用的设置工具?

Python:展平 XML 文档(删除换行符)

java - 如何用 PHP、Python 或 Java 编写下载/上传速度监视器?

python - 在 Python 3 中已存在 NLTK 时在 Python 2.7 中安装它