I've been having a little issue with my code. I am trying to make a minesweeper game. as you know, minesweeper has a reset button and a timer at the top, so I've made my list start 50 from the top, to be able to add them down the line, however, this also means when I click on a square, the square a couple of rows down gets clicked instead.
How can I avoid this? is there a way to make it start lower without the mouse click also being lower? or which values will I need to change for it to accommodate it?
Thank you
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
WIDTH = 20
HEIGHT = 20
MARGIN = 5
grid = []
for row in range(10):
grid.append([])
for column in range(10):
grid[row].append(0)
print(grid)
pygame.init()
WINDOW_SIZE = [255, 265]
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("Array Backed Grid")
done = False
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
column = pos[0] // (WIDTH + MARGIN)
row = pos[1] // (HEIGHT + MARGIN)
grid[row][column] = 1
print("Click ", pos, "Grid coordinates: ", row, column)
screen.fill(BLACK)
for row in range(10):
for column in range(10):
color = WHITE
if grid[row][column] == 1:
color = GREEN
pygame.draw.rect(screen,
color,
[(MARGIN + WIDTH) * (column) + MARGIN,
50+(MARGIN + HEIGHT) * row + MARGIN,
WIDTH,
HEIGHT])
clock.tick(60)
pygame.display.flip()
pygame.quit()
Your grid Y starts at 50, so subtract 50 from the mouse position when determining the row.
row = (pos[1]-50) // (HEIGHT + MARGIN)
Also add 50 to your window size to see the entire grid.
WINDOW_SIZE = [255, 315]