def _make_wedge_bar(self, nsegments): ''' Create a wedged-shaped bar with n segments ''' s = 3.5 # add provision for stroke width svg = svg_header(self._width, BAR_HEIGHT * self._scale + s, 1.0) dx = self._width / float(nsegments) dy = (BAR_HEIGHT * self._scale) / float(nsegments) for i in range(int(nsegments) / 2): svg += svg_wedge(dx, BAR_HEIGHT * self._scale + s, i * 2 * dx + s, i * 2 * dy + s, (i * 2 + 1) * dy + s, '#000000', '#FFFFFF') svg += svg_wedge(dx, BAR_HEIGHT * self._scale + s, (i * 2 + 1) * dx + s, (i * 2 + 1) * dy + s, (i * 2 + 2) * dy + s, '#000000', '#FFFFFF') if int(nsegments) % 2 == 1: # odd svg += svg_wedge(dx, BAR_HEIGHT * self._scale + s, (i * 2 + 2) * dx + s, (i * 2 + 2) * dy + s, BAR_HEIGHT * self._scale + s, '#000000', '#FFFFFF') svg += svg_footer() self.bars[nsegments] = Sprite(self._sprites, 0, 0, svg_str_to_pixbuf(svg)) self.bars[nsegments].set_layer(2) self.bars[nsegments].set_label_attributes(18, horiz_align="left", i=0) self.bars[nsegments].set_label_attributes(18, horiz_align="right", i=1) self.bars[nsegments].set_label_color('black', i=0) self.bars[nsegments].set_label_color('white', i=1) self.bars[nsegments].set_label(' 0', i=0) self.bars[nsegments].set_label('1 ', i=1) self.bars[nsegments].move( (0, self._height - BAR_HEIGHT * self._scale))
def new_ball(self, filename): ''' Create a ball object and Easter Egg animation from an SVG file. ''' self.ball.set_shape(svg_str_to_pixbuf(svg_from_file(filename))) ball = extract_svg_payload(file(filename, 'r')) for i in range(8): self._frames[i].set_shape(svg_str_to_pixbuf( svg_header(SIZE[0], SIZE[1], 1.0) + TRANSFORMS[i] + ball + PUNCTURE + AIR + '</g>' + svg_footer()))
def new_ball_from_fraction(self, fraction): ''' Create a ball with a section of size fraction. ''' r = SIZE[0] / 2.0 self.ball.set_shape(svg_str_to_pixbuf( svg_header(SIZE[0], SIZE[1], 1.0) + svg_sector(r, r + BOX[1], r - 1, 1.999 * pi, COLORS[0], COLORS[1]) + svg_sector(r, r + BOX[1], r - 1, fraction * 2 * pi, COLORS[1], COLORS[0]) + svg_rect(BOX[0], BOX[1], 4, 4, 0, 0, '#FFFFFF', 'none') + svg_footer()))
def _make_mark(self): ''' Make a mark to show the fraction position on the bar. ''' mark = svg_header(self._ball_size / 2., BAR_HEIGHT * self._scale + 4, 1.0) mark += svg_rect(self._ball_size / 2., BAR_HEIGHT * self._scale + 4, 0, 0, 0, 0, '#FF0000', '#FF0000') mark += svg_rect(1, BAR_HEIGHT * self._scale + 4, 0, 0, self._ball_size / 4., 0, '#000000', '#000000') mark += svg_footer() self.mark = Sprite(self._sprites, 0, self._height, # hide off bottom of screen svg_str_to_pixbuf(mark)) self.mark.set_layer(1)
def make_labels(self): ''' Label the bar ''' 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_size / 4), self.bar_y(), svg_str_to_pixbuf(num)) self.left.set_label(_('0')) self.right = Sprite(self.sprites, self.screen_width - int(self.ball_size / 2), self.bar_y(), svg_str_to_pixbuf(num)) self.right.set_label(_('1'))
def make_bar(self, nsegments): ''' Create a bar with n segments ''' svg = svg_header(self.screen_width - self.ball_size, BAR_HEIGHT, 1.0) dx = (self.screen_width - self.ball_size) / float(nsegments) for i in range(int(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') if int(nsegments) % 2 == 1: # odd svg += svg_rect(dx, BAR_HEIGHT * self.scale, 0, 0, (i * 2 + 2) * dx, 0, '#FFFFFF', '#FFFFFF') svg += svg_footer() self.bars[nsegments] = Sprite(self.sprites, 0, 0, svg_str_to_pixbuf(svg)) self.bars[nsegments].move( (int(self.ball_size / 2), self.screen_height - \ int((self.ball_size + self.height()) / 2)))
def __init__(self, sprites, filename): self._current_frame = 0 self._frames = [] # Easter Egg animation self._sprites = sprites self.ball = Sprite(self._sprites, 0, 0, svg_str_to_pixbuf( svg_from_file(filename))) self.ball.set_layer(3) self.ball.set_label_attributes(24, vert_align='top') ball = extract_svg_payload(file(filename, 'r')) for i in range(8): self._frames.append(Sprite( self._sprites, 0, 0, svg_str_to_pixbuf( svg_header(SIZE[0], SIZE[1], 1.0) + TRANSFORMS[i] + ball + PUNCTURE + AIR + '</g>' + svg_footer()))) for frame in self._frames: frame.set_layer(3) frame.move((0, -SIZE[1])) # move animation frames off screen
def _create_sprites(self, path): ''' Create all of the sprites we'll need ''' self.smiley_graphic = svg_str_to_pixbuf(svg_from_file( os.path.join(path, 'images', 'smiley.svg'))) self.frown_graphic = svg_str_to_pixbuf(svg_from_file( os.path.join(path, 'images', 'frown.svg'))) self.blank_graphic = svg_str_to_pixbuf( svg_header(REWARD_HEIGHT, REWARD_HEIGHT, 1.0) + svg_rect(REWARD_HEIGHT, REWARD_HEIGHT, 5, 5, 0, 0, 'none', 'none') + svg_footer()) self.ball = Ball(self._sprites, os.path.join(path, 'images', 'soccerball.svg')) self._current_frame = 0 self.bar = Bar(self._sprites, self.ball.width(), COLORS) self._current_bar = self.bar.get_bar(2) self.ball_y_max = self.bar.bar_y() - self.ball.height() + \ int(BAR_HEIGHT / 2.) self.ball.move_ball((int((self._width - self.ball.width()) / 2), self.ball_y_max)) self._backgrounds = {} width, height = self._calc_background_size() pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size( os.path.join(path, 'images', 'grass_background.png'), width, height) if Gdk.Screen.height() > Gdk.Screen.width(): pixbuf = self._crop_to_portrait(pixbuf) self._backgrounds['grass_background.png'] = pixbuf self._background = Sprite(self._sprites, 0, 0, pixbuf) self._background.set_layer(-100) self._background.type = 'background' self._current_bg = 'grass_background.png'
def _make_wedge_mark(self): ''' Make a mark to show the fraction position on the bar. ''' dx = self._ball_size / 2. n = (self._width - self._ball_size) / dx dy = (BAR_HEIGHT * self._scale) / n s = 3.5 i = int(n / 2) - 1 mark = svg_header(self._ball_size, BAR_HEIGHT * self._scale + s, 1.0) mark += svg_wedge(dx, BAR_HEIGHT * self._scale + s, s, i * 2 * dy + s, (i * 2 + 1) * dy + s, '#FF0000', '#FFFFFF') mark += svg_wedge(dx, BAR_HEIGHT * self._scale + s, dx + s, (i * 2 + 1) * dy + s, (i * 2 + 2) * dy + s, '#FF0000', '#FFFFFF') mark += svg_footer() self.mark = Sprite(self._sprites, 0, self._height, # hide off bottom of screen svg_str_to_pixbuf(mark)) self.mark.set_layer(1)
def _make_rect_bar(self, nsegments): ''' Create a bar with n segments ''' svg = svg_header(self._width - self._ball_size, BAR_HEIGHT * self._scale, 1.0) dx = self._width / float(nsegments) for i in range(int(nsegments) / 2): svg += svg_rect(dx, BAR_HEIGHT * self._scale, 0, 0, i * 2 * dx, 0, self._colors[0], self._colors[0]) svg += svg_rect(dx, BAR_HEIGHT * self._scale, 0, 0, (i * 2 + 1) * dx, 0, self._colors[1], self._colors[1]) if int(nsegments) % 2 == 1: # odd svg += svg_rect(dx, BAR_HEIGHT * self._scale, 0, 0, (i * 2 + 2) * dx, 0, self._colors[0], self._colors[0]) svg += svg_footer() self.bars[nsegments] = Sprite(self._sprites, 0, 0, svg_str_to_pixbuf(svg)) self.bars[nsegments].move( (0, self._height - int((self._ball_size + self._height()) / 2)))