Skip to main content
Engineering LibreTexts

9.31: The getRandomVelocity() Function

  • Page ID
  • def getRandomVelocity():
        speed = random.randint(SQUIRRELMINSPEED, SQUIRRELMAXSPEED)
        if random.randint(0, 1) == 0:
            return speed
            return -speed

    The getRandomVelocity() function is used to randomly determine how fast an enemy squirrel will move. The range of this velocity is set in the SQUIRRELMINSPEED and SQUIRRELMAXSPEED constants, but on top of that, the speed is either negative (indicating the squirrel goes to the left or up) or positive (indicating the squirrel goes to the right or down). There is a fifty-fifty chance for the random speed to be positive or negative.