psychopy.org | Reference | Downloads | Github

Cursor moves in random direction

Hi there! I am trying to move a cursor (a circle) to left and right visual stimuli (two squares) and when the cursor reaches the positions of these squares at left and right, it should be selected.
When I press the event keys, the cursor is now moving in some random direction instead of left or right. I want the cursor to be moved with one steps to right and left with each of the corresponding key presses.

Can someone look into my code and let me know where I have gone wrong?

from psychopy import visual, core, event 
import numpy as np


# Create a window.
# For configuring and debugging the code turn off full screen.
fullscr = False
win = visual.Window(
    [1200,1000],
    monitor="testMonitor",
    units="deg",
    fullscr=fullscr
    )
    
cursor = visual.Circle(win, radius=0.2)
mouse = event.Mouse(visible=True)


# Sinusoidal control version.
freq_one = 0.5
freq_two = 1.5
# Colors of the rectangles.
color_one = 'red'
color_two = 'green'
# Positions of the rectanges.
pos_one = (-10, 0)
pos_two = (10, 0)


start = core.getTime()
cnt = 0
while cnt<600:
    second = core.getTime() - start

    sin_val_one = 0.5+0.5*np.sin(2 * np.pi * second * float(freq_one))
    sin_val_two = 0.5+0.5*np.sin(2 * np.pi * second * float(freq_two))

#while not mouse.getPressed()[0]:
    # Do something if mouse moved
    move = mouse.getPos()
    
    for key in event.getKeys():
        if key == 'escape': 
           core.quit()
        elif key == 'r':
           cursor.pos = move + 2
        elif key =='l':
           cursor.pos = move - 2 
           
#        if cursor.pos == pos_one:
#            mouse.getpressed(rect_one)
#        elif cursor.pos == pos_two:
#            mouse.getpressed(rect_two)
           
    mouse.setPos(cursor.pos)  
    mouse.lastPos = cursor.pos  
    
    rect_one = visual.Rect(
        win=win,
        fillColor=color_one,
        lineColor=color_one, 
        size=15,
        pos=pos_one,
        opacity=sin_val_one
        )
    rect_two = visual.Rect(
        win=win,
        fillColor=color_two,
        lineColor=color_two, 
        size=15,
        pos=pos_two,
        opacity=sin_val_two
        )

    rect_one.draw()
    rect_two.draw()
    cursor.draw()
    win.flip()
    cnt += 1

win.close()

Any help is greatly appreciated. Thanks !

The issue is resolved.