Review past

Python implements the “bunny and Bun” game

Python to achieve eight notes small game

Preface:

Today we are going to make a jigsaw puzzle. Let’s begin happily

Here’s what it looks like:

The development tools

Python version: 3.6.4

Related modules:

Pygame module;

And some of the modules that come with Python

Environment set up

Install Python and add it to the environment variable, and the PIP will install the appropriate module.

The principle is introduced

About the game:

The image is divided into m×n rectangular blocks, and the rectangular blocks in the lower right corner of the image are replaced with blank blocks, and these rectangular blocks are randomly placed into the shape of the original image. The goal of the game is to restore a randomly placed image to its original appearance by moving a non-blank block, and the movement is only required to move a non-blank block to a blank block.

For example:

Step by step:

Step1: the initial interface of the game

Since it’s a game, there has to be an initial interface, right?

OK, let’s write a game initial screen:

Def showStartInterface (screen, width, height): screen.fill(cfg.BACKGROUNDCOLOR) tfont = pygame.font.Font(cfg.FONTPATH, Width //4) cFONT = PyGame.font-size (cfg.fontpath, width//20) title = tFONT.render (' Puppet ', True, Cfg. RED) content1 = CFONT. render(' Press H or M or L to start the game ', True, cfg.BLUE) content2 = CFONT. render('H = 5*5 mode, M = 4*4 mode, L = 3*3 mode ', True, cfg.BLUE) trect = title.get_rect() trect.midtop = (width/2, height/10) crect1 = content1.get_rect() crect1.midtop = (width/2, Height /2) cRect2 = content2.get_rect() cRect2. MidTop = (width/2, height/1.8) screen.blit(title, 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()

Depending on the player’s own level, you can choose different difficulty puzzles.

Step2: Define the move operation

The purpose of the movement action is to move the puzzle, which is very simple to implement:

"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 def moveL(board, blank_cell_idx) return blank_cell_idx-1 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 "" def moveD(board, blank_cell_idx, 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], Blank_cell_idx-num_cols board[blank_cell_idx-num_cols] return blank_cell_idx-num_cols" 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

Step3: Main interface of the game

OK, now that we’ve set the stage, we can start implementing our game’s main interface.

First, we need to scramble the puzzle, but random scramble is likely to lead to no solution for the puzzle, so we move the puzzle randomly to achieve the effect of scrambling the puzzle, which is the main reason why we define the movement of the puzzle in the first place:

Def createBoard (num_rows, num_cols, num_cells): board = [] for I in range(num_cells): Blank_cell_idx = blank_cell_idx; blank_cell_idx = blank_cell_idx; blank_cell_idx = blank_cell_idx; blank_cell_idx = blank_cell_idx; blank_cell_idx = blank_cell_idx; blank_cell_idx = blank_cell_idx; left, 1: right, 2: up, 3: down direction = random.randint(0, 3) if direction == 0: blank_cell_idx = moveL(board, blank_cell_idx, num_cols) elif direction == 1: blank_cell_idx = moveR(board, blank_cell_idx, num_cols) elif direction == 2: blank_cell_idx = moveU(board, blank_cell_idx, num_rows, num_cols) elif direction == 3: blank_cell_idx = moveD(board, blank_cell_idx, num_cols) return board, blank_cell_idx

Game main interface initialization:

Finally, the main interface display refresh and event response functions are realized:

while True: game_board, blank_cell_idx = CreateBoard(num_rows, num_cols, num_cells) if not isGameOver(game_board, size): Break # is_running = True while is_running: # -- event capture 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() # ---- Keyboard Action elif Event. Type == PyGame.KeyDown: if event.key == pygame.K_LEFT or event.key == ord('a'): blank_cell_idx = moveL(game_board, blank_cell_idx, num_cols) elif event.key == pygame.K_RIGHT or event.key == ord('d'): blank_cell_idx = moveR(game_board, blank_cell_idx, num_cols) elif event.key == pygame.K_UP or event.key == ord('w'): blank_cell_idx = moveU(game_board, blank_cell_idx, num_rows, num_cols) elif event.key == pygame.K_DOWN or event.key == ord('s'): blank_cell_idx = moveD(game_board, blank_cell_idx, Num_cols) # ---- Mouse manipulation elif event.type == PyGame.MouseButtonDown and event.button == 1: x, y = pygame.mouse.get_pos() x_pos = x // cell_width y_pos = y // cell_height idx = x_pos + y_pos * num_cols if idx == blank_cell_idx-1: blank_cell_idx = moveR(game_board, blank_cell_idx, num_cols) elif idx == blank_cell_idx+1: blank_cell_idx = moveL(game_board, blank_cell_idx, num_cols) elif idx == blank_cell_idx+num_cols: blank_cell_idx = moveU(game_board, blank_cell_idx, num_rows, num_cols) elif idx == blank_cell_idx-num_cols: Blank_cell_idx = moveD(game_board, blank_cell_idx, num_cols) # -- if isGameOver(game_board, size): Game_board [blank_cell_idx] = num_cell-1 is_running = False # -- update screen screen.fill(cfg.BACKGROUNDCOLOR) for I in range(num_cells): if game_board[i] == -1: continue x_pos = i // num_cols y_pos = i % num_cols rect = pygame.Rect(y_pos*cell_width, x_pos*cell_height, cell_width, cell_height) img_area = pygame.Rect((game_board[i]%num_cols)*cell_width, (game_board[i]//num_cols)*cell_height, cell_width, cell_height) screen.blit(game_img_used, rect, img_area) for i in range(num_cols+1): pygame.draw.line(screen, cfg.BLACK, (i*cell_width, 0), (i*cell_width, game_img_used_rect.height)) for i in range(num_rows+1): pygame.draw.line(screen, cfg.BLACK, (0, i*cell_height), (game_img_used_rect.width, i*cell_height)) pygame.display.update() clock.tick(cfg.FPS)

Step4: End of game interface

When the player completes the puzzle, the end of the game screen needs to be displayed, which is similar to the initial screen of the game, which is relatively simple to implement:

Def showendInterface (screen, width, height): Screen.fill (cfg.backgroundColor) font-size = PyGame.font-size (cfg.fontPath, width//15) title = font-size (' You completed the jigsaw puzzle! Rect.midTop = (width/2, height/2.5) screen.blit(width/2, height/2.5)) rect) pygame.display.update() 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() pygame.display.update()

This is the end of the article, thank you for watching, PYTHON 24 games series, the next article to share skiing games

To thank you readers, I’d like to share with you some of my recent collection of dry programming goodies and give something back to every reader in the hope of helping you out.

Dry goods mainly include:

① More than 2000 Python e-books (mainstream and classic books are available)

(2) The Python Standard Library (Chinese version)

③ project source code (40 or 50 interesting and classic hands-on project and source code)

④Python basic introduction, crawler, web development, big data analysis video (suitable for small white learning)

⑤Python Learning Roadmap (Goodbye to Slow Learning)

⑥ Two days of Python crawler training camp live access

All done~ thumb up + Comments ~ See bio or private message for complete source code.