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


Python Simulator.update方法代码示例

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


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

示例1: main

# 需要导入模块: from simulator import Simulator [as 别名]
# 或者: from simulator.Simulator import update [as 别名]
def main():
    if len(sys.argv) < 2:
        genomes = [ random_genome(INIT_GENOME_LEN) for _ in range(INIT_GENOME_POOL) ]
        sim = Simulator(WIDTH, HEIGHT, ANIMAL_COUNT, PREDATOR_COUNT, genomes)
    else:
        sim = Simulator(WIDTH, HEIGHT, 0, PREDATOR_COUNT, [])
        with open(sys.argv[1]) as f:
            for line in f:
                sim.insert_animal(line.strip())

    next_output = 0
    time = 0
    deltatime = TIME_TICK

    while True:
        # output to file
        next_output -= deltatime
        if next_output <= 0:
            next_output = OUTPUT_PERIOD
            with open(OUTPUT_PREFIX + "{:.2f}.txt".format(time), 'w') as f:
                f.writelines(a.brain.genome + "\n" for a in sim.animals)
        # update state
        time += deltatime
        sim.update(deltatime)
        # debug
        if DEBUG_TIME:
            mini = min(a.energy for a in sim.animals)
            maxi = max(a.energy for a in sim.animals)
            print("{} ({:.2f} -- {:.2f}) alive at {:.2f}".format(len(sim.animals), mini, maxi, time))
开发者ID:vberger,项目名称:evolving-project,代码行数:31,代码来源:evolving-cli.py

示例2: main

# 需要导入模块: from simulator import Simulator [as 别名]
# 或者: from simulator.Simulator import update [as 别名]
def main():
    # init phase

    pygame.init()
    s = pygame.display.set_mode((WIDTH, HEIGHT))
    clock = pygame.time.Clock()
    s.fill((255,255,255))
    
    if len(sys.argv) < 2:
        genomes = [ random_genome(INIT_GENOME_LEN) for _ in range(INIT_GENOME_POOL) ]
        sim = Simulator(WIDTH, HEIGHT, ANIMAL_COUNT, PREDATOR_COUNT, genomes)
    else:
        sim = Simulator(WIDTH, HEIGHT, 0, PREDATOR_COUNT, [])
        with open(sys.argv[1]) as f:
            for line in f:
                sim.insert_animal(line.strip())

    while True:
        deltatime = float(clock.tick(50)) / 1000
        deltatime = TIME_TICK
        s.fill((255, 255, 255))
        for e in pygame.event.get():
            if e.type == QUIT:
                sys.exit(0)

        sim.update(deltatime)

        # display the pheromones
        for p in sim.pheromones:
            i = min(p.power(), 1.0)
            (r,g,b) = PHEROMONES_COLORS[p.sid]
            col = (int(255*(1.0-i) + r*i),int(255*(1.0-i) + g*i),int(255*(1.0-i) + b*i), 255)
            pygame.draw.circle(s, col, (int(p.x), int(p.y)), int(p.radius), 1)

        # display the objects
        for f in sim.objects:
            i = max(min(f.amount, 10),0)/10.0
            (r,g,b) = PHEROMONES_COLORS[f.kind]
            col = (int(255*(1.0-i) + r*i),int(255*(1.0-i) + g*i),int(255*(1.0-i) + b*i), 255)
            pygame.draw.circle(s, col, (f.x, f.y), 5)
            pygame.draw.circle(s, (0,0,0), (f.x, f.y), 5, 1)

        # display the animals
        for a in sim.animals:
            # choose a color:
            col = pygame.Color(int((100-max(0,a.energy))*255/100), int(max(0,a.energy)*255/100), 0, 255)
            # draw a head
            pygame.draw.circle(s, col, (int(a.x + math.cos(a.theta)*7), int(a.y + math.sin(a.theta)*7)), 5)
            pygame.draw.circle(s, (0,0,0), (int(a.x + math.cos(a.theta)*7), int(a.y + math.sin(a.theta)*7)), 5, 1)
            # draw a body
            pygame.draw.circle(s, col, (int(a.x), int(a.y)), 8)
            pygame.draw.circle(s, (0,0,0), (int(a.x), int(a.y)), 8, 1)
        # display the predators
        for p in sim.predators:
            # choose a color:
            col = pygame.Color(0,0,0,0)
            pygame.draw.circle(s, col, (int(p.x), int(p.y)), 12)
            pygame.draw.circle(s, (0,0,0), (int(p.x), int(p.y)), 12, 1)
        pygame.display.update()
开发者ID:vberger,项目名称:evolving-project,代码行数:61,代码来源:evolving.py

示例3: __init__

# 需要导入模块: from simulator import Simulator [as 别名]
# 或者: from simulator.Simulator import update [as 别名]
class Main:

  def __init__(self):
    pygame.init()

    self.screenDim = (1024, 786)
    self.screen = pygame.display.set_mode(self.screenDim)
    self.background = (255, 255, 255)
    self.running = False

    self.scoreCheckInterval = 10000
    self.lastScoreCheck = 0
    self.currentBlobManagerScore = 0

    self.mapData = None
    #self.mapDataPath = "data/test_"
    self.mapDataPath = "map.png"

    self.updateBlobs = True

    self.blobManager = BlobManager(self.screenDim, self.mapData)
    self.roadManager = RoadManager(self.screenDim, self.blobManager)
    self.simulator = Simulator(self.blobManager, self.roadManager)

    self.mapReloadInterval = 1000
    self.lastMapReload = 0

    self.wasRight = False
    self.drawBackground = True

  def reloadMap(self):
    list = range(1, 11)
    list.reverse()
    for i in list:
      filename = self.mapDataPath# + str(i) + ".png"
      if path.isfile(filename):
        #print filename
        img = pygame.image.load(filename)
        self.mapData = pygame.transform.scale(img, self.screenDim)
        self.blobManager.mapData = self.mapData
        break
    #print "done " + str(self.mapData)

  def poll(self):
    events = pygame.event.get()
    for e in events:
      if e.type == pygame.QUIT:
        self.running = False
      elif e.type == pygame.KEYUP:
        if e.key == pygame.K_ESCAPE:
          self.running = False
        elif e.key == pygame.K_f:
          pygame.display.toggle_fullscreen()
        elif e.key == pygame.K_b:
          self.drawBackground = not self.drawBackground
        elif e.key == pygame.K_r:
          self.roadManager.drawRoad = not self.roadManager.drawRoad
        elif e.key == pygame.K_l:
          self.roadManager.drawLine = not self.roadManager.drawLine
        elif e.key == pygame.K_c:
          self.blobManager.drawBlobs = not self.blobManager.drawBlobs
        elif e.key == pygame.K_u:
          self.updateBlobs = not self.updateBlobs
      elif e.type == pygame.MOUSEBUTTONDOWN:
          btns = pygame.mouse.get_pressed()
          self.wasRight = btns[2]
      elif e.type == pygame.MOUSEBUTTONUP:
        if self.wasRight:
          self.blobManager.removeAt(e.pos)
        else:
          self.blobManager.spawnAt(e.pos)

  def update(self, dt):

    time = pygame.time.get_ticks()

    if self.lastMapReload == 0 or self.lastMapReload + self.mapReloadInterval < time:
      self.lastMapReload = time
      #print "reloading map..."
      self.reloadMap()

    if self.updateBlobs:
      self.blobManager.update(dt)

      self.roadManager.update(dt)

    self.simulator.update(dt)


    if self.lastScoreCheck == 0 or self.lastScoreCheck + self.scoreCheckInterval < time:
      self.lastScoreCheck = time
      print "calculating scores..."
      newScore = self.blobManager.calculateScore()
      if newScore > self.currentBlobManagerScore:
        self.currentBlobManagerScore = newScore
        print "updating roads (score:" + str(self.currentBlobManagerScore) + ")"
        self.roadManager.regenerate()
      else:
        print "no score change"

#.........这里部分代码省略.........
开发者ID:fablab-ka,项目名称:cityscape,代码行数:103,代码来源:map.py


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