У меня есть функция, которая позволяет пользователю щелкнуть значок и установить логическое значение, чтобы основной цикл знал, какое действие выполнить.Код предназначен для работы в виде щелчка значка аэрографа -> установите airbrushMode
в значение true -> вернуть airbrushMode
, чтобы paintScreen()
мог его обнаружить -> выполнить действие, установленное airbrushMode
в цикле while.Я добавил в операторах печати, чтобы найти проблему.Переменная меняется, но переменная не возвращается, а функция аэрографии не работает.Это работает, когда я устанавливаю airbrushMode
в true внутри paintScreen()
, но установка в false в функции и за ее пределами не работает.
Основная функция
def paintScreen():
airbrushMode = False
paint = True
gameDisplay.fill(cyan)
message_to_screen('Welcome to PyPaint', black, -300, 'large')
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
pygame.draw.rect(gameDisplay, white, (50, 120, displayWidth - 100, displayHeight - 240))
while paint:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
button('X', 20, 20, 50, 50, red, lightRed, action = 'quit')
icon(airbrushIcon, white, 50, displayHeight - 101, 51, 51, white, grey, 'airbrush')
icon(pencilIcon, white, 140, displayHeight - 101, 51, 51, white, grey, 'pencil')
icon(calligraphyIcon, white, 230, displayHeight - 101, 51, 51, white, grey, 'calligraphy')
pygame.display.update()
if cur[0] >= 50 <= displayWidth - 50 and cur[1] >= 120 <= displayHeight - 120:
if airbrushMode == True:
airbrush()
функция, которая создает значки и обнаруживает действие, а затем возвращает его
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 clicked
print('click')
if action == 'quit':
pygame.quit()
quit()
elif action == 'airbrush':
airbrushMode = True
print('airbrush set')
return airbrushMode
print('airbrush active')