Draw lines with Pygame

The pygame.draw.lines method which takes in 1) Screen surface 2) Line color 3) Boolean value to indicate whether should close the lines or not 4) Line coordinates and 5) Line width, will produce the below outcome.

Pygame draw lines
Pygame draw lines

Just like pygame.draw.line, there is also an antialiased version of pygame.draw.lines which will draw smooth lines on the screen.

The drawlines script is as follow.

import pygame
from pygame.locals import *
from sys import exit

pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)

coordinate  = []

while True:
    
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
            
        if event.type == MOUSEMOTION:
            coordinate.append(event.pos)
            
    screen.fill((255, 255, 255))

    if len(coordinate)>1:
        pygame.draw.lines(screen, (0,255,0), False, coordinate, 3)
            
    pygame.display.update()

The script above will append a tuple which consists of x, y coordinate to the coordinate array every time we touch a point on the screen, the coordinate array will then be used in the pygame.draw.lines method.