简体   繁体   English

pygame中的线图

[英]Line drawing in pygame

I am trying to draw a line in pygame that has a rounded cap unlike the image below.我试图在 pygame 中画一条线,它的圆帽与下图不同。 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:我能想到的最好的方法是在关节处使用pygame.draw.lines()并绘制额外的圆圈( pygame.draw.circle ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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