我正在为油漆应用程序编写代码,我想拥有多个刷子。目前唯一的问题是,在此处使用此代码,刷子可以正常工作,但是只有在光标高于实际图标时才起作用。这是代码:
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"更改为相反(el of Off on或Off),然后在单击任何地方时,它会检查此变量是否已打开,如果Curso不在在工具栏中,如果两个都满足,则绘制。
这就是您的方式。我不能保证此代码可以工作,因为我自己从未使用过Pygame,但我知道这是做这种事情的最佳方法,除非有某种内置功能。