예제 #1
0
class Motive:
  def __init__(self, pos, direction, t):
    self.pos = Vector(pos)
    self.direction = direction
    self.type = t
    self.speed = random()
    self.age = pygame.time.get_ticks()
    self.ttl = 15000 + 5000 * random()

    self.color = (255, 255, 255)

    size = 1
    if self.type == MotiveType.Pedestrian:
      self.size = int(size)
    if self.type == MotiveType.Car:
      self.size = [int(size*2 + random()), int(size*2 + random() * 2)]

  def update(self, dt, checkForCollision):
    newPos = self.pos + self.direction * self.speed

    if checkForCollision(newPos):
      self.direction = choice([self.direction.getLeftPerpendicular(), self.direction.getRightPerpendicular()])
    else:
      self.pos = newPos


  def draw(self, screen):
    if self.type == MotiveType.Pedestrian:
      pygame.draw.circle(screen, self.color, self.pos.toIntArr(), self.size)
    if self.type == MotiveType.Car:
      pygame.draw.rect(screen, self.color, self.pos.toIntArr() + self.size)
예제 #2
0
class Motive:
  def __init__(self, start, target, targetId, t):
    self.pos = Vector(start)
    self.start = start
    self.target = target
    self.targetId = targetId
    self.type = t
    self.speed = random()
    self.age = pygame.time.get_ticks()
    self.ttl = 15000 + 5000 * random()

    self.arrivedAtTarget = False
    self.minTargetDistance = 10
    self.color = (255, 0, 0)

    size = 1
    if self.type == MotiveType.Pedestrian:
      self.size = int(size)
    if self.type == MotiveType.Car:
      self.size = [int(size + random() * 2), int(size + random() * 2)]

  def update(self, dt):
    direction = (self.target - self.start).getNormalized()
    variation = 0
    if random() > 0.5:
      variation = direction.getLeftPerpendicular()
    else:
      variation = direction.getRightPerpendicular()

    direction = (direction * (random() * 10) + variation * random()).getNormalized()

    self.pos += direction * self.speed

    self.arrivedAtTarget = (self.target - self.pos).getLengthSqr() < self.minTargetDistance

  def draw(self, screen):
    if self.type == MotiveType.Pedestrian:
      pygame.draw.circle(screen, self.color, self.pos.toIntArr(), self.size)
    if self.type == MotiveType.Car:
      pygame.draw.rect(screen, self.color, self.pos.toIntArr() + self.size)
예제 #3
0
  def isSet(self, pos):
    if not self.mapData:
      print "no mapData yet"
      return True

    if not isinstance(pos, Vector):
      pos = Vector(pos)

    if pos.x < 0 or pos.y < 0:
      return True
    if pos.x >= self.screenDim[0] or pos.y >= self.screenDim[1]:
      return True

    px = self.mapData.get_at(pos.toIntArr())
    return (px.r + px.g + px.b) < 600