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


Python Brain.getDNA方法代码示例

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


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

示例1: Creature

# 需要导入模块: from brain import Brain [as 别名]
# 或者: from brain.Brain import getDNA [as 别名]

#.........这里部分代码省略.........

        self.velocity -= SPEED_DECAY
        if self.velocity < 0:
            self.velocity = 0

        if self.angularVelocity < 0:
            self.angularVelocity = min(0,
                            self.angularVelocity + ANGULAR_SPEED_DECAY)
        elif self.angularVelocity > 0:
            self.angularVelocity = max(0,
                            self.angularVelocity - ANGULAR_SPEED_DECAY)

        return 1

    def canEat(self, food):
        if food.contains(self.mouthPos[0],self.mouthPos[1]):
            return True
        return False

    def eat(self, food):
        if food.size < self.EAT_RATE:
            self.energy += food.size
            food.size = 0
        else:
            food.size -= self.EAT_RATE
            self.energy += self.EAT_RATE

        if self.energy > self.MAX_ENERGY:
            self.energy = self.MAX_ENERGY

    def die(self):
        self.brain.die()

    def getDNA(self):
        DNA = ""

        a = (self.fBooster.force/self.MAX_BOOST_FORCE) * 255
        DNA += brainUtils.intToBin(int(a),8)

        a = (self.lTurn.force/self.MAX_TURN_FORCE) * 255
        DNA += brainUtils.intToBin(int(a),8)
        
        a = (self.ANTENNA_ANGLE/self.MAX_ANTENNA_ANGLE) * 255
        DNA += brainUtils.intToBin(int(a),8)

        a = (self.ANTENNA_LENGTH/self.MAX_ANTENNA_LENGTH) * 255
        DNA += brainUtils.intToBin(int(a),8)

        a = (self.fANTENNA_LENGTH/self.MAX_ANTENNA_LENGTH) * 255
        DNA += brainUtils.intToBin(int(a),8)
        
        DNA += self.brain.getDNA()

        return DNA

    def loadFromDNA(self, DNA):
        self.fBooster.force = self.MAX_BOOST_FORCE * (brainUtils.binToInt(DNA[:8])/255.0)
        self.fBooster.force = 3.0
        DNA = DNA[8:]
        
        turnSpeed = self.MAX_TURN_FORCE * (brainUtils.binToInt(DNA[:8])/255.0)
        turnSpeed = 0.05
        self.lTurn.force = turnSpeed
        self.rTurn.force = -turnSpeed
        DNA = DNA[8:]
        
开发者ID:DiscoViking,项目名称:PyBrains,代码行数:69,代码来源:creature.py


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