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


Python Vector.rotate方法代码示例

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


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

示例1: _circleCircle

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import rotate [as 别名]
def _circleCircle(sp):
	c1 = sp.shapes[0]
	c2 = sp.shapes[1]
	
	# Displacement between the circles
	dVector = c2.center.sub(c1.center)
	d = dVector.len()
	dAngle = dVector.angle()
	
	if d >= c2.radius + c1.radius:
		# Circles are too far apart; no intersects
		return []
	elif d <= abs(c2.radius - c1.radius):
		# One circle contains the other; no intersects
		return []
	else:
		# Aliases
		r1 = c1.radius
		r2 = c2.radius
		# Distance of intersect along displacement axis
		d1 = (d*d + r1*r1 - r2*r2) / (2*d)
		# Displacement of intersect perpendicularly from displacement axis
		y1 = math.sqrt(r1*r1 - d1*d1)
		
		# Intersections
		i1 = Vector(d1, -y1)
		i2 = Vector(d1, y1)
		i1 = i1.rotate(dAngle)
		i2 = i2.rotate(dAngle)
		i1 = i1.add(c1.center)
		i2 = i2.add(c1.center)
	
	return [i1, i2]
开发者ID:NicoAdams,项目名称:SphericalCow,代码行数:35,代码来源:intersections.py

示例2: Fish

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import rotate [as 别名]
class Fish(pygame.sprite.Sprite):

  def __init__(self):
    self.unflipped = pygame.image.load('img/fish_happy.png').convert_alpha()
    self.flipped = pygame.transform.flip(self.unflipped, True, False)
    self.unflipped_sad = pygame.image.load('img/fish_sad.png').convert_alpha()
    self.unflipped_sad = pygame.transform.flip(self.unflipped_sad, False, True)
    self.flipped_sad = pygame.transform.flip(self.unflipped_sad, True, False)
    self.image = self.unflipped

    self.rect = self.image.get_rect()
    self.direction = Vector(3,1)
    self.position = Vector(0, 0)
    self.MAX_ANGLE = math.pi/16
    self.speed = self._speed = random.randint(1, 3)
    self.delay = 0

    self._dead = False


    self.randomize_direction()


  @property
  def dead(self):
      return self._dead

  @dead.setter
  def dead(self, isDead):
    self._dead = isDead

    if isDead:
      self.speed = random.uniform(0.1, 0.5)
    else:
      self.speed = self._speed
  

  def set_direction(self, x, y):
    self.direction = Vector(x, y)

  def randomize_direction(self):
    random_angle = random.uniform(-self.MAX_ANGLE, self.MAX_ANGLE)
    self.direction = self.direction.rotate(random_angle).normalize()

  def move(self, dx, dy):
    self.position += Vector(dx, dy)

  def general_direction(self):
    deg = math.degrees(self.direction.angle)

    while deg < 0:
      deg += 360

    if deg > 90 and deg < 270:
      return Vector(-1, 0)
    else:
      return Vector(1, 0)


  def update(self, bounds):
    if bounds.height <= self.rect.height:
      self.direction = self.general_direction()
      self.position = self.position.set_y(bounds.bottom - self.rect.height)

    if self.delay != 0:
      self.delay -= 1
      return # Quit early
    else:
      direction_step = self.direction.normalize(self.speed)
      self.position += direction_step
      self.rect.top = round(self.position.y)
      self.rect.left = round(self.position.x)


    # Check top bound
    if self.rect.top < bounds.top and bounds.height > self.rect.height:
      self.position = self.position.set_y(bounds.top)
      self.randomize_direction()
      self.direction = self.direction.set_y(abs(self.direction.y))

    # Check bottom bound
    if self.rect.bottom > bounds.bottom and bounds.height > self.rect.height:
      self.position = self.position.set_y(bounds.bottom - self.rect.height)
      self.direction = self.direction.set_y(-abs(self.direction.y))
      self.randomize_direction()

    # Check right and left bound
    if self.rect.left > bounds.right or self.rect.right < bounds.left:
      if bounds.height < self.rect.height:
        self.position = self.position.set_y(bounds.bottom - self.rect.height)
      else:
        self.position = self.position.set_y(random.randint(bounds.top, bounds.bottom - self.rect.height))

      self.position = self.position.set_x(random.choice([bounds.left-self.rect.width, bounds.right]))

      # Flip direction depending on what edge we're at
      if self.position.x == -self.rect.width:
        self.direction = Vector(1, 0)
      else:
        self.direction = Vector(-1, 0)
#.........这里部分代码省略.........
开发者ID:dnjstrom,项目名称:Fishies,代码行数:103,代码来源:fish.py


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