Hello and Thank You in advance. I worked on a script, only to realize it has a major flaw.
In this code below BUTTON
# 8 calls the schedule. It works very well, until I press the BUTTON
# 8 more than once.
game_state = True
run = True
while run:
for event in pygame.event.get():
if event.type == JOYBUTTONDOWN:
print(event)
if event.button == 8:
if game_state == True:
schedule.every(3).seconds.until(timedelta(minutes=60)).do(sendCommands)
else:
game_state = True # schedule on
if event.type == JOYHATMOTION:
if event.value[1] == 1:
game_state = False
if event.value[1] == -1:
game_state = False
if event.type == JOYBUTTONDOWN:
if event.button == 4:
game_state = False
if game_state == True:
schedule.run_pending()
pygame.quit()
sys.exit()
I would like JOYBUTTON
# 8 to only call the schedule once, and not again until game_state = False
. I noticed that if I accidentally hit the button more than once, the schedule gets called multiple times, running several instances at once. How do I prevent multiple instances from running at once?
Is there a way, once I press BUTTON
# 8, to disable it UNTIL I press either JOYHATMOTION
or BUTTON
# 4?
Read the sentence in your question carefully. The answer is hidden in the question:
I would like JOYBUTTON # 8 to only call the schedule once, and not again until
game_state = False
.
You need to initialize game_state = False
and start the schedule when game_state
is False
and the button is pressed. Set game_state = True
when the schedule is started:
game_state = False
run = True
while run:
for event in pygame.event.get():
if event.type == JOYBUTTONDOWN:
print(event)
if game_state == False and event.button == 8:
schedule.every(3).seconds.until(timedelta(minutes=60)).do(sendCommands)
game_state = True
if event.button == 4:
game_state = False
if event.type == JOYHATMOTION:
if event.value[1] == 1 or event.value[1] == -1:
game_state = False