The treasure puzzle is mysteriously online. Three ways to play brush and explode the circle of friends - players call it directly. It's too addictive.

Introduction

Little friends who have played puzzles should know that even if there are more than 1000 pieces of puzzles, the position of each piece has its own position. If they are misspelled, they are not right.

But when you embed each piece of puzzle in its place, the sense of satisfaction and achievement is hard to forget after trying once.

Jigsaw puzzle is a game with almost no threshold. It just completes a simple thing attentively and quietly, and slowly restores the scattered and disordered fragments into a complete small world.

Let you concentrate on enjoying a few hours of quiet time without distractions.

This time, our store has arranged a puzzle for you to relieve your boredom. The difficulty is from high to low. There are 3 * 3 pieces, 4 * 4 pieces and 5 * 5 pieces.

text

Today's update - I learned a big man's article, sorted it out, and then made a puzzle game of school flowers and school grass!

This puzzle game - from simple to difficult, if you have broken through and have someone you like, be brave to express it ~ let him and her know your mind!

Start——

Show the effect first:

Proud ~ ha ha ha ha, it's all done ha! My technology is still good. When I am a person, this is the simplest 3 * 3 mode, and the later more difficult mode will be handed over to you!

The game steps are as follows:

(1) First, install the corresponding environment.

Python, pycharm, pygame modules.

pip install  pygame

(2) Configuration file.

'''Screen size'''
SCREENSIZE = (640, 640)
'''Picture material root directory'''
PICTURE_ROOT_DIR = os.path.join(os.getcwd(), 'resources/pictures')
'''Font path'''
FONTPATH = os.path.join(os.getcwd(), 'resources/font/FZSTK.TTF')
'''Define some colors'''
BACKGROUNDCOLOR = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
'''FPS'''
FPS = 40
'''Number of random puzzles'''
NUMRANDOM = 100

(3)The blank space moves up, down, left and right.

'''Leave blank Cell sinister Cell Move right to blank Cell position'''
def moveR(board, blank_cell_idx, num_cols):
    if blank_cell_idx % num_cols == 0: return blank_cell_idx
    board[blank_cell_idx-1], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx-1]
    return blank_cell_idx - 1


'''Leave blank Cell dexter Cell Move left to blank Cell position'''
def moveL(board, blank_cell_idx, num_cols):
    if (blank_cell_idx+1) % num_cols == 0: return blank_cell_idx
    board[blank_cell_idx+1], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx+1]
    return blank_cell_idx + 1


'''Leave blank Cell Upper Cell Move down to blank Cell position'''
def moveD(board, blank_cell_idx, num_cols):
    if blank_cell_idx < num_cols: return blank_cell_idx
    board[blank_cell_idx-num_cols], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx-num_cols]
    return blank_cell_idx - num_cols


'''Leave blank Cell Below Cell Move up to blank Cell position'''
def moveU(board, blank_cell_idx, num_rows, num_cols):
    if blank_cell_idx >= (num_rows-1) * num_cols: return blank_cell_idx
    board[blank_cell_idx+num_cols], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx+num_cols]
    return blank_cell_idx + num_cols

(4) Game interface.

def ShowStartInterface(screen, width, height):
    screen.fill(cfg.BACKGROUNDCOLOR)
    tfont = pygame.font.Font(cfg.FONTPATH, width//4)
    cfont = pygame.font.Font(cfg.FONTPATH, width//20)
    title = tfont.render('School flower puzzle', True, cfg.RED)
    content1 = cfont.render('Press the corresponding key to start the game', True, cfg.BLUE)
    content2 = cfont.render('H—High difficulty, M—intermediate, L—simple', True, cfg.BLUE)
    trect = title.get_rect()
    trect.midtop = (width/2, height/10)
    crect1 = content1.get_rect()
    crect1.midtop = (width/2, height/2.2)
    crect2 = content2.get_rect()
    crect2.midtop = (width/2, height/1.8)
    screen.blit(title, trect)
    screen.blit(content1, crect1)
    screen.blit(content2, crect2)
    while True:
        for event in pygame.event.get():
            if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == ord('l'): return 3
                elif event.key == ord('m'): return 4
                elif event.key == ord('h'): return 5
        pygame.display.update()

As shown in the figure below:

summary

When the full text is finished, the article is finished. Ha ~ take it and have a try!

Source code free of charge: private letter Xiaobian 01 can 💖

Welcome to read. If it helps you, remember to learn, grow and communicate together!

Tags: Python pygame

Posted on Sat, 30 Oct 2021 00:58:38 -0400 by inutero