Any one know where I can find the pygame code to create a screen where you can draw simple lines by click and dragging the mouse? I know how to create lines with the pygame.draw.line(screen, (0,0, 0), (3,200), (9, 200)) script (using that as an example), but am looking for the code for the user to create a line with the convenience of just using their mouse. Thanks!

Any one know where I can find the pygame code to create a screen where you can draw simple lines by click and dragging the mouse? I know how to create lines with the pygame.draw.line(screen, (0,0, 0), (3,200), (9, 200)) script (using that as an example), but am looking for the code for the user to create a line with the convenience of just using their mouse. Thanks!

Yes. This code does it

import pygame
pygame.init()

def main():
    screen = pygame.display.set_mode((640, 480))
    pygame.display.set_caption("Draw lines with the mouse")
    
    background = pygame.Surface(screen.get_size())
    background.fill((255, 255,255))
    
    clock = pygame.time.Clock()
    keepGoing = True
    while(keepGoing):
        clock.tick(30)
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                lineStart = pygame.mouse.get_pos()
            elif event.type == pygame.MOUSEBUTTONUP:
                lineEnd = pygame.mouse.get_pos()
                pygame.draw.line(background, (0, 0, 0), lineStart, lineEnd, 3)
                
            
        screen.blit(background, (0, 0))
        pygame.display.flip()
    
if __name__ == "__main__":
    main()
This article has been dead for over six months. Start a new discussion instead.