-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonManager.py
More file actions
100 lines (75 loc) · 4.01 KB
/
ButtonManager.py
File metadata and controls
100 lines (75 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from Button import *
import random
from constants import *
from numpy import array, isin, logical_not
from numpy.random import choice
from pygame.locals import *
class ButtonManager():
WIDTH = 15
HEIGHT = 15
BUTTONS_PER_COLUMN = 25
ROW_SPACING = 17
BUTTONS_PER_ROW = 30
spriteSheetImage = pygame.image.load("images/minesweeper_tiles.jpeg")
def __init__(self, window, outerBox):
self.imageList = []
self.spriteSheetImage = pygame.Surface.convert(ButtonManager.spriteSheetImage)
self.updateImageList(12, 128, 128)
self.tileImg, self.flagImg, self.bombImg, self.emptyImg, self.oneImg, self.twoImg, self.threeImage, self.fourImage, self.fiveImage, self.sixImage, self.sevenImg, self.eightImage = [img for img in self.imageList]
self.window = window
self.buttonRow = [(i * ButtonManager.WIDTH ) + (2 * i) + OUTERBOX_OFFSET/2 for i in range(ButtonManager.BUTTONS_PER_ROW)]
self.buttonList = [Button(window, outerBox.width, outerBox.height, x=row, y=(col * 17) + 32, hasBomb=choice([True, False], p = [0.15, 0.85]), buttonWidth=ButtonManager.WIDTH, buttonHeight=ButtonManager.HEIGHT, bottomImg=None, topImg=self.tileImg, ID=(((array(row) - 66) / 17) + 1) + (col * 30), buttonDownImg=self.emptyImg) for row in self.buttonRow for col in range(ButtonManager.BUTTONS_PER_COLUMN)]
self.triggered = False
self.bombsList = array([array([button.x, button.y]) for button in self.buttonList if button.hasBomb])
self.giveIcons()
def handleEvent(self, event):
for button in self.buttonList:
if not(self.triggered):
button.handleEvent(event)
def checkForBomb(self):
for button in self.buttonList:
if all(button.leftClicked) and button.hasBomb:
self.triggered = True
def draw(self):
for button in self.buttonList:
if self.triggered:
if button.hasBomb:
button.currentImg = self.bombImg
else:
if all(button.leftClicked):
button.currentImg = button.bottomImg
if button.bottomImg == self.emptyImg:
self.clearOut(button)
elif button.rightClicked:
if not(all(button.leftClicked)) and button.flagged:
button.currentImg = self.flagImg
elif not(all(button.leftClicked)) and not(button.flagged):
button.currentImg = self.tileImg
button.draw()
def updateImageList(self, nImages, width, height):
nCols = self.spriteSheetImage.get_width()
row = 0
col = 0
for imageNumber in range(nImages):
x = row * width
y = col * height
subRect = pygame.Rect(x,y, width, height)
image = self.spriteSheetImage.subsurface(subRect)
image = pygame.transform.scale(image, (ButtonManager.WIDTH, ButtonManager.HEIGHT))
self.imageList.append(image)
row += 1
if x == (nCols - width):
row = 0
col = col + 1
def giveIcons(self):
for button in self.buttonList:
button.bottomImg = self.imageList[3 + len([pair for pair in self.bombsList - array([button.x, button.y]) if (sum(abs(pair)) <= 34) & logical_not(isin(34, abs(pair)))])]
def clearOut(self, btn):
for button in self.buttonList:
#If up, right, left or down
if not(button.flagged):
#if button.rightClicked and adjacent button is not(leftClicked)
#pair = array([button.x, button.y])
if(button.x in [btn.x, btn.x + 17, btn.x - 17] and
button.y in [btn.y, btn.y + 17, btn.y - 17]):
button.leftClicked = [True, True]