# Drag & drop end trial subject to specific condition

**URL:** <https://discourse.psychopy.org/t/drag-drop-end-trial-subject-to-specific-condition/17904>\
**Category:** Coding\
**Created:** [November 7, 2020, 11:56pm UTC](https://discourse.psychopy.org/t/drag-drop-end-trial-subject-to-specific-condition/17904 "2020-11-07T23:56:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marta\_G](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/marta_g/32/10701_2.png) [@Marta\_G](https://discourse.psychopy.org/u/Marta_G)\
**Post date:** [November 7, 2020, 11:56pm UTC](https://discourse.psychopy.org/t/drag-drop-end-trial-subject-to-specific-condition/17904/1 "2020-11-07T23:56:10Z")

</div>

Hello,

I am looking for some help with the code element of my experiment. Perhaps someone can help.

I am designing an experiment in the Builder where in each trial participants will be presented with 3 images at a time & need to place each one of these images in a designated space (there are 3 spaces i.e. polygons).

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/7/7/77ea7642fb5fd0be259e7294395337963deb0c50.png)  
Participants aren’t permitted to progress onto the next trial unless the 3 images have been placed correctly.

I’ve implemented the drag & drop design ([Drag & Drop Stimuli](https://discourse.psychopy.org/t/drag-drop-stimuli/8503)) which allows me to drag the images & place them anywhere I want at the moment & without restrictions. What I am missing is a condition (presuming in the ‘End routine’ tab of my code element) which specifies that unless the images have been placed in the correct spot the next trial won’t begin. I have a condition file from which images are randomly selected. I have also included a column per image (image1corPos, image1corPos, image3corPos) which specifies its designated position (corresponds with the 3 polygons).

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/a/9/a9a8937d73513e368b70a3b8245e065f816f6804.png)  
I have read numerous posts but was unable to find a suitable line of code. Solutions which I have come across all specify the position of the mouse as a prompt to begin the next trial. I can’t see how that would work here since I have 3 images which need to be placed in the exact location before the next trial can commence. But again I have no coding experience.

Any help/suggestions would be appreciated.  
Many thanks!

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [November 8, 2020, 11:20am UTC](https://discourse.psychopy.org/t/drag-drop-end-trial-subject-to-specific-condition/17904/2 "2020-11-08T11:20:37Z")

</div>

> [@Marta\_G](#):
>
> presuming in the ‘End routine’ tab of my code element)

No, you want this code in the “each frame” tab, as the code in the “end routine” tab already presumes that the routine is ending. There is no way to stop it ending at that stage.

How is the end of the routine supposed to be initiated? e.g. does the subject push a key, or click a button, or should it be triggered automatically by the last stimulus being placed in the correct position? (If the latter, it probably needs to be integrated with your current drag-and-drop code.)

---

<div class="post-metadata">

**Author:** ![Marta\_G](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/marta_g/32/10701_2.png) [@Marta\_G](https://discourse.psychopy.org/u/Marta_G)\
**Post date:** [November 8, 2020, 1:40pm UTC](https://discourse.psychopy.org/t/drag-drop-end-trial-subject-to-specific-condition/17904/3 "2020-11-08T13:40:49Z")

</div>

Hi Michael,

Thank you for responding so quickly.

Point about the location of the code noted - thank you.

Preferably I would like for the trial to cease once all 3 images have been placed correctly (without my participants having to push any other button i.e. continue or next) and then for the next trial to commence. Ideally after each trial I would like to include a ‘correct!’ text, but this can be skipped if it complicates things. In another task I’ve implemented correct/incorrect feedback but this was done by including another routine in the loop with a text and code element - not sure whether that would mess things up here so the positive feedback upon correct placement of all 3 items can be skipped. My main priority is ending each trial once 3 items are in their designated location.

Currently this is the only code I have:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/d/b/dbed23a392e80a7209011eee2287ad539e9f1747.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/a/9/a90eeae7bc196fd3e55fbbdbaf00f5f666d2671f.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/2/b/2b4a64bda3658aec45e359992a5a3adb860df0db.png)  
Any suggestions on how to implement it would be much appreciated.

Thank you!

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [November 8, 2020, 9:22pm UTC](https://discourse.psychopy.org/t/drag-drop-end-trial-subject-to-specific-condition/17904/4 "2020-11-08T21:22:14Z")

</div>

Hi,

- You might want to look at this thread, because this sort of code can fail if the subject moves the mouse quite quickly:

> [@Click and Drag lag](https://discourse.psychopy.org/t/click-and-drag-lag/10156/4):
>
> OK, this isn’t a bug per se but the inherent fact that one can move the mouse faster than the screen can update. i.e. what is happening here is that you have relatively small stimuli and a screen that I guess is updating at the typical rate of 60 Hz. So you can detect a click in a stimulus and then move the stimulus to the current mouse position. But on the next screen refresh, the new mouse position can easily have moved beyond the boundaries of the stimulus, so the .isPressedIn() check fails. …

- You will need to implement some sort of “snap to grid” feature, as you can’t rely on the subject being able to exactly drop the stimulus in the precise location you need in order to be able to check in code if it is in the correct place:

> [@Click and drag images to lock in a specified area](https://discourse.psychopy.org/t/click-and-drag-images-to-lock-in-a-specified-area/11233/4):
>
> It can certainly be done but will be complicated I can give you some general pointers but realistically, this is code that should be developed and tested interactively, rather than something I could just type out in one go. I’d suggest you start by associating each stimulus with its ordinal location, say counting lifting to right from 0 to 19: stimuli = [square0, square1, square2, …] # up to 20, you already have this list # create a dictionary that associates each stimulus with an ordinal #…

- Lastly, it is best to share your code by pasting in the text rather than using screenshots. From a screenshot, a person can’t make small modifications to your code without typing it all out from scratch.

> [@Please surround python code with backticks](https://discourse.psychopy.org/t/please-surround-python-code-with-backticks/1775):
>
> When you include python code on this forum please put triple “backticks” like this ``` on the line before and the line after your code. If you’ve done it correctly then your code will appear in a grey box with correct indentation like this (check the preview of your post on the right hand side before posting it) and other people will be better able to work out what might be right/wrong with your code. if myVal==True: break If you’ve already posted something that didn’t follow this rule th…
