Mouse click to pause an infinite loop?

42 views
Skip to first unread message

Scott Pflaumer

unread,
Apr 18, 2015, 7:26:50 PM4/18/15
to glowscri...@googlegroups.com
I would like to pause an infinite loop with a mouse click. I'm sure it's possible, but I'm having trouble accomplishing it. I'm not a terribly experienced programmer. 

Thanks,
-Scott

Bruce Sherwood

unread,
Apr 18, 2015, 7:42:08 PM4/18/15
to glowscri...@googlegroups.com
Don't feel bad. It's not obvious and probably there should be a simpler way to do this:

run = True
clicked = False

def getclick():
    global clicked
    clicked = True

scene.bind('click', getclick)

while True:
    rate(200)
    if not run:
        if clicked:
            clicked = False
            run = True
        else: continue
    # rest of the infinite loop

Larry Engelhardt

unread,
Oct 24, 2021, 7:08:35 PM10/24/21
to Glowscript Users
This response from 2015 was is helpful (I just had the same question), but I don't think it quite works as written.  Here is a slightly modified version, with comments to describe the logic:

running = True  # Indicates that the simulation is currently set to be running
clicked = False # Indicates that the mouse has not been clicked

# Create a function to keep track of left-clicks of the mouse
def left_click():
    global clicked # To be able to modify the variable "clicked" within the function
    clicked = True # If the mouse is clicked, set "clicked" to be True
# When the mouse is left clicked, the "left_click" function (above) will execute
scene.bind('left click', left_click)

while True:
    rate(200)
    if clicked: # If the mouse gets clicked...
        running = !running # If running == True, set to False. If False, set to True.
        clicked = False    # Done with the current click, so set clicked = False
    if running == False:
        continue # Skip the rest of the loop if running is set to False
    

Reply all
Reply to author
Forward
0 new messages