Answer the question
In order to leave comments, you need to log in
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)
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
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)
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 questionAsk a Question
731 491 924 answers to any question