Arrow in circular path (clockwise)

Hello everyone!
I need to set up an analog clock, with an arrow-shaped pointer that will go through a circular path (clockwise) and that full revolution will last 24 seconds, that is, it will be 8 steps,and each stop should last 3 seconds. The pointer will start at 270º. I know that my code is still very poor, I need some help to understand how to configure the steps that will give the clock movement.
Thanks

import math, numpy, random #to have handy system and math functions
from psychopy import core, event, visual, gui #these are the PsychoPy modules

#creates a window
myWin = visual.Window(color = 'white', units = 'pix', size = [800, 800],
allowGUI = False, fullscr = False) 


#Drawclock
disk = visual.Circle(myWin, radius = 80, fillColor = 'gray', lineColor = None)

#Drawclock_hand
clock_hand = visual.Line(myWin, start = [0,0], end = [-80, 0], lineColor = 'black', lineWidth = 3, ori = 0)
  
   #Here I need help!
        for angle in range(0, 360, 45):
        angleR = math.radians(angle)
        x =math.cos(angleR) * radius
        y =math.sin(angleR) * radius
        clock_hand.setPos([x,y])
        clock_hand.draw()
        
        
disk.draw()
myWin.flip()

core.wait(5) #waits for 5 seconds
myWin.close() #closes the window
exit()

Could you be a little more precise with what the problem is? There are a couple of issues with the code but it would be helpful to know exactly what problem(s) you want address.

But as a first step, would suggest that you don’t actually want to change the position of your clock hand. It starts at the origin, so all you need to do is update its orientation to match the current value of your angle variable.

Lastly, also bring the drawing of both stimuli and the win.flip() within your for loop.

My problem is with time. How should I change the direction from the angle to the next position (in a constant step of 45 degrees) as a function of time, so, considering that at the beginning of the trajectory, the pointer (which corresponds to a clock hand), is at 270 degrees, I need to move its angular position, precisely, to every 4 seconds. For example: clock hand in 270 ° (wait 4 seconds), jump to 315 (wait another 4 seconds), jump to 360 … and so on until the lap is complete.
Thanks!

You need to flip the display every time you want to update the position of the line.

# ...
current_angle = 0.0

while 1:
    angleR = math.radians(angle)
    x =math.cos(angleR) * radius
    y =math.sin(angleR) * radius
    clock_hand.setPos([x,y])
    clock_hand.draw()
    disk.draw()
    myWin.flip()

    core.wait(5) #waits for 5 seconds
    # update the angle 
    current_angle += 45.0

    if current_angle > 360.0:
        current_angle = 0.0

    # have some code here that checks for a button press and exits the loop
    if event.getKeys('q'):
        break

myWin.close() #closes the window

Okay, I get the main idea, thank you so much!
But my code is still pretty messed up. I need to study how to set it so that it runs from 270º to 270º (that is from 9 to 9 hours), and fix the pointer, which apparently is wrong, since it is rotating without angle, and horizontally.

Right I made some mistakes in there, this might be closer to what you want.

import math, numpy, random  # to have handy system and math functions
from psychopy import core, event, visual, gui  # these are the PsychoPy modules

# creates a window
myWin = visual.Window(color='white', units='pix', size=[800, 800],
                      allowGUI=False, fullscr=False)

# Drawclock
disk = visual.Circle(myWin, radius=80, fillColor='gray', lineColor=None)

# Drawclock_hand
clock_hand = visual.Line(myWin, start=[0, 0], end=[-80, 0], lineColor='black',
                         lineWidth=3, ori=0)

current_angle = 0.0

while 1:
    disk.draw()
    clock_hand.ori = current_angle
    clock_hand.draw()

    # update the angle
    current_angle += 45.0

    if current_angle >= 360.0:
        current_angle = 0.0

    # have some code here that checks for a button press and exits the loop
    if event.getKeys('q'):
        break

    myWin.flip()

    core.wait(0.5) # waits for 0.5 seconds

myWin.close() #closes the window

EDIT: updated some bits

1 Like

Yes, it’s working now! Thank you very much!

Hi, this seems quite similar to one of the existing demos: clockface.py.
Look in the menu Demos/stimuli/clockface.py
cheers, M

1 Like

Oh!! thank you! I’ll check this option too!