简体   繁体   中英

Line drawing in pygame

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)

在此处输入图像描述

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM