import pygame
import math
# Main loop
running = True
while running:
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if pygame.mouse.get_pressed()[0] == True:
pos = pygame.mouse.get_pos()
print(pos)
# Fill the screen with white
screen.fill((255, 255, 255))
x_1 = 2 * HEX_SIZE + (HEX_SIZE / 2) # Distance btween each centers in x axis
y_1 = math.sin(math.radians(60)) * HEX_SIZE
center = (HEX_SIZE, HEX_SIZE)
# Draw hexagonal grid
for i in range(0, int(WIDTH / HEX_SIZE)):
# x_1=2*HEX_SIZE+(HEX_SIZE/2)
x1 = (5 * (HEX_SIZE / 2)) + (3 * HEX_SIZE * i)
for j in range(0, int(HEIGHT / HEX_SIZE)):
y = y_1 + y_1 * 2 * j
center = (x1, y)
draw_hexagon(screen, center, HEX_SIZE)
y_3 = HEX_SIZE * 1.732
for k in range(0, int(WIDTH / HEX_SIZE)):
x2 = HEX_SIZE + (k * 2 * HEX_SIZE + k * HEX_SIZE)
for l in range(0, int(HEIGHT / HEX_SIZE)):
y2 = y_3 + y_3 * l
center = (x2, y2)
draw_hexagon(screen, center, HEX_SIZE)
pygame.display.flip()
# Quit Pygame
pygame.quit()
In this hexagonal grids that I built, i wanted to change the hexagon's colour just by clicking on it. I have used pygame.mouse.get_pos() function for this step and it gives the position where I clicked.Now the challenge I'm facing here is how can I change the colour of the hexagon, i thought of make a new hexagon on the top of that existing hexagon but it wouldn't be possible to draw over the hexagon without knowing the exact center coordinates. Please if any one know how to solve this problem it will be really helpful or you guys know any other way of changing its color.