当前位置: 首页>>代码示例>>Python>>正文


Python InstructionGroup.clear方法代码示例

本文整理汇总了Python中kivy.graphics.instructions.InstructionGroup.clear方法的典型用法代码示例。如果您正苦于以下问题:Python InstructionGroup.clear方法的具体用法?Python InstructionGroup.clear怎么用?Python InstructionGroup.clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在kivy.graphics.instructions.InstructionGroup的用法示例。


在下文中一共展示了InstructionGroup.clear方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: RealtimeTouchMove

# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import clear [as 别名]
class RealtimeTouchMove(State):
    def __init__(self, target, cells, **kwargs):
        super(RealtimeTouchMove, self).__init__(target, **kwargs)
        self.cells = cells
        self.moving = False
        self.instructions = InstructionGroup()
        self.highlight_tiles(cells)
        self.foreshadowed = self.target._current_cell
        self.velocity = [0, 0]
        self.target.anim_delay = .2
        self.movement_axis = 0

    def touch(self, touch, *args):
        pos = self.target.map.pixel_from_screen(*touch.pos)
        cell = self.target.map.layers.by_name['below'].get_at(*pos)
        origin_cell = self.target.map.layers.by_name['below'].get_at(*self.target.pos)

        if cell is not None and cell.tile.is_passable():
            cells = cell.layer.a_star_search(origin_cell, cell)
            # if there's only two cells selected, and goal is not origin neighbor... then run away, RUN AWAY!
            if len(cells) == 2 and cell not in self.target.map.layers.by_name['below'].get_neighbor_cells(origin_cell):
                return
            if len(cells) > 1:
                cells.reverse()
                self.instructions.clear()
                self.instructions = InstructionGroup()
                self.highlight_tiles(cells)
                self.cells = cells
                self.moving = False

    def update(self, dt):
        if not self.moving:
            self.check_moving()
        else:
            self.move(dt)

    def move(self, dt):
        delta_x = self.target.x - self.foreshadowed.px
        delta_y = self.target.y - self.foreshadowed.py
        distance = Vector(*self.target.pos).distance((self.foreshadowed.px, self.foreshadowed.py))
        if distance >= 1.0:
            delta_x = (delta_x / distance) * (dt * 50)
            delta_y = (delta_y / distance) * (dt * 50)
            x, y = self.target.pos
            x += -delta_x
            y += -delta_y
            self.target.set_position(x, y)
            distance = Vector(*self.target.pos).distance((self.foreshadowed.px, self.foreshadowed.py))
        if distance <= 1.0:
            self.check_moving()

    def done_moving(self, *args):
        self.target.set_position(self.foreshadowed.px, self.foreshadowed.py)
        self.moving = False

    def highlight_tiles(self, tiles):
        self.instructions.clear()
        for tile in tiles:
            self.instructions.add(Color(rgba=[.3, 1, .3, .3]))
            self.instructions.add(Rectangle(pos=(tile.px, tile.py), size=(tile.px_width, tile.px_height)))
        self.target.game.layer_widgets['below'].canvas.add(self.instructions)

    def check_moving(self, *args):
        last_axis = self.movement_axis
        last_cell = self.foreshadowed
        if self.cells:
            self.moving = True
            self.foreshadowed = self.cells.pop()
            print('moving to', self.foreshadowed.bottomleft)
            if self.foreshadowed.px != last_cell.px:
                self.movement_axis = 0
                if self.foreshadowed.px < last_cell.px:
                    self.target.set_face('left')
                    self.velocity = [self.target.map.tile_width * -1, 0]
                else:
                    self.target.set_face('right')
                    self.velocity = [self.target.map.tile_width, 0]
            elif self.foreshadowed.py != last_cell.py:
                self.movement_axis = 1
                if self.foreshadowed.py < last_cell.py:
                    self.target.set_face('down')
                    self.velocity = [0, self.target.map.tile_width * -1]
                else:
                    self.target.set_face('up')
                    self.velocity = [0, self.target.map.tile_width]
            if last_axis != self.movement_axis:
                print('axis changed!')
                print(self.target.pos)
                self.target.set_position(last_cell.px, last_cell.py)
                print(self.target.pos)
                print(last_cell.bottomleft, self.foreshadowed.bottomleft)

        else:
            self.done_moving()
            self.end()
            print('no cells')
            return
        self.target.activate_object(target='floor', button_press=False)

    def end(self):
#.........这里部分代码省略.........
开发者ID:spinningD20,项目名称:kivy_rpg,代码行数:103,代码来源:realtime.py

示例2: Beam

# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import clear [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)
开发者ID:adparadise,项目名称:pmgj6-harvesters,代码行数:89,代码来源:beam.py

示例3: EntityFollow

# 需要导入模块: from kivy.graphics.instructions import InstructionGroup [as 别名]
# 或者: from kivy.graphics.instructions.InstructionGroup import clear [as 别名]
class EntityFollow(State):
    def __init__(self, target, following):
        super(EntityFollow, self).__init__(target)
        self.following = following
        self.cells = []
        self.moving = False
        self.instructions = InstructionGroup()
        targeted = self.target.map.layers.by_name['below'].get_at(*self.following.pos)
        # targeted = self.target.map.layers.by_name['below'].get_neighbor_cells(following_cell)[0]
        origin_cell = self.target.map.layers.by_name['below'].get_at(*self.target.pos)

        if targeted is not None and targeted.tile.is_passable():
            cells = targeted.layer.a_star_search(origin_cell, targeted)
            # if there's only two cells selected, and goal is not origin neighbor... then run away, RUN AWAY!
            if len(cells) == 2 and targeted not in self.target.map.layers.by_name['below'].get_neighbor_cells(origin_cell):
                return
            if len(cells) > 1:
                cells.reverse()
                # self.instructions.clear()
                self.instructions = InstructionGroup()
                # self.highlight_tiles(cells)
                self.cells = cells
                self.moving = False
        if not self.cells:
            print('no cells here dawg')
            self.target.state = EntityIdle(self.target)
            self.end()
            return
        # self.highlight_tiles(self.cells)
        self.foreshadowed = None
        self.velocity = [0, 0]
        self.target.anim_delay = .2
        self.task = Clock.schedule_interval(self.slow_update, .6)

    def slow_update(self, *args):
        print([self.following.pos])
        distance = Vector(*self.target.pos).distance(self.following.pos)
        if distance < 100:
            pass
        else:
            self.moving = False
            # self.instructions.clear()
            self.target.set_position(self.foreshadowed.px, self.foreshadowed.py)
            if distance > 500:
                self.target.state = EntityIdle(self.target)
                self.end()
            else:
                self.target.state = EntityFollow(self.target, self.following)
                self.end()

    def update(self, dt):
        if not self.moving:
            if self.cells:
                self.foreshadowed = self.cells.pop()
                print('moving to', self.foreshadowed)
                # if self.foreshadowed.occupied:
                #     self.end()
                if self.foreshadowed.px != self.target.x:
                    if self.foreshadowed.px < self.target.x:
                        self.target.set_face('left')
                        self.velocity = [self.target.map.tile_width * -1, 0]
                    else:
                        self.target.set_face('right')
                        self.velocity = [self.target.map.tile_width, 0]
                elif self.foreshadowed.py != self.target.y:
                    if self.foreshadowed.py < self.target.y:
                        self.target.set_face('down')
                        self.velocity = [0, self.target.map.tile_width * -1]
                    else:
                        self.target.set_face('up')
                        self.velocity = [0, self.target.map.tile_width]
                self.start_moving()
            else:
                self.end()
                self.target.get_current_cell()
                self.target.state = EntityIdle(self.target)
        else:
            self.move(dt)

    def move(self, dt):
        done = move(dt, self.target, self.foreshadowed.px, self.foreshadowed.py)
        if done:
            self.done_moving()

    def done_moving(self, *args):
        self.target.set_position(self.foreshadowed.px, self.foreshadowed.py)
        self.moving = False

    def highlight_tiles(self, tiles):
        self.instructions.clear()
        for tile in tiles:
            self.instructions.add(Color(rgba=[.3, 1, .3, .3]))
            self.instructions.add(Rectangle(pos=(tile.px, tile.py), size=(tile.px_width, tile.px_height)))
        self.target.game.layer_widgets['below'].canvas.add(self.instructions)

    def start_moving(self, *args):
        print('start moving')
        self.moving = True

    def end(self):
#.........这里部分代码省略.........
开发者ID:spinningD20,项目名称:kivy_rpg,代码行数:103,代码来源:realtime.py


注:本文中的kivy.graphics.instructions.InstructionGroup.clear方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。