【发布时间】:2019-02-19 02:22:01
【问题描述】:
我正在创建一个基本的绘画应用程序,我想创建一个喷枪功能,并为不同风格的画笔提供其他功能。基本上,用户单击一个图标来选择画笔的样式,然后当他们单击并按住画布时,相应的画笔将在画布上绘制。这是我的代码: 主循环:
def paintScreen():
intro = True
gameDisplay.fill(cyan)
message_to_screen('Welcome to PyPaint', black, -300, 'large')
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
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')
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 airbrush == 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 action == 'quit':
pygame.quit()
quit()
elif action == 'pencil':
pencil = True
return pencil
elif action == 'airbrush':
airbrush = True
return airbrush
elif action == 'calligraphy':
calligraphy = True
return calligraphy
elif action == 'erase':
eraser = True
return eraser
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()
while airbrush == True:
if click[0] == True:
if cur[0] > 50 < displayWidth - 50 and cur[1] > 120 < displayHeight - 120: #if the cursor is above the canvas
#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))
pygame.display.update()
clock.tick(60)
如何使喷枪功能正常工作?运行应用程序时,它不会返回任何错误,只是根本不起作用
【问题讨论】:
-
您确定
airbrush()函数被正确调用了吗?也许一些print()语句将有助于识别逻辑流程中的任何错误。 -
我确定它正在被调用。我什至将绘制画布的语句移到了 while 循环之外,这样它就不会从喷枪函数中绘制墨水。
标签: python python-3.x pygame