Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/bounce.py
blob: 3137f1c551b5bc959ca913a6facbce21f22e7f2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# -*- coding: utf-8 -*-
#Copyright (c) 2011, Walter Bender, Paulina Clares, Chris Rowe
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

EASY = [('1/2', 0.5, 12), ('1/3', 1 / 3., 12), ('3/4', 0.75, 12),
        ('1/4', 0.25, 12), ('2/3', 2 / 3., 12), ('1/6', 1 / 6., 12),
        ('5/6', 5 / 6., 12)]
MEDIUM = [('2/8', 0.25, 12), ('2/4', 0.5, 12),  ('3/6', 0.5, 12),
          ('6/12', 0.5, 12), ('4/6', 2 / 3., 12), ('2/6', 1 / 3., 12),
          ('5/12', 5 / 12., 12), ('3/12', 0.25, 12), ('7/12', 7 / 12., 12),
          ('8/12', 2 / 3., 12), ('4/8', 0.5, 12), ('6/12', 0.5, 12),
          ('9/12', 0.75, 12), ('2/12', 1 / 6., 12), ('4/12', 1 / 3., 12),
          ('10/12', 5 / 6., 12), ('11/12', 11 / 12., 12)]
HARD = [('2/5', 0.4, 10), ('4/5', 0.8, 10), ('3/5', 0.6, 10),
        ('1/10', 0.1, 10), ('1/5', 0.2, 10), ('5/10', 0.5, 10),
        ('3/10', 0.3, 10), ('7/10', 0.7, 10), ('8/10', 0.8, 10),
        ('2/7', 2 / 7., 14), ('4/7', 4 / 7., 14), ('3/7', 3 / 7., 14),
        ('1/14', 1 / 14., 14), ('1/7', 1 / 7., 14), ('5/14', 5 / 14., 14),
        ('3/14', 3 / 14., 14), ('7/14', 0.5, 14), ('8/14', 4 / 7., 14)]
EXPERT = 100  # after many correct answers, don't segment the bar
BAR_HEIGHT = 20
STEPS = 100.  # number of time steps per bounce rise and fall
STEP_PAUSE = 50  # milliseconds between steps
BOUNCE_PAUSE = 3000  # milliseconds between bounces

ACCELEROMETER_DEVICE = '/sys/devices/platform/lis3lv02d/position'

import gtk
from random import uniform
import os
import gobject

from gettext import gettext as _

import logging
_logger = logging.getLogger('fractionbounce-activity')

try:
    from sugar.graphics import style
    GRID_CELL_SIZE = style.GRID_CELL_SIZE
except ImportError:
    GRID_CELL_SIZE = 0

from sprites import Sprites, Sprite


def _svg_str_to_pixbuf(svg_string):
    ''' Load pixbuf from SVG string '''
    pl = gtk.gdk.PixbufLoader('svg')
    pl.write(svg_string)
    pl.close()
    pixbuf = pl.get_pixbuf()
    return pixbuf


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))
    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" 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'
    return svg_string


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. '''

    def __init__(self, canvas, path, parent=None):
        ''' Initialize the canvas and set up the callbacks. '''
        self.activity = parent

        if parent is None:        # Starting from command line
            self.sugar = False
            self.canvas = canvas
        else:                     # Starting from Sugar
            self.sugar = True
            self.canvas = canvas
            parent.show_all()

        if os.path.exists(ACCELEROMETER_DEVICE):
            self.accererometer = True
        else:
            self.accererometer = False

        self.canvas.set_flags(gtk.CAN_FOCUS)
        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.add_events(gtk.gdk.KEY_PRESS_MASK)
        self.canvas.add_events(gtk.gdk.KEY_RELEASE_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('key_press_event', self._keypress_cb)
        self.canvas.connect('key_release_event', self._keyrelease_cb)
        self.width = gtk.gdk.screen_width()
        self.height = gtk.gdk.screen_height() - GRID_CELL_SIZE
        self.sprites = Sprites(self.canvas)
        self.scale = gtk.gdk.screen_height() / 900.0

        # Create the sprites we'll need
        self.smiley_graphic = _svg_str_to_pixbuf(svg_from_file(
                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'))))
        self.ball.set_layer(1)
        self.ball.set_label_attributes(24)

        mark = _svg_header(self.ball.rect[2] / 2, BAR_HEIGHT * self.scale + 4,
                           1.0) + \
               _svg_rect(self.ball.rect[2] / 2,
                         BAR_HEIGHT * self.scale + 4, 0, 0, 0, 0,
                         '#FF0000', '#FF0000') + \
               _svg_footer()
        self.mark = Sprite(self.sprites, 0,
                           self.height,  # hide off bottom of screen
                           _svg_str_to_pixbuf(mark))
        self.mark.set_layer(2)

        # divide into two segments
        self.bar = Sprite(self.sprites, 0, 0,
                          _svg_str_to_pixbuf(self._gen_bar(2)))
        # divide into twelve segments
        self.bar12 = Sprite(self.sprites, 0, 0,
                            _svg_str_to_pixbuf(self._gen_bar(12)))
        # divide into ten segments
        self.bar10 = Sprite(self.sprites, 0, 0,
                            _svg_str_to_pixbuf(self._gen_bar(10)))
        # divide into fourteen segments
        self.bar14 = Sprite(self.sprites, 0, 0,
                            _svg_str_to_pixbuf(self._gen_bar(14)))

        hoffset = int((self.ball.rect[3] + self.bar.rect[3]) / 2)
        self.bar.move((int(self.ball.rect[2] / 2), self.height - hoffset))
        self.bar12.move((int(self.ball.rect[2] / 2), self.height - hoffset))
        self.bar10.move((int(self.ball.rect[2] / 2), self.height - hoffset))
        self.bar14.move((int(self.ball.rect[2] / 2), self.height - hoffset))
        num = _svg_header(BAR_HEIGHT * self.scale, BAR_HEIGHT * self.scale,
                           1.0) + \
              _svg_rect(BAR_HEIGHT * self.scale,
                        BAR_HEIGHT * self.scale, 0, 0, 0, 0,
                        'none', 'none') + \
              _svg_footer()
        self.left = Sprite(self.sprites, int(self.ball.rect[2] / 4),
                           self.height - hoffset, _svg_str_to_pixbuf(num))
        self.left.set_label('0')
        self.right = Sprite(self.sprites,
                            self.width - int(self.ball.rect[2] / 2),
                            self.height - hoffset, _svg_str_to_pixbuf(num))
        self.right.set_label('1')

        self.ball_y_max = self.bar.rect[1] - self.ball.rect[3]
        self.ball.move((int((self.width - self.ball.rect[2]) / 2),
                        self.ball_y_max))

        self.challenges = []
        for challenge in EASY:
            self.challenges.append(challenge)
        self.dx = 0  # ball horizontal trajectory
        self.fraction = 0.5  # the target of the current challenge
        self.count = 0  # number of bounces played
        self.correct = 0  # number of correct answers
        self.press = None  # sprite under mouse click
        self.new_bounce = False

        delta = self.height / STEPS
        self.ddy = 6.67 * delta / STEPS  # acceleration (with dampening)
        self.dy = self.ddy * (1 - STEPS) / 2  # initial step size
        _logger.debug('delta: %f, ddy: %f, dy: %f', delta, self.ddy, self.dy)

        self.activity.challenge.set_label(_("Click the ball to start"))

    def _gen_bar(self, nsegments):
        ''' Return a bar with n segments '''
        svg = _svg_header(self.width - self.ball.rect[2], BAR_HEIGHT, 1.0)
        dx = (self.width - self.ball.rect[2]) / nsegments
        for i in range(nsegments / 2):
            svg += _svg_rect(dx, BAR_HEIGHT * self.scale, 0, 0,
                             i * 2 * dx, 0, '#FFFFFF', '#FFFFFF')
            svg += _svg_rect(dx, BAR_HEIGHT * self.scale, 0, 0,
                             (i * 2 + 1) * dx, 0, '#AAAAAA', '#AAAAAA')
        svg += _svg_footer()
        return svg

    def _button_press_cb(self, win, event):
        ''' 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 _button_release_cb(self, win, event):
        ''' Callback to handle the button releases '''
        win.grab_focus()
        x, y = map(int, event.get_coords())
        if self.press is not None:
            if self.press == self.ball:
                self._choose_a_fraction()
                self._move_ball()
        return True

    def _move_ball(self):
        ''' Move the ball and test boundary conditions '''
        if self.new_bounce:
            self.mark.move((0, self.height))  # hide the mark
            self._choose_a_fraction()
            self.new_bounce = False
            self.dy = self.ddy * (1 - STEPS) / 2  # initial step size

        if self.accererometer:
            fh = open(ACCELEROMETER_DEVICE)
            string = fh.read()
            xyz = string[1:-2].split(',')
            self.dx = int(float(xyz[0]) / 18.)
            fh.close()

        if self.ball.get_xy()[0] + self.dx > 0 and \
           self.ball.get_xy()[0] + self.dx < self.width - self.ball.rect[2]:
            self.ball.move_relative((self.dx, self.dy))
        else:
            self.ball.move_relative((0, self.dy))

        self.dy += self.ddy

        if self.ball.get_xy()[1] >= self.ball_y_max:
            # hit the bottom
            self.ball.move((self.ball.get_xy()[0], self.ball_y_max))
            self._test()
            self.new_bounce = True
            gobject.timeout_add(  # wait less and less as game goes on
                max(STEP_PAUSE, BOUNCE_PAUSE - self.count * STEP_PAUSE),
                self._move_ball)
        else:
            gobject.timeout_add(STEP_PAUSE, self._move_ball)

    def _choose_a_fraction(self):
        ''' Select a new fraction challenge from the table '''
        n = int(uniform(0, len(self.challenges)))
        self.activity.reset_label(self.challenges[n][0])
        self.ball.set_label(self.challenges[n][0])
        self.fraction = self.challenges[n][1]

        if self.correct > EXPERT:  # show two-segment bar
            self.bar.set_layer(0)
            self.bar10.set_layer(-1)
            self.bar12.set_layer(-1)
            self.bar14.set_layer(-1)
        elif self.challenges[n][2] == 12:  # show twelve-segment bar
            self.bar.set_layer(-1)
            self.bar10.set_layer(-1)
            self.bar12.set_layer(0)
            self.bar14.set_layer(-1)
        elif self.challenges[n][2] == 10:  # show ten-segment bar
            self.bar.set_layer(-1)
            self.bar10.set_layer(0)
            self.bar12.set_layer(-1)
            self.bar14.set_layer(-1)
        else:  # show fourteen-segment bar
            self.bar.set_layer(-1)
            self.bar10.set_layer(-1)
            self.bar12.set_layer(-1)
            self.bar14.set_layer(0)

    def _test(self):
        ''' 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])
        if x > f - delta and x < f + delta:
            smiley = Sprite(self.sprites, 0, 0, self.smiley_graphic)
            x = int(self.count * 25 % self.width)
            y = int(self.count / int(self.width / 25)) * 25
            smiley.move((x, y))
            smiley.set_layer(-1)
            self.correct += 1
        self.mark.move((int(f - self.mark.rect[2] / 2), self.bar.rect[1] - 2))

        # after enough correct answers, up the difficulty
        if self.correct == len(EASY) * 2:
            for challenge in MEDIUM:
                self.challenges.append(challenge)
            _logger.debug('%s', self.challenges)
        elif self.correct == len(EASY) * 4:
            for challenge in HARD:
                self.challenges.append(challenge)
            _logger.debug('%s', self.challenges)

        self.count += 1
        self.dx = 0  # stop horizontal movement between bounces

    def _keypress_cb(self, area, event):
        ''' Keypress: moving the slides with the arrow keys '''
        k = gtk.gdk.keyval_name(event.keyval)
        if k in ['h', 'Left', 'KP_Left']:
            self.dx = -5
        elif k in ['l', 'Right', 'KP_Right']:
            self.dx = 5
        elif k in ['KP_Page_Up', 'Return']:
            self._choose_a_fraction()
            self._move_ball()
        else:
            self.dx = 0
        return True

    def _keyrelease_cb(self, area, event):
        ''' Keyrelease: stop horizontal movement '''
        self.dx = 0
        return True

    def _expose_cb(self, win, event):
        ''' Callback to handle window expose events '''
        self.sprites.redraw_sprites(event.area)
        return True

    def _destroy_cb(self, win, event):
        ''' Callback to handle quit '''
        gtk.main_quit()


def svg_from_file(pathname):
    ''' Read SVG string from a file '''
    f = file(pathname, 'r')
    svg = f.read()
    f.close()
    return(svg)