Skip to main content
Engineering LibreTexts

5.27: Animating the Tile Slides

  • Page ID
  • def slideAnimation(board, direction, message, animationSpeed):
        # Note: This function does not check if the move is valid.
        blankx, blanky = getBlankPosition(board)
        if direction == UP:
            movex = blankx
            movey = blanky + 1
        elif direction == DOWN:
            movex = blankx
            movey = blanky - 1
        elif direction == LEFT:
            movex = blankx + 1
            movey = blanky
        elif direction == RIGHT:
            movex = blankx - 1
            movey = blanky

    The first thing our tile sliding animation code needs to calculate is where the blank space is and where the moving tile is. The comment on line 2 [255] reminds us that the code that calls slideAnimation() should make sure that the slide it passes for the direction parameter is a valid move to make.

    The blank space’s coordinates come from a call to getBlankPosition(). From these coordinates and the direction of the slide, we can figure out the XY board coordinates of the tile that will slide. These coordinates will be stored in the movex and movey variables.