Builder isn’t Coder
I strongly discourage the use of PsychoPy Coder for the creation of psychology experiments because Builder is easier to debug, share with others and translate into PsychoJS for use online.
If you already have an experiment written in Coder, pasting the code into Builder code components is not sufficient to turn it into a Builder experiment. The logic of how to arrange an experiment is slightly different because of the way Builder works with components, routines and loops.
For example, don’t import packages that are imported as standard by Builder. The following code is at the top of every Builder generated Python script. Specifically, do not import numpy, random or os.
# --- Import packages ---
from psychopy import locale_setup
from psychopy import prefs
from psychopy import plugins
plugins.activatePlugins()
prefs.hardware['audioLib'] = 'ptb'
prefs.hardware['audioLatencyMode'] = '3'
from psychopy import sound, gui, visual, core, data, event, logging, clock, colors, layout, hardware
from psychopy.tools import environmenttools
from psychopy.constants import (NOT_STARTED, STARTED, PLAYING, PAUSED,
STOPPED, FINISHED, PRESSED, RELEASED, FOREVER, priority)
import numpy as np # whole numpy lib is available, prepend 'np.'
from numpy import (sin, cos, tan, log, log10, pi, average,
sqrt, std, deg2rad, rad2deg, linspace, asarray)
from numpy.random import random, randint, normal, shuffle, choice as randchoice
import os # handy system and path functions
import sys # to get file system encoding
import psychopy.iohub as io
from psychopy.hardware import keyboard
Don’t create a window, unless you are using your experiment across two screens. PsychoPy will create one and call it win.
if win is None:
# if not given a window to setup, make one
win = visual.Window(
size=_winSize, fullscr=_fullScr, screen=0,
winType='pyglet', allowGUI=False, allowStencil=False,
monitor='testMonitor', color=[0,0,0], colorSpace='rgb',
backgroundImage='', backgroundFit='none',
blendMode='avg', useFBO=True,
units='height',
checkTiming=False # we're going to do this ourselves in a moment
)
else:
# if we have a window, just set the attributes which are safe to set
win.color = [0,0,0]
win.colorSpace = 'rgb'
win.backgroundImage = ''
win.backgroundFit = 'none'
win.units = 'height'
Make use of the variables t (routine timer) and frameN.
while continueRoutine:
# get current time
t = routineTimer.getTime()
tThisFlip = win.getFutureFlipTime(clock=routineTimer)
tThisFlipGlobal = win.getFutureFlipTime(clock=None)
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
Do not use win.flip(). One is automatically added at the end of each frame. If you are controlling two screens, then you will need to flip the second screen.
# refresh the screen
if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
win.flip()
Don’t use event.waitKeys(), core.wait() or any other code that interrupts the natural flow of the experiment.
Finally, if you use AI to help you code be aware that you are likely to be given code suitable for Coder rather than Builder.