【发布时间】: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