0

The code I'm writing puts up a shape on screen and allows for manipulation of the shape via the up/down arrow keys. What I've been trying to get it to do is make the amount the shape changes dependent on the sequence of keypresses; as long as the input is the same as the initial keypress, the amount the shape changes will be large. However, when the first 'reversal' of key press occurs (for instance, to make fine adjustments), every key press after that point (regardless if it is the same as the initial button press) should change the circle by a much smaller proportion (not interatively, just 0.1cm change rather than 2cm). The code has been written in Psychopy.

I think I've not grasped the way the loops should be set out for this, but I can't see how they might be changed to do what I want. Apologies for the actual code as opposed to a minimal example - any advice is much appreciated.

for thisTrial in trials:
    endKey = 0
    nKeypress = 0
    count = 0
    counting = 0
    if thisTrial == 'ellipse':
        ellipseHeightinit = 7.6,1.9 + (round(numpy.random.uniform(-1,1),1))
    elif thisTrial == 'circle':
        ellipseHeightinit = 7.6,7.6 + (round(numpy.random.uniform(-1,1),1))
    ellipseHeight = ellipseHeightinit
    ellipseStim.setSize(ellipseHeight, log = False) # set the initial size of the shape  
    while endKey == 0:
        ellipseStim.setAutoDraw(True)
        win.flip() # flip the window to see the stimuli
        allKeys = event.waitKeys() 
        if count < 1: #store the first keypress made
            for thisKey in allKeys:
                firstKeypress = thisKey
                count += 1
                event.clearEvents()
        for thisKey in allKeys: # change the size of the shape depending on key pressed
            if thisKey == 'up':
                nKeypress = nKeypress + 1
            elif thisKey == 'down':
                nKeypress = nKeypress - 1
            elif thisKey == 'space':
                endKey = 1
            while counting < 1: # attempt to make step size large until reversal
                if thisKey == firstKeypress:
                    ellipseHeight = 7.6, ellipseHeightinit[1] + nKeypress*20
                    break
                elif thisKey != firstKeypress:
                    ellipseHeight = 7.6, ellipseHeightinit[1] + nKeypress*0.1
                    counting += 1
                    break
       ellipseStim.setSize(ellipseHeight, log = False) # set new shape size     
    ellipseStim.setAutoDraw(False)
2
  • After reading this, I'm not really sure where to start but have you tried thinking about using a Chronological backtracking algorithm (for instance, to make fine adjustments)? Commented Feb 8, 2013 at 9:59
  • I'm very confused by this code. Inside the while loop, neither thisKey nor firstKeypress is changed. So, either you're going to hit the if and break out the first time, or hit the elif and break out the first time, or spin forever doing nothing and never leave the loop. Commented Feb 8, 2013 at 10:22

1 Answer 1

1

You should store somewhere which was the last key pressed, and which is the amount of movement last used. If the new key pressed is the same, you enlarge your amount. Otherwise you set it to the minimum value.

Sign up to request clarification or add additional context in comments.

2 Comments

variable thisKey should fulfil that function; in fact, I think that (in theory) the while counting <1: loop is doing what you suggest. However, at the moment, once I make the first reversal the shape refuses to change at all (despite the system definitely registering the key presses).
It seems like thisKey is the current key, the one you're processing. You also need a keyBeforeThisKey to compare to. Each time through the for thisKey in allKeys: loop, if thisKey != keyBeforeThisKey: you start the movement over. Then, either way, you set keyBeforeThisKey = thisKey. You don't need another loop inside the loop to do this, just an if.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.