Reputation: 119
I am trying to draw a line in pygame that has a rounded cap unlike the image below. Is there a way I can do this. Ideally I would want a smooth line with no breaks and a rounded edge as opposed to the flat edge it currently has. My current code for drawing a line is as follows
pygame.draw.line(window, (0, 0, 0), start, end, 50)
Upvotes: 1
Views: 1247
Reputation: 211258
The best I can think of is to use pygame.draw.lines()
and draw additional circles (pygame.draw.circle
) at the joints:
points = [(100, 100), (300, 150), (250, 300)]
pygame.draw.lines(window, (0, 0, 0), False, points, 50)
for p in points:
pygame.draw.circle(window, (0, 0, 0), p, 25)
See also Paint
Minimal example:
repl.it/@Rabbid76/PyGame-PaintFreeThickLine
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
lines = []
draw = False
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
draw = not draw
if draw:
lines.append([event.pos])
if event.type == pygame.MOUSEMOTION and draw:
lines[-1].append(event.pos)
window.fill((255, 255, 255))
for points in lines:
if len(points) > 1:
pygame.draw.lines(window, (0, 0, 0), False, points, 50)
for p in points:
pygame.draw.circle(window, (0, 0, 0), p, 25)
pygame.display.flip()
pygame.quit()
exit()
Upvotes: 1