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). 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. Try this out:
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
mousecis but that will be drawn over the background every time. So you will never see a circle drawn from your mouse clickMy 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:
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
setand draw them back each time.