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


Python Point.length_sqr方法代码示例

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


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

示例1: __init__

# 需要导入模块: from Point import Point [as 别名]
# 或者: from Point.Point import length_sqr [as 别名]
class PointMass:
    def __init__(self, x,  y, inverse_mass):
        self.inverse_mass = inverse_mass
        self.position = Point(x, y)
        self.velocity = Point(0, 0)
        self.acceleration = Point(0, 0)
        self.damping = MASS_DAMPING

    def apply(self, force):
        self.acceleration += force * self.inverse_mass

    def update(self):
        if (self.acceleration.length_sqr() == 0 and
            self.velocity.length_sqr() == 0):
            return

        self.velocity += self.acceleration
        self.position += self.velocity
        self.acceleration.coords = (0, 0)
        if (self.velocity.length_sqr() < sys.float_info.epsilon):
            self.velocity.coords = (0, 0)

        self.velocity *= self.damping
        self.damping = self.damping
开发者ID:Botyto,项目名称:PythonGeometryWars,代码行数:26,代码来源:Grid.py

示例2: Avoider

# 需要导入模块: from Point import Point [as 别名]
# 或者: from Point.Point import length_sqr [as 别名]
class Avoider(Enemy):
    def __init__(self, scene, x, y):
        super().__init__(scene, x, y)
        self._velocity = Point(0, 0)
        self.set_shape(sh.STAR_SHAPE, Colors.avoid, True, 5)

    def update(self):
        player = self._scene.player
        vec_to_player = player.position - self.position
        dir_to_player = math.atan2(vec_to_player.y, vec_to_player.x)

        angle = self._angle_distance(player.direction, dir_to_player)
        if angle < math.pi/2:
            dirvec = vec_to_player
            speed = ACCELERATION
        else:
            avoid_point = Point(1, 1)
            avoid_count = 0

            for obj in self.scene.objects:
                if isinstance(obj, pl.Bullet):
                    distance = (obj.position - self.position).length_sqr()
                    if distance <= DISTANCE_SQR:
                        avoid_point += obj.position
                        avoid_count += 1

            if avoid_count > 0:
                avoid_point /= avoid_count
                dirvec = self.position - avoid_point
                speed = DODGE
            else:
                dirvec = vec_to_player
                speed = ACCELERATION

        self._velocity += dirvec * speed / dirvec.length()

        if self._velocity.length_sqr() > SPEED_SQR:
            self._velocity.normalize()
            self._velocity *= SPEED

        self.position += self._velocity
        self.direction += ANGULAR_SPEED
        super().update()

    def _angle_distance(self, alpha, beta):
        phi = abs(beta - alpha) % 360
        distance = 360 - phi if phi > 180 else phi
        return distance
开发者ID:Botyto,项目名称:PythonGeometryWars,代码行数:50,代码来源:Avoider.py

示例3: Brick

# 需要导入模块: from Point import Point [as 别名]
# 或者: from Point.Point import length_sqr [as 别名]
class Brick(Enemy):
    def __init__(self, scene, x, y, n=2):
        super().__init__(scene, x, y)
        self._velocity = Point(0, 0)
        self._size = n
        myscale = SCALE - n*2
        self.set_shape(sh.BRICK_SHAPE[n], Colors.split, True, myscale)
        self._sleep = SLEEP_TIME if n < 2 else 0

    def destroy(self):
        if self._size > 0:
            s = SCALE
            off = self._size*s
            n = self._size - 1
            for x in range(0, 2):
                for y in range(0, 2):
                    celloffset = Point(off + x*s, off + y*s)
                    cellpos = self.position + celloffset
                    child = Brick(self._scene, cellpos.x, cellpos.y, n)
                    child._velocity = (cellpos - self.position)*2
                    self._scene.add_object(child)

        super().destroy()

    def update(self):
        if self._sleep <= 0:
            player = self._scene.player
            dirvec = player.position - self.position
            self._velocity += dirvec * ACCELERATION / dirvec.length()
        else:
            self._sleep -= 1

        if self._velocity.length_sqr() > SPEED:
                self._velocity.normalize()
                self._velocity *= SPEED

        self.position += self._velocity
        self.direction += self._velocity.length()*ANGULAR_SPEED
        super().update()
开发者ID:Botyto,项目名称:PythonGeometryWars,代码行数:41,代码来源:Brick.py


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