From 544a3b37dc38c6643dee1f31b6c34420e5fe020e Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Thu, 15 Dec 2011 16:50:56 +0000 Subject: added robot play --- diff --git a/ReflectionActivity.py b/ReflectionActivity.py index 43194c7..4a25ea7 100644 --- a/ReflectionActivity.py +++ b/ReflectionActivity.py @@ -128,6 +128,13 @@ class ReflectionActivity(activity.Activity): self.status = label_factory(self.toolbar, '') if _have_toolbox: + separator_factory(toolbox.toolbar, False, True) + + self.robot_button = button_factory( + 'robot-off', self.toolbar, self._robot_cb, + tooltip= _('Play with the robot.')) + + if _have_toolbox: separator_factory(toolbox.toolbar, True, False) if _have_toolbox: @@ -140,6 +147,18 @@ class ReflectionActivity(activity.Activity): ''' Start a new game. ''' self._game.new_game(orientation) + def _robot_cb(self, button=None): + ''' Play with the computer (or not). ''' + if not self._game.playing_with_robot: + self.set_robot_status(True, 'robot-on') + else: + self.set_robot_status(False, 'robot-off') + + def set_robot_status(self, status, icon): + ''' Reset robot icon and status ''' + self._game.playing_with_robot = status + self.robot_button.set_icon(icon) + def write_file(self, file_path): """ Write the grid status to the Journal """ [dot_list, orientation] = self._game.save_game() diff --git a/game.py b/game.py index c517f06..15a7f3c 100644 --- a/game.py +++ b/game.py @@ -61,6 +61,7 @@ class Game(): self._space = int(self._dot_size / 5.) self._orientation = 'horizontal' self.we_are_sharing = False + self.playing_with_robot = False # Generate the sprites we'll need... self._sprites = Sprites(self._canvas) @@ -179,6 +180,10 @@ class Game(): spr.type += 1 spr.type %= 4 spr.set_shape(self._new_dot(self._colors[spr.type])) + + if self.playing_with_robot: + self._robot_play(spr) + self._test_game_over() if self.we_are_sharing: @@ -187,6 +192,50 @@ class Game(): spr.type) return True + def _robot_play(self, dot): + ''' Robot reflects dot clicked. ''' + x, y = self._dot_to_grid(self._dots.index(dot)) + if self._orientation == 'horizontal': + _logger.debug('%d: %d, %d' % (self._dots.index(dot), x, y)) + x = TEN - x - 1 + i = self._grid_to_dot((x, y)) + _logger.debug('%d: %d, %d' % (i, x, y)) + self._dots[i].type = dot.type + self._dots[i].set_shape(self._new_dot(self._colors[dot.type])) + if self.we_are_sharing: + _logger.debug('sending a robot click to the share') + self._parent.send_dot_click(i, dot.type) + elif self._orientation == 'vertical': + y = SIX - y - 1 + i = self._grid_to_dot((x, y)) + self._dots[i].type = dot.type + self._dots[i].set_shape(self._new_dot(self._colors[dot.type])) + if self.we_are_sharing: + _logger.debug('sending a robot click to the share') + self._parent.send_dot_click(i, dot.type) + else: + x = TEN - x - 1 + i = self._grid_to_dot((x, y)) + self._dots[i].type = dot.type + self._dots[i].set_shape(self._new_dot(self._colors[dot.type])) + if self.we_are_sharing: + _logger.debug('sending a robot click to the share') + self._parent.send_dot_click(i, dot.type) + y = SIX - y - 1 + i = self._grid_to_dot((x, y)) + self._dots[i].type = dot.type + self._dots[i].set_shape(self._new_dot(self._colors[dot.type])) + if self.we_are_sharing: + _logger.debug('sending a robot click to the share') + self._parent.send_dot_click(i, dot.type) + x = TEN - x - 1 + i = self._grid_to_dot((x, y)) + self._dots[i].type = dot.type + self._dots[i].set_shape(self._new_dot(self._colors[dot.type])) + if self.we_are_sharing: + _logger.debug('sending a robot click to the share') + self._parent.send_dot_click(i, dot.type) + def remote_button_press(self, dot, color): ''' Receive a button press from a sharer ''' self._dots[dot].type = color @@ -243,7 +292,7 @@ class Game(): def _dot_to_grid(self, dot): ''' calculate the grid column and row for a dot ''' - return [dot % TEN, int(dot / SIX)] + return [dot % TEN, int(dot / TEN)] def game_over(self, msg=_('Game over')): ''' Nothing left to do except show the results. ''' diff --git a/icons/robot-off.svg b/icons/robot-off.svg new file mode 100644 index 0000000..549e5c7 --- /dev/null +++ b/icons/robot-off.svg @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + diff --git a/icons/robot-on.svg b/icons/robot-on.svg new file mode 100644 index 0000000..85b9b19 --- /dev/null +++ b/icons/robot-on.svg @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + -- cgit v0.9.1