本文介绍了向下循环鼠标按钮以绘制线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要用 mousebuttondown 画线而不是点

当点击鼠标程序绘制点时,我假设需要另一个循环来在按住鼠标按钮的情况下绘制线条.

同时继续:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:keep_going = 错误如果 event.type == pygame.MOUSEBUTTONDOWN:现场 = event.pospygame.draw.circle(屏幕,绿色,点,半径)pygame.display.update()

我想在窗口中画线而不是点.

解决方案

使用

run = True宽度 = 3点数 = []运行时:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误如果 event.type == pygame.MOUSEBUTTONUP:点附加(事件.pos)屏幕填充(0)如果 len(points) >1:pygame.draw.lines(screen, (255, 255, 255), False, points, width)如果 len(点):pygame.draw.line(screen, (255, 255, 255), points[-1], pygame.mouse.get_pos(), width)pygame.display.flip()

Need to draw lines rather than dots with mousebuttondown

When mouse is clicked program draws dots, I assume another loop is needed to draw lines with the mouse button held down.


while keep_going:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keep_going = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            spot = event.pos
            pygame.draw.circle(screen, GREEN, spot, radius)
            pygame.display.update()

I would like to draw lines instead of dots in my window.

解决方案

Use pygame.draw.lines, to connect a point list by a line.

Append the current mouse position to a list, if the mouse button is released:

if event.type == pygame.MOUSEBUTTONUP:
    points.append(event.pos)

Draw the list of points, if there is more than 1 point in the list:

if len(points) > 1:
    pygame.draw.lines(screen, (255, 255, 255), False, points, width)

Draw a "rubber band" from the last point in the list to the current mouse position:

if len(points):
    pygame.draw.line(screen, (255, 255, 255), points[-1], pygame.mouse.get_pos(), width)

See the simple example:

run = True
width = 3
points = []
while run:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.MOUSEBUTTONUP:
            points.append(event.pos)

    screen.fill(0)
    if len(points) > 1:
        pygame.draw.lines(screen, (255, 255, 255), False, points, width)
    if len(points):
        pygame.draw.line(screen, (255, 255, 255), points[-1], pygame.mouse.get_pos(), width)
    pygame.display.flip()

这篇关于向下循环鼠标按钮以绘制线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 11:47