PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/python_games/slidepuzzle.py

https://gitlab.com/omair.hassaan/RaspberryPi_Codes-Tutorials
Python | 332 lines | 247 code | 57 blank | 28 comment | 70 complexity | 0d293ff3f8851a85258ec29f685d73f2 MD5 | raw file
  1. # Slide Puzzle
  2. # By Al Sweigart al@inventwithpython.com
  3. # http://inventwithpython.com/pygame
  4. # Released under a "Simplified BSD" license
  5. import pygame, sys, random
  6. from pygame.locals import *
  7. # Create the constants (go ahead and experiment with different values)
  8. BOARDWIDTH = 4 # number of columns in the board
  9. BOARDHEIGHT = 4 # number of rows in the board
  10. TILESIZE = 80
  11. WINDOWWIDTH = 640
  12. WINDOWHEIGHT = 480
  13. FPS = 30
  14. BLANK = None
  15. # R G B
  16. BLACK = ( 0, 0, 0)
  17. WHITE = (255, 255, 255)
  18. BRIGHTBLUE = ( 0, 50, 255)
  19. DARKTURQUOISE = ( 3, 54, 73)
  20. GREEN = ( 0, 204, 0)
  21. BGCOLOR = DARKTURQUOISE
  22. TILECOLOR = GREEN
  23. TEXTCOLOR = WHITE
  24. BORDERCOLOR = BRIGHTBLUE
  25. BASICFONTSIZE = 20
  26. BUTTONCOLOR = WHITE
  27. BUTTONTEXTCOLOR = BLACK
  28. MESSAGECOLOR = WHITE
  29. XMARGIN = int((WINDOWWIDTH - (TILESIZE * BOARDWIDTH + (BOARDWIDTH - 1))) / 2)
  30. YMARGIN = int((WINDOWHEIGHT - (TILESIZE * BOARDHEIGHT + (BOARDHEIGHT - 1))) / 2)
  31. UP = 'up'
  32. DOWN = 'down'
  33. LEFT = 'left'
  34. RIGHT = 'right'
  35. def main():
  36. global FPSCLOCK, DISPLAYSURF, BASICFONT, RESET_SURF, RESET_RECT, NEW_SURF, NEW_RECT, SOLVE_SURF, SOLVE_RECT
  37. pygame.init()
  38. FPSCLOCK = pygame.time.Clock()
  39. DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
  40. pygame.display.set_caption('Slide Puzzle')
  41. BASICFONT = pygame.font.Font('freesansbold.ttf', BASICFONTSIZE)
  42. # Store the option buttons and their rectangles in OPTIONS.
  43. RESET_SURF, RESET_RECT = makeText('Reset', TEXTCOLOR, TILECOLOR, WINDOWWIDTH - 120, WINDOWHEIGHT - 90)
  44. NEW_SURF, NEW_RECT = makeText('New Game', TEXTCOLOR, TILECOLOR, WINDOWWIDTH - 120, WINDOWHEIGHT - 60)
  45. SOLVE_SURF, SOLVE_RECT = makeText('Solve', TEXTCOLOR, TILECOLOR, WINDOWWIDTH - 120, WINDOWHEIGHT - 30)
  46. mainBoard, solutionSeq = generateNewPuzzle(80)
  47. SOLVEDBOARD = getStartingBoard() # a solved board is the same as the board in a start state.
  48. allMoves = [] # list of moves made from the solved configuration
  49. while True: # main game loop
  50. slideTo = None # the direction, if any, a tile should slide
  51. msg = 'Click tile or press arrow keys to slide.' # contains the message to show in the upper left corner.
  52. if mainBoard == SOLVEDBOARD:
  53. msg = 'Solved!'
  54. drawBoard(mainBoard, msg)
  55. checkForQuit()
  56. for event in pygame.event.get(): # event handling loop
  57. if event.type == MOUSEBUTTONUP:
  58. spotx, spoty = getSpotClicked(mainBoard, event.pos[0], event.pos[1])
  59. if (spotx, spoty) == (None, None):
  60. # check if the user clicked on an option button
  61. if RESET_RECT.collidepoint(event.pos):
  62. resetAnimation(mainBoard, allMoves) # clicked on Reset button
  63. allMoves = []
  64. elif NEW_RECT.collidepoint(event.pos):
  65. mainBoard, solutionSeq = generateNewPuzzle(80) # clicked on New Game button
  66. allMoves = []
  67. elif SOLVE_RECT.collidepoint(event.pos):
  68. resetAnimation(mainBoard, solutionSeq + allMoves) # clicked on Solve button
  69. allMoves = []
  70. else:
  71. # check if the clicked tile was next to the blank spot
  72. blankx, blanky = getBlankPosition(mainBoard)
  73. if spotx == blankx + 1 and spoty == blanky:
  74. slideTo = LEFT
  75. elif spotx == blankx - 1 and spoty == blanky:
  76. slideTo = RIGHT
  77. elif spotx == blankx and spoty == blanky + 1:
  78. slideTo = UP
  79. elif spotx == blankx and spoty == blanky - 1:
  80. slideTo = DOWN
  81. elif event.type == KEYUP:
  82. # check if the user pressed a key to slide a tile
  83. if event.key in (K_LEFT, K_a) and isValidMove(mainBoard, LEFT):
  84. slideTo = LEFT
  85. elif event.key in (K_RIGHT, K_d) and isValidMove(mainBoard, RIGHT):
  86. slideTo = RIGHT
  87. elif event.key in (K_UP, K_w) and isValidMove(mainBoard, UP):
  88. slideTo = UP
  89. elif event.key in (K_DOWN, K_s) and isValidMove(mainBoard, DOWN):
  90. slideTo = DOWN
  91. if slideTo:
  92. slideAnimation(mainBoard, slideTo, 'Click tile or press arrow keys to slide.', 8) # show slide on screen
  93. makeMove(mainBoard, slideTo)
  94. allMoves.append(slideTo) # record the slide
  95. pygame.display.update()
  96. FPSCLOCK.tick(FPS)
  97. def terminate():
  98. pygame.quit()
  99. sys.exit()
  100. def checkForQuit():
  101. for event in pygame.event.get(QUIT): # get all the QUIT events
  102. terminate() # terminate if any QUIT events are present
  103. for event in pygame.event.get(KEYUP): # get all the KEYUP events
  104. if event.key == K_ESCAPE:
  105. terminate() # terminate if the KEYUP event was for the Esc key
  106. pygame.event.post(event) # put the other KEYUP event objects back
  107. def getStartingBoard():
  108. # Return a board data structure with tiles in the solved state.
  109. # For example, if BOARDWIDTH and BOARDHEIGHT are both 3, this function
  110. # returns [[1, 4, 7], [2, 5, 8], [3, 6, BLANK]]
  111. counter = 1
  112. board = []
  113. for x in range(BOARDWIDTH):
  114. column = []
  115. for y in range(BOARDHEIGHT):
  116. column.append(counter)
  117. counter += BOARDWIDTH
  118. board.append(column)
  119. counter -= BOARDWIDTH * (BOARDHEIGHT - 1) + BOARDWIDTH - 1
  120. board[BOARDWIDTH-1][BOARDHEIGHT-1] = BLANK
  121. return board
  122. def getBlankPosition(board):
  123. # Return the x and y of board coordinates of the blank space.
  124. for x in range(BOARDWIDTH):
  125. for y in range(BOARDHEIGHT):
  126. if board[x][y] == BLANK:
  127. return (x, y)
  128. def makeMove(board, move):
  129. # This function does not check if the move is valid.
  130. blankx, blanky = getBlankPosition(board)
  131. if move == UP:
  132. board[blankx][blanky], board[blankx][blanky + 1] = board[blankx][blanky + 1], board[blankx][blanky]
  133. elif move == DOWN:
  134. board[blankx][blanky], board[blankx][blanky - 1] = board[blankx][blanky - 1], board[blankx][blanky]
  135. elif move == LEFT:
  136. board[blankx][blanky], board[blankx + 1][blanky] = board[blankx + 1][blanky], board[blankx][blanky]
  137. elif move == RIGHT:
  138. board[blankx][blanky], board[blankx - 1][blanky] = board[blankx - 1][blanky], board[blankx][blanky]
  139. def isValidMove(board, move):
  140. blankx, blanky = getBlankPosition(board)
  141. return (move == UP and blanky != len(board[0]) - 1) or \
  142. (move == DOWN and blanky != 0) or \
  143. (move == LEFT and blankx != len(board) - 1) or \
  144. (move == RIGHT and blankx != 0)
  145. def getRandomMove(board, lastMove=None):
  146. # start with a full list of all four moves
  147. validMoves = [UP, DOWN, LEFT, RIGHT]
  148. # remove moves from the list as they are disqualified
  149. if lastMove == UP or not isValidMove(board, DOWN):
  150. validMoves.remove(DOWN)
  151. if lastMove == DOWN or not isValidMove(board, UP):
  152. validMoves.remove(UP)
  153. if lastMove == LEFT or not isValidMove(board, RIGHT):
  154. validMoves.remove(RIGHT)
  155. if lastMove == RIGHT or not isValidMove(board, LEFT):
  156. validMoves.remove(LEFT)
  157. # return a random move from the list of remaining moves
  158. return random.choice(validMoves)
  159. def getLeftTopOfTile(tileX, tileY):
  160. left = XMARGIN + (tileX * TILESIZE) + (tileX - 1)
  161. top = YMARGIN + (tileY * TILESIZE) + (tileY - 1)
  162. return (left, top)
  163. def getSpotClicked(board, x, y):
  164. # from the x & y pixel coordinates, get the x & y board coordinates
  165. for tileX in range(len(board)):
  166. for tileY in range(len(board[0])):
  167. left, top = getLeftTopOfTile(tileX, tileY)
  168. tileRect = pygame.Rect(left, top, TILESIZE, TILESIZE)
  169. if tileRect.collidepoint(x, y):
  170. return (tileX, tileY)
  171. return (None, None)
  172. def drawTile(tilex, tiley, number, adjx=0, adjy=0):
  173. # draw a tile at board coordinates tilex and tiley, optionally a few
  174. # pixels over (determined by adjx and adjy)
  175. left, top = getLeftTopOfTile(tilex, tiley)
  176. pygame.draw.rect(DISPLAYSURF, TILECOLOR, (left + adjx, top + adjy, TILESIZE, TILESIZE))
  177. textSurf = BASICFONT.render(str(number), True, TEXTCOLOR)
  178. textRect = textSurf.get_rect()
  179. textRect.center = left + int(TILESIZE / 2) + adjx, top + int(TILESIZE / 2) + adjy
  180. DISPLAYSURF.blit(textSurf, textRect)
  181. def makeText(text, color, bgcolor, top, left):
  182. # create the Surface and Rect objects for some text.
  183. textSurf = BASICFONT.render(text, True, color, bgcolor)
  184. textRect = textSurf.get_rect()
  185. textRect.topleft = (top, left)
  186. return (textSurf, textRect)
  187. def drawBoard(board, message):
  188. DISPLAYSURF.fill(BGCOLOR)
  189. if message:
  190. textSurf, textRect = makeText(message, MESSAGECOLOR, BGCOLOR, 5, 5)
  191. DISPLAYSURF.blit(textSurf, textRect)
  192. for tilex in range(len(board)):
  193. for tiley in range(len(board[0])):
  194. if board[tilex][tiley]:
  195. drawTile(tilex, tiley, board[tilex][tiley])
  196. left, top = getLeftTopOfTile(0, 0)
  197. width = BOARDWIDTH * TILESIZE
  198. height = BOARDHEIGHT * TILESIZE
  199. pygame.draw.rect(DISPLAYSURF, BORDERCOLOR, (left - 5, top - 5, width + 11, height + 11), 4)
  200. DISPLAYSURF.blit(RESET_SURF, RESET_RECT)
  201. DISPLAYSURF.blit(NEW_SURF, NEW_RECT)
  202. DISPLAYSURF.blit(SOLVE_SURF, SOLVE_RECT)
  203. def slideAnimation(board, direction, message, animationSpeed):
  204. # Note: This function does not check if the move is valid.
  205. blankx, blanky = getBlankPosition(board)
  206. if direction == UP:
  207. movex = blankx
  208. movey = blanky + 1
  209. elif direction == DOWN:
  210. movex = blankx
  211. movey = blanky - 1
  212. elif direction == LEFT:
  213. movex = blankx + 1
  214. movey = blanky
  215. elif direction == RIGHT:
  216. movex = blankx - 1
  217. movey = blanky
  218. # prepare the base surface
  219. drawBoard(board, message)
  220. baseSurf = DISPLAYSURF.copy()
  221. # draw a blank space over the moving tile on the baseSurf Surface.
  222. moveLeft, moveTop = getLeftTopOfTile(movex, movey)
  223. pygame.draw.rect(baseSurf, BGCOLOR, (moveLeft, moveTop, TILESIZE, TILESIZE))
  224. for i in range(0, TILESIZE, animationSpeed):
  225. # animate the tile sliding over
  226. checkForQuit()
  227. DISPLAYSURF.blit(baseSurf, (0, 0))
  228. if direction == UP:
  229. drawTile(movex, movey, board[movex][movey], 0, -i)
  230. if direction == DOWN:
  231. drawTile(movex, movey, board[movex][movey], 0, i)
  232. if direction == LEFT:
  233. drawTile(movex, movey, board[movex][movey], -i, 0)
  234. if direction == RIGHT:
  235. drawTile(movex, movey, board[movex][movey], i, 0)
  236. pygame.display.update()
  237. FPSCLOCK.tick(FPS)
  238. def generateNewPuzzle(numSlides):
  239. # From a starting configuration, make numSlides number of moves (and
  240. # animate these moves).
  241. sequence = []
  242. board = getStartingBoard()
  243. drawBoard(board, '')
  244. pygame.display.update()
  245. pygame.time.wait(500) # pause 500 milliseconds for effect
  246. lastMove = None
  247. for i in range(numSlides):
  248. move = getRandomMove(board, lastMove)
  249. slideAnimation(board, move, 'Generating new puzzle...', animationSpeed=int(TILESIZE / 3))
  250. makeMove(board, move)
  251. sequence.append(move)
  252. lastMove = move
  253. return (board, sequence)
  254. def resetAnimation(board, allMoves):
  255. # make all of the moves in allMoves in reverse.
  256. revAllMoves = allMoves[:] # gets a copy of the list
  257. revAllMoves.reverse()
  258. for move in revAllMoves:
  259. if move == UP:
  260. oppositeMove = DOWN
  261. elif move == DOWN:
  262. oppositeMove = UP
  263. elif move == RIGHT:
  264. oppositeMove = LEFT
  265. elif move == LEFT:
  266. oppositeMove = RIGHT
  267. slideAnimation(board, oppositeMove, '', animationSpeed=int(TILESIZE / 2))
  268. makeMove(board, oppositeMove)
  269. if __name__ == '__main__':
  270. main()