Trying to create a sodoku task using Psychopy/JS

Hi, I am trying to create a sodoku task that can be executed through PsychoJS. I know that this can be created through PyGame (Building and visualizing Sudoku Game Using Pygame - GeeksforGeeks) but I am not sure whether I should mix the capabilities of PyGame with Psychopy. As a result, I’d rather recreate this using PsychoPy natively. Is there any way I can adopt the pygame code above to work in psychopy?

I’ve also checked Pavlovia for a template and it looks like there’s no existing sodoku task via PsychoJS (yet!)

OS (e.g. Win10): Mac 14.0
PsychoPy version (e.g. 1.84.x): 2022.1.4

An update: I have converted the script into Psychopy, and created a sodoku grid. However, when I click on one cell in the grid, the cell that is registered by Psychopy is incorrect. For example, if I click the cell that is located in column 6, Psychopy will register that as me clicking a cell in column 7. I believe the issue is with the following function — would be grateful for any and all feedback!

# Function to convert mouse click position to grid coordinates based on cell boundaries
def get_grid_coordinates(mouse_pos):
    mouse_x, mouse_y = mouse_pos

    # Check if the click is within the boundaries of a single cell
    for grid_x in range(9):
        for grid_y in range(9):
            # Calculate the boundaries for the current cell
            left_boundary = grid_origin_x + grid_x * cell_size
            right_boundary = left_boundary + cell_size
            top_boundary = grid_origin_y - grid_y * cell_size
            bottom_boundary = top_boundary - cell_size

            print('grid x:', grid_x)
            print('LEFT BOUNDARY: %i, RIGHT BOUNDARY: %i' % (left_boundary, right_boundary))

            # Check if the mouse click falls within this cell's boundaries
            if left_boundary <= mouse_x < right_boundary and bottom_boundary <= mouse_y < top_boundary:
                # Debugging output
                print(f"Mouse Position: {mouse_pos}, Grid Coordinates: ({grid_x}, {grid_y})")
                return grid_x, grid_y

                print('\n')

    return None

Do the print statements give you any useful information?

Yes! So, the x axis left boundary and right boundary of the sixth column in the sodoku grid are 1 and 3, respectively. However, the actual x axis mouse position that is registered when I click on the cell (i.e., between the left and right boundary of this sixth column) will register as 9. Ideally, the mouse position should be somewhere between 1 and 3.

In a similar vein, the x coordinate of the left edge of the entire sodoku grid is -10. However, when I click on that ‘edge’, the x axis position of the reported mouse coordinate is -40.

I am wondering if this is related to the issues with mouse coordinates and Mac retina screens reported around 2022, but I have recently updated to the 2024 Psychopy version and I still run into the same issue.