r/pygame • u/False_Minute_2345 • Sep 19 '26
How to stop drawing
I'm making a simple drawing software in pygame because why not, and right now, i need to stop drawing when the user left clicks. For more context, here's my code:
while True:
# blah blah blah the other code #
if has_selected_line:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
last_mouse_pos = event.pos
print(last_mouse_pos)
start_drawing_line = True
if start_drawing_line:
pygame.draw.line(screen, "#000000", last_mouse_pos, current_mouse_pos, 10)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
pygame.draw.line(screen, "#000000", last_mouse_pos, current_mouse_pos, 10) #stop drawingIn that last line of code, whenever the user left clicks, i want the line that they made to permanently stamp on the screen and not follow them around anymore.
If anyone knows how to do this, thanks! And if you need more context, i can give it to you
1
Upvotes
2
u/kjunith Sep 19 '26 •
First: I wouldn't have the check for events in a scope, Swap that, otherwise it might not trigger the events, and some functions might not work as you assume.
And I believe you want you want to draw when button is pressed; and you can check and store that within the event loop:
clicked = Falsepressed = Falsedef events():clicked = Falsefor e in pygame.event.get():if e.type == pygame.MOUSEBUTTONDOWN:clicked = Truepressed = Trueif e.type == pygame.MOUSEBUTTONUP:pressed = False3
u/GABE_EDD Sep 19 '26 edited Sep 19 '26
start_drawing_line is never set to False, so once it's set to True it's True forever. And you're also telling it to draw the line two different times for some reason.
2
u/xvDeresh Sep 19 '26
set
start_drawing_line = Falsebefore the loop and changestart_drawing_line = Truetostart_drawing_line = not start_drawing_line2
u/Windspar Sep 19 '26 edited Sep 20 '26
You don't want to code. Your events get in a if block.