Skip to main content
Engineering LibreTexts

8.35: Creating Grass Data Structures

  • Page ID
    14656
  • def makeNewGrass(camerax, cameray):
        gr = {}
        gr['grassImage'] = random.randint(0, len(GRASSIMAGES) - 1)
        gr['width']  = GRASSIMAGES[0].get_width()
        gr['height'] = GRASSIMAGES[0].get_height()
        gr['x'], gr['y'] = getRandomOffCameraPos(camerax, cameray, gr['width'], gr['height'])
        gr['rect'] = pygame.Rect( (gr['x'], gr['y'], gr['width'], gr['height']) )
        return gr
    

    The grass game objects are dictionaries with the usual 'x', 'y', 'width', 'height', and 'rect' keys but also a 'grassImage' key which is a number from 0 to one less than the length of the GRASSIMAGES list. This number will determine what image the grass game object has. For example, if the value of the grass object’s 'grassImage' key is 3, then it will use the Surface object stored at GRASSIMAGES[3] for its image.