O
O
OccamaRazor2017-01-25 12:47:43
Python
OccamaRazor, 2017-01-25 12:47:43

How to display X,Y coordinates of checkerboard cells?

Give me an idea how to do this? Messed up with cycles

CELL_WIDTH = CELL_HEIGHT = 75
        for X in range(CELL_WIDTH, 610, CELL_WIDTH):
            for Y in range(CELL_HEIGHT, 610, CELL_HEIGHT):
                if (Y + X) % 2 == 0:
                    pygame.draw.rect(DISPLAY, WHITE_CELL, [X, Y, CELL_WIDTH, CELL_HEIGHT])
                else:
                    pygame.draw.rect(DISPLAY, BLACK_CELL, [X, Y, CELL_WIDTH, CELL_HEIGHT])

                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        if (X+75,Y) > event.pos > (X, Y):
                            print(X,Y)

When you click on a cell, it displays the entire line at once, where X is the correct coordinate and Y is simply iterated, so how can you make sure that Y is determined and then displayed?:
300 75
300 150
300 225
300 300
300 375
300 450
300 525
300 600

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
Ulyana Illiterate, 2017-01-25
@OccamaRazor

CELL_WIDTH = CELL_HEIGHT = 75
for p in [(x, y) for x in  range(CELL_WIDTH, 610, CELL_WIDTH) for y  in range(CELL_HEIGHT, 610, CELL_HEIGHT)]:
     if (p[0] + p[1] ) % 2 == 0:
                    pygame.draw.rect(DISPLAY, WHITE_CELL, [X, Y, CELL_WIDTH, CELL_HEIGHT])
                else:
                    pygame.draw.rect(DISPLAY, BLACK_CELL, [X, Y, CELL_WIDTH, CELL_HEIGHT])

                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        if (X+75,Y) > event.pos > (X, Y):
                            print(X,Y)

Generally something like this)

D
DennyD314, 2017-01-25
@DennyD314

For the first time I heard about pygame, I don’t really understand the logic.
In the loop, a checkerboard is drawn and at the same time a click event is expected on some area?
Mb this thing should be taken out of the main cycle and write something like

if event.type == pygame.MOUSEBUTTONDOWN:
    if event.button == 1:
        for i in range(CELL_WIDTH, 610, CELL_WIDTH):
            for j in range(CELL_HEIGHT, 610, CELL_HEIGHT):              
                 if (i+75,j) > event.pos > (i, j):
                            print(i,j)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question