Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make photos into jigsaw puzzles through Python

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to turn photos into jigsaw puzzles through Python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Get the effect first

I made a jigsaw puzzle for her with the photos of the sister poetry teacher next door.

As a result, I can't spell my own.

Configure the environment

Install the pygame module

# pip install pygame PS C:\ Users\ lex > pip install pygame Looking in indexes: http://mirrors.aliyun.com/pypi/simple Requirement already satisfied: pygame in f:\ develop\ python36\ lib\ site-packages (2.0.1) PS C:\ Users\ lex > configuration file

Cfg.py

Configure the photo path of the senior sister that needs to be read, and introduce the fonts and colors referenced in the game.

'' profile 'import os' screen size 'SCREENSIZE = (640,640)' read Sister photos' 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 pieces of jigsaw puzzle' 'NUMRANDOM = 100Introduced resources

Add the photos of Sister Poetry to the resources/pictures path.

When the game starts, according to our configuration in cfg.py, the photos of this path will be automatically taken.

Loading becomes the raw material of our puzzle.

Main function code

Pintu.py

Keep the code structure simple. A profile cfg, a resource path resources, stores fonts and images.

The main function code is put here:

1. Define four movable functions that allow you to move in the direction of a space when there is a space.

2. Createboard: randomly split the picture and disrupt it.

3. At the beginning, get a picture randomly from the picture folder: if you want to play games for the whole dormitory

Put everyone's photos in, so that every time you open it, a photo of your sister will be randomly generated as the background of the game.

'' Function: jigsaw puzzle Mini Game author: LexSaints'''import osimport sysimport cfgimport randomimport pygame 'judge whether the game is over' 'def isGameOver (board, size): assert isinstance (size, int) num_cells = size * size for i in range (num_cells-1): if board [I]! = I: return False return True' move the Cell on the left side of the blank Cell to the blank Cell position''def moveR (board). Blank_cell_idx, num_cols): if blank_cell_idx% num_cols = = 0: return blank_cell_idx cell_idx-1 [blank _ cell_idx] = board [blank _ cell_idx], Cell [blank _ cell_idx-1] return blank_cell_idx-1 'move the Cell to the right of the blank Cell to the blank Cell position' 'def moveL (board, blank_cell_idx) Num_cols): if (blank_cell_idx+1)% num_cols = 0: return blank_cell_idx cell_idx+1 [blank _ cell_idx] = board [blank _ cell_idx], Cell [blank _ cell_idx+1] return blank_cell_idx+1 'move the Cell above the blank Cell down to the blank Cell location' '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 '''将空白Cell下边的Cell上移到空白Cell位置'''def moveU(board, blank_cell_idx, num_rows, num_cols): if blank_cell_idx >

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report