本文整理汇总了Python中sprites.Sprite.move_relative方法的典型用法代码示例。如果您正苦于以下问题:Python Sprite.move_relative方法的具体用法?Python Sprite.move_relative怎么用?Python Sprite.move_relative使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sprites.Sprite
的用法示例。
在下文中一共展示了Sprite.move_relative方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Stator
# 需要导入模块: from sprites import Sprite [as 别名]
# 或者: from sprites.Sprite import move_relative [as 别名]
class Stator():
""" Create a sprite for a stator """
def __init__(self, sprites, path, name, x, y, w, h, svg_engine=None,
calculate=None, result=None):
if svg_engine is None:
self.spr = Sprite(sprites, x, y, file_to_pixbuf(path, name, w, h))
else:
self.spr = Sprite(sprites, x, y,
svg_str_to_pixbuf(svg_engine().svg))
self.spr.type = name
self.name = name
self.calculate = calculate
self.result = result
def draw(self, layer=1000):
self.spr.set_layer(layer)
def match(self, sprite):
if self.spr == sprite:
return True
return False
def move(self, dx, dy):
self.spr.move((dx, dy))
def move_relative(self, dx, dy):
self.spr.move_relative((dx, dy))
def hide(self):
self.spr.hide()
示例2: Slide
# 需要导入模块: from sprites import Sprite [as 别名]
# 或者: from sprites.Sprite import move_relative [as 别名]
class Slide(Stator):
""" Create a sprite for a slide """
def __init__(self, sprites, path, name, x, y, w, h, svg_engine=None,
function=None):
if svg_engine is None:
self.spr = Sprite(sprites, x, y, file_to_pixbuf(path, name, w, h))
else:
self.spr = Sprite(sprites, x, y,
svg_str_to_pixbuf(svg_engine().svg))
self.tab_dx = [0, SWIDTH - TABWIDTH]
self.tab_dy = [2 * SHEIGHT, 2 * SHEIGHT]
self.tabs = []
self.tabs.append(Tab(sprites, path, 'tab', x + self.tab_dx[0],
y + self.tab_dy[0], TABWIDTH, SHEIGHT))
self.tabs.append(Tab(sprites, path, 'tab', x + self.tab_dx[1],
y + self.tab_dy[1], TABWIDTH, SHEIGHT))
self.calculate = function
self.name = name
def add_textview(self, textview, i=0):
self.tabs[i].textview = textview
self.tabs[i].textbuffer = textview.get_buffer()
def set_fixed(self, fixed):
for tab in self.tabs:
tab.fixed = fixed
def match(self, sprite):
if sprite == self.spr or sprite == self.tabs[0].spr or \
sprite == self.tabs[1].spr:
return True
return False
def draw(self, layer=1000):
self.spr.set_layer(layer)
self.spr.draw()
for tab in self.tabs:
tab.draw()
def move(self, dx, dy):
self.spr.move((dx, dy))
for i, tab in enumerate(self.tabs):
tab.move(dx + self.tab_dx[i], dy + self.tab_dy[i])
def move_relative(self, dx, dy):
self.spr.move_relative((dx, dy))
for i, tab in enumerate(self.tabs):
tab.move_relative(dx, dy)
def hide(self):
self.spr.hide()
for tab in self.tabs:
tab.hide()
def label(self, label, i=0):
self.tabs[i].label(label)
示例3: Tab
# 需要导入模块: from sprites import Sprite [as 别名]
# 或者: from sprites.Sprite import move_relative [as 别名]
class Tab():
""" Create tabs for the slide; include a TextView for OSK input """
def __init__(self, sprites, path, name, x, y, w, h):
self.spr = Sprite(sprites, x, y, file_to_pixbuf(path, name, w, h))
self.spr.label = "1.0"
self.spr.type = name
self.name = name
self.width = w
self.textview = None
self.textbuffer = None
self.fixed = None
self.textview_y_offset = 0
def label(self, label):
if self.textbuffer is not None:
self.textbuffer.set_text(label)
def _move_textview(self, x, y):
y += self.textview_y_offset
if self.textview is not None:
if x > 0 and x < Gdk.Screen.width() - self.width and y > 0:
self.fixed.move(self.textview, x, y)
self.textview.show()
else:
self.textview.hide()
def move(self, x, y):
self.spr.move((x, y))
self._move_textview(x, y)
def move_relative(self, dx, dy):
self.spr.move_relative((dx, dy))
x, y = self.spr.get_xy()
self._move_textview(x, y)
def draw(self, layer=100):
self.spr.set_layer(layer)
self.spr.draw()
x, y = self.spr.get_xy()
self._move_textview(x, y)
def hide(self):
self.spr.hide()
示例4: Ball
# 需要导入模块: from sprites import Sprite [as 别名]
# 或者: from sprites.Sprite import move_relative [as 别名]
class Ball():
''' The Bounce class is used to define the ball and the user
interaction. '''
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 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_image(self, filename, save_path):
''' Just create a ball object from an image file '''
if filename == '':
_logger.debug('Image file not found.')
return
try:
pixbuf = GdkPixbuf.Pixbuf.new_from_file(filename)
if pixbuf.get_width() > pixbuf.get_height():
size = pixbuf.get_height()
x = int((pixbuf.get_width() - size) / 2)
else:
size = pixbuf.get_width()
x = int((pixbuf.get_height() - size) / 2)
crop = GdkPixbuf.Pixbuf.new(0, True, 8, size, size)
pixbuf.copy_area(x, 0, size, size, crop, 0, 0)
scale = crop.scale_simple(85, 85, GdkPixbuf.InterpType.BILINEAR)
scale.savev(save_path, 'png', [], [])
self.ball.set_shape(
svg_str_to_pixbuf(generate_ball_svg(save_path)))
except Exception as e:
_logger.error('Could not load image from %s: %s' % (filename, e))
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 ball_x(self):
return self.ball.get_xy()[0]
def ball_y(self):
return self.ball.get_xy()[1]
def frame_x(self, i):
return self._frames[i].get_xy()[0]
def frame_y(self, i):
return self._frames[i].get_xy()[1]
def width(self):
return self.ball.rect[2]
def height(self):
return self.ball.rect[3]
def move_ball(self, pos):
self.ball.move(pos)
def move_ball_relative(self, pos):
self.ball.move_relative(pos)
def move_frame(self, i, pos):
self._frames[i].move(pos)
def move_frame_relative(self, i, pos):
self._frames[i].move_relative(pos)
def hide_frames(self):
for frame in self._frames:
#.........这里部分代码省略.........
示例5: Game
# 需要导入模块: from sprites import Sprite [as 别名]
# 或者: from sprites.Sprite import move_relative [as 别名]
class Game():
def __init__(self, canvas, parent=None, colors=['#A0FFA0', '#FF8080']):
self._activity = parent
self._colors = colors
self._canvas = canvas
parent.show_all()
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 * 1.5)
self._scale = self._height / (14.0 * DOT_SIZE * 1.2)
self._dot_size = int(DOT_SIZE * self._scale)
self._turtle_offset = 0
self._space = int(self._dot_size / 5.)
self._orientation = 0
self.level = 0
self.custom_strategy = None
self.strategies = [BEGINNER_STRATEGY, INTERMEDIATE_STRATEGY,
EXPERT_STRATEGY, self.custom_strategy]
self.strategy = self.strategies[self.level]
self._timeout_id = None
# Generate the sprites we'll need...
self._sprites = Sprites(self._canvas)
self._dots = []
for y in range(THIRTEEN):
for x in range(THIRTEEN):
xoffset = int((self._width - THIRTEEN * (self._dot_size + \
self._space) - self._space) / 2.)
if y % 2 == 1:
xoffset += int((self._dot_size + self._space) / 2.)
if x == 0 or y == 0 or x == THIRTEEN - 1 or y == THIRTEEN - 1:
self._dots.append(
Sprite(self._sprites,
xoffset + x * (self._dot_size + self._space),
y * (self._dot_size + self._space),
self._new_dot('#B0B0B0')))
else:
self._dots.append(
Sprite(self._sprites,
xoffset + x * (self._dot_size + self._space),
y * (self._dot_size + self._space),
self._new_dot(self._colors[FILL])))
self._dots[-1].type = False # not set
# Put a turtle at the center of the screen...
self._turtle_images = []
self._rotate_turtle(self._new_turtle())
self._turtle = Sprite(self._sprites, 0, 0,
self._turtle_images[0])
self._move_turtle(self._dots[int(THIRTEEN * THIRTEEN / 2)].get_xy())
# ...and initialize.
self._all_clear()
def _move_turtle(self, pos):
''' Move turtle and add its offset '''
self._turtle.move(pos)
self._turtle.move_relative(
(-self._turtle_offset, -self._turtle_offset))
def _all_clear(self):
''' Things to reinitialize when starting up a new game. '''
# Clear dots
for dot in self._dots:
if dot.type:
dot.type = False
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)
#.........这里部分代码省略.........
示例6: Bar
# 需要导入模块: from sprites import Sprite [as 别名]
# 或者: from sprites.Sprite import move_relative [as 别名]
class Bar():
''' The Bar class is used to define the bars at the bottom of the
screen '''
def __init__(self, sprites, ball_size, colors=['#FFFFFF', '#AAAAAA']):
''' Initialize the 2-segment bar, labels, and mark '''
self._sprites = sprites
self._colors = colors[:]
self.bars = {}
self._width = Gdk.Screen.width()
self._height = Gdk.Screen.height() - style.GRID_CELL_SIZE
self._scale = Gdk.Screen.height() / 900.0
self._ball_size = ball_size
self.make_bar(2)
self._make_wedge_mark()
def resize_all(self):
self._width = Gdk.Screen.width()
self._height = Gdk.Screen.height() - style.GRID_CELL_SIZE
self._scale = Gdk.Screen.height() / 900.0
for bar in self.bars.keys():
self.bars[bar].hide()
self.mark.hide()
for bar in self.bars.keys():
self.make_bar(bar)
self._make_wedge_mark()
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_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 mark_width(self):
return self.mark.rect[2]
def bar_x(self):
return self.bars[2].get_xy()[0]
def bar_y(self):
if self.bars[2].get_xy()[1] < 0:
return self.bars[2].get_xy()[1] + 3000
else:
return self.bars[2].get_xy()[1]
def width(self):
return self.bars[2].rect[2]
def height(self):
return self.bars[2].rect[3]
def show_bar(self, n):
if n in self.bars:
self.bars[n].move([self.bar_x(), self.bar_y()])
def bump_bars(self, direction='up'):
''' when the toolbars expand and contract, we need to move the bar '''
if direction == 'up':
dy = -style.GRID_CELL_SIZE
else:
dy = style.GRID_CELL_SIZE
for bar in self.bars:
self.bars[bar].move_relative([0, dy])
#.........这里部分代码省略.........