# baic library imports pygame and random
import pygame
import sys
import random
from pygame.locals import *
# this section holds all the variables that we will use in Puzzle Game In Python
w_of_board = 4 # total number of columns in the board of Puzzle Game In Python
h_of_board = 4 # total number of rows in the board
block_size = 80
win_width = 640
win_height = 480
FPS = 30
BLANK = None
# this is bascially for managing the different colors of the component
# we have also used variables for mantaining the text size in Puzzle Game In Python
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BRIGHTBLUE = (0, 50, 255)
DARKTURQUOISE = (255, 255, 255)
BLUE = (0, 0, 0)
GREEN = (0, 128, 0)
RED = (255, 0, 0)
BGCOLOR = DARKTURQUOISE
TILECOLOR = BLUE
TEXTCOLOR = WHITE
BORDERCOLOR = RED
BASICFONTSIZE = 20
TEXT = GREEN
BUTTONCOLOR = WHITE
BUTTONTEXTCOLOR = BLACK
MESSAGECOLOR = GREEN
# this is to leave the space on both the sides of the block
XMARGIN = int((win_width - (block_size * w_of_board + (w_of_board - 1))) / 2)
YMARGIN = int((win_height - (block_size * h_of_board + (h_of_board - 1))) / 2)
# these are the variables for handling the keyboard keys
UP = 'up'
DOWN = 'down'
LEFT = 'left'
RIGHT = 'right'
# this is the main function
def main():
global FPSCLOCK, DISPLAYSURF, BASICFONT, RESET_SURF, RESET_RECT, NEW_SURF, NEW_RECT, SOLVE_SURF, SOLVE_RECT
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((win_width, win_height))
# we gave a title using set_caption function in pygame
pygame.display.set_caption('Slide Puzzle - CopyAssignment')
BASICFONT = pygame.font.Font('freesansbold.ttf', BASICFONTSIZE)
# these variables stores the various options that will be displayed to the right side of our main grid
# these below only handles the design part of the options
RESET_SURF, RESET_RECT = makeText(
'重置', TEXT, BGCOLOR, win_width - 120, win_height - 310)
NEW_SURF, NEW_RECT = makeText(
'新游戏', TEXT, BGCOLOR, win_width - 120, win_height - 280)
SOLVE_SURF, SOLVE_RECT = makeText(
'求解', TEXT, BGCOLOR, win_width - 120, win_height - 250)
mainBoard, solutionSeq = generateNewPuzzle(80)
# this is simply the board that is same as that of the solved board in Puzzle Game In Python
# bascially the game will shuffle all blocks of the solved game
SOLVEDBOARD = start_playing()
# a list that tracks the moves made from the solved configuration
allMoves = []
# main game loop
while True:
slideTo = None
# the below variable contains the message to show in the top left corner.
msg = 'Click a block or press arrow keys to slide the block.'
if mainBoard == SOLVEDBOARD:
msg = 'Solved!'
drawBoard(mainBoard, msg)
check_exit_req()
# the below for loop is to handle the various events of keyboard
for event in pygame.event.get():
if event.type == MOUSEBUTTONUP:
spotx, spoty = getSpotClicked(
mainBoard, event.pos[0], event.pos[1])
if (spotx, spoty) == (None, None):
# this is to check if the user clicked on an option button
if RESET_RECT.collidepoint(event.pos):
# this below linw will come into action of the user clicked on Reset button
rst_animation(mainBoard, allMoves)
allMoves = []
elif NEW_RECT.collidepoint(event.pos):
# this below linw will come into action of the user clicked on New Game button
mainBoard, solutionSeq = generateNewPuzzle(80)
allMoves = []
elif SOLVE_RECT.collidepoint(event.pos):
# this below linw will come into action of the user clicked on Solve button
rst_animation(mainBoard, solutionSeq + allMoves)
allMoves = []
else:
# this else block in Puzzle Game In Python is just to check that the moved tile has a blank
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
elif event.type == KEYUP:
# this elif block will handle the checking if the user pressed a key to slide a tile
if event.key in (K_LEFT, K_a) and isValidMove(mainBoard, LEFT):
slideTo = LEFT
elif event.key in (K_RIGHT, K_d) and isValidMove(mainBoard, RIGHT):
slideTo = RIGHT
elif event.key in (K_UP, K_w) and isValidMove(mainBoard, UP):
slideTo = UP
elif event.key in (K_DOWN, K_s) and isValidMove(mainBoard, DOWN):
slideTo = DOWN
# this block will handle the fucntionality of displaying the message for controls
if slideTo:
# show slide on screen
sliding_animation(
mainBoard, slideTo, 'Click a block or press arrow keys to slide the block.', 8)
take_turn(mainBoard, slideTo)
allMoves.append(slideTo)
pygame.display.update()
FPSCLOCK.tick(FPS)
def terminate():
pygame.quit()
sys.exit()
def check_exit_req():
# get all the QUIT events
for event in pygame.event.get(QUIT):
# terminate() will kill all the events. terminate if any QUIT events are present
terminate()
# this for loop will get all the KEYUP events
for event in pygame.event.get(KEYUP):
if event.key == K_ESCAPE:
# if the user presses the ESC key then it will terminate the session and if the KEYUP event was for the Esc key
terminate()
# put the other KEYUP event objects back
pygame.event.post(event)
def start_playing():
# Return a board structure with blocks in the solved state.
counter = 1
board = []
for x in range(w_of_board):
column = []
for y in range(h_of_board):
column.append(counter)
counter += w_of_board
board.append(column)
counter -= w_of_board * (h_of_board - 1) + w_of_board - 1
board[w_of_board-1][h_of_board-1] = BLANK
return board
def getBlankPosition(board):
# Return the x and y of board coordinates of the blank space.
for x in range(w_of_board):
for y in range(h_of_board):
if board[x][y] == BLANK:
return (x, y)
def take_turn(board, move):
blankx, blanky = getBlankPosition(board)
if move == UP:
board[blankx][blanky], board[blankx][blanky +
1] = board[blankx][blanky + 1], board[blankx][blanky]
elif move == DOWN:
board[blankx][blanky], board[blankx][blanky -
1] = board[blankx][blanky - 1], board[blankx][blanky]
elif move == LEFT:
board[blankx][blanky], board[blankx +
1][blanky] = board[blankx + 1][blanky], board[blankx][blanky]
elif move == RIGHT:
board[blankx][blanky], board[blankx -
1][blanky] = board[blankx - 1][blanky], board[blankx][blanky]
def isValidMove(board, move):
blankx, blanky = getBlankPosition(board)