为什么pygame不会画一个圆圈?

时间:2012-08-09 21:03:41

标签: python pygame geometry

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

每当我点击没有任何反应。为什么不画圆圈?谢谢你的帮助!

1 个答案:

答案 0 :(得分:4)

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

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

<强>更新

在评论中回答您的问题...修改第二个示例以保留所有鼠标点击的方法是将它们保存在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()