Why having just a singe code component in a single routing don't work?

Hello everyone, I have a simple question!
I want to know why by having just a single routine containing just a code component, the code won’t be executed.
For example, I created a code component and put the following code in it but it didn’t work. What would be the reason?

import psychopy.visual
import psychopy.event

win = psychopy.visual.Window(
    size=[400, 400],
    color=[1, 1, 1]
text = psychopy.visual.TextStim(
    text="Hello, world!",
    color=[-1, -1, -1]


This sort of code isn’t really what is supposed to run in a Builder code component. i.e. Builder generates most of what you need for you (including importing PsychoPy modules, creating a window, flipping it, and so on). It also is built around a drawing loop that runs on every screen refresh, so indefinite functions like event.waitKeys() would break that. Code components are just meant to supplement the script that Builder generates, and have to be compatible with it. For example, all Builder scripts create a window called win. Your code would effectively destroy that window and create another one with the same name. This would break pretty much everything.

For a script like this, you should switch to the Coder view instead. There you get to control everything, including creating your own window to draw into, and you decide when to flip it, and can wait indefinitely for responses if you wish.

The good news is, this script should run fully in the Coder environment. But the question should be: why do you want to do that? Builder will do most of the heavy lifting for you, meaning that your custom code components just need to tweak things slightly as required, instead of doing everything from scratch.

Many thanks for your reply. In fact, the reason I did it is because I want to code my experiment which include showing participants a moving object, and what i did was sort of practice to see how it works.
Do you know what should I do to create moving objects in psychopy? I’m quite a beginner in psychopy and also in python programming but I want to learn.
Do you know any good source (documents or videos) to refer to?

All you need to do is put an expression in the “position” field of a stimulus component that is a function of time. Builder maintains a variable called t that is the time in seconds since a routine started. So if you put:

[5 * t, 0]

in a position field, set to update on every frame, then it will move to the right at 5 pixels per second (if the stimulus is set to use pixel units).

[cos(t) * 100, sin(t) * 100]

would move the stimulus around a circle of radius 100 pixels, and so on.