Draw grid and and make "binary" 2d-array from clicked cells with stddraw-module in python

305 Views Asked by At

I would like to draw a grid and show it to the user, like the grid here:

enter image description here

Which was generated with this code:

def draw_canvas(x,y):
    # get width and height of the screen
    width, height = pygame.display.Info().current_w, pygame.display.Info().current_h
    # make it full screen
    stddraw.setCanvasSize(width, height)
    # set x and y scale  (add a 10% margin)
    stddraw.setXscale(-(x/10), x+(x/10))
    stddraw.setYscale(-(y/10), y+(y/10))
    # draw grid
    for r in range(y):
        for c in range(x):
            stddraw.setPenRadius(0.001)
            stddraw.rectangle(c, r, 1, 1)
    
    stddraw.show()

Now I'd like to have the possibility to click in the cells and return a "binary" array (with 1s where it was clicked and 0s where it wasn't clicked). I know there are some function like hasNextKeyTyped() and nextKeyTyped()- But I'm not really sure how to use them, how to determine the cell, the user clicked and how to end the input of the user.

Super thankful for any advice:)!!

0

There are 0 best solutions below