I'm currently creating a minesweeper game and my code looks something like this (for creating the rectangles). I've already created a list of every rectangle and their coordinates but there's one thing I now need. I need to know if the mouse pointer clicks on a rectangle (and which rectangle - each will have different information). When it clicks on one, it will check if that one has a bomb.
for i in range(1,boardSquaresY):
global rectList
for x in range(1,boardSquaresX):
pygame.draw.rect(display,gray,pygame.Rect(squareX*x,squareY,tileSize,tileSize))
rectList.append(Rect(squareX*x,squareY,tileSize,tileSize))
print(rectList)
for stroke in range(4):
pygame.draw.rect(display, (0,0,0), pygame.Rect(squareX*x-2, squareY-2, tileSize + 2, tileSize + 2), 1)
pygame.draw.rect(display, (220, 220, 220), pygame.Rect(squareX * x - 4, squareY - 4, tileSize + 2, tileSize + 2), 1)
pygame.draw.rect(display, (150, 150, 150), pygame.Rect(squareX * x - 4, squareY - 4, tileSize + 0.5, tileSize + 1), 1)
squareY += 50
Full code below (currently) - only done UI so far.
import pygame
from pygame.locals import *
#Beginner has a 9x9 board and 10 mines
rectList = []
def Main():
pygame.init()
display = pygame.display.set_mode((850,600),0,32)
white = (255,255,255)
blue = (0,0,255)
gray = (220,220,220)
display.fill(white)
squareX = 50
squareY = 50
tileSize = 50
boardSquaresX = 10
boardSquaresY = 10
mouse = pygame.mouse.get_pos()
print(mouse)
for i in range(1,boardSquaresY):
global rectList
for x in range(1,boardSquaresX):
pygame.draw.rect(display,gray,pygame.Rect(squareX*x,squareY,tileSize,tileSize))
rectList.append(Rect(squareX*x,squareY,tileSize,tileSize))
print(rectList)
for stroke in range(4):
pygame.draw.rect(display, (0,0,0), pygame.Rect(squareX*x-2, squareY-2, tileSize + 2, tileSize + 2), 1)
pygame.draw.rect(display, (220, 220, 220), pygame.Rect(squareX * x - 4, squareY - 4, tileSize + 2, tileSize + 2), 1)
pygame.draw.rect(display, (150, 150, 150), pygame.Rect(squareX * x - 4, squareY - 4, tileSize + 0.5, tileSize + 1), 1)
squareY += 50
while True:
mouse = pygame.mouse.get_pos()
if mouse[0]
if squareX * x + tileSize > mouse[0] > squareX * x - tileSize and squareY + tileSize > mouse[1] > squareY - tileSize:
print(mouse)
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Main()
rectList = []