需要使用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()
我想在窗口中绘制线条而不是点。
答案 0 :(得分:4)
使用pygame.draw.lines
通过线连接点列表。
如果释放了鼠标按钮,则将当前鼠标位置添加到列表中:
if event.type == pygame.MOUSEBUTTONUP:
points.append(event.pos)
绘制点列表,如果列表中有多个点:
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()