3

我是python的初学者,我试图在鼠标所在的地方画一个圆圈(我也有鼠标和背景图像)。这是我的代码:

while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit

        if event.type == MOUSEBUTTONDOWN:
            color = (100,100,100)
            posx,posy = pygame.mouse.get_pos()
            screen.lock()
            pygame.draw.circle(screen, color, (posx,posy), 50)
            screen.unlock()

    screen.blit(background,(0,0))
    x,y = pygame.mouse.get_pos()
    x -= mousec.get_width()/2
    y -= mousec.get_height()/2

    screen.blit(mousec, (x,y))

    pygame.display.update()

每当我点击什么都没有发生。为什么不画一个圆圈?谢谢您的帮助!

4

1 回答 1

5

我对 pygame 几乎一无所知,所以我无法提供比这更多的帮助......但我认为你正在做的总是在你的圈子里拉回。试试这个:

pygame.init()
screen = pygame.display.set_mode((640, 480))

screen.fill((0,0,0))

while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        if event.type == MOUSEBUTTONDOWN:
            # draw background first (however)
            screen.fill((0,0,0))

            # draw your other layers (mouse image)

            # draw the circle
            color = (255,255,255)
            posx,posy = pygame.mouse.get_pos()
            pygame.draw.circle(screen, color, (posx,posy), 50)

    pygame.display.update()

基本上,您的示例中发生的情况是,当您按下鼠标事件进行绘制时,您将再次使用背景绘制它。我不确定是什么mousec,但每次都会在背景上绘制。因此,您将永远不会看到通过鼠标单击绘制的圆圈

我的例子开始填充背景一次,然后当鼠标按下时,它会再次填充背景以绘制之前的状态,然后绘制圆圈。

使用您的确切示例的另一种方法是仅在检查事件时记录鼠标位置,并将圆的绘制推迟到您的图层之后。您将“记住”最后一个鼠标位置,以便在每个循环上不断重绘该圆圈:

last_mouse_pos = None

while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit

        if event.type == MOUSEBUTTONDOWN:
            last_mouse_pos = pygame.mouse.get_pos()

        elif event.type == KEYDOWN and event.unicode == 'c':
            # clear the circle when pressing the 'c' key
            last_mouse_pos = None

    screen.blit(background,(0,0))
    x,y = pygame.mouse.get_pos()
    x -= mousec.get_width()/2
    y -= mousec.get_height()/2

    screen.blit(mousec, (x,y))

    if last_mouse_pos:
        color = (100,100,100)
        posx,posy = last_mouse_pos
        pygame.draw.circle(screen, color, (posx,posy), 50)

    pygame.display.update()

这种方法的不同之处在于,您总是在每个循环上绘制所有内容,而不是仅响应事件的变化。

更新

为了回答您在评论中的问题...修改第二个示例以保留所有鼠标点击的方法是将它们保存在 a 中set并每次将它们拉回来。

mouse_clicks = set()

while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit

        if event.type == MOUSEBUTTONDOWN:
            mouse_clicks.add(pygame.mouse.get_pos())

        elif event.type == KEYDOWN and event.unicode == 'c':
            # clear the circle when pressing the 'c' key
            mouse_clicks.clear()

    screen.blit(background,(0,0))
    x,y = pygame.mouse.get_pos()
    x -= mousec.get_width()/2
    y -= mousec.get_height()/2

    screen.blit(mousec, (x,y))

    for pos in mouse_clicks:
        color = (100,100,100)
        posx,posy = pos
        pygame.draw.circle(screen, color, (posx,posy), 50)

    pygame.display.update()
于 2012-08-09T21:18:50.253 回答