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


Python Point.normalize方法代码示例

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


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

示例1: Avoider

# 需要导入模块: from Point import Point [as 别名]
# 或者: from Point.Point import normalize [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

示例2: calcDirection

# 需要导入模块: from Point import Point [as 别名]
# 或者: from Point.Point import normalize [as 别名]
	def calcDirection(self):
		del self.direction[:]
		n = len(self.points)
		for i in range(0,n-1):
			a,b = self.points[i].getCoords()
			c,d = self.points[i+1].getCoords()

			dx = c - a
			dy = d - b

			d = Point(dx, dy)

			if d.length() < 0.001:
				d = Point(0, 0)
			else:
				d.normalize()

			self.direction.append(d)
开发者ID:datjwu,项目名称:CMC_Integration,代码行数:20,代码来源:TrackInfo.py

示例3: Brick

# 需要导入模块: from Point import Point [as 别名]
# 或者: from Point.Point import normalize [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.normalize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。