Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Perform functions according to coordinates within xy_grid?

edited July 2016 in OpenSesame

Hi there,
I was just wondering if it is possible to use the xy_grid function to draw a grid with different stimuli, according to the coordinates? Can you reference the coordinates somehow?
Essentially, I am trying to produce a grid of letters whose positions are random.
Many thanks,
Joff

Comments

  • edited 8:35AM

    Hi Joff,

    I'm not sure I understand your question correctly, but what you're describing is exactly what the xy_grid() function is for. A simple 3x3 grid with letters at random locations can be created like this:

    import random
    
    # Get a list of 9 letters
    letters = 'ABCDEFGHI'
    # Get a list of 3x3=9 (x, y) coordinates
    # and shuffle the order
    xy_list = xy_grid(n=3, spacing=40)
    random.shuffle(xy_list)
    
    # Now create a canvas with a letter on
    # each location and show it.
    my_canvas = canvas()
    for letter, (x, y) in zip(letters, xy_list):
        my_canvas.text(text=letter, x=x, y=y)
    my_canvas.show()
    

    If you have no experience with Python, you may also want to walk through one of the beginner Python tutorials:

    Cheers!
    Sebastiaan

  • edited 8:35AM

    Hi Sebastian,
    Sorry for the initial vagueness, regardless your advice was very helpful and I think I am very close to achieving what I want to do.

    Rather than these being completely random letters there are some rules. It is a visual search task where a letter F is presented in a grid of distracter letters (E's and t's). The size of the grid will change depending on the performance of the individual and the letters can be oriented as normal, or they can be mirror images.

    import random
    
    # Create canvas called c
    c = canvas(color='green', penwidth=2)
    print('Canvas size is:', c.size)
    
    # Set starting values for column and row variables
    i = 2
    j = 2
    
    # Create empty list with length i*j
    search_list = [None]*(i*j)
    print('Empty search list:', search_list)
    
    # Set lists for distractors and targets
    distractors = ['normal_e', 'mirror_e', 'left_t', 'right_t']
    targets = ['normal_f', 'mirror_f']
    
    # Randomly select distractors to fill search array
    # and then replace one distractor with a random target
    for n in range (i*j):
        search_list[n] = random.choice(distractors)
    
    # Set target variable and randomly select a target F to fill a random position
    # in the array
    var.target = 'target'
    var.target = random.choice(targets)
    search_list[random.randint(0, i*j)] = var.target
    print('The randomised search array is:', search_list)
    
    # Create empty xy_grid called search array
    search_array = xy_grid((i, j), 40)
    
    # Combine the stimuli with the search array !!!!!!!!!!!!
    for z, (x, y) in zip (search_list, search_array)
        if z == 'normal_e':
            c.line(x, y, x, y+20)
            c.line(x, y, x+10, y)
            c.line(x, y+10, x+10, y+10)
            c.line(x, y+21, x+10, y+21)
        elif z == 'mirror_e':
            c.line(x+11, y, x+11, y+20)
            c.line(x, y, x+10, y)
            c.line(x, y+10, x+10, y+10)
            c.line(x, y+21, x+10, y+21)
        elif z == 'left_t':
            c.line(x, y, x, y+20)
            c.line(x, y+10, x+10, y+10)
        elif z == 'right_t':
            c.line(x+11, y, x+11, y+20)
            c.line(x, y+10, x+10, y+10)
        elif z == 'normal_f':
            c.line(x, y, x, y+20)
            c.line(x, y, x+10, y)
            c.line(x, y+10, x+10, y+10)
        elif z == 'mirror_f':
            c.line(x+11, y, x+11, y+20)
            c.line(x, y, x+10, y)
            c.line(x, y+10, x+10, y+10)
    

    I am having a problem with the final loop which draws the letters into their corresponding positions in the grid. I imagine it is just a question of syntax, but I can't see it myself.

    I would greatly appreciate the help of your trained eye!

    Many thanks,
    Joff

  • Hi Joff,

    There are a few things that quite work out in the code you posted.

    1) There is a : missing in the end of the line in which you initiate the for loop
    2) Your usage of random.randint allows indices to be selected that are out of the range of your list. You should use i*j-1 instead
    3) You don't present anything yet. I added two lines in the end, that will actually show your displays (until a key is pressed)

    Here is your (updated) code:

    import random
    
    # Create canvas called c
    c = canvas(color='green', penwidth=2)
    # create also a keyboard
    k = keyboard()
    print('Canvas size is:', c.size)
    
    # Set starting values for column and row variables
    i = 2
    j = 2
    
    # Create empty list with length i*j
    search_list = [None]*(i*j)
    print('Empty search list:', search_list)
    
    # Set lists for distractors and targets
    distractors = ['normal_e', 'mirror_e', 'left_t', 'right_t']
    targets = ['normal_f', 'mirror_f']
    
    # Randomly select distractors to fill search array
    # and then replace one distractor with a random target
    for n in range (i*j):
        search_list[n] = random.choice(distractors)
    
    # Set target variable and randomly select a target F to fill a random position
    # in the array
    var.target = 'target'
    var.target = random.choice(targets)
    search_list[random.randint(0, i*j-1)] = var.target
    print('The randomised search array is:', search_list)
    
    # Create empty xy_grid called search array
    search_array = xy_grid((i, j), 40)
    
    # Combine the stimuli with the search array !!!!!!!!!!!!
    for z, (x, y) in zip (search_list, search_array):
        if z == 'normal_e':
            c.line(x, y, x, y+20)
            c.line(x, y, x+10, y)
            c.line(x, y+10, x+10, y+10)
            c.line(x, y+21, x+10, y+21)
        elif z == 'mirror_e':
            c.line(x+11, y, x+11, y+20)
            c.line(x, y, x+10, y)
            c.line(x, y+10, x+10, y+10)
            c.line(x, y+21, x+10, y+21)
        elif z == 'left_t':
            c.line(x, y, x, y+20)
            c.line(x, y+10, x+10, y+10)
        elif z == 'right_t':
            c.line(x+11, y, x+11, y+20)
            c.line(x, y+10, x+10, y+10)
        elif z == 'normal_f':
            c.line(x, y, x, y+20)
            c.line(x, y, x+10, y)
            c.line(x, y+10, x+10, y+10)
        elif z == 'mirror_f':
            c.line(x+11, y, x+11, y+20)
            c.line(x, y, x+10, y)
            c.line(x, y+10, x+10, y+10)
    
    # show canvas   
    c.show()
    k.get_key()
    

    I hope this solved your issue.

    Best,

    Eduard

    Buy Me A Coffee

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games