What I am struggling with is getting a pygame program to stop my function after 5 presses of the "y" key in a previous function.
I am creating a program in pygame for a study with participants. I have created a function that displays a sentence on the screen, each sentence taken from a row in a dataframe. Whether that sentence is true or not according to a given participant, the "y" or "n" key will be pressed and is recorded in a new column in the dataframe.
Right now my function is iterating over all 100 sentences in the dataframe. I would like it to stop iterating after the "y" key has been pressed 5 times.
In getting the response from the keyboard, I have written:
def getResponse(trialNum):
while True:
sentence = pygame.event.wait()
if sentence.type == pygame.QUIT:
self.gameExit = True
if sentence.type == pygame.KEYDOWN:
if sentence.key == pygame.K_y:
keyname = 'yes'
**yesCount = len(pygame.key.get_pressed())
break
if sentence.key == pygame.K_n:
keyname = 'no'
break
screen.fill((white))
The row with the asterisk (*) indicates my attempt to count the number of times the "y" key has been pressed, which I had hoped to refer to in the following function:
Starting the program:
def start():
screen.fill(white)
# trials loop
start_val = 0
stop_val = 5
while start_val < stop_val:
for trial in range(0,numEvents): #specifies number of events
showOneEvent(trial)
end()
I have tried to go to this Question about while-for loops and have been unsuccessful. Can someone please let me know what I am missing syntactically in defining the program to stop after I have pressed the "yes" button 5 times?
Thank you.
Inside your while loop try adding a break statement to exit the loop.
while start_val < stop_val:
for trial in range(0,numEvents): #specifies number of events
if (yesCount == 5)
break;
showOneEvent(trial)
you could also try adding to your start_val since you're not reusing it.
while start_val < stop_val:
for trial in range(0,numEvents): #specifies number of events
start_val = start_val + 1
showOneEvent(trial)