Hi,
I am struggling to get to grips with using a code component in my experiment and could really use some more experienced input. I have become pretty familiar with the builder and defining stimuli this way. I am very much a novice in using python though and have got this far mostly by trial and error.
I have described the problem below and what I have tried so far:
OS: Windows 10.
What are you trying to achieve?:
I am creating an experiment where participants view an image for 20s and are then asked to resize a circle to complete a size judgment task.
The passive stimuli and circle are all presented as image components in the builder and I have had no problems so far getting them to display. There are two routines, one to display the passive stimuli and the other to complete the resizing task.
The problem I am having is in trying to get the circle to be resized based on keyboard input using a code component.
What did you try to make it work?:
I have defined the circle as an image component called ‘ResizeCircle’. The image is linked to the filepath for the circle picture (constant) and I have defined the size as the original size of this image in pixels (194,194) and set this to change every frame. I have not placed an end duration on this component.
To create the change in size based on input I have defined a keyboard component ‘keyR’ and unticked the option to ‘force end of routine’. Allowed keys are: ‘1’, ‘9’, ‘space’, ‘escape’.
I then created a code component (placed at the top of the component list) and entered the following code in the ‘Each Frame’ section (the notes indicate what I am hoping each line should do):
if keyR == '1':
# press 1 to decrease circle size by 10 pix
ResizeCircle.size = ResizeCircle.size - 10.0
elif keyR == '9':
# inc circle size by 10 pix
ResizeCircle.size = ResizeCircle.size + 10.0
elif keyR == 'space':
# store the selected size in the data file and end the routine
thisExp.addData('size_judgement', ResizeCircle.size[0])
continueRoutine = False
elif keyR == 'escape':
core.quit()
#manually allow the experiment to be ended during the routine
What specifically went wrong when you tried that?:
No error messages appear and the experiment runs until it hits the routine. The circle appears in the centre of the screen as planned, however, pressing the keys does not alter the size of the circle or progress the experiment. ESC quits the experiment as ususal.
I suspect that the issue is either in the actual code I have written or in the way, I am defining/using the keyboard component but I am not knowledgeable to know where the issue lies, Changing either of these seems to either result in no change or a syntax error. I have looked a little into adding a keyboard from scratch using code, though to be honest, I did not understand much of what I found.
A more minor consideration:
Currently, the circle always starts the routine at the same size. I would like to modify this so that the circle can start at a range of sizes and be resized in the same way. I would then record the start and end size during the routine. I am thinking that I might achieve this using a variable component or the conditions file for the loop? I am not sure if this would influence the approach I need to take for the resizing problem though.
Thank you in advance and apologies for the wall of text!