所以我目前在绘图应用程序中拥有喷枪的代码。当该功能处于事件状态时,它应该在 Canvas 上绘制,基本上就像喷枪一样。但现在,我不知道如何让 pygame 检测鼠标按下或鼠标松开并从中进行 while 循环。这是代码:
def airbrush():
airbrush = True
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
while click == True:
pygame.draw.circle(gameDisplay, colorChosen, (cur[0] + random.randrange(brushSize), cur[1] + random.randrange(brushSize)), random.randrange(1, 5))
pygame.display.update()
clock.tick(60)
现在,我的“点击时”不起作用。我应该用什么替换“单击”才能完成这项工作,以便在按住鼠标时,它会绘画,但当鼠标“向上”时,它会停止?
最佳答案
pygame.mouse.get_pressed()
返回的状态当 pygame.event.get()
时评估一次叫做。 pygame.mouse.get_pressed() 的返回值是按钮状态的元组。
不要在函数中实现单独的事件处理。在主循环中进行事件处理:
done = False
while not done:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
airbrush()
pygame.display.flip()
在函数 airbrush
中评估当前帧的按钮(例如左按钮)的当前状态:
def airbrush():
airbrush = True
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if click[0] == True: # evaluate left button
pygame.draw.circle(gameDisplay, colorChosen, (cur[0] + random.randrange(brushSize), cur[1] + random.randrange(brushSize)), random.randrange(1, 5))
关于python - 如何在 pygame 中创建 "while mouse down"循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54666587/