diff options
author | Walter Bender <walter.bender@gmail.com> | 2011-10-01 13:40:42 (GMT) |
---|---|---|
committer | Walter Bender <walter.bender@gmail.com> | 2011-10-01 13:40:42 (GMT) |
commit | 085a198575e189b611f2222b70fbd0464c31484a (patch) | |
tree | a1523fa1240f41402b8cb100ca804a011c5bc49c /bounce.py | |
parent | 1e94b44433b9ab624a8d27ecfcdf1dd748b557fd (diff) |
don't reset fraction on first bounce
Diffstat (limited to 'bounce.py')
-rw-r--r-- | bounce.py | 100 |
1 files changed, 49 insertions, 51 deletions
@@ -26,7 +26,7 @@ import gobject from gettext import gettext as _ import logging -_logger = logging.getLogger("fractionbounce-activity") +_logger = logging.getLogger('fractionbounce-activity') try: from sugar.graphics import style @@ -38,7 +38,7 @@ from sprites import Sprites, Sprite def _svg_str_to_pixbuf(svg_string): - """ Load pixbuf from SVG string """ + ''' Load pixbuf from SVG string ''' pl = gtk.gdk.PixbufLoader('svg') pl.write(svg_string) pl.close() @@ -47,53 +47,51 @@ def _svg_str_to_pixbuf(svg_string): def _svg_rect(w, h, rx, ry, x, y, fill, stroke): - """ Returns an SVG rectangle """ - svg_string = " <rect\n" - svg_string += " width=\"%f\"\n" % (w) - svg_string += " height=\"%f\"\n" % (h) - svg_string += " rx=\"%f\"\n" % (rx) - svg_string += " ry=\"%f\"\n" % (ry) - svg_string += " x=\"%f\"\n" % (x) - svg_string += " y=\"%f\"\n" % (y) - svg_string += _svg_style("fill:%s;stroke:%s;" % (fill, stroke)) + ''' Returns an SVG rectangle ''' + svg_string = ' <rect\n' + svg_string += ' width="%f"\n' % (w) + svg_string += ' height="%f"\n' % (h) + svg_string += ' rx="%f"\n' % (rx) + svg_string += ' ry="%f"\n' % (ry) + svg_string += ' x="%f"\n' % (x) + svg_string += ' y="%f"\n' % (y) + svg_string += _svg_style('fill:%s;stroke:%s;' % (fill, stroke)) return svg_string def _svg_header(w, h, scale, hscale=1.0): - """ Returns SVG header; some beads are elongated (hscale) """ - svg_string = "<?xml version=\"1.0\" encoding=\"UTF-8\"" - svg_string += " standalone=\"no\"?>\n" - svg_string += "<!-- Created with Python -->\n" - svg_string += "<svg\n" - svg_string += " xmlns:svg=\"http://www.w3.org/2000/svg\"\n" - svg_string += " xmlns=\"http://www.w3.org/2000/svg\"\n" - svg_string += " version=\"1.0\"\n" - svg_string += "%s%f%s" % (" width=\"", w * scale, "\"\n") - svg_string += "%s%f%s" % (" height=\"", h * scale * hscale, "\">\n") - svg_string += "%s%f%s%f%s" % ("<g\n transform=\"matrix(", - scale, ",0,0,", scale, - ",0,0)\">\n") + ''' Returns SVG header; some beads are elongated (hscale) ''' + svg_string = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n' + svg_string += '<!-- Created with Python -->\n' + svg_string += '<svg\n' + svg_string += ' xmlns:svg="http://www.w3.org/2000/svg"\n' + svg_string += ' xmlns="http://www.w3.org/2000/svg"\n' + svg_string += ' version="1.0"\n' + svg_string += ' width="%f"\n' % (w * scale) + svg_string += ' height="%f">\n' % (h * scale * hscale) + svg_string += '<g\n transform="matrix(%f,0,0,%f,0,0)">\n' % ( + scale, scale) return svg_string def _svg_footer(): - """ Returns SVG footer """ - svg_string = "</g>\n" - svg_string += "</svg>\n" + ''' Returns SVG footer ''' + svg_string = '</g>\n' + svg_string += '</svg>\n' return svg_string -def _svg_style(extras=""): - """ Returns SVG style for shape rendering """ - return "%s%s%s" % ("style=\"", extras, "\"/>\n") +def _svg_style(extras=''): + ''' Returns SVG style for shape rendering ''' + return 'style="%s"/>\n' % (extras) class Bounce(): - """ The Bounce class is used to define the ball and the user - interaction. """ + ''' The Bounce class is used to define the ball and the user + interaction. ''' def __init__(self, canvas, path, parent=None): - """ Initialize the canvas and set up the callbacks. """ + ''' Initialize the canvas and set up the callbacks. ''' self.activity = parent if parent is None: # Starting from command line @@ -108,11 +106,11 @@ class Bounce(): self.canvas.add_events(gtk.gdk.BUTTON_PRESS_MASK) self.canvas.add_events(gtk.gdk.BUTTON_RELEASE_MASK) self.canvas.add_events(gtk.gdk.POINTER_MOTION_MASK) - self.canvas.connect("expose-event", self._expose_cb) - self.canvas.connect("button-press-event", self._button_press_cb) - self.canvas.connect("button-release-event", self._button_release_cb) - self.canvas.connect("motion-notify-event", self._mouse_move_cb) - self.canvas.connect("key_press_event", self._keypress_cb) + self.canvas.connect('expose-event', self._expose_cb) + self.canvas.connect('button-press-event', self._button_press_cb) + self.canvas.connect('button-release-event', self._button_release_cb) + self.canvas.connect('motion-notify-event', self._mouse_move_cb) + self.canvas.connect('key_press_event', self._keypress_cb) self.width = gtk.gdk.screen_width() self.height = gtk.gdk.screen_height() - GRID_CELL_SIZE self.sprites = Sprites(self.canvas) @@ -120,11 +118,11 @@ class Bounce(): # Create the sprites we'll need self.smiley_graphic = _svg_str_to_pixbuf(svg_from_file( - os.path.join(path, "smiley.svg"))) + os.path.join(path, 'smiley.svg'))) self.ball = Sprite(self.sprites, 0, 0, _svg_str_to_pixbuf(svg_from_file( - os.path.join(path, "basketball.svg")))) + os.path.join(path, 'basketball.svg')))) self.ball.set_layer(1) self.ball.set_label(_('click')) @@ -170,7 +168,7 @@ class Bounce(): self.press = None # sprite under mouse click self._choose_a_fraction() self.reached_the_top = False - self.new_bounce = True + self.new_bounce = False def _gen_bar(self, n): ''' Return a bar with n segments ''' @@ -185,18 +183,18 @@ class Bounce(): return bar def _button_press_cb(self, win, event): - """ Callback to handle the button presses """ + ''' Callback to handle the button presses ''' win.grab_focus() x, y = map(int, event.get_coords()) self.press = self.sprites.find_sprite((x, y)) return True def _mouse_move_cb(self, win, event): - """ Callback to handle the mouse moves """ + ''' Callback to handle the mouse moves ''' return True def _button_release_cb(self, win, event): - """ Callback to handle the button releases """ + ''' Callback to handle the button releases ''' win.grab_focus() x, y = map(int, event.get_coords()) if self.press is not None: @@ -206,7 +204,7 @@ class Bounce(): return True def _move_ball(self): - """ Move the ball and test boundary conditions """ + ''' Move the ball and test boundary conditions ''' if self.new_bounce: self.mark.move((0, self.height)) # hide the mark self._choose_a_fraction() @@ -237,7 +235,7 @@ class Bounce(): gobject.timeout_add(50, self._move_ball) def _choose_a_fraction(self): - """ Select a new fraction challenge from the table """ + ''' Select a new fraction challenge from the table ''' n = int(uniform(0, len(FRACTIONS))) _logger.debug(n) self.activity.reset_label(FRACTIONS[n][0]) @@ -250,7 +248,7 @@ class Bounce(): self.bar10.set_layer(0) def _test(self): - """ Test to see if we estimated correctly """ + ''' Test to see if we estimated correctly ''' delta = self.ball.rect[2] / 4 x = self.ball.get_xy()[0] + self.ball.rect[2] / 2 f = self.ball.rect[2] / 2 + int(self.fraction * self.bar.rect[2]) @@ -266,7 +264,7 @@ class Bounce(): self.mark.move((int(f - self.mark.rect[2] / 2), self.bar.rect[1] - 2)) def _keypress_cb(self, area, event): - """ Keypress: moving the slides with the arrow keys """ + ''' Keypress: moving the slides with the arrow keys ''' k = gtk.gdk.keyval_name(event.keyval) if k in ['h', 'Left']: self.dx = -5 @@ -277,17 +275,17 @@ class Bounce(): return True def _expose_cb(self, win, event): - """ Callback to handle window expose events """ + ''' Callback to handle window expose events ''' self.sprites.redraw_sprites(event.area) return True def _destroy_cb(self, win, event): - """ Callback to handle quit """ + ''' Callback to handle quit ''' gtk.main_quit() def svg_from_file(pathname): - """ Read SVG string from a file """ + ''' Read SVG string from a file ''' f = file(pathname, 'r') svg = f.read() f.close() |