當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。