本文整理汇总了Python中kivy.graphics.instructions.InstructionGroup.add方法的典型用法代码示例。如果您正苦于以下问题:Python InstructionGroup.add方法的具体用法?Python InstructionGroup.add怎么用?Python InstructionGroup.add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kivy.graphics.instructions.InstructionGroup
的用法示例。
在下文中一共展示了InstructionGroup.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import add [as 别名]
def __init__(self, *args, **kwargs):
super(LevelProgressBar, self).__init__(*args, **kwargs)
texture = Texture.create(size=(1, 16))
size = 1 * 16 * 3
buf = [
int(Color(
.66 - (float(data) / size) * .66,
.75,
.75,
mode='hsv'
).rgb[data % 3] * 255) for data in range(size)
]
buf = b''.join(map(chr, buf))
texture.blit_buffer(buf, colorfmt='rgb', bufferfmt='ubyte')
self.progress_bar = Rectangle(texture=texture)
self.progress_mask = Rectangle()
group = InstructionGroup()
group.add(Color(0, 0, 0))
group.add(self.progress_mask)
self.canvas.add(Color(1, 1, 1))
self.canvas.add(self.progress_bar)
self.canvas.add(group)
self.bind(pos=self.redraw, size=self.redraw)
示例2: add_explosion
# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import add [as 别名]
def add_explosion(self, line):
line += self.currently_exploding
self.currently_exploding += 1
explosion = InstructionGroup()
color = Color(1, 1, .5, .8)
explosion.add(color)
explosion.add(Rectangle(
pos=self.coord_to_pos(0, line),
size=(
self.tile_size()[0] * self.cols,
self.tile_size()[1]
)
))
self.canvas.add(explosion)
def remove(self):
self.canvas.remove(explosion)
self.currently_exploding -= 1
anim = Animation(
a=0,
duration=.125
)
anim.bind(on_complete=lambda *args: remove(self))
anim.start(color)
示例3: Renderer
# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import add [as 别名]
class Renderer(Widget):
def __init__(self, **kw):
self.shader_file = kw.pop("shader_file", None)
self.canvas = Canvas()
super(Renderer, self).__init__(**kw)
with self.canvas:
self._viewport = Rectangle(size=self.size, pos=self.pos)
self.fbo = Fbo(size=self.size,
with_depthbuffer=True, compute_normal_mat=True)
self._config_fbo()
self.texture = self.fbo.texture
self.camera = None
self.scene = None
def _config_fbo(self):
# set shader file here
self.fbo.shader.source = self.shader_file or \
os.path.join(kivy3_path, "default.glsl")
with self.fbo:
Callback(self._setup_gl_context)
PushMatrix()
# instructions set for all instructions
self._instructions = InstructionGroup()
PopMatrix()
Callback(self._reset_gl_context)
def _setup_gl_context(self, *args):
glEnable(GL_DEPTH_TEST)
self.fbo.clear_buffer()
def _reset_gl_context(self, *args):
glDisable(GL_DEPTH_TEST)
def render(self, scene, camera):
self.scene = scene
self.camera = camera
self.camera.bind_to(self)
self._instructions.add(scene.as_instructions())
Clock.schedule_once(self._update_matrices, -1)
def on_size(self, instance, value):
self.fbo.size = value
self._viewport.texture = self.fbo.texture
self._viewport.size = value
self._update_matrices()
def on_texture(self, instance, value):
self._viewport.texture = value
def _update_matrices(self, dt=None):
if self.camera:
self.fbo['projection_mat'] = self.camera.projection_matrix
self.fbo['modelview_mat'] = self.camera.modelview_matrix
else:
raise RendererError("Camera is not defined for renderer")
def set_clear_color(self, color):
self.fbo.clear_color = color
示例4: put_pixel
# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import add [as 别名]
def put_pixel(x, y, color, canvas, token, alpha=None, thickness=2):
r, g, b = color.r, color.g, color.b
c = Color(r, g, b)
if alpha:
c.a = alpha
group = InstructionGroup(group=token)
group.add(c)
group.add(Rectangle(pos=(x, y), size=(thickness, thickness)))
canvas.add(group)
示例5: __init__
# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import add [as 别名]
def __init__(self, body, size, texture=None, halfShift=True):
ig = InstructionGroup()
ig.add(Color(1,1,1, 1.0))
ig.add(Rectangle(pos=(0,0), size=size, texture=texture))
offset = None
if halfShift:
offset = [-1.0*s/2.0 for s in size]
super(TextrueRect, self).__init__(body, instruction=ig, offset=offset)
示例6: PaintAreaBackgroundWidget
# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import add [as 别名]
class PaintAreaBackgroundWidget(RelativeLayout):
selectedColor = ListProperty([1,0,0,1])
def __init__(self, *args, **kwargs):
super(PaintAreaBackgroundWidget, self).__init__(*args, **kwargs)
self.lineGroup = None
self.line = None
self.isDrawing = False
def on_touch_down(self, touch):
# only draw if no widget is selected
if self.paintWidget.selectedItem is None:
if self.collide_point(*touch.pos):
self.isDrawing = True
width = self.lineWidth
self.lineGroup = InstructionGroup()
#print self.lineGroup
self.line = Line(points=(touch.x, touch.y),width=width,dash_offset=2)
self.lineGroup.add(Color(*self.selectedColor))
self.lineGroup.add(self.line)
self.canvas.add(self.lineGroup)
def on_touch_move(self, touch):
if self.paintWidget.selectedItem is None:
if self.collide_point(*touch.pos):
self.line.points += [touch.x, touch.y]
def on_touch_up(self, touch):
if self.paintWidget.selectedItem is None:
if self.collide_point(*touch.pos) and len(self.line.points)>1:
self.canvas.remove(self.lineGroup)
lp = numpy.array(self.line.points)
scatterPolyLine = ScatterPolyLineWidget(paintWidget=self.paintWidget,
linePoints=lp, lineWidth=self.lineWidth, lineColor=self.selectedColor)
self.line.points = []
scatterPolyLine.pos = [scatterPolyLine.minX,
scatterPolyLine.minY]
#.size = polyLineWidget.size
self.addPaintedThingsWidget.add_widget(scatterPolyLine)
#self.addedOne = True
self.isDrawing = False
示例7: get_graphics
# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import add [as 别名]
def get_graphics(self, gc, polygons, points_line, rgbFace, closed=False):
'''Return an instruction group which contains the necessary graphics
instructions to draw the respective graphics.
'''
instruction_group = InstructionGroup()
if isinstance(gc.line['dash_list'], tuple):
gc.line['dash_list'] = list(gc.line['dash_list'])
if rgbFace is not None:
if len(polygons.meshes) != 0:
instruction_group.add(Color(*rgbFace))
for vertices, indices in polygons.meshes:
instruction_group.add(Mesh(
vertices=vertices,
indices=indices,
mode=str("triangle_fan")
))
instruction_group.add(Color(*gc.get_rgb()))
if _mpl_1_5 and closed:
points_poly_line = points_line[:-2]
else:
points_poly_line = points_line
if gc.line['width'] > 0:
instruction_group.add(Line(points=points_poly_line,
width=int(gc.line['width'] / 2),
dash_length=gc.line['dash_length'],
dash_offset=gc.line['dash_offset'],
dash_joint=gc.line['joint_style'],
dash_list=gc.line['dash_list']))
return instruction_group
示例8: Sprite
# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import add [as 别名]
class Sprite():
def __init__(self, **kwargs):
self.canvas = InstructionGroup()
self.sizeScalar = 50
self.color = Color(0.0, 0.5, 0.2)
self.centerPos = (MIDDLE_X, MIDDLE_Y)
self.rect = Rectangle(pos=(330, 220), size=(50, 45))
self.setCenterPos((330, 220))
self.repos()
self.canvas.add(self.color)
self.canvas.add(self.rect)
def repos(self):
size = (self.sizeScalar, self.sizeScalar * ASPECT)
self.rect.pos = (self.centerPos[0] - (size[0] / 2), self.centerPos[1] - (size[1] / 2))
self.rect.size = size
def setSizeScalar(self, sizeScalar):
self.sizeScalar = sizeScalar
self.repos()
def setCenterPos(self, centerPos):
self.centerPos = centerPos
self.repos()
def collidesWithLine(self, lineCoords):
halfWidth = self.rect.size[0] / 2
halfHeight = self.rect.size[1] / 2
topLeft = (self.centerPos[0] - halfWidth, self.centerPos[1] + halfHeight)
topRight = (self.centerPos[0] + halfWidth, self.centerPos[1] + halfHeight)
bottomLeft = (self.centerPos[0] - halfWidth, self.centerPos[1] - halfHeight)
bottomRight = (self.centerPos[0] + halfWidth, self.centerPos[1] - halfHeight)
intersection1 = Vector.segment_intersection(topLeft, bottomRight, (lineCoords[0], lineCoords[1]), (lineCoords[2], lineCoords[3]))
intersection2 = Vector.segment_intersection(bottomLeft, topRight, (lineCoords[0], lineCoords[1]), (lineCoords[2], lineCoords[3]))
return True if intersection1 or intersection2 else False
示例9: calc_hillbodies
# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import add [as 别名]
def calc_hillbodies(self, force, planet):
# typecasting problem while crosscompiling
foo = App.get_running_app().config.get('planetapp','showforcemode')
if foo == u'0':
return
if ((force / self.mass) > (self.showforcelimit * 0.0002)):
if not planet.fixed:
if not planet in self.hillbodies:
self.hillbodies.append(planet)
else:
if planet in self.hillbodies:
self.hillbodies.remove(planet)
for dude in self.canvas.children:
if 'InstructionGroup' in type(dude).__name__:
self.canvas.remove(dude)
shit = InstructionGroup()
for body in self.hillbodies:
shit.add(Line(points=(self.center_x,self.center_y,
body.center_x,body.center_y),
width=1,
group=str(self.uid)))
if len(self.hillbodies) > 0:
self.canvas.add(shit)
if self.drawtrajectory and not self.fixed:
shit2 = InstructionGroup()
sunpos = (self.parent.width/2,self.parent.height/2)
trajectory = self.parent.calc_trajectory((self.center_x,self.center_y),
self.velocity,
self.mass, sunpos,
self.parent.sunmass, 1,
1000)
shit2.add(Line(points=trajectory, width=1, group=str(self.uid)))
self.canvas.add(shit2)
else:
pass
示例10: calc_hillbodies
# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import add [as 别名]
def calc_hillbodies(self, force, planet):
# typecasting problem while crosscompiling
foo = Utilitys.get_app_config('planetapp','showforcemode')
if foo == u'0':
return
if ((force / self.mass) > (self.showforcelimit * 0.0002)):
if not planet.fixed:
if not planet in self.hillbodies:
self.hillbodies.append(planet)
else:
if planet in self.hillbodies:
self.hillbodies.remove(planet)
for dude in self.canvas.children:
if 'InstructionGroup' in type(dude).__name__:
self.canvas.remove(dude)
shit = InstructionGroup()
for body in self.hillbodies:
shit.add(Line(points=(self.center_x,self.center_y,
body.center_x,body.center_y),
width=1,
group=str(self.uid)))
if len(self.hillbodies) > 0:
self.canvas.add(shit)
示例11: __setitem__
# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import add [as 别名]
def __setitem__(self, key, cell):
ret = super().__setitem__(key, cell)
group = InstructionGroup()
if cell.food == 0:
group.add(self.color)
group.add(Mesh(vertices=self._mesh_vertices(cell),
indices=list(range(6)),
mode=self.mesh_mode))
else:
group.add(Color(0, 1, 0, 1))
group.add(Mesh(vertices=self._mesh_vertices(cell),
indices=list(range(6)),
mode='triangle_fan'))
self.canvas_groups[key] = group
self.canvas.add(group)
return ret
示例12: draw_point
# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import add [as 别名]
def draw_point(point):
token = str(hash(point))
group = InstructionGroup(group=token)
point.obj.widget.canvas.remove_group(token)
x, y = point.x, point.y
if point.texture is not None:
group.add(
Ellipse(
pos=(x - point.size/2, y - point.size/2),
size=(point.size, point.size),
texture=point.texture
)
)
else:
group.add(POINT_COLOR)
group.add(
Ellipse(
pos=(x - point.size/2, y - point.size/2),
size=(point.size, point.size)
)
)
point.obj.widget.canvas.add(group)
示例13: Beam
# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import add [as 别名]
class Beam():
enemies = []
player1 = None
player2 = None
def __init__(self, player1, player2, **kwargs):
self.canvas = InstructionGroup()
self.beamColor = Color(0.0, 0.0, 0.0, 1.0)
self.beamGroup = InstructionGroup()
self.beamThickness = 40
self.canvas.add(self.beamGroup)
self.player1 = player1
self.player2 = player2
self.beamState = 0
self.isColliding = False
def setKeyReport(self, keyReport):
self.keyReport = keyReport
self.player1.setPlayerKeyReport(keyReport.player1)
self.player2.setPlayerKeyReport(keyReport.player2)
def setIsColliding(self, isColliding):
self.isColliding = isColliding
def reset(self):
self.beamState = 0
self.isColliding = False
def updateBeamState(self):
bothButton1 = self.keyReport.player1.button1 and self.keyReport.player2.button1
bothButton2 = self.keyReport.player1.button2 and self.keyReport.player2.button2
beamState = self.beamState
if not bothButton1 and not bothButton2:
beamState = 0
else:
beamState = 1
isChanged = False
if not beamState == self.beamState:
isChanged = True
self.beamState = beamState
return isChanged
def updateBeam(self, p1Pos, p2Pos):
xDelta = p2Pos[0] - p1Pos[0]
yDelta = p2Pos[1] - p1Pos[1]
distanceSquared = math.pow(xDelta, 2) + math.pow(yDelta, 2)
theta = math.atan2(yDelta, xDelta)
distance = math.sqrt(distanceSquared)
self.beamGroup.clear()
self.beamGroup.add(PushMatrix())
self.beamGroup.add(self.beamColor)
self.beamGroup.add(Translate(p1Pos[0], p1Pos[1], 0))
self.beamGroup.add(Rotate(theta * 180 / math.pi, 0, 0, 1))
self.beamGroup.add(Scale(distance, self.beamThickness, 1))
self.beamGroup.add(Rectangle(pos=(0, -0.5), size=(1, 1)))
self.beamGroup.add(PopMatrix())
def update(self, dt):
beamLineCoords = (self.player2.pos[0], self.player2.pos[1], self.player1.pos[0], self.player1.pos[1])
if self.isColliding:
self.beamColor.r = 0.8
self.beamColor.g = 0.5
self.beamColor.b = 0.3
self.beamColor.a = 1
else:
self.beamColor.r = 0.3
self.beamColor.g = 0.3
self.beamColor.b = 0.3
self.beamColor.a = 1
self.updateBeamState()
if self.beamState == 0:
self.beamThickness = 1
elif self.beamState == 1:
self.beamThickness = 40
self.updateBeam(self.player1.pos, self.player2.pos)
示例14: Object3D
# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import add [as 别名]
class Object3D(EventDispatcher):
"""Base class for all 3D objects in rendered
3D world.
"""
def __init__(self, **kw):
super(Object3D, self).__init__(**kw)
self.name = kw.pop('name', '')
self.children = list()
self.parent = None
self.scale = Scale(1., 1., 1.)
self._position = Vector3(0, 0, 0)
self._rotation = Vector3(0, 0, 0)
self._position.set_change_cb(self.on_pos_changed)
self._rotation.set_change_cb(self.on_angle_change)
# general instructions
self._pop_matrix = PopMatrix()
self._push_matrix = PushMatrix()
self._translate = Translate(*self._position)
self._rotors = {
"x": Rotate(self._rotation.x, 1, 0, 0),
"y": Rotate(self._rotation.y, 0, 1, 0),
"z": Rotate(self._rotation.z, 0, 0, 1),
}
self._instructions = InstructionGroup()
def add(self, *objs):
for obj in objs:
self._add_child(obj)
def _add_child(self, obj):
self.children.append(obj)
obj.parent = self
def _set_position(self, val):
if isinstance(val, Vector3):
self._position = val
else:
self._position = Vector3(val)
self._position.set_change_cb(self.on_pos_changed)
def _get_position(self):
return self._position
position = AliasProperty(_get_position, _set_position)
pos = position # just shortcut
def _set_rotation(self, val):
if isinstance(val, Vector3):
self._rotation = val
else:
self._rotation = Vector3(val)
self._rotation.set_change_cb(self.on_angle_change)
self._rotors["x"].angle = self._rotation.x
self._rotors["y"].angle = self._rotation.y
self._rotors["z"].angle = self._rotation.z
def _get_rotation(self):
return self._rotation
rotation = AliasProperty(_get_rotation, _set_rotation)
rot = rotation
def on_pos_changed(self, coord, v):
""" Some coordinate was changed """
self._translate.xyz = self._position
def on_angle_change(self, axis, angle):
self._rotors[axis].angle = angle
def as_instructions(self):
""" Get instructions set for renderer """
if not self._instructions.children:
self._instructions.add(self._push_matrix)
self._instructions.add(self._translate)
self._instructions.add(self.scale)
for rot in self._rotors.itervalues():
self._instructions.add(rot)
self._instructions.add(UpdateNormalMatrix())
for instr in self.custom_instructions():
self._instructions.add(instr)
for child in self.get_children_instructions():
self._instructions.add(child)
self._instructions.add(self._pop_matrix)
return self._instructions
def custom_instructions(self):
""" Should be overriden in subclasses to provide some extra
instructions
"""
return []
def get_children_instructions(self):
for child in self.children:
yield child.as_instructions()
示例15: Enemy
# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import add [as 别名]
class Enemy():
def __init__(self, enemyType, **kwargs):
self.canvas = InstructionGroup()
self.enemyType = enemyType
self.sprite = Sprite()
if self.enemyType == 'normal':
self.setOffsetTheta(0)
self.sprite.color.r = 0.0
self.sprite.color.g = 0.2
self.sprite.color.b = 0.5
else:
self.setOffsetTheta(math.pi / 2)
self.sprite.color.r = 0.5
self.sprite.color.g = 0.1
self.sprite.color.b = 0.1
self.health = 100
self.pos = (0, 0)
self.velocity = [0, 0]
self.updateAppearance()
self.canvas.add(self.sprite.canvas)
self.shouldRemove = False
def setOffsetTheta(self, offsetTheta):
self.offsetTheta = offsetTheta
self.otcos = math.cos(self.offsetTheta)
self.otsin = math.sin(self.offsetTheta)
def reset(self, isRespawned):
sample = random.random()
theta = math.pi * 2 * sample
speed = 0.1
self.isRespawned = isRespawned
self.velocity = [math.cos(theta) * speed,
math.sin(theta) * speed]
def setWorld(self, world):
self.world = world
def setCenterPos(self, centerPos):
self.pos = centerPos
self.sprite.setCenterPos(centerPos)
def decrement(self, beamState):
delta = 0
if beamState == 1:
self.health -= 10
if self.enemyType == 'normal':
delta = 100
else:
delta = -500
if self.health <= 0:
self.shouldRemove = True
else:
self.updateAppearance()
return delta
def updateAppearance(self):
baseSize = 30
if not self.enemyType == 'normal':
baseSize = 15
factor = math.log(100 - self.health + 1)
self.sprite.setSizeScalar(baseSize + factor * 10)
def update(self, dt):
worldVector = (self.world.direction[0] * self.world.speed,
self.world.direction[1] * self.world.speed)
worldOffset = (worldVector[0] * self.otcos - worldVector[1] * self.otsin,
worldVector[0] * self.otsin - worldVector[1] * self.otcos)
centerPos = (self.pos[0] + self.velocity[0] + worldOffset[0],
self.pos[1] + self.velocity[1] + worldOffset[1])
if self.isRespawned:
self.isRespawned = False
if centerPos[0] < self.world.left:
self.shouldRemove = True
if centerPos[0] > self.world.right:
self.shouldRemove = True
if centerPos[1] < self.world.left:
self.shouldRemove = True
if centerPos[1] > self.world.right:
self.shouldRemove = True
#.........这里部分代码省略.........