Hello,
I need a help to finish my psychopy code. The following code opens a psychopy window and waits for the mouse to be clicked. Once it is clicked it draws the path with the mouse cursor. However, I don’t seem to be able to program how to stop the drawing with another mouse click. Does anyone know how mouse can be told to disengage and to break the while statement?
My current solution is commented out at the bottom, but it doesn’t work the way it should. I guess this has something to do with the logical statement regarding the mouse, but at this stage these are frustrating guesses rather than informed ones.
Any help/advice is appreciated.
I’m using: PsychoPy3, version 2023.1.2 on macOS Monterey 12.6.6.
# Import libraries
from psychopy import visual, core, event, gui, data, logging
import os
import random
import math
import matplotlib.pyplot as plt
import numpy as np
import csv
# Set up window
win = visual.Window(size=(800, 600), fullscr=False)
# Component blob
fixationClock = core.Clock()
fixation = visual.Circle(win, size=0.1, lineColor='white', fillColor='white', pos=(0, 0))
fixation.draw()
win.flip()
core.wait(1)
# Set up mouse
mouse = event.Mouse(visible=True, win=win)
mouse.setPos(newPos=(0, 0))
# set the circle to the mouse position
circ = visual.Circle(win, size=0.02, lineColor='white', fillColor='white', pos=(0, 0))
# inititate the path variable to store the mouse positions
line_paths = [[0, 0]]
line_path = []
create_line = True
# Working
# wait for first click and start tracking mouse movements
while create_line:
mouse_click = mouse.getPressed()
if mouse_click[0] or mouse_click[1] or mouse_click[2] == 1:
# start tracking mouse movements and draw line every frame
x_start, y_start = mouse.getPos()
line_path.append(visual.Line(win, start=(x_start, y_start), end=(x_start, y_start), lineColor='white', lineWidth=2))
mouse.clickReset()
while True:
mouse_pos = mouse.getPos()
line_paths.append(mouse_pos)
#append the line path with start at the last mouse position and end at the current mouse position
line_path.append(visual.Line(win, start=(line_paths[-2][0], line_paths[-2][1]), end=(mouse_pos[0], mouse_pos[1]), lineColor='white', lineWidth=4))
for line in line_path:
line.setAutoDraw(True)
circ.pos = mouse_pos
circ.autoDraw = True
win.flip()
#if mouse_click[0] or mouse_click[1] or mouse_click[2] == 1:
# create_line = False
# break