Represent a cell in the minefield map
| 21 | |
| 22 | |
| 23 | class Cell(gui.TableItem): |
| 24 | """ |
| 25 | Represent a cell in the minefield map |
| 26 | """ |
| 27 | |
| 28 | def __init__(self, width, height, x, y, game): |
| 29 | super(Cell, self).__init__('') |
| 30 | self.set_size(width, height) |
| 31 | self.x = x |
| 32 | self.y = y |
| 33 | self.has_mine = False |
| 34 | self.state = 0 # unknown - doubt - flag |
| 35 | self.opened = False |
| 36 | self.nearest_mine = 0 # number of mines adjacent with this cell |
| 37 | self.game = game |
| 38 | |
| 39 | self.style['font-weight'] = 'bold' |
| 40 | self.style['text-align'] = 'center' |
| 41 | self.style['background-size'] = 'contain' |
| 42 | if ((x + y) % 2) > 0: |
| 43 | self.style['background-color'] = 'rgb(255,255,255)' |
| 44 | else: |
| 45 | self.style['background-color'] = 'rgb(245,245,240)' |
| 46 | self.oncontextmenu.do(self.on_right_click, js_stop_propagation=True, js_prevent_default=True) |
| 47 | self.onclick.do(self.check_mine) |
| 48 | |
| 49 | def on_right_click(self, widget): |
| 50 | """ Here with right click the change of cell is changed """ |
| 51 | if self.opened: |
| 52 | return |
| 53 | self.state = (self.state + 1) % 3 |
| 54 | self.set_icon() |
| 55 | self.game.check_if_win() |
| 56 | |
| 57 | def check_mine(self, widget, notify_game=True): |
| 58 | if self.state == 1: |
| 59 | return |
| 60 | if self.opened: |
| 61 | return |
| 62 | self.opened = True |
| 63 | if self.has_mine and notify_game: |
| 64 | self.game.explosion(self) |
| 65 | self.set_icon() |
| 66 | return |
| 67 | if notify_game: |
| 68 | self.game.no_mine(self) |
| 69 | self.set_icon() |
| 70 | |
| 71 | def set_icon(self): |
| 72 | self.style['background-image'] = "''" |
| 73 | if self.opened: |
| 74 | if self.has_mine: |
| 75 | self.style['background-image'] = "url('/my_resources:mine.png')" |
| 76 | else: |
| 77 | if self.nearest_mine > 0: |
| 78 | self.set_text(str(self.nearest_mine)) |
| 79 | else: |
| 80 | self.style['background-color'] = 'rgb(200,255,100)' |