Add pygame.Rect member to Square objects

This commit is contained in:
filifa 2024-03-19 20:38:20 -05:00
parent be52cb854d
commit a348da8785
2 changed files with 8 additions and 9 deletions

View File

@ -100,10 +100,9 @@ class Board:
mousex, mousey = mousepos
for row in self.squares:
for s in row:
sx, sy = s.coords
too_far_right = sx+self.square_size < mousex
too_far_up = sy+self.square_size < mousey
too_far_down = sy > mousey
too_far_right = mousex > s.dimensions.right
too_far_up = mousey < s.dimensions.top
too_far_down = mousey > s.dimensions.bottom
if not (too_far_right or too_far_up or too_far_down):
return s
return None

View File

@ -12,14 +12,14 @@ class Square:
self.is_questioned = False
self.mines_touching = 0
self.coords = (x, y)
self.dimensions = pygame.Rect(x, y, self.size, self.size)
self.set_img("png/Minesweeper_unopened_square.png")
def draw_line(self):
RED = (255,0,0)
top_left = self.coords
bottom_right = (self.coords[0]+self.size, self.coords[1]+self.size)
pygame.draw.line(self.surface, RED, top_left, bottom_right, 10)
topleft = self.dimensions.topleft
bottomright = self.dimensions.bottomright
pygame.draw.line(self.surface, RED, topleft, bottomright, 10)
def update_img(self):
if self.is_mine:
@ -46,7 +46,7 @@ class Square:
def set_img(self, img):
self.img = pygame.image.load(img)
self.img = pygame.transform.scale(self.img, (self.size, self.size))
self.surface.blit(self.img, self.coords)
self.surface.blit(self.img, self.dimensions.topleft)
def reveal(self):
if not self.is_flagged and not self.is_clicked: