diff options
author | Philip Withnall <philip@tecnocode.co.uk> | 2013-08-18 22:34:22 (GMT) |
---|---|---|
committer | Philip Withnall <philip@tecnocode.co.uk> | 2013-08-18 22:34:22 (GMT) |
commit | 5c2b53bf4e268dc145ff74255ff09c39fda8e828 (patch) | |
tree | dcbdfe004e0a41c4ec356e1aa4c236bc897b0c3b /PascalTriangle.activity/pascaltriangle.py | |
parent | 1f1c8f55b7cbee6ccb4c2588f62239d72718536f (diff) |
Implement a ‘win’ dialogue box when the game is won
Diffstat (limited to 'PascalTriangle.activity/pascaltriangle.py')
-rwxr-xr-x | PascalTriangle.activity/pascaltriangle.py | 57 |
1 files changed, 43 insertions, 14 deletions
diff --git a/PascalTriangle.activity/pascaltriangle.py b/PascalTriangle.activity/pascaltriangle.py index fa4782e..ea7b0e4 100755 --- a/PascalTriangle.activity/pascaltriangle.py +++ b/PascalTriangle.activity/pascaltriangle.py @@ -1,3 +1,5 @@ +# coding=utf-8 + from sugar3.activity import activity from sugar3.graphics.toolbarbox import ToolbarBox import math, random @@ -27,6 +29,16 @@ class PascalTriangleActivity(activity.Activity): # Parent and show the drawing area. self.set_canvas(drawing_area) drawing_area.show() + + # Start a new game. + self._start_game(drawing_area) + + + """ + Start a new game, clearing the previous game. + """ + def _start_game(self, drawing_area): + # Focus the drawing area so it can receive keyboard events. drawing_area.grab_focus() # Set the initial size of the Pascal triangle to be drawn. This is @@ -45,6 +57,8 @@ class PascalTriangleActivity(activity.Activity): # Generate a list of blank cells which the user needs to fill in. self._blank_cells = self._generate_blank_cell_list() + drawing_area.queue_draw() + """ Calculate the number of cells in the triangle. This is the Nth triangle @@ -256,7 +270,8 @@ class PascalTriangleActivity(activity.Activity): if cell_text != None: extents = ctx.text_extents(cell_text) - ctx.move_to(centre[0] - extents[2] / 2.0, centre[1] + extents[3] / 2.0) + ctx.move_to(centre[0] - extents[2] / 2.0, + centre[1] + extents[3] / 2.0) ctx.set_font_size(50) ctx.show_text(cell_text) @@ -267,16 +282,30 @@ class PascalTriangleActivity(activity.Activity): cells and hence has won. """ def _check_current_cell_text(self, widget): - # Check whether the answer is correct. If so, change the cell to be - # uneditable. - expected_num = self._calculate_pascal_number(self._current_cell[0], - self._current_cell[1]) - if int(self._current_cell_text) == expected_num: - self._blank_cells.remove(self._current_cell) - self._current_cell = (-1, -1) - self._current_cell_text = '' - widget.queue_draw() - - # Check whether all blank cells have been filled. - if len(self._blank_cells) == 0: - print('Well done!') + # Check whether the answer is correct. If so, change the cell to be + # uneditable. + expected_num = self._calculate_pascal_number(self._current_cell[0], + self._current_cell[1]) + if int(self._current_cell_text) == expected_num: + self._blank_cells.remove(self._current_cell) + self._current_cell = (-1, -1) + self._current_cell_text = '' + widget.queue_draw() + + # Check whether all blank cells have been filled. + if len(self._blank_cells) == 0: + dialog = Gtk.MessageDialog(self, + Gtk.DialogFlags.DESTROY_WITH_PARENT, + Gtk.MessageType.INFO, Gtk.ButtonsType.NONE, + 'Well done! You’ve completed the Pascal Triangle. ' + + 'Do you want to play again?') + dialog.add_buttons('_New Game', Gtk.ResponseType.ACCEPT, + '_Quit', Gtk.ResponseType.CLOSE) + + if dialog.run() == Gtk.ResponseType.ACCEPT: + # Start a new game. + self._start_game(widget) + dialog.destroy() + else: + # Quit. + Gtk.main_quit() |