From a90dc68554c5329ec82217b8fefbb048ea3379e3 Mon Sep 17 00:00:00 2001 From: sparshg <43041139+sparshg@users.noreply.github.com> Date: Mon, 4 Apr 2022 16:19:15 +0530 Subject: [PATCH 1/3] Decrease the gridsize with increasing difficulty. --- game.py | 74 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/game.py b/game.py index af4b6da..e9bfd14 100644 --- a/game.py +++ b/game.py @@ -39,7 +39,6 @@ FILL = 1 STROKE = 0 -THIRTEEN = 13 DOT_SIZE = 20 DOT_SIZE_GAMEOVER = 70 CIRCLE = [[(0, -1), (1, 0), (0, 1), (-1, 1), (-1, 0), (-1, -1)], @@ -117,6 +116,8 @@ def __init__(self, canvas, parent=None, colors=['#A0FFA0', '#FF8080']): self.strategies = [BEGINNER_STRATEGY, INTERMEDIATE_STRATEGY, EXPERT_STRATEGY, self.custom_strategy] self.strategy = self.strategies[self.level] + self.sizes = [13, 12, 11, 10] + self.size = self.sizes[self.level] self._timeout_id = None self.best_time = self.load_best_time() # Generate the sprites we'll need... @@ -126,13 +127,24 @@ def __init__(self, canvas, parent=None, colors=['#A0FFA0', '#FF8080']): self._your_time = [] self._best_time = [] self._win_lose = [] - for y in range(THIRTEEN): - for x in range(THIRTEEN): - offset_x = int((self._width - THIRTEEN * (self._dot_size + \ + + # Put a turtle at the center of the screen... + self._turtle_images = [] + self._rotate_turtle(self._new_turtle()) + self._turtle = Sprite(self._sprites, 0, 0, + self._turtle_images[0]) + # ...and initialize. + self._all_clear() + + def _init_grid(self): + self._dots = [] + for y in range(self.size): + for x in range(self.size): + offset_x = int((self._width - self.size * (self._dot_size + \ self._space) - self._space) / 2.) if y % 2 == 1: offset_x += int((self._dot_size + self._space) / 2.) - if x == 0 or y == 0 or x == THIRTEEN - 1 or y == THIRTEEN - 1: + if x == 0 or y == 0 or x == self.size - 1 or y == self.size - 1: self._dots.append( Sprite(self._sprites, offset_x + x * (self._dot_size + self._space), @@ -146,16 +158,7 @@ def __init__(self, canvas, parent=None, colors=['#A0FFA0', '#FF8080']): self._new_dot(self._colors[FILL], self._dot_size))) self._dots[-1].type = False # not set - - # Put a turtle at the center of the screen... - self._turtle_images = [] - self._rotate_turtle(self._new_turtle()) - self._turtle = Sprite(self._sprites, 0, 0, - self._turtle_images[0]) - self._move_turtle(self._dots[int(THIRTEEN * THIRTEEN / 2)].get_xy()) - - # ...and initialize. - self._all_clear() + self._dots[-1].set_layer(100) # not set def _move_turtle(self, pos): ''' Move turtle and add its offset ''' @@ -174,16 +177,12 @@ def _all_clear(self): your_time_shape.hide() for highscore_shape in self._best_time: highscore_shape.hide() - for dot in self._dots: - if dot.type: - dot.type = False - dot.set_shape(self._new_dot(self._colors[FILL], - self._dot_size)) - dot.set_label('') - dot.set_layer(100) + for sprite in self._dots: + sprite.hide() + self._init_grid() self._turtle.set_layer(100) # Recenter the turtle - self._move_turtle(self._dots[int(THIRTEEN * THIRTEEN / 2)].get_xy()) + self._move_turtle(self._dots[int(self.size * (self.size + (0 if self.size % 2 else 1)) / 2)].get_xy()) self._turtle.set_shape(self._turtle_images[0]) self._set_label('') if self._timeout_id is not None: @@ -194,10 +193,12 @@ def new_game(self, saved_state=None): ''' Start a new game. ''' self.gameover_flag = False self.game_lost = False + self.strategy = self.strategies[self.level] + self.size = self.sizes[self.level] self._all_clear() # Fill in a few dots to start for i in range(15): - n = int(uniform(0, THIRTEEN * THIRTEEN)) + n = int(uniform(0, self.size * self.size)) if self._dots[n].type is not None: self._dots[n].type = True self._dots[n].set_shape(self._new_dot(self._colors[STROKE], @@ -205,7 +206,6 @@ def new_game(self, saved_state=None): # Calculate the distances to the edge self._initialize_weights() self.game_start_time = time.time() - self.strategy = self.strategies[self.level] self._timeout_id = None def _set_label(self, string): @@ -270,19 +270,19 @@ def _test_game_over(self, new_dot): self._happy_turtle_dance() self._timeout_id = GLib.timeout_add(10000, self._game_over) return True - c = int(self._turtle_dot / THIRTEEN) % 2 + c = int(self._turtle_dot / self.size) % 2 if self._dots[ - new_dot + CIRCLE[c][0][0] + THIRTEEN * CIRCLE[c][0][1]].type and \ + new_dot + CIRCLE[c][0][0] + self.size * CIRCLE[c][0][1]].type and \ self._dots[ - new_dot + CIRCLE[c][1][0] + THIRTEEN * CIRCLE[c][1][1]].type and \ + new_dot + CIRCLE[c][1][0] + self.size * CIRCLE[c][1][1]].type and \ self._dots[ - new_dot + CIRCLE[c][2][0] + THIRTEEN * CIRCLE[c][2][1]].type and \ + new_dot + CIRCLE[c][2][0] + self.size * CIRCLE[c][2][1]].type and \ self._dots[ - new_dot + CIRCLE[c][3][0] + THIRTEEN * CIRCLE[c][3][1]].type and \ + new_dot + CIRCLE[c][3][0] + self.size * CIRCLE[c][3][1]].type and \ self._dots[ - new_dot + CIRCLE[c][4][0] + THIRTEEN * CIRCLE[c][4][1]].type and \ + new_dot + CIRCLE[c][4][0] + self.size * CIRCLE[c][4][1]].type and \ self._dots[ - new_dot + CIRCLE[c][5][0] + THIRTEEN * CIRCLE[c][5][1]].type: + new_dot + CIRCLE[c][5][0] + self.size * CIRCLE[c][5][1]].type: # Game-over feedback for dot in self._dots: dot.set_label(':)') @@ -410,11 +410,11 @@ def rings(self, num, text, shape): def _grid_to_dot(self, pos): ''' calculate the dot index from a column and row in the grid ''' - return pos[0] + pos[1] * THIRTEEN + return pos[0] + pos[1] * self.size def _dot_to_grid(self, dot): ''' calculate the grid column and row for a dot ''' - return [dot % THIRTEEN, int(dot / THIRTEEN)] + return [dot % self.size, int(dot / self.size)] def _happy_turtle_dance(self): ''' Turtle dances along the edge ''' @@ -431,9 +431,9 @@ def _happy_turtle_dance(self): x += 1 if x == 0: y -= 1 - if x == THIRTEEN - 1: + if x == self.size - 1: y += 1 - if y == THIRTEEN - 1: + if y == self.size - 1: x -= 1 i = self._grid_to_dot((x, y)) self._dots[i].set_label(':)') @@ -489,7 +489,7 @@ def _initialize_weights(self): self._weights.append(1000) else: pos = self._dot_to_grid(d) - pos2 = (THIRTEEN - pos[0], THIRTEEN - pos[1]) + pos2 = (self.size - pos[0], self.size - pos[1]) self._weights.append(min(min(pos[0], pos2[0]), min(pos[1], pos2[1]))) def _my_strategy_import(self, f, arg): From 67fdf023dff7eb107f268d24c2b0d58aa07054c6 Mon Sep 17 00:00:00 2001 From: sparshg <43041139+sparshg@users.noreply.github.com> Date: Tue, 19 Apr 2022 01:03:17 +0530 Subject: [PATCH 2/3] Update game.py Co-authored-by: Srevin Saju --- game.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game.py b/game.py index e9bfd14..7c2bc8f 100644 --- a/game.py +++ b/game.py @@ -182,7 +182,7 @@ def _all_clear(self): self._init_grid() self._turtle.set_layer(100) # Recenter the turtle - self._move_turtle(self._dots[int(self.size * (self.size + (0 if self.size % 2 else 1)) / 2)].get_xy()) + self._move_turtle(self._dots[int(self.size * (self.size + 1 - self.size % 2) / 2)].get_xy()) self._turtle.set_shape(self._turtle_images[0]) self._set_label('') if self._timeout_id is not None: From b2a75476a953a646d553a00ceee144945590b72c Mon Sep 17 00:00:00 2001 From: sparshg <43041139+sparshg@users.noreply.github.com> Date: Tue, 19 Apr 2022 02:29:18 +0530 Subject: [PATCH 3/3] Add toggle for classic mode --- TurtlePondActivity.py | 23 +++++++++++++++++++++++ game.py | 4 +++- icons/classic.svg | 10 ++++++++++ icons/vary.svg | 10 ++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 icons/classic.svg create mode 100644 icons/vary.svg diff --git a/TurtlePondActivity.py b/TurtlePondActivity.py index 33601ee..60a6572 100644 --- a/TurtlePondActivity.py +++ b/TurtlePondActivity.py @@ -48,6 +48,7 @@ INTERMEDIATE = 1 EXPERT = 2 CUSTOM = 3 + LEVEL_LABELS = [_('Beginner'), _('Intermediate'), _('Expert'), _('My strategy')] @@ -131,6 +132,24 @@ def _setup_toolbars(self): tooltip=LEVEL_LABELS[CUSTOM], group=self.beginner_button) + separator_factory(self.toolbar, False, True) + + self.classic_mode_button = radio_factory( + 'classic', + self.toolbar, + self._gridsize_cb, + cb_arg=True, + tooltip="Classic Mode", + group=None) + self.gridchange_mode_button = radio_factory( + 'vary', + self.toolbar, + self._gridsize_cb, + cb_arg=False, + tooltip="Scalable Mode: Decreases gridsize with difficulty", + group=self.classic_mode_button) + + self.status = label_factory(self.toolbar, '') separator_factory(toolbox.toolbar, True, False) @@ -145,6 +164,10 @@ def _setup_toolbars(self): toolbox.toolbar.insert(stop_button, -1) stop_button.show() + def _gridsize_cb(self, button, classic_mode): + self._game.classic_mode = classic_mode + self._game.new_game() + def _level_cb(self, button, level): if level == CUSTOM and self._game.strategies[CUSTOM] is None: level = EXPERT diff --git a/game.py b/game.py index e9bfd14..dc76cb3 100644 --- a/game.py +++ b/game.py @@ -40,6 +40,7 @@ FILL = 1 STROKE = 0 DOT_SIZE = 20 +DEFAULT_GRID_SIZE = 13 DOT_SIZE_GAMEOVER = 70 CIRCLE = [[(0, -1), (1, 0), (0, 1), (-1, 1), (-1, 0), (-1, -1)], [(1, -1), (1, 0), (1, 1), (0, 1), (-1, 0), (0, -1)]] @@ -112,6 +113,7 @@ def __init__(self, canvas, parent=None, colors=['#A0FFA0', '#FF8080']): self._space_gameover = int(self._dot_size_gameover / 5.) self._orientation = 0 self.level = 0 + self.classic_mode = True self.custom_strategy = None self.strategies = [BEGINNER_STRATEGY, INTERMEDIATE_STRATEGY, EXPERT_STRATEGY, self.custom_strategy] @@ -194,7 +196,7 @@ def new_game(self, saved_state=None): self.gameover_flag = False self.game_lost = False self.strategy = self.strategies[self.level] - self.size = self.sizes[self.level] + self.size = DEFAULT_GRID_SIZE if self.classic_mode else self.sizes[self.level] self._all_clear() # Fill in a few dots to start for i in range(15): diff --git a/icons/classic.svg b/icons/classic.svg new file mode 100644 index 0000000..664117f --- /dev/null +++ b/icons/classic.svg @@ -0,0 +1,10 @@ + + + +Created with Fabric.js 3.6.3 + + + + + + \ No newline at end of file diff --git a/icons/vary.svg b/icons/vary.svg new file mode 100644 index 0000000..286c178 --- /dev/null +++ b/icons/vary.svg @@ -0,0 +1,10 @@ + + + +Created with Fabric.js 3.6.3 + + + + + + \ No newline at end of file