Skip to content

Commit 5221fd9

Browse files
committed
Add flags.
1 parent 467a524 commit 5221fd9

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

Diff for: dreamsweeper-gtk.py

+21-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
mine_num_color = gtk.gdk.color_parse('#FFF')
3535

3636
unknown_color = gtk.gdk.color_parse('#aaa')
37-
unknown_num_color = gtk.gdk.color_parse('#555')
3837

3938
def pango_layout_from_box(context, text, width, height):
4039
desc = pango.FontDescription('Sans')
@@ -111,16 +110,23 @@ def on_area_expose(self, widget, event):
111110
gc.set_rgb_fg_color(unknown_color)
112111
drawable.draw_polygon(gc, True, polygon)
113112

113+
if space in self.board.flagged_spaces:
114+
value = self.board.flagged_spaces[space]
115+
116+
if value:
117+
gc.set_rgb_fg_color(mine_color)
118+
else:
119+
gc.set_rgb_fg_color(clear_color)
120+
121+
drawable.draw_rectangle(gc, True, box[0] + box[2]/4, box[1] + box[3]/4, box[2]/2, box[3]/2)
122+
114123
adjacent = -1
115124
if space in self.board.known_spaces:
116125
value, adjacent = self.board.known_spaces[space]
117126
if value:
118127
gc.set_rgb_fg_color(mine_num_color)
119128
else:
120129
gc.set_rgb_fg_color(clear_num_color)
121-
elif space in self.board.flagged_spaces:
122-
value, adjacent = self.board.known_spaces[space]
123-
gc.set_rgb_fg_color(unknown_num_color)
124130

125131
if adjacent != -1:
126132
context = widget.get_pango_context()
@@ -138,6 +144,9 @@ def on_area_expose(self, widget, event):
138144
return True
139145

140146
def on_button_press(self, widget, event):
147+
if self.held_mouse_button is not None:
148+
return
149+
141150
allocation = widget.get_allocation()
142151
mouse_space = self.board.space_at_point(event.x, event.y, allocation.width, allocation.height)
143152

@@ -148,11 +157,17 @@ def on_button_press(self, widget, event):
148157
self.drawing_area.queue_draw()
149158

150159
def on_button_release(self, widget, event):
160+
if event.button != self.held_mouse_button:
161+
return
162+
151163
allocation = widget.get_allocation()
152164
mouse_space = self.board.space_at_point(event.x, event.y, allocation.width, allocation.height)
153165

154-
if self.held_mouse_button == 1 and mouse_space is not None:
155-
self.board.reveal_space(mouse_space)
166+
if mouse_space is not None:
167+
if self.held_mouse_button == 1:
168+
self.board.reveal_space(mouse_space)
169+
elif self.held_mouse_button == 3:
170+
self.board.flag_space(mouse_space, 1)
156171

157172
self.mouse_space = None
158173
self.held_mouse_button = None

Diff for: dreamsweeper.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def add_known_space(self, space, value, adjacent):
9191

9292
def reveal_space(self, space):
9393
if space in self.known_spaces:
94-
return
94+
return False
9595

9696
if self.first_space_zero and not self.known_spaces:
9797
self.add_known_space(space, 0, 0)
@@ -112,6 +112,22 @@ def reveal_space(self, space):
112112

113113
return True
114114

115+
def flag_space(self, space, value=None):
116+
if space in self.known_spaces:
117+
return False
118+
119+
if value is None:
120+
new_value = self.flagged_spaces.get(space, 2)-1
121+
else:
122+
new_value = value if self.flagged_spaces.get(space, -1) != value else -1
123+
124+
if new_value == -1:
125+
del self.flagged_spaces[space]
126+
else:
127+
self.flagged_spaces[space] = new_value
128+
129+
return True
130+
115131
class SquareBoard(Board):
116132
def __init__(self, width=12, height=12, mines=36):
117133
self.spaces = frozenset((x, y) for x in range(width) for y in range(height))

0 commit comments

Comments
 (0)