Skip to main content
Engineering LibreTexts

9.33: Creating Enemy Squirrel Data Structures

  • Page ID
  • def makeNewSquirrel(camerax, cameray):
        sq = {}
        generalSize = random.randint(5, 25)
        multiplier = random.randint(1, 3)
        sq['width']  = (generalSize + random.randint(0, 10)) * multiplier
        sq['height'] = (generalSize + random.randint(0, 10)) * multiplier
        sq['x'], sq['y'] = getRandomOffCameraPos(camerax, cameray, sq['width'], sq['height'])
        sq['movex'] = getRandomVelocity()
        sq['movey'] = getRandomVelocity()

    Creating enemy squirrel game objects is similar to making the grass game objects. The data for each enemy squirrel is also stored in a dictionary. The width and height are set to random sizes on line 5 [360] and 6 [361]. The generalSize variable is used so that the width and height of each squirrel aren’t too different from each other. Otherwise, using completely random numbers for width and height could give us very tall and skinny squirrels or very short and wide squirrels. The width and height of the squirrel are this general size with a random number from 0 to 10 added to it (for slight variation), and then multiplied by the multiplier variable.

    The original XY coordinate position of the squirrel will be a random location that the camera cannot see, to prevent the squirrels from just "popping" into existence on the screen.

    The speed and direction are also randomly selected by the getRandomVelocity() function.