Skip to content Skip to sidebar Skip to footer

Looping Mousebutton Down To Draw Lines

Need to draw lines rather than dots with mousebuttondown When mouse is clicked program draws dots, I assume another loop is needed to draw lines with the mouse button held down. w

Solution 1:

Use pygame.draw.lines, to connect a point list by a line.

Append the current mouse position to a list, if the mouse button is released:

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

Draw the list of points, if there is more than 1 point in the list:

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

Draw a "rubber band" from the last point in the list to the current mouse position:

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

See the simple example:

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

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = Falseif event.type == pygame.MOUSEBUTTONUP:
            points.append(event.pos)

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

Post a Comment for "Looping Mousebutton Down To Draw Lines"