本文整理匯總了Python中sprites.Sprites.redraw_sprites方法的典型用法代碼示例。如果您正苦於以下問題:Python Sprites.redraw_sprites方法的具體用法?Python Sprites.redraw_sprites怎麽用?Python Sprites.redraw_sprites使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sprites.Sprites
的用法示例。
在下文中一共展示了Sprites.redraw_sprites方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: Yupana
# 需要導入模塊: from sprites import Sprites [as 別名]
# 或者: from sprites.Sprites import redraw_sprites [as 別名]
#.........這裏部分代碼省略.........
elif m in [5, 6, 7]:
return (60 ** e) * 5
else:
return (60 ** e) * 20
else: # custom
if m in [10]:
return (self.custom[4] ** e) * self.custom[0]
elif m in [8, 9]:
return (self.custom[4] ** e) * self.custom[1]
elif m in [5, 6, 7]:
return (self.custom[4] ** e) * self.custom[2]
else:
return (self.custom[4] ** e) * self.custom[3]
def remote_button_press(self, dot, color):
''' Receive a button press from a sharer '''
self._dots[dot].type = color
self._dots[dot].set_shape(self._new_dot(self._colors[color]))
def set_sharing(self, share=True):
_logger.debug('enabling sharing')
self.we_are_sharing = share
def _grid_to_dot(self, pos):
''' calculate the dot index from a column and row in the grid '''
return pos[0] + pos[1] * TEN
def _dot_to_grid(self, dot):
''' calculate the grid column and row for a dot '''
return [dot % TEN, int(dot / TEN)]
def __draw_cb(self, canvas, cr):
self._sprites.redraw_sprites(cr=cr)
def do_expose_event(self, event):
''' Handle the expose-event by drawing '''
# Restrict Cairo to the exposed area
cr = self._canvas.window.cairo_create()
cr.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
cr.clip()
# Refresh sprite list
self._sprites.redraw_sprites(cr=cr)
def _destroy_cb(self, win, event):
Gtk.main_quit()
def _new_dot(self, color):
''' generate a dot of a color color '''
def darken(color):
''' return a darker color than color '''
gdk_fill_color = Gdk.color_parse(self._fill)
gdk_fill_dark_color = Gdk.Color(
int(gdk_fill_color.red * 0.5),
int(gdk_fill_color.green * 0.5),
int(gdk_fill_color.blue * 0.5)).to_string()
return str(gdk_fill_dark_color)
self._dot_cache = {}
if not color in self._dot_cache:
self._stroke = color
self._fill = color
self._fill_dark = darken(color)
self._svg_width = self._dot_size
self._svg_height = self._dot_size
示例2: Game
# 需要導入模塊: from sprites import Sprites [as 別名]
# 或者: from sprites.Sprites import redraw_sprites [as 別名]
#.........這裏部分代碼省略.........
for x in range(SIX):
if self._dots[y * TEN + x].type != \
self._dots[y * TEN + TEN - x - 1].type:
self._set_label(_('keep trying'))
return False
self._set_label(_('good work'))
self._smile()
return True
if self._orientation == 'vertical':
for y in range(int(SIX / 2)):
for x in range(TEN):
if self._dots[y * TEN + x].type != \
self._dots[(SIX - y - 1) * TEN + x].type:
self._set_label(_('keep trying'))
return False
self._set_label(_('good work'))
else:
for y in range(SIX):
for x in range(SIX):
if self._dots[y * TEN + x].type != \
self._dots[y * TEN + TEN - x - 1].type:
self._set_label(_('keep trying'))
return False
for y in range(int(SIX / 2)):
for x in range(TEN):
if self._dots[y * TEN + x].type != \
self._dots[(SIX - y - 1) * TEN + x].type:
self._set_label(_('keep trying'))
return False
self._set_label(_('good work'))
self._smile()
return True
def __draw_cb(self,canvas,cr):
self._sprites.redraw_sprites(cr=cr)
def _grid_to_dot(self, pos):
''' calculate the dot index from a column and row in the grid '''
return pos[0] + pos[1] * TEN
def _dot_to_grid(self, dot):
''' calculate the grid column and row for a dot '''
return [dot % TEN, int(dot / TEN)]
def _expose_cb(self, win, event):
self.do_expose_event(event)
def do_expose_event(self, event):
''' Handle the expose-event by drawing '''
# Restrict Cairo to the exposed area
cr = self._canvas.window.cairo_create()
cr.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
cr.clip()
# Refresh sprite list
self._sprites.redraw_sprites(cr=cr)
def _destroy_cb(self, win, event):
Gtk.main_quit()
def _new_dot(self, color):
''' generate a dot of a color color '''
self._dot_cache = {}
if not color in self._dot_cache:
self._stroke = color
self._fill = color
self._svg_width = self._dot_size
self._svg_height = self._dot_size
示例3: Game
# 需要導入模塊: from sprites import Sprites [as 別名]
# 或者: from sprites.Sprites import redraw_sprites [as 別名]
#.........這裏部分代碼省略.........
''' Is there a path to nowhere? '''
self._hide_errormsgs()
self._there_are_errors = False
if tile is not None:
self._check_tile(tile, [int(tile / COL), 0], NORTH,
tile + OFFSETS[0])
self._check_tile(tile, [tile % ROW, ROW - 1], EAST,
tile + OFFSETS[1])
self._check_tile(tile, [int(tile / COL), COL - 1], SOUTH,
tile + OFFSETS[2])
self._check_tile(tile, [tile % ROW, 0], WEST, tile + OFFSETS[3])
def _check_tile(self, i, edge_check, direction, neighbor):
''' Can a tile be placed at position i? '''
if edge_check[0] == edge_check[1]:
for path in self.grid.grid[i].paths:
if path[direction] == 1:
self._display_errormsg(i, direction)
else:
if self.grid.grid[neighbor] is not None:
my_path = 0
your_path = 0
for c in self.grid.grid[i].paths:
if c[direction] == 1:
my_path = 1
for c in self.grid.grid[neighbor].paths:
if c[(direction + 2) % 4] == 1:
your_path = 1
if my_path != your_path:
self._display_errormsg(i, direction)
def _display_errormsg(self, i, direction):
''' Display an error message where and when appropriate. '''
if self._press is not None:
dxdy = [[0.375, -0.125], [0.875, 0.375], [0.375, 0.875],
[-0.125, 0.375]]
x, y = self._press.get_xy()
self._errormsg[direction].move(
(x + dxdy[direction][0] * self.tile_width,
y + dxdy[direction][1] * self.tile_height))
self._errormsg[direction].set_layer(OVER_THE_TOP)
self._there_are_errors = True
def _hide_errormsgs(self):
''' Hide all the error messages. '''
for i in range(4):
self._errormsg[i].move((self.grid.left, self.grid.top))
self._errormsg[i].set_layer(HIDE)
def _hide_highlight(self):
''' No tile is selected. '''
for i in range(4):
self._highlight[i].move((self.grid.left, self.grid.top))
self._highlight[i].set_layer(HIDE)
def _move_relative_highlight(self, pos):
for i in range(4):
self._highlight[i].move_relative(pos)
def _move_highlight(self, pos):
x, y = pos
self._highlight[0].move((x, y))
self._highlight[1].move((x + 7 * self.tile_width / 8, y))
self._highlight[2].move((x + 7 * self.tile_width / 8,
y + 7 * self.tile_height / 8))
self._highlight[3].move((x, y + 7 * self.tile_height / 8))
def _show_highlight(self, pos=None):
''' Highlight the tile that is selected. '''
if self.last_spr_moved is None and pos is None:
self._hide_highlight()
else:
if pos is None:
x, y = self.last_spr_moved.get_xy()
else: # Giving a hint.
x, y = pos
self._move_highlight((x, y))
for i in range(4):
self._highlight[i].set_layer(OVER_THE_TOP)
def _keypress_cb(self, area, event):
return True
def _expose_cb(self, win, event):
''' Callback to handle window expose events '''
self.do_expose_event(event)
return True
def do_expose_event(self, event):
''' Handle the expose-event by drawing '''
# Restrict Cairo to the exposed area
cr = self._canvas.window.cairo_create()
cr.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
cr.clip()
# Refresh sprite list
self._sprites.redraw_sprites(cr=cr)
def _destroy_cb(self, win, event):
gtk.main_quit()
示例4: Game
# 需要導入模塊: from sprites import Sprites [as 別名]
# 或者: from sprites.Sprites import redraw_sprites [as 別名]
#.........這裏部分代碼省略.........
if spr.type == self._target:
self._smile.set_layer(200)
self._tries = 3
else:
self._frown.set_layer(200)
self._tries += 1
if self._tries == 3:
self.food_cards[self._target].set_label_color('blue')
label = self.food_cards[self._target].labels[0]
self.food_cards[self._target].set_label(label)
elif self.level == 1:
i = FOOD[self._target][GROUP]
if spr.type == i:
self._smile.set_layer(200)
self._tries = 3
else:
self._frown.set_layer(200)
self._tries += 1
if self._tries == 3:
self._group_cards[i].set_label_color('blue')
label = self._group_cards[i].labels[0]
self._group_cards[i].set_label(label)
elif self.level == 2:
if spr.type == self._target:
self._smile.set_layer(200)
self._tries = 3
else:
self._frown.set_layer(200)
self._tries += 1
if self._tries == 3:
self.food_cards[self._target].set_label_color('blue')
label = self.food_cards[self._target].labels[0]
self.food_cards[self._target].set_label(label)
elif self.level == 3:
i = MYPLATE[FOOD[self._target][GROUP]][QUANT]
if spr.type == i:
self._smile.set_layer(200)
self._tries = 3
else:
self._frown.set_layer(200)
self._tries += 1
if self._tries == 3:
self._quantity_cards[i].set_label_color('blue')
label = self._quantity_cards[i].labels[0]
self._quantity_cards[i].set_label(label)
elif self.level == 4:
if self._target == spr.type:
self._smile.set_layer(200)
self._tries = 3
else:
self._frown.set_layer(200)
self._tries += 1
if self._tries == 3:
self._balance_cards[self._target].set_label_color('blue')
label = self._balance_cards[self._target].labels[0]
self._balance_cards[self._target].set_label(label)
else:
_logger.debug('unknown play level %d' % (self.level))
# Play again
if self._tries == 3:
GObject.timeout_add(2000, self.new_game)
else:
GObject.timeout_add(1000, self._reset_game)
return True
def _reset_game(self):
self._frown.hide()
if self.level in [0, 2]:
for i, w in enumerate(self.food_cards):
w.set_label_color('black')
w.set_label(FOOD[i][NAME])
elif self.level == 1:
for i, w in enumerate(self._group_cards):
w.set_label_color('black')
w.set_label(MYPLATE[i][0])
elif self.level == 3:
for i, w in enumerate(self._quantity_cards):
w.set_label_color('black')
w.set_label(QUANTITIES[i])
elif self.level == 4:
for i, w in enumerate(self._balance_cards):
w.set_label_color('black')
w.set_label(BALANCE[i])
def __draw_cb(self, canvas, cr):
self._sprites.redraw_sprites(cr=cr)
def do_expose_event(self, event):
''' Handle the expose-event by drawing '''
# Restrict Cairo to the exposed area
cr = self._canvas.window.cairo_create()
cr.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
cr.clip()
# Refresh sprite list
self._sprites.redraw_sprites(cr=cr)
def _destroy_cb(self, win, event):
Gtk.main_quit()
示例5: Game
# 需要導入模塊: from sprites import Sprites [as 別名]
# 或者: from sprites.Sprites import redraw_sprites [as 別名]
#.........這裏部分代碼省略.........
self._flip(self._dots[dot + self._edge])
def _flip(self, spr):
''' flip a dot '''
spr.type += 1
spr.type %= 2
spr.set_shape(self._new_dot(self._colors[spr.type]))
def remote_button_press(self, dot):
''' Receive a button press from a sharer '''
self._flip_them(dot)
self._test_game_over()
def set_sharing(self, share=True):
_logger.debug('enabling sharing')
self.we_are_sharing = share
def _smile(self):
for dot in self._dots:
dot.set_label(':)')
def _test_game_over(self):
''' Check to see if game is over: all dots the same color '''
match = self._dots[0].type
for y in range(self._edge):
for x in range(self._edge):
if self._dots[y * self._edge + x].type != match:
self._set_label(_('keep trying'))
return False
self._set_label(_('good work'))
self._smile()
GObject.timeout_add(2000, self.more_dots)
return True
def _grid_to_dot(self, pos):
''' calculate the dot index from a column and row in the grid '''
return pos[0] + pos[1] * self._edge
def _dot_to_grid(self, dot):
''' calculate the grid column and row for a dot '''
return [dot % self._edge, int(dot / self._edge)]
def game_over(self, msg=_('Game over')):
''' Nothing left to do except show the results. '''
self._set_label(msg)
def __draw_cb(self, canvas, cr):
self._sprites.redraw_sprites(cr=cr)
def do_expose_event(self, event):
''' Handle the expose-event by drawing '''
# Restrict Cairo to the exposed area
cr = self._canvas.window.cairo_create()
cr.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
cr.clip()
# Refresh sprite list
self._sprites.redraw_sprites(cr=cr)
def _destroy_cb(self, win, event):
Gtk.main_quit()
def _new_dot(self, color):
''' generate a dot of a color color '''
self._dot_cache = {}
if color not in self._dot_cache:
self._stroke = color
self._fill = color
self._svg_width = self._dot_size
self._svg_height = self._dot_size
pixbuf = svg_str_to_pixbuf(
self._header() +
self._circle(self._dot_size / 2., self._dot_size / 2.,
self._dot_size / 2.) +
self._footer())
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
self._svg_width, self._svg_height)
context = cairo.Context(surface)
Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0)
context.rectangle(0, 0, self._svg_width, self._svg_height)
context.fill()
self._dot_cache[color] = surface
return self._dot_cache[color]
def _header(self):
return '<svg\n' + 'xmlns:svg="http://www.w3.org/2000/svg"\n' + \
'xmlns="http://www.w3.org/2000/svg"\n' + \
'xmlns:xlink="http://www.w3.org/1999/xlink"\n' + \
'version="1.1"\n' + 'width="' + str(self._svg_width) + '"\n' + \
'height="' + str(self._svg_height) + '">\n'
def _circle(self, r, cx, cy):
return '<circle style="fill:' + str(self._fill) + ';stroke:' + \
str(self._stroke) + ';" r="' + str(r - 0.5) + '" cx="' + \
str(cx) + '" cy="' + str(cy) + '" />\n'
def _footer(self):
return '</svg>\n'
示例6: Game
# 需要導入模塊: from sprites import Sprites [as 別名]
# 或者: from sprites.Sprites import redraw_sprites [as 別名]
class Game():
def __init__(self, canvas, parent=None, path=None,
colors=['#A0FFA0', '#FF8080']):
self._canvas = canvas
self._parent = parent
self._parent.show_all()
self._path = path
self.level = 1
self._colors = ['#FFFFFF']
self._colors.append(colors[0])
self._colors.append(colors[1])
self._colors.append(colors[0])
self._colors.append('#FF0000')
self._canvas.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
self._canvas.connect("draw", self.__draw_cb)
self._canvas.connect("button-press-event", self._button_press_cb)
self._width = Gdk.Screen.width()
self._height = Gdk.Screen.height() - GRID_CELL_SIZE
if self._width < self._height:
self.portrait = True
self.grid_height = TEN
self.grid.width = SEVEN
else:
self.portrait = False
self.grid_height = SEVEN
self.grid_width = TEN
self._scale = min(self._width / (self.grid_width * DOT_SIZE * 1.2),
self._height / (self.grid_height * DOT_SIZE * 1.2))
self._dot_size = int(DOT_SIZE * self._scale)
self._space = int(self._dot_size / 5.)
self.we_are_sharing = False
# '-1' Workaround for showing 'second 0'
self._game_time_seconds = -1
self._game_time = "00:00"
self._timeout_id = None
# Generate the sprites we'll need...
self._sprites = Sprites(self._canvas)
self._dots = []
for y in range(self.grid_height):
for x in range(self.grid_width):
xoffset = int((self._width - self.grid_width * self._dot_size -
(self.grid_width - 1) * self._space) / 2.)
self._dots.append(
Sprite(self._sprites,
xoffset + x * (self._dot_size + self._space),
y * (self._dot_size + self._space),
self._new_dot(self._colors[0])))
self._dots[-1].type = 0 # not set
self._dots[-1].set_label_attributes(40)
self._all_clear()
Gdk.Screen.get_default().connect('size-changed', self._configure_cb)
def _configure_cb(self, event):
dot_list = self.save_game()
self._width = Gdk.Screen.width()
self._height = Gdk.Screen.height() - GRID_CELL_SIZE
if self._width < self._height:
self.portrait = True
self.grid_height = TEN
self.grid_width = SEVEN
else:
self.portrait = False
self.grid_height = SEVEN
self.grid_width = TEN
i = 0
for y in range(self.grid_height):
for x in range(self.grid_width):
xoffset = int((self._width - self.grid_width * self._dot_size -
(self.grid_width - 1) * self._space) / 2.)
self._dots[i].move(
(xoffset + x * (self._dot_size + self._space),
y * (self._dot_size + self._space)))
i += 1
self.restore_game(dot_list)
def __draw_cb(self, canvas, cr):
self._sprites.redraw_sprites(cr=cr)
def _all_clear(self):
''' Things to reinitialize when starting up a new game. '''
for dot in self._dots:
if dot.type != 1:
dot.type = 1
dot.set_shape(self._new_dot(self._colors[dot.type]))
dot.set_label('')
self._stop_timer()
#.........這裏部分代碼省略.........
示例7: SlideRule
# 需要導入模塊: from sprites import Sprites [as 別名]
# 或者: from sprites.Sprites import redraw_sprites [as 別名]
#.........這裏部分代碼省略.........
if not self.sugar:
self.update_textview_y_offset(self.parent.menu_height)
self.active_slide = self.name_to_slide('C')
self.active_stator = self.name_to_stator('D')
self.update_slide_labels()
self.update_result_label()
def update_textview_y_offset(self, dy):
''' Need to account for menu height in GNOME '''
self.reticule.tabs[0].textview_y_offset += dy
self.reticule.tabs[1].textview_y_offset += dy
for slide in self.slides:
slide.tabs[0].textview_y_offset += dy
slide.tabs[1].textview_y_offset += dy
def _text_focus_out_cb(self, widget=None, event=None):
''' One of the four textviews was in focus '''
i = None
if widget in self.text_entries:
i = self.text_entries.index(widget)
bounds = self.text_buffers[i].get_bounds()
text = self.text_buffers[i].get_text(bounds[0], bounds[1], True)
text = text.strip()
self._process_numeric_input(i, text)
def _set_screen_dpi(self):
dpi = _get_screen_dpi()
font_map_default = PangoCairo.font_map_get_default()
font_map_default.set_resolution(dpi)
def __draw_cb(self, canvas, cr):
self.sprites.redraw_sprites(cr=cr)
# Handle the expose-event by drawing
def do_expose_event(self, event):
# Create the cairo context
cr = self.canvas.window.cairo_create()
print 'set cr in do_expose'
self.sprites.set_cairo_context(cr)
# Restrict Cairo to the exposed area; avoid extra work
cr.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
cr.clip()
# Refresh sprite list
self.sprites.redraw_sprites(cr=cr)
def _destroy_cb(self, win, event):
Gtk.main_quit()
def _keypress_cb(self, area, event):
""" Keypress: moving the slides with the arrow keys """
k = Gdk.keyval_name(event.keyval)
if not self.sugar:
return
if k == 'a':
self.parent.show_a()
elif k == 'k':
self.parent.show_k()
elif k in ['c', 'asterisk', 'x']:
self.parent.show_c()
elif k in ['i', '/']:
示例8: Game
# 需要導入模塊: from sprites import Sprites [as 別名]
# 或者: from sprites.Sprites import redraw_sprites [as 別名]
#.........這裏部分代碼省略.........
return
self.startpos = spr.get_xy()
self.press = spr
def _mouse_move_cb(self, win, event):
""" Drag a rule with the mouse. """
if self.press is None:
self.dragpos = [0, 0]
return True
win.grab_focus()
x, y = map(int, event.get_coords())
dx = x - self.dragpos[0]
dy = y - self.dragpos[1]
self.press.move_relative((dx, dy))
self.dragpos = [x, y]
def _button_release_cb(self, win, event):
if self.press == None:
return True
if _distance(self.press.get_xy(), self.startpos) < 20:
if type(self.press.type) == int:
self.i = self.press.type
self._new_surface()
self.press.move(self.startpos)
self.press = None
def _new_surface(self):
self.colors[0] = colors[self.i][0]
self.colors[1] = colors[self.i][1]
self._xo_man.set_image(self._new_xo_man(colors[self.i]))
self._xo_man.set_layer(100)
def __draw_cb(self, canvas, cr):
self._sprites.redraw_sprites(cr=cr)
def do_expose_event(self, event):
''' Handle the expose-event by drawing '''
# Restrict Cairo to the exposed area
cr = self._canvas.window.cairo_create()
cr.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
cr.clip()
# Refresh sprite list
self._sprites.redraw_sprites(cr=cr)
def _destroy_cb(self, win, event):
Gtk.main_quit()
def _new_dot(self, color):
''' generate a dot of a color color '''
if True: # not color in self._dot_cache:
self._stroke = color[0]
self._fill = color[1]
self._svg_width = int(60 * self._scale)
self._svg_height = int(60 * self._scale)
pixbuf = svg_str_to_pixbuf(
self._header() + \
'<circle cx="%f" cy="%f" r="%f" stroke="%s" fill="%s" \
stroke-width="%f" visibility="visible" />' % (
30 * self._scale, 30 * self._scale,
self._radius * self._scale, self._stroke,
self._fill, self._stroke_width * self._scale) + \
self._footer())
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
self._svg_width, self._svg_height)
示例9: __init__
# 需要導入模塊: from sprites import Sprites [as 別名]
# 或者: from sprites.Sprites import redraw_sprites [as 別名]
#.........這裏部分代碼省略.........
if self._game in [0, 1, 3]:
for i in range(self._level):
self._dots[i].set_layer(100)
else:
for dot in self._dots:
dot.set_shape(self._new_dot_surface(image=self._recall_list[self._answer]))
dot.set_layer(100)
if self._correct == 0:
self._timeout_id = GObject.timeout_add(5000, self.new_game)
else:
self._timeout_id = GObject.timeout_add(3000, self.new_game)
return True
def remote_button_press(self, dot, color):
""" Receive a button press from a sharer """
self._dots[dot].type = color
self._dots[dot].set_shape(self._new_dot_surface(color=self._colors[color]))
def set_sharing(self, share=True):
_logger.debug("enabling sharing")
self.we_are_sharing = share
def _expose_cb(self, win, event):
self.do_expose_event(event)
def do_expose_event(self, event):
""" Handle the expose-event by drawing """
# Restrict Cairo to the exposed area
cr = self._canvas.get_window().cairo_create()
cr.rectangle(event.area.x, event.area.y, event.area.width, event.area.height)
cr.clip()
# Refresh sprite list
self._sprites.redraw_sprites(cr=cr)
def _destroy_cb(self, win, event):
Gtk.main_quit()
def _new_dot_surface(self, color="#000000", image=None, color_image=None):
""" generate a dot of a color color """
self._dot_cache = {}
if color_image is not None:
if color_image + 10000 in self._dot_cache:
return self._dot_cache[color_image + 10000]
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
os.path.join(self._path, self._CPATHS[color_image]), self._svg_width, self._svg_height
)
elif image is not None:
if image in self._dot_cache:
return self._dot_cache[image]
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
os.path.join(self._path, self._PATHS[image]), self._svg_width, self._svg_height
)
else:
if color in self._dot_cache:
return self._dot_cache[color]
self._stroke = color
self._fill = color
self._svg_width = self._dot_size
self._svg_height = self._dot_size
i = self._colors.index(color)
pixbuf = svg_str_to_pixbuf(
self._header()
+ self._circle(self._dot_size / 2.0, self._dot_size / 2.0, self._dot_size / 2.0)
+ self._footer()
示例10: Game
# 需要導入模塊: from sprites import Sprites [as 別名]
# 或者: from sprites.Sprites import redraw_sprites [as 別名]
#.........這裏部分代碼省略.........
self._man_cards[0].move((x - int(self._loco_dim[0] / 2.),
y - int(self._loco_dim[1] / 2.)))
self._man_cards[0].set_layer(LOCO_LAYER)
self._correct += 1
self._counter += 1
GObject.timeout_add(1000, self.new_game, False)
elif self.level in [4, 5]:
# For Game 4 and 5, we allow dragging
if self._press is None:
self._drag_pos = [0, 0]
return True
dx = x - self._drag_pos[0]
dy = y - self._drag_pos[1]
self._press.move_relative((dx, dy))
self._drag_pos = [x, y]
elif self.level == 3:
# For Game 3, we are dragging
if self._press is None:
self._drag_pos = [0, 0]
return True
dx = x - self._drag_pos[0]
dy = y - self._drag_pos[1]
self._press.move_relative((dx, dy))
self._drag_pos = [x, y]
if x > self._width / 2.:
self._press.set_shape(self._man_pixbuf)
if self._press.type == 'loco':
self._correct += 1
self._press.type = 'man'
return True
def _button_release_cb(self, win, event):
# Game 3 uses release
if self.level == 3:
# Move to release
if self._correct == self._counter + 1:
self._counter += 1
self._correct = 0
GObject.timeout_add(2000, self.new_game, False)
self._press = None
self._drag_pos = [0, 0]
return True
def _button_press_cb(self, win, event):
self._press = None
x, y = map(int, event.get_coords())
if self.level == 0:
return
spr = self._sprites.find_sprite((x, y))
if spr is None:
return
if spr.type != 'loco':
return
if self.level < 2 and self._timeout_id is None:
return
if self._clicked:
return
# Games 1, 2, and 3 involve clicks; Games 4 and 5 allow click to drag
if self.level == 1:
self._all_clear()
self._man_cards[0].move((x - int(self._loco_dim[0] / 2.),
y - int(self._loco_dim[1] / 2.)))
self._man_cards[0].set_layer(LOCO_LAYER)
self._clicked = True
self._counter += 1
self._correct += 1
if self._timeout_id is not None:
GObject.source_remove(self._timeout_id)
GObject.timeout_add(2000, self.new_game, False)
elif self.level == 2:
spr.set_shape(self._ghost_pixbuf)
spr.type = 'ghost'
if self._correct == self._counter:
self._counter += 1
self._correct = 0
GObject.timeout_add(2000, self.new_game, False)
else:
self._correct += 1
elif self.level in [3, 4, 5]:
# In Games 4 and 5, dragging is used to remove overlaps
self._press = spr
self._drag_pos = [x, y]
return True
def __draw_cb(self, canvas, cr):
self._sprites.redraw_sprites(cr=cr)
def do_expose_event(self, event):
''' Handle the expose-event by drawing '''
# Restrict Cairo to the exposed area
cr = self._canvas.window.cairo_create()
cr.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
cr.clip()
# Refresh sprite list
self._sprites.redraw_sprites(cr=cr)
def _destroy_cb(self, win, event):
Gtk.main_quit()
示例11: Game
# 需要導入模塊: from sprites import Sprites [as 別名]
# 或者: from sprites.Sprites import redraw_sprites [as 別名]
#.........這裏部分代碼省略.........
self.release = None
return True
# take note of card under button press
self.press = spr
return True
def _button_release_cb(self, win, event):
win.grab_focus()
x, y = map(int, event.get_coords())
spr = self.sprites.find_sprite((x, y))
if spr is None:
self.press = None
self.release = None
return True
# take note of card under button release
self.release = spr
# if press and release are the same card (click), then rotate
if self.press == self.release:
self.press.set_layer(0)
self.grid.card_table[self.grid.grid[self.grid.spr_to_i(
self.press)]].rotate_ccw()
if self.mode == 'hexagon': # Rotate a second time
self.grid.card_table[self.grid.grid[self.grid.spr_to_i(
self.press)]].rotate_ccw()
self.press.set_layer(100)
else:
self.grid.swap(self.press, self.release, self.mode)
self.press = None
self.release = None
if self.test() == True:
if self.level < 2:
gobject.timeout_add(3000, self.activity.change_play_level_cb,
None)
return True
def _keypress_cb(self, area, event):
""" Keypress is used to ... """
k = gtk.gdk.keyval_name(event.keyval)
def _expose_cb(self, win, event):
''' Callback to handle window expose events '''
self.do_expose_event(event)
return True
def do_expose_event(self, event):
''' Handle the expose-event by drawing '''
# Restrict Cairo to the exposed area
cr = self.canvas.window.cairo_create()
cr.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
cr.clip()
# Refresh sprite list
self.sprites.redraw_sprites(cr=cr)
def _destroy_cb(self, win, event):
gtk.main_quit()
def mask(self, level):
""" mask out cards not on play level """
self.grid.hide_list(MASKS[level])
self.bounds = LEVEL_BOUNDS[level]
self.level = level
def test(self):
""" Test the grid to see if the level is solved """
if self.mode != 'rectangle':
return False
for i in range(24):
if i not in MASKS[self.level]:
if not self.test_card(i):
return False
return True
def test_card(self, i):
""" Test a card with its neighbors; tests are bounded by the level """
row = int(i/6)
col = i%6
if row > self.bounds[0][0] and row <= self.bounds[0][1]:
if C[self.grid.grid[i]][rotate_index(0,
self.grid.card_table[self.grid.grid[i]].orientation)] != \
C[self.grid.grid[i - 6]][rotate_index(1,
self.grid.card_table[self.grid.grid[i - 6]].orientation)]:
return False
if C[self.grid.grid[i]][rotate_index(3,
self.grid.card_table[self.grid.grid[i]].orientation)] != \
C[self.grid.grid[i - 6]][rotate_index(2,
self.grid.card_table[self.grid.grid[i - 6]].orientation)]:
return False
if col > self.bounds[2][0] and col <= self.bounds[2][1]:
if C[self.grid.grid[i]][rotate_index(3,
self.grid.card_table[self.grid.grid[i]].orientation)] != \
C[self.grid.grid[i - 1]][rotate_index(0,
self.grid.card_table[self.grid.grid[i - 1]].orientation)]:
return False
if C[self.grid.grid[i]][rotate_index(2,
self.grid.card_table[self.grid.grid[i]].orientation)] != \
C[self.grid.grid[i - 1]][rotate_index(1,
self.grid.card_table[self.grid.grid[i - 1]].orientation)]:
return False
return True
示例12: BBoardActivity
# 需要導入模塊: from sprites import Sprites [as 別名]
# 或者: from sprites.Sprites import redraw_sprites [as 別名]
#.........這裏部分代碼省略.........
''' Display a preview image and title as a thumbnail. '''
pixbuf = self.slides[i].pixbuf
if pixbuf is not None:
pixbuf_thumb = pixbuf.scale_simple(
int(w), int(h), gtk.gdk.INTERP_TILES)
else:
pixbuf_thumb = svg_str_to_pixbuf(
genblank(int(w), int(h), self.slides[i].colors))
# Create a Sprite for this thumbnail
self._thumbs.append([Sprite(self._sprites, x, y, pixbuf_thumb),
x, y, i])
self._thumbs[i][0].set_image(
svg_str_to_pixbuf(svg_rectangle(int(w), int(h),
self.slides[i].colors)), i=1)
self._thumbs[i][0].set_layer(TOP)
def _expose_cb(self, win, event):
''' Callback to handle window expose events '''
self.do_expose_event(event)
return True
# Handle the expose-event by drawing
def do_expose_event(self, event):
# Create the cairo context
cr = self.canvas.window.cairo_create()
# Restrict Cairo to the exposed area; avoid extra work
cr.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
cr.clip()
# Refresh sprite list
self._sprites.redraw_sprites(cr=cr)
def write_file(self, file_path):
''' Clean up '''
if self._dirty:
self._save_descriptions_cb()
self._dirty = False
if os.path.exists(os.path.join(self.datapath, 'output.ogg')):
os.remove(os.path.join(self.datapath, 'output.ogg'))
def do_fullscreen_cb(self, button):
''' Hide the Sugar toolbars. '''
self.fullscreen()
def invalt(self, x, y, w, h):
''' Mark a region for refresh '''
self._canvas.window.invalidate_rect(
gtk.gdk.Rectangle(int(x), int(y), int(w), int(h)), False)
def _spr_to_thumb(self, spr):
''' Find which entry in the thumbnails table matches spr. '''
for i, thumb in enumerate(self._thumbs):
if spr == thumb[0]:
return i
return -1
def _spr_is_thumbnail(self, spr):
''' Does spr match an entry in the thumbnails table? '''
if self._spr_to_thumb(spr) == -1:
return False
else:
return True
示例13: Game
# 需要導入模塊: from sprites import Sprites [as 別名]
# 或者: from sprites.Sprites import redraw_sprites [as 別名]
#.........這裏部分代碼省略.........
''' Restore a grid from the share '''
self.show(dot_list)
self.this_pattern = boolean
def save_grid(self):
''' Return dot list for sharing '''
dot_list = []
for dot in self._dots:
dot_list.append(dot.type)
return(dot_list, self.this_pattern)
def _grid_to_dot(self, pos):
''' calculate the dot index from a column and row in the grid '''
return pos[0] + pos[1] * GRID
def _dot_to_grid(self, dot):
''' calculate the grid column and row for a dot '''
return [dot % GRID, int(dot / GRID)]
def _set_label(self, string):
''' Set the label in the toolbar or the window frame. '''
self._activity.status.set_label(string)
def _generate_pattern(self, f):
''' Run Python code passed as argument '''
userdefined = {}
try:
exec f in globals(), userdefined
return userdefined['generate_pattern'](self)
except ZeroDivisionError as e:
self._set_label('Python zero-divide error: %s' % (str(e)))
except ValueError as e:
self._set_label('Python value error: %s' % (str(e)))
except SyntaxError as e:
self._set_label('Python syntax error: %s' % (str(e)))
except NameError as e:
self._set_label('Python name error: %s' % (str(e)))
except OverflowError as e:
self._set_label('Python overflow error: %s' % (str(e)))
except TypeError as e:
self._set_label('Python type error: %s' % (str(e)))
except BaseException:
self._set_label('Python error')
traceback.print_exc()
return None
def __draw_cb(self, canvas, cr):
self._sprites.redraw_sprites(cr=cr)
def do_expose_event(self, event):
''' Handle the expose-event by drawing '''
# Restrict Cairo to the exposed area
cr = self._canvas.window.cairo_create()
cr.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
cr.clip()
# Refresh sprite list
self._sprites.redraw_sprites(cr=cr)
def _destroy_cb(self, win, event):
Gtk.main_quit()
def _new_dot(self, color):
''' generate a dot of a color color '''
self._dot_cache = {}
if color not in self._dot_cache:
self._stroke = color
self._fill = color
self._svg_width = self._dot_size
self._svg_height = self._dot_size
pixbuf = svg_str_to_pixbuf(
self._header() +
self._circle(self._dot_size / 2., self._dot_size / 2.,
self._dot_size / 2.) +
self._footer())
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
self._svg_width, self._svg_height)
context = cairo.Context(surface)
Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0)
context.rectangle(0, 0, self._svg_width, self._svg_height)
context.fill()
self._dot_cache[color] = surface
return self._dot_cache[color]
def _header(self):
return '<svg\n' + 'xmlns:svg="http://www.w3.org/2000/svg"\n' + \
'xmlns="http://www.w3.org/2000/svg"\n' + \
'xmlns:xlink="http://www.w3.org/1999/xlink"\n' + \
'version="1.1"\n' + 'width="' + str(self._svg_width) + '"\n' + \
'height="' + str(self._svg_height) + '">\n'
def _circle(self, r, cx, cy):
return '<circle style="fill:' + str(self._fill) + ';stroke:' + \
str(self._stroke) + ';" r="' + str(r - 0.5) + '" cx="' + \
str(cx) + '" cy="' + str(cy) + '" />\n'
def _footer(self):
return '</svg>\n'
示例14: NapierActivity
# 需要導入模塊: from sprites import Sprites [as 別名]
# 或者: from sprites.Sprites import redraw_sprites [as 別名]
#.........這裏部分代碼省略.........
cb_arg=7, tooltip=_('seven'))
button_factory('number-8', self._bones_toolbar, self._number_cb,
cb_arg=8, tooltip=_('eight'))
button_factory('number-9', self._bones_toolbar, self._number_cb,
cb_arg=9, tooltip=_('nine'))
separator_factory(toolbox.toolbar, True, False)
stop_button = StopButton(self)
stop_button.props.accelerator = '<Ctrl>q'
toolbox.toolbar.insert(stop_button, -1)
stop_button.show()
self._bones_toolbar_button.set_expanded(True)
def _new_calc_cb(self, button=None):
''' Start a new calculation. '''
for bone in range(self._max_bones):
self._bones[bone].set_shape(self._blank_image)
self._bones[bone].inval()
self._number_of_bones = 0
self._number = 0
self._status.set_label('')
return
def _number_cb(self, button=None, value=0):
''' Try to add a digit. '''
if self._number_of_bones == self._max_bones:
return
self._number_of_bones += 1
if self._bone_images[value] is None:
self._bone_images[value] = _svg_str_to_pixbuf(
_bone_factory(value, scale=self._scale))
self._bones[self._number_of_bones].set_shape(self._bone_images[value])
self._bones[self._number_of_bones].inval()
self._number = self._number * 10 + value
def _mouse_move_cb(self, win, event):
''' Determine which row we are in and then calculate the product. '''
win.grab_focus()
x, y = map(int, event.get_coords())
factor = int(y / self._bone_width) # The row determines a factor
if self._number == 0 or factor == 0:
self._status.set_label('')
self._circles[0].move((0, -100))
self._circles[1].move((0, -100))
for number in range(self._max_bones - 1):
self._ovals[number].move((0, -100))
else:
c0dx = int(4 * self._scale)
c0dy = int(12 * self._scale)
c1dx = int(44 * self._scale)
c1dy = int(47 * self._scale)
odx = int(42 * self._scale)
ody = int(2 * self._scale)
self._circles[0].move((self._bone_width + c0dx,
factor * self._bone_width + c0dy))
self._circles[1].move((
self._number_of_bones * self._bone_width + c1dx,
factor * self._bone_width + c1dy))
for number in range(self._number_of_bones - 1):
self._ovals[number].move(((number + 1) * self._bone_width + odx,
factor * self._bone_width + ody))
self._status.set_label('%d × %d = %d' % (
factor + 1, self._number, (factor + 1) * self._number))
return True
def _key_press_cb(self, win, event):
''' TODO: Add bones by typing numbers '''
return True
def __draw_cb(self, canvas, cr):
self._sprites.redraw_sprites(cr=cr)
def do_expose_event(self, event):
''' Handle the expose-event by drawing '''
# Restrict Cairo to the exposed area
cr = self._canvas.window.cairo_create()
cr.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
cr.clip()
# Refresh sprite list
self._sprites.redraw_sprites(cr=cr)
def _destroy_cb(self, win, event):
Gtk.main_quit()
def _restore(self):
''' Try to restore previous state. '''
if 'number' in self.metadata and self.metadata['number'] != '0':
for digit in range(len(self.metadata['number'])):
self._number_cb(button=None,
value=int(self.metadata['number'][digit]))
def write_file(self, file_path):
''' Write the status to the Journal. '''
if not hasattr(self, '_number'):
return
self.metadata['number'] = str(self._number)
示例15: Bounce
# 需要導入模塊: from sprites import Sprites [as 別名]
# 或者: from sprites.Sprites import redraw_sprites [as 別名]
#.........這裏部分代碼省略.........
else:
if self.mode == 'percents':
nseg = 10
else:
nseg = self._challenges[self._n][1]
# generate new bar on demand
self._current_bar = self.bar.get_bar(nseg)
self.bar.show_bar(nseg)
def _easter_egg_test(self):
''' Test to see if we show the Easter Egg '''
delta = self.ball.width() / 8
x = self.ball.ball_x() + self.ball.width() / 2
f = self.bar.width() * self._easter_egg / 100.
if x > f - delta and x < f + delta:
return True
else:
return False
def _test(self, easter_egg=False):
''' Test to see if we estimated correctly '''
self._timeout = None
if self._expert:
delta = self.ball.width() / 6
else:
delta = self.ball.width() / 3
x = self.ball.ball_x() + self.ball.width() / 2
f = int(self._fraction * self.bar.width())
self.bar.mark.move((int(f - self.bar.mark_width() / 2),
int(self.bar.bar_y() + self._mark_offset(f))))
if self._challenges[self._n][2] == 0: # label the column
spr = Sprite(self._sprites, 0, 0, self.blank_graphic)
spr.set_label(self._label)
spr.move((int(self._n * 27), 0))
spr.set_layer(-1)
self._challenges[self._n][2] += 1
if x > f - delta and x < f + delta:
spr = Sprite(self._sprites, 0, 0, self.smiley_graphic)
self._correct += 1
GObject.idle_add(play_audio_from_file, self, self._path_to_success)
else:
spr = Sprite(self._sprites, 0, 0, self.frown_graphic)
GObject.idle_add(play_audio_from_file, self, self._path_to_failure)
spr.move((int(self._n * 27), int(self._challenges[self._n][2] * 27)))
spr.set_layer(-1)
# after enough correct answers, up the difficulty
if self._correct == len(self._challenges) * 2:
self._challenge += 1
if self._challenge < len(CHALLENGES):
for challenge in CHALLENGES[self._challenge]:
self._challenges.append(challenge)
else:
self._expert = True
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 = Gdk.keyval_name(event.keyval)
if k in ['KP_Page_Down', 'KP_Home', 'h', 'Left', 'KP_Left']:
self._dx = -DX * self._scale
elif k in ['KP_Page_Up', 'KP_End', 'l', 'Right', 'KP_Right']:
self._dx = DX * self._scale
elif k in ['Return']:
self._choose_a_fraction()
self._move_ball()
else:
self._dx = 0.
if self._accel_flip:
self._dx = -self._dx
return True
def _keyrelease_cb(self, area, event):
''' Keyrelease: stop horizontal movement '''
self._dx = 0.
return True
def __draw_cb(self, canvas, cr):
self._sprites.redraw_sprites(cr=cr)
def do_expose_event(self, event=None):
''' Handle the expose-event by drawing '''
# Restrict Cairo to the exposed area
cr = self._activity.get_window().cairo_create()
if event is None:
cr.rectangle(0, 0, Gdk.Screen.width(), Gdk.Screen.height())
else:
cr.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
cr.clip()
self._sprites.redraw_sprites(cr=cr)
def _destroy_cb(self, win, event):
''' Callback to handle quit '''
Gtk.main_quit()