diff options
author | Aneesh Dogra <lionaneesh@gmail.com> | 2012-12-29 20:06:24 (GMT) |
---|---|---|
committer | Aneesh Dogra <lionaneesh@gmail.com> | 2012-12-29 20:11:34 (GMT) |
commit | ce8fd48864a02ebbc007d064db92bcc0db30b94b (patch) | |
tree | 872e4c48f415bb6749be58bd5303bceec89af552 /LevelActivity.py | |
parent | d402b99e24c45346e1789e313a13926bcd78654d (diff) |
Make sure the ball is always in the circle.
Diffstat (limited to 'LevelActivity.py')
-rw-r--r-- | LevelActivity.py | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/LevelActivity.py b/LevelActivity.py index e90ed3b..224f178 100644 --- a/LevelActivity.py +++ b/LevelActivity.py @@ -20,7 +20,8 @@ from gi.repository import GObject from sugar3.activity import widgets from sugar3.activity.widgets import StopButton from sugar3.activity import activity -from math import pi, sin, cos +from math import pi, sqrt +from gettext import gettext as _ ACCELEROMETER_DEVICE = '/sys/devices/platform/lis3lv02d/position' #ACCELEROMETER_DEVICE = 'a.txt' @@ -36,6 +37,12 @@ def read_accelerometer(canvas): fh.close() GObject.timeout_add(100, read_accelerometer, canvas) +def clip(a, minimum, maximum): + if a < minimum: + a = minimum + if a > maximum: + a = maximum + return a class MyCanvas(Gtk.DrawingArea): ''' Create a GTK+ widget on which we will draw using Cairo ''' @@ -102,6 +109,7 @@ class MyCanvas(Gtk.DrawingArea): self.update_ball_and_text() def update_ball_and_text(self): + center = (self.width / 2, self.height / 2) self.cr.set_source_rgb(0, 0.453, 0) self.cr.arc(self.x, self.y, 20, 0, 2 * pi) self.cr.fill() @@ -118,18 +126,29 @@ class MyCanvas(Gtk.DrawingArea): self.cr.set_source_rgb(0, 0, 0) self.cr.move_to(self.width - 100, self.height - 80) self.cr.set_font_size(20) - self.cr.show_text("X: %.2f" % (self.x - self.width / 2,)) + # TRANS: X is for X axis + self.cr.show_text(_("X: %.2f") % (self.x - self.width / 2,)) self.cr.move_to(self.width - 99, self.height - 60) self.cr.set_font_size(20) - self.cr.show_text("Y: %.2f" % (self.y - self.height / 2,)) + # TRANS: Y is for Y axis + self.cr.show_text(_("Y: %.2f") % (self.y - self.height / 2,)) def motion_cb(self, x, y): - angle_x = pi / 2 * x - angle_y = pi / 2 * y - self.x = self.width / 2 + self.radius * sin(angle_x) - self.y = self.height / 2 + self.radius * sin(angle_y) + self.x = self.radius * x + self.y = self.radius * y + + if self.x and self.y: + r = sqrt((self.x * self.x) + (self.y * self.y)) + r1 = clip(r, 0, self.radius) + scale = r1 / r + self.x *= scale + self.y *= scale + + self.x += self.width / 2 + self.y += self.height / 2 + self.queue_draw() def get_dpi(self): |