简体   繁体   English

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

[英]Why won't pygame draw a circle?

I am a beginner in python, and I'm trying to draw a circle wherever the mouse is(I also have a mouse and background image). 我是python的初学者,我试图在鼠标所在的地方绘制一个圆圈(我还有一个鼠标和背景图像)。 Here is my code: 这是我的代码:

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

Whenever I click nothing happens. 每当我点击没有任何反应。 Why doesn't it draw a circle? 为什么不画圆圈? Thanks for the help! 谢谢您的帮助!

I know almost nothing about pygame so I can't be much more help than this... but what I think you are doing is always drawing back over your circle. 我对pygame几乎一无所知,所以我不能比这更有帮助...但我认为你所做的总是在你的圈子上退缩。 Try this out: 试试这个:

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

Basically what is happening in your example is that when you mouse down event does a draw, you will then draw back over it again with the background. 基本上你的例子中发生的事情是,当你向下鼠标按下事件进行绘制时,你将再次使用背景绘制它。 I am not sure what mousec is but that will be drawn over the background every time. 我不确定mousec是什么,但每次都会在背景上绘制。 So you will never see a circle drawn from your mouse click 因此,您永远不会看到从鼠标单击中绘制的圆圈

My example fills the background once to start, and then when there is a mouse down, it will fill the background again to draw over the previous state, and then draw the circle. 我的示例填充背景一次开始,然后当鼠标按下时,它将再次填充背景以绘制先前的状态,然后绘制圆圈。

Another approach, using your exact example, is to only record the mouse position when checking the event, and defer the drawing of the circle until after your layers. 另一种方法,使用您的确切示例,仅在检查事件时记录鼠标位置,并将绘图推迟到图层之后。 You would "remember" the last mouse position to constantly redraw that circle on every loop: 您将“记住”最后一个鼠标位置,以便在每个循环中不断重绘该圆圈:

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

The difference in this approach is that you are always drawing everything on each loop as opposed to only in response to changes in events. 这种方法的不同之处在于,您总是在每个循环上绘制所有内容,而不是仅响应事件的变化。

Update 更新

In response to your question in the comments... a way to modify that second example to retain all the mouse clicks would be to save them up in a set and draw them back each time. 在评论中回答你的问题...修改第二个例子以保留所有鼠标点击的方法是将它们保存在一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()

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

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