本文整理汇总了Python中sprites.Sprites.find_sprite方法的典型用法代码示例。如果您正苦于以下问题:Python Sprites.find_sprite方法的具体用法?Python Sprites.find_sprite怎么用?Python Sprites.find_sprite使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sprites.Sprites
的用法示例。
在下文中一共展示了Sprites.find_sprite方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Yupana
# 需要导入模块: from sprites import Sprites [as 别名]
# 或者: from sprites.Sprites import find_sprite [as 别名]
#.........这里部分代码省略.........
self._dots[o - 1].set_label(str(self.custom[4]))
if self.we_are_sharing:
_logger.debug('sending a new yupana')
self._parent.send_new_yupana()
def restore_yupana(self, dot_list):
''' Restore a yumpana from the Journal or share '''
for i, dot in enumerate(dot_list):
self._dots[i].type = dot
self._dots[i].set_shape(self._new_dot(
self._colors[self._dots[i].type]))
if self._dots[i].type == 1:
self._sum += self._calc_bead_value(i)
self._set_label(str(self._sum))
def save_yupana(self):
''' Return dot list and orientation for saving to Journal or
sharing '''
dot_list = []
for dot in self._dots:
dot_list.append(dot.type)
return [self._mode, dot_list]
def _set_label(self, string):
''' Set the label in the toolbar or the window frame. '''
self._number_box.set_label(string)
# self._activity.status.set_label(string)
def _button_press_cb(self, win, event):
win.grab_focus()
x, y = map(int, event.get_coords())
spr = self._sprites.find_sprite((x, y))
if spr == None:
return
if spr.type is not None:
spr.type += 1
spr.type %= 2
spr.set_shape(self._new_dot(self._colors[spr.type]))
if self.we_are_sharing:
_logger.debug('sending a click to the share')
self._parent.send_dot_click(self._dots.index(spr),
spr.type)
if spr.type == 1:
self._sum += self._calc_bead_value(self._dots.index(spr))
else:
self._sum -= self._calc_bead_value(self._dots.index(spr))
self._set_label(str(self._sum))
return True
def _calc_bead_value(self, i):
''' Calculate a bead value based on the index and the mode '''
e = 5 - i / (TEN + 1)
m = i % 11
if self._mode == 'ten':
return 10 ** e
elif self._mode == 'twenty':
if m in [7, 10]:
return 20 ** e
else:
return (20 ** e) * 2
elif self._mode == 'factor':
示例2: Game
# 需要导入模块: from sprites import Sprites [as 别名]
# 或者: from sprites.Sprites import find_sprite [as 别名]
#.........这里部分代码省略.........
self._dots[n].set_shape(self._new_dot(
self._colors[self._dots[n].type]))
if self.we_are_sharing:
_logger.debug('sending a new game')
self._parent.send_new_game()
def restore_game(self, dot_list, orientation):
''' Restore a game from the Journal or share '''
for i, dot in enumerate(dot_list):
self._dots[i].type = dot
self._dots[i].set_shape(self._new_dot(
self._colors[self._dots[i].type]))
self._orientation = orientation
self._set_orientation()
def save_game(self):
''' Return dot list and orientation for saving to Journal or
sharing '''
dot_list = []
for dot in self._dots:
dot_list.append(dot.type)
return [dot_list, self._orientation]
def _set_label(self, string):
''' Set the label in the toolbar or the window frame. '''
self._activity.status.set_label(string)
def _button_press_cb(self, win, event):
win.grab_focus()
x, y = map(int, event.get_coords())
self._press = True
spr = self._sprites.find_sprite((x, y))
if spr == None:
return True
self.last_spr = spr
if spr.type is not None:
if not self._timer is None:
GObject.source_remove(self._timer)
self._increment_dot(spr)
return True
def _button_release_cb(self, win, event):
self._press = False
if not self._timer is None:
GObject.source_remove(self._timer)
def _increment_dot(self, spr):
spr.type += 1
if self.roygbiv:
if spr.type >= len(self._colors):
spr.type = 2
else:
spr.type %= 4
spr.set_shape(self._new_dot(self._colors[spr.type]))
if self.playing_with_robot:
self._robot_play(spr)
self._test_game_over()
if self.we_are_sharing:
_logger.debug('sending a click to the share')
self._parent.send_dot_click(self._dots.index(spr), spr.type)
示例3: Game
# 需要导入模块: from sprites import Sprites [as 别名]
# 或者: from sprites.Sprites import find_sprite [as 别名]
#.........这里部分代码省略.........
self.its_my_turn()
elif self._initiating():
self.whos_turn += 1
if self.whos_turn == len(self.buddies):
self.whos_turn = 0
else:
self.its_their_turn(self.buddies[self.whos_turn])
self._activity.send_event('t|%s' % (
self.buddies[self.whos_turn]))
def _robot_turn(self):
self._robot_play()
self.show_connected_tiles()
if not self._waiting_for_robot:
self.its_my_turn()
def its_their_turn(self, nick):
# It is someone else's turn.
if self._running_sugar:
if not self.playing_with_robot:
self._activity.set_player_on_toolbar(nick)
self._activity.dialog_button.set_icon('media-playback-stop')
self._activity.dialog_button.set_tooltip(_('Wait your turn.'))
self._set_label(_('Waiting for') + ' ' + nick)
self._waiting_for_my_turn = True # I am still waiting.
def _button_press_cb(self, win, event):
win.grab_focus()
x, y = map(int, event.get_coords())
self._dragpos = [x, y]
self._total_drag = [0, 0]
spr = self._sprites.find_sprite((x, y))
# If it is not my turn, do nothing.
if self._waiting_for_my_turn:
self._press = None
return
self._release = None
# Ignore clicks on background except to indicate you took your turn
if spr is None or spr in self.grid.blanks or spr == self.deck.board:
if self.placed_a_tile and spr is None:
self.took_my_turn()
self._press = None
return True
# Are we clicking on a tile in the hand?
if self.hands[self._my_hand].spr_to_hand(spr) is not None and \
not self._there_are_errors:
self.last_spr_moved = spr
clicked_in_hand = True
if self.placed_a_tile:
self._press = None
self.took_my_turn()
else:
clicked_in_hand = False
# We cannot switch to an old tile.
if spr == self.last_spr_moved:
self._press = spr
spr.set_layer(TOP)
self._show_highlight()
示例4: Game
# 需要导入模块: from sprites import Sprites [as 别名]
# 或者: from sprites.Sprites import find_sprite [as 别名]
#.........这里部分代码省略.........
meal = []
for i in range(n[0]): # Sweets
j = int(uniform(0, len(self._my_plate[0])))
meal.append(self._my_plate[0][j])
for i in range(n[1]): # Dairy
j = int(uniform(0, len(self._my_plate[1])))
meal.append(self._my_plate[1][j])
for i in range(n[2]): # Protein and Fruits
j = int(uniform(0, len(self._my_plate[2])))
meal.append(self._my_plate[2][j])
for i in range(n[3]): # Veggies and Grains
j = int(uniform(0, len(self._my_plate[3])))
meal.append(self._my_plate[3][j])
if n[0] < 2 and n[1] < 2 and n[2] < n[3]:
self._target = 0 # Balanced meal
else:
self._target = 1
for i in range(6):
if self.food_cards[meal[i]] is None:
self.allocate_food(meal[i])
# Randomly position small cards
self._small_picture_cards[meal[3] * 6].set_layer(100)
self._small_picture_cards[meal[4] * 6 + 1].set_layer(100)
self._small_picture_cards[meal[1] * 6 + 2].set_layer(100)
self._small_picture_cards[meal[2] * 6 + 3].set_layer(100)
self._small_picture_cards[meal[5] * 6 + 4].set_layer(100)
self._small_picture_cards[meal[0] * 6 + 5].set_layer(100)
def _button_press_cb(self, win, event):
win.grab_focus()
x, y = map(int, event.get_coords())
spr = self._sprites.find_sprite((x, y))
if spr == None:
return
# We only care about clicks on word cards
if type(spr.type) != int:
return
# Which card was clicked? Set its label to red.
spr.set_label_color('red')
label = spr.labels[0]
spr.set_label(label)
if self.level == 0:
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')
示例5: Game
# 需要导入模块: from sprites import Sprites [as 别名]
# 或者: from sprites.Sprites import find_sprite [as 别名]
#.........这里部分代码省略.........
_logger.debug('sending a new game')
self._parent.send_new_game()
def restore_game(self, dot_list, move_list):
''' Restore a game from the Journal or share '''
edge = int(sqrt(len(dot_list)))
if edge > MAX:
edge = MAX
while self._edge < edge:
self.more_dots()
for i, dot in enumerate(dot_list):
self._dots[i].type = dot
self._dots[i].set_shape(self._new_dot(
self._colors[self._dots[i].type]))
if move_list is not None:
self._move_list = move_list[:]
def save_game(self):
''' Return dot list, move_list for saving to Journal or
sharing '''
dot_list = []
for dot in self._dots:
dot_list.append(dot.type)
return (dot_list, self._move_list)
def _set_label(self, string):
''' Set the label in the toolbar or the window frame. '''
self._activity.status.set_label(string)
def _button_press_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:
return
if spr.type is not None:
self._flip_them(self._dots.index(spr))
self._test_game_over()
if self.we_are_sharing:
_logger.debug('sending a click to the share')
self._parent.send_dot_click(self._dots.index(spr))
return True
def solve(self):
''' Solve the puzzle by undoing moves '''
if self._move_list == []:
return
self._flip_them(self._move_list.pop(), append=False)
GObject.timeout_add(750, self.solve)
def _flip_them(self, dot, append=True):
''' flip the dot and its neighbors '''
if append:
self._move_list.append(dot)
x, y = self._dot_to_grid(dot)
self._flip(self._dots[dot])
if x > 0:
self._flip(self._dots[dot - 1])
if y > 0:
self._flip(self._dots[dot - self._edge])
if x < self._edge - 1:
self._flip(self._dots[dot + 1])
if y < self._edge - 1:
示例6: Game
# 需要导入模块: from sprites import Sprites [as 别名]
# 或者: from sprites.Sprites import find_sprite [as 别名]
#.........这里部分代码省略.........
neighbors.append(self._dots[self._grid_to_dot((x + 1, y + 1))])
return neighbors
def _count(self, count_type, spr):
''' Count the number of surrounding dots of type count_type '''
counter = 0
for dot in self._neighbors(spr):
if dot.type in count_type:
counter += 1
return counter
def _floodfill(self, old_type, spr):
if spr.type not in old_type:
return
spr.type = 0
spr.set_shape(self._new_dot(self._colors[spr.type]))
if self.we_are_sharing:
_logger.debug('sending a click to the share')
self._parent.send_dot_click(self._dots.index(spr), spr.type)
counter = self._count([2, 4], spr)
if counter > 0:
spr.set_label(str(counter))
else:
spr.set_label('')
for dot in self._neighbors(spr):
self._floodfill(old_type, dot)
def _button_press_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:
return
if event.button > 1: # right click
if spr.type != 0:
self._flip_the_cookie(spr)
return True
else:
if spr.type != 0:
red, green, blue, alpha = spr.get_pixel((x, y))
if red > 190 and red < 215: # clicked the cookie
self._flip_the_cookie(spr)
return True
if spr.type in [2, 4]:
spr.set_shape(self._new_dot(self._colors[4]))
self._frown()
return True
if spr.type is not None:
self._floodfill([1, 3], spr)
self._test_game_over()
return True
def _flip_the_cookie(self, spr):
if spr.type in [1, 2]:
spr.set_shape(self._new_dot(self._colors[2]))
spr.type += 2
else: # elif spr.type in [3, 4]:
spr.set_shape(self._new_dot(self._colors[1]))
spr.type -= 2
示例7: Game
# 需要导入模块: from sprites import Sprites [as 别名]
# 或者: from sprites.Sprites import find_sprite [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()
示例8: Game
# 需要导入模块: from sprites import Sprites [as 别名]
# 或者: from sprites.Sprites import find_sprite [as 别名]
#.........这里部分代码省略.........
self._dots[i].move((x, y))
def get_dot_xy(self, i):
return self._dots[i].get_xy()
def move_xo_man(self, x, y):
self._xo_man.move((x, y))
def get_xo_man_xy(self):
return self._xo_man.get_xy()
def rotate(self):
x, y = self._dots[0].get_xy()
for i in range(len(colors) - 1):
self._dots[i].move(self._dots[i + 1].get_xy())
self._dots[-1].move((x, y))
def _generate_bg(self, color):
''' a background color '''
self._bg = Sprite(self._sprites, 0, 0, self._new_background(color))
self._bg.set_layer(0)
self._bg.type = None
def adj_background(self, color):
''' Change background '''
self._bg.set_image(self._new_background(color))
self._bg.set_layer(0)
def _button_press_cb(self, win, event):
win.grab_focus()
x, y = map(int, event.get_coords())
self.dragpos = [x, y]
spr = self._sprites.find_sprite((x, y))
if spr == None or spr == self._bg:
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)
示例9: __init__
# 需要导入模块: from sprites import Sprites [as 别名]
# 或者: from sprites.Sprites import find_sprite [as 别名]
#.........这里部分代码省略.........
self._opts[i].set_layer(100)
def restore_game(self, dot_list, correct=0, level=3, game=0):
""" Restore a game from the Journal or share """
# TODO: Save/restore recall list for game 2
self._correct = correct
self._level = level
self._game = game
for i, dot in enumerate(dot_list):
self._dots[i].type = dot
if dot == -1:
self._dots[i].hide()
self._new_game(restore=True)
def save_game(self):
""" Return dot list for saving to Journal or sharing """
dot_list = []
for dot in self._dots:
dot_list.append(dot.type)
return dot_list, self._correct, self._level, self._game
def _set_label(self, string):
""" Set the label in the toolbar or the window frame. """
self._parent.status.set_label(string)
def _button_press_cb(self, win, event):
if self._timeout_id is not None:
_logger.debug("still in timeout... ignoring click")
return
win.grab_focus()
x, y = map(int, event.get_coords())
spr = self._sprites.find_sprite((x, y), inverse=True)
if spr == None:
return
if self._game in [0, 1, 3]:
for i in range(3):
if self._opts[i] == spr:
break
self._opts[i].set_shape(self._new_dot_surface(color=self._colors[0]))
if i == self._answer:
self._opts[i].set_label("☻")
self._correct += 1
else:
self._opts[i].set_label("☹")
self._correct = 0
else:
for i in range(3):
if self._opts[i] == spr:
break
self._opts[i].set_shape(self._new_dot_surface(color=self._colors[0]))
if self._opts[i].type == self._recall_list[self._answer]:
self._opts[i].set_label("☻")
self._correct += 1
else:
self._opts[i].set_label("☹")
self._correct = 0
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]))
示例10: Game
# 需要导入模块: from sprites import Sprites [as 别名]
# 或者: from sprites.Sprites import find_sprite [as 别名]
class Game():
""" The game play -- called from within Sugar or GNOME """
def __init__(self, canvas, path, parent=None):
""" Initialize the playing surface """
self.path = path
self.activity = parent
# starting from command line
# we have to do all the work that was done in CardSortActivity.py
if parent is None:
self.sugar = False
self.canvas = canvas
# starting from Sugar
else:
self.sugar = True
self.canvas = canvas
parent.show_all()
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.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.width = gtk.gdk.screen_width()
self.height = gtk.gdk.screen_height() - GRID_CELL_SIZE
self.card_dim = CARD_DIM
self.scale = 0.6 * self.height / (self.card_dim * 3)
# Initialize the sprite repository
self.sprites = Sprites(self.canvas)
# Initialize the grid
self.mode = 'rectangle'
self.grid = Grid(self, self.mode)
self.bounds = LEVEL_BOUNDS[0]
self.level = 0
# Start solving the puzzle
self.press = None
self.release = None
self.start_drag = [0, 0]
def _button_press_cb(self, win, event):
win.grab_focus()
x, y = map(int, event.get_coords())
self.start_drag = [x, y]
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 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 '''
#.........这里部分代码省略.........
示例11: BBoardActivity
# 需要导入模块: from sprites import Sprites [as 别名]
# 或者: from sprites.Sprites import find_sprite [as 别名]
#.........这里部分代码省略.........
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
def _button_press_cb(self, win, event):
''' The mouse button was pressed. Is it on a thumbnail sprite? '''
win.grab_focus()
x, y = map(int, event.get_coords())
self._dragpos = [x, y]
self._total_drag = [0, 0]
spr = self._sprites.find_sprite((x, y))
self._press = None
self._release = None
# Are we clicking on a thumbnail?
if not self._spr_is_thumbnail(spr):
return False
self.last_spr_moved = spr
self._press = spr
self._press.set_layer(DRAG)
return False
def _mouse_move_cb(self, win, event):
""" Drag a thumbnail with the mouse. """
spr = self._press
if spr is None:
self._dragpos = [0, 0]
return False
win.grab_focus()
x, y = map(int, event.get_coords())
dx = x - self._dragpos[0]
dy = y - self._dragpos[1]
spr.move_relative([dx, dy])
# Also move the star
self._dragpos = [x, y]
self._total_drag[0] += dx
self._total_drag[1] += dy
return False
def _button_release_cb(self, win, event):
''' Button event is used to swap slides or goto next slide. '''
win.grab_focus()
示例12: Game
# 需要导入模块: from sprites import Sprites [as 别名]
# 或者: from sprites.Sprites import find_sprite [as 别名]
#.........这里部分代码省略.........
dot.set_shape(self._new_dot(self._colors[FILL]))
dot.set_label('')
# Recenter the turtle
self._move_turtle(self._dots[int(THIRTEEN * THIRTEEN / 2)].get_xy())
self._turtle.set_shape(self._turtle_images[0])
self._set_label('')
if self._timeout_id is not None:
GObject.source_remove(self._timeout_id)
def new_game(self, saved_state=None):
''' Start a new game. '''
self._all_clear()
# Fill in a few dots to start
for i in range(15):
n = int(uniform(0, THIRTEEN * THIRTEEN))
if self._dots[n].type is not None:
self._dots[n].type = True
self._dots[n].set_shape(self._new_dot(self._colors[STROKE]))
# Calculate the distances to the edge
self._initialize_weights()
self.strategy = self.strategies[self.level]
def _set_label(self, string):
''' Set the label in the toolbar or the window frame. '''
self._activity.status.set_label(string)
def _button_press_cb(self, win, event):
win.grab_focus()
x, y = map(int, event.get_coords())
spr = self._sprites.find_sprite((x, y), inverse=True)
if spr == None:
return
if spr.type is not None and not spr.type:
spr.type = True
spr.set_shape(self._new_dot(self._colors[STROKE]))
self._weights[self._dots.index(spr)] = 1000
self._test_game_over(self._move_the_turtle())
return True
def _find_the_turtle(self):
turtle_pos = self._turtle.get_xy()
turtle_dot = None
for dot in self._dots:
pos = dot.get_xy()
# Turtle is offset
if pos[0] == turtle_pos[0] + self._turtle_offset and \
pos[1] == turtle_pos[1] + self._turtle_offset:
turtle_dot = self._dots.index(dot)
break
if turtle_dot is None:
_logger.debug('Cannot find the turtle...')
return None
return turtle_dot
def _move_the_turtle(self):
''' Move the turtle after each click '''
self._turtle_dot = self._find_the_turtle()
if self._turtle_dot is None:
return
# Given the col and row of the turtle, do something
示例13: Bounce
# 需要导入模块: from sprites import Sprites [as 别名]
# 或者: from sprites.Sprites import find_sprite [as 别名]
#.........这里部分代码省略.........
self._activity.set_player_on_toolbar(self._activity.nick)
self._activity.reset_label(
_("Click on the bar to choose a fraction."))
def its_their_turn(self, nick):
''' When sharing, it is nick's turn... '''
GObject.timeout_add(1000, self._wait_your_turn, nick)
def _wait_your_turn(self, nick):
''' Wait for nick to choose a fraction. '''
self._my_turn = False
self._activity.set_player_on_toolbar(nick)
self._activity.reset_label(
_('Waiting for %(buddy)s') % {'buddy': nick})
def play_a_fraction(self, fraction):
''' Play this fraction '''
fraction_is_new = True
for i, c in enumerate(self._challenges):
if c[0] == fraction:
fraction_is_new = False
self._n = i
break
if fraction_is_new:
self.add_fraction(fraction)
self._n = len(self._challenges)
self._choose_a_fraction()
self._move_ball()
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.we_are_sharing():
if self.select_a_fraction and self._press == self._current_bar:
# Find the fraction closest to the click
fraction = self._search_challenges(
(x - self.bar.bar_x()) / float(self.bar.width()))
self.select_a_fraction = False
self._activity.send_a_fraction(fraction)
self.play_a_fraction(fraction)
else:
if self._timeout is None and self._press == self.ball.ball:
self._choose_a_fraction()
self._move_ball()
return True
def _search_challenges(self, f):
''' Find the fraction which is closest to f in the list. '''
dist = 1.
closest = '1/2'
for c in self._challenges:
numden = c[0].split('/')
delta = abs((float(numden[0]) / float(numden[1])) - f)
if delta <= dist:
dist = delta
closest = c[0]
return closest