gpt4 book ai didi

python - 向下循环鼠标按钮以绘制线条

转载 作者:太空宇宙 更新时间:2023-11-03 14:38:29 24 4
gpt4 key购买 nike

需要用mousebuttondown画线而不是点

当单击鼠标时程序绘制点,我假设需要另一个循环来在按住鼠标按钮的情况下绘制线条。


while keep_going:
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
if event.type == pygame.MOUSEBUTTONDOWN:
spot = event.pos
pygame.draw.circle(screen, GREEN, spot, radius)
pygame.display.update()

我想在窗口中画线而不是点。

最佳答案

使用pygame.draw.lines , 用线连接点列表。

如果鼠标按钮被释放,则将当前鼠标位置附加到列表中:

if event.type == pygame.MOUSEBUTTONUP:
points.append(event.pos)

绘制点列表,如果列表中有超过1个点:

if len(points) > 1:
pygame.draw.lines(screen, (255, 255, 255), False, points, width)

从列表中的最后一个点到当前鼠标位置绘制一条“橡皮筋”:

if len(points):
pygame.draw.line(screen, (255, 255, 255), points[-1], pygame.mouse.get_pos(), width)

看简单的例子:

run = True
width = 3
points = []
while run:

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

if event.type == pygame.MOUSEBUTTONUP:
points.append(event.pos)

screen.fill(0)
if len(points) > 1:
pygame.draw.lines(screen, (255, 255, 255), False, points, width)
if len(points):
pygame.draw.line(screen, (255, 255, 255), points[-1], pygame.mouse.get_pos(), width)
pygame.display.flip()

关于python - 向下循环鼠标按钮以绘制线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55477799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com