Add pygame.Rect member to Square objects
This commit is contained in:
parent
be52cb854d
commit
a348da8785
7
board.py
7
board.py
|
@ -100,10 +100,9 @@ class Board:
|
||||||
mousex, mousey = mousepos
|
mousex, mousey = mousepos
|
||||||
for row in self.squares:
|
for row in self.squares:
|
||||||
for s in row:
|
for s in row:
|
||||||
sx, sy = s.coords
|
too_far_right = mousex > s.dimensions.right
|
||||||
too_far_right = sx+self.square_size < mousex
|
too_far_up = mousey < s.dimensions.top
|
||||||
too_far_up = sy+self.square_size < mousey
|
too_far_down = mousey > s.dimensions.bottom
|
||||||
too_far_down = sy > mousey
|
|
||||||
if not (too_far_right or too_far_up or too_far_down):
|
if not (too_far_right or too_far_up or too_far_down):
|
||||||
return s
|
return s
|
||||||
return None
|
return None
|
||||||
|
|
10
square.py
10
square.py
|
@ -12,14 +12,14 @@ class Square:
|
||||||
self.is_questioned = False
|
self.is_questioned = False
|
||||||
|
|
||||||
self.mines_touching = 0
|
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")
|
self.set_img("png/Minesweeper_unopened_square.png")
|
||||||
|
|
||||||
def draw_line(self):
|
def draw_line(self):
|
||||||
RED = (255,0,0)
|
RED = (255,0,0)
|
||||||
top_left = self.coords
|
topleft = self.dimensions.topleft
|
||||||
bottom_right = (self.coords[0]+self.size, self.coords[1]+self.size)
|
bottomright = self.dimensions.bottomright
|
||||||
pygame.draw.line(self.surface, RED, top_left, bottom_right, 10)
|
pygame.draw.line(self.surface, RED, topleft, bottomright, 10)
|
||||||
|
|
||||||
def update_img(self):
|
def update_img(self):
|
||||||
if self.is_mine:
|
if self.is_mine:
|
||||||
|
@ -46,7 +46,7 @@ class Square:
|
||||||
def set_img(self, img):
|
def set_img(self, img):
|
||||||
self.img = pygame.image.load(img)
|
self.img = pygame.image.load(img)
|
||||||
self.img = pygame.transform.scale(self.img, (self.size, self.size))
|
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):
|
def reveal(self):
|
||||||
if not self.is_flagged and not self.is_clicked:
|
if not self.is_flagged and not self.is_clicked:
|
||||||
|
|
Loading…
Reference in New Issue