diff options
author | Agustin Zubiaga <aguz@sugarlabs.org> | 2012-09-03 02:43:29 (GMT) |
---|---|---|
committer | Agustin Zubiaga <aguz@sugarlabs.org> | 2012-09-03 02:43:29 (GMT) |
commit | e6303c498402792714e2c4006c1e522f7a669159 (patch) | |
tree | 3d1b7f28dcb7e483e3d2cc791eb2a429c7439afe | |
parent | bc430de19d30138c0acb2c52339b1888b8dc0173 (diff) |
Not draw dots over crosses
-rwxr-xr-x | game.py | 122 |
1 files changed, 67 insertions, 55 deletions
@@ -61,10 +61,15 @@ class Canvas(Gtk.DrawingArea): def set_mode(self, mode=MODE_CROSS): self._mode = mode + def clear(self): + self.circles = [] + self.crosses = [] + self.queue_draw() + def _draw_cb(self, widget, context): alloc = self.get_allocation() - # White Background + # Background context.rectangle(0, 0, alloc.width, alloc.height) context.set_source_rgb(*BACKGROUND_COLOR) context.fill() @@ -116,23 +121,19 @@ class Canvas(Gtk.DrawingArea): self.squares.extend([(cx1, cy2), (cx2, cy2), (cx3, cy2)]) self.squares.extend([(cx1, cy3), (cx2, cy3), (cx3, cy3)]) - # Draw cursor dot - if not self.cursor: - self.cursor = self.squares[1] - cx, cy = self.cursor - context.arc(cx, cy, 9, 0, 360) - context.fill_preserve() - context.stroke() - # Draw circles and crosses context.set_source_rgb(*CIRCLE_COLOR) for circle in self.circles: + if circle == self.cursor: + self.cursor = None x, y = circle context.arc(x, y, SIZE / 2, 0, 360) context.stroke() context.set_source_rgb(*CROSS_COLOR) for cross in self.crosses: + if cross == self.cursor: + self.cursor = None x, y = cross a = SIZE / 2 context.move_to(x - a, y - a) @@ -142,15 +143,22 @@ class Canvas(Gtk.DrawingArea): context.line_to(x - a, y + a) context.stroke() + # Draw cursor dot + try: + context.set_source_rgb(*TICTACTOE_COLOR) + cx, cy = self.cursor + context.arc(cx, cy, 9, 0, 360) + context.fill_preserve() + context.stroke() + except: + pass + def _motion_notify_cb(self, widget, event): self.cursor = self.get_pos(event.x, event.y) self.queue_draw() def _click_cb(self, widget, event): - try: - pos = self.get_pos(event.x, event.y) - except IndexError: - pos = None + pos = self.get_pos(event.x, event.y) if self._mode == MODE_CIRCLE and pos: self.circles.append(pos) @@ -161,49 +169,52 @@ class Canvas(Gtk.DrawingArea): self.queue_draw() def get_pos(self, mx, my): - next_square = 0 - ypos = 0 - xpos = 0 - all_x = [s[0] for s in self.squares] - all_y = [s[1] for s in self.squares] - - size = SIZE / 2 + style.zoom(25) - - if mx > all_x[0] and not mx > all_x[-1]: - count = 0 - for x in all_x: - if not xpos: - count += 1 - next_x = all_x[count] - if mx >= (x - size) and mx <= (next_x - size): - xpos = x - - elif mx < all_x[0]: - xpos = all_x[0] - - elif mx > all_x[-1]: - xpos = all_x[-1] - - if my > all_y[0] and not my > all_y[-1]: - count = 0 - for y in all_y: - if not ypos: - count += 1 - next_y = all_y[count] - if my >= (y - size) and my <= (next_y - size): - ypos = y - - elif my < all_y[0]: - ypos = all_y[0] - - elif my > all_y[-1]: - ypos = all_y[-1] - - pos = xpos, ypos - if pos in self.crosses and pos in self.circles: - pos = None - return pos + try: + next_square = 0 + ypos = 0 + xpos = 0 + all_x = [s[0] for s in self.squares] + all_y = [s[1] for s in self.squares] + + size = SIZE / 2 + style.zoom(25) + + if mx > all_x[0] and not mx > all_x[-1]: + count = 0 + for x in all_x: + if not xpos: + count += 1 + next_x = all_x[count] + if mx >= (x - size) and mx <= (next_x - size): + xpos = x + + elif mx < all_x[0]: + xpos = all_x[0] + + elif mx > all_x[-1]: + xpos = all_x[-1] + + if my > all_y[0] and not my > all_y[-1]: + count = 0 + for y in all_y: + if not ypos: + count += 1 + next_y = all_y[count] + if my >= (y - size) and my <= (next_y - size): + ypos = y + + elif my < all_y[0]: + ypos = all_y[0] + + elif my > all_y[-1]: + ypos = all_y[-1] + + pos = xpos, ypos + if pos in self.crosses and pos in self.circles: + pos = None + return pos + except IndexError: + return None if __name__ == "__main__": window = Gtk.Window() @@ -212,3 +223,4 @@ if __name__ == "__main__": window.maximize() window.show_all() Gtk.main() + |