diff options
author | Agustin Zubiaga <aguz@sugarlabs.org> | 2012-09-02 22:49:45 (GMT) |
---|---|---|
committer | Agustin Zubiaga <aguz@sugarlabs.org> | 2012-09-02 22:49:45 (GMT) |
commit | 5dc65f60360173bb729e58cb95a469fa9a771844 (patch) | |
tree | b81cdb0b5317b69ac69c8d8b56cdc824a4f57f65 | |
parent | 8eb166545a73894d491912b8ac5a693a4b2e9226 (diff) |
Put the crosses and circles in the correct square
-rwxr-xr-x | game.py | 62 |
1 files changed, 52 insertions, 10 deletions
@@ -39,8 +39,8 @@ class Canvas(Gtk.DrawingArea): Gtk.DrawingArea.__init__(self) self._mode = MODE_CROSS - self._circles = [] - self._crosses = [] + self.circles = [] + self.crosses = [] self.squares = [] self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK) @@ -50,7 +50,7 @@ class Canvas(Gtk.DrawingArea): self.show_all() - def set_mode(self, mode=MODE_CIRCLE): + def set_mode(self, mode=MODE_CROSS): self._mode = mode def _draw_cb(self, widget, context): @@ -116,12 +116,12 @@ class Canvas(Gtk.DrawingArea): self.squares.extend([(cx1, cy3), (cx2, cy3), (cx3, cy3)]) context.set_source_rgb(*fill) - for circle in self._circles: + for circle in self.circles: x, y = circle context.arc(x, y, SIZE / 2, 0, 360) context.stroke() - for cross in self._crosses: + for cross in self.crosses: x, y = cross a = SIZE / 2 context.move_to(x - a, y - a) @@ -135,16 +135,58 @@ class Canvas(Gtk.DrawingArea): pos = self.get_pos(event.x, event.y) if self._mode == MODE_CIRCLE and pos: - self._circles.append(pos) + self.circles.append(pos) elif self._mode == MODE_CROSS and pos: - self._crosses.append(pos) + self.crosses.append(pos) self.queue_draw() - def get_pos(self, x, y): - # TODO: Return the specific square coords - return self.squares[4] + 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] + + 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 and mx < next_x or \ + mx > x - BETWEEN_LINE_SPACE and \ + mx < next_x - BETWEEN_LINE_SPACE: + 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 and my < next_y or \ + my > y - BETWEEN_LINE_SPACE and \ + my < next_y - BETWEEN_LINE_SPACE: + 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 if __name__ == "__main__": |