5.8: Sliding Tiles with the Mouse
- Page ID
- 14499
else: # check if the clicked tile was next to the blank spot blankx, blanky = getBlankPosition(mainBoard) if spotx == blankx + 1 and spoty == blanky: slideTo = LEFT elif spotx == blankx - 1 and spoty == blanky: slideTo = RIGHT elif spotx == blankx and spoty == blanky + 1: slideTo = UP elif spotx == blankx and spoty == blanky - 1: slideTo = DOWN
If getSpotClicked()
did not return (None, None)
, then it will have returned a tuple of two integer values that represent the X and Y coordinate of the spot on the board that was clicked. Then the if
and elif
statements on lines 5 [89] to 12 [96] check if the spot that was clicked is a tile that is next to the blank spot (otherwise the tile will have no place to slide).
Our getBlankPosition()
function will take the board data structure and return the X and Y board coordinates of the blank spot, which we store in the variables blankx
and blanky
. If the spot the user clicked on was next to the blank space, we set the slideTo
variable with the value that the tile should slide.