In this article we will draw a new line with Pygame every time the user clicks on a new position on the screen. The method we will use here is the pygame.draw.line method which takes in these arguments:
1) The screen surface to draw on.
2) The color of the line.
3) The first point of the line.
4) The second point of the line.
5) An optional width value of the line.
The python script to draw the line is as follow:
import pygame from pygame.locals import * from sys import exit pygame.init() screen = pygame.display.set_mode((640, 480), 0, 32) origin = (30, 30) while True: for event in pygame.event.get(): if event.type == QUIT: exit() if event.type == MOUSEBUTTONDOWN: screen.fill((255,255,255)) screen.lock() pygame.draw.line(screen, (30, 0, 20), origin, event.pos, 2) screen.unlock() pygame.display.update()
Below is the outcome.
The line will change every time you click on the different place on the screen!
Besides the pygame.draw.line method, Pygame also offers the pygame.draw.aaline method which takes in the same parameters as pygame.draw.line but draws smoother line by using the antialiasing technique to draw the line!