-
Notifications
You must be signed in to change notification settings - Fork 0
/
cellwidget.py
57 lines (46 loc) · 1.87 KB
/
cellwidget.py
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
import kivy
from kivy.uix.button import Button
from kivy.graphics import Rectangle
from kivy.properties import StringProperty
import weakref
class CellWidget(Button):
"""One cell widget"""
images = {
"empty": "images/empty_cell.png",
"on_step": "images/onstep.png",
"unit": "images/unit.png",
"checked": "images/checked_cell.png",
"deactivated": "images/deactivated_cell.png",
"X": "images/bad_cell.png",
"default": "images/empty_cell.png",
"default_unit": "images/ship.png",
"bomb_unit": "images/bomb.png",
"nuclear_bomb_unit": "images/nuclear_bomb.png",
"biology_bomb_unit": "images/biology_bomb.png",
}
def __init__(self, game, cell, field, **kwargs):
super(CellWidget, self).__init__(**kwargs)
self.cell = cell
self.field = field
self.game = game
self.cell.stateObservers.append(weakref.proxy(self))
self.cell.addActivateObserver(weakref.proxy(self))
self.bind(on_press = self.onPress)
def onPress(self, e):
self.game.pushOn(self.cell, self.field)
def onCellStateChanged(self, cell, state):
self.redraw()
def redraw(self):
state = self.cell.state
if state != "default":
with self.canvas.before:
for type, decorator in self.cell.decorators.items():
Rectangle(source=self.images[decorator], pos=self.pos, size=self.size)
self.background_normal = self.images[state]
self.background_down = self.images[state]
def onDeactivated(self):
if self.cell.state == "default" or self.cell.state == 'empty':
self.background_normal = self.images['deactivated']
self.background_down = self.images['deactivated']
def onActivated(self):
self.redraw()