Python自制图片拼图小游戏

时间:2022-11-19 09:50:38

前言

唉,怎么说,感觉只有上班的时候摸鱼玩游戏才是最爽的

等于带薪摸鱼,现在不是有点流行说什么 带薪…带薪** 干嘛的

今天也是有点无聊,就想起之前搞了个拼图的小游戏,可以自己更改照片的 嘿嘿

这不刚玩了一局,就来分享给你们了

Python自制图片拼图小游戏

前期准备

字体文件

Python自制图片拼图小游戏

图片素材

Python自制图片拼图小游戏

这个你们可以自己准备,也可以找我拿哈

代码展示

导入模块

源码.点击领取即可

import os
import sys
import random
import pygame
from pygame.locals import *

定义一些常量

BACKGROUNDCOLOR = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
FPS = 40
NUMRANDOM = 100

退出

def Stop():
	pygame.quit()
	sys.exit()

判断游戏是否结束

python学习交流Q群:770699889 ### 源码领取
def isOver(board, size):
	try:
		num_cells = size * size
	except:
		num_cells = size[0] * size[1]
	for i in range(num_cells-1):
		if board[i] != i:
			return False
	return True

将空白Cell左边的Cell右移到空白Cell位置

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

将空白Cell右边的Cell左移到空白Cell位置

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

将空白Cell上边的Cell下移到空白Cell位置

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 >= (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

获得打乱的拼图

python学习交流Q群:770699889 ### 源码领取
def CreateBoard(num_rows, num_cols, num_cells):
	board = []
	for i in range(num_cells):
		board.append(i)
	# 去掉右下角那块
	blank_cell_idx = num_cells - 1
	board[blank_cell_idx] = -1
	for i in range(NUMRANDOM):
		# 0: 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

随机选取一张图片

def GetImagePath(filepath):
	imgs = os.listdir(filepath)
	if len(imgs) == 0:
		raise ValueError('No pictures in <%s>...' % filepath)
	return os.path.join(filepath, random.choice(imgs))

游戏结束界面

源码.点击领取即可

def ShowEndInterface(screen, width, height):
	screen.fill(BACKGROUNDCOLOR)
	font = pygame.font.Font('font/simkai.ttf', width // 8)
	title = font.render('Finished!', True, (233, 150, 122))
	rect = title.get_rect()
	rect.midtop = (width/2, height/2.5)
	screen.blit(title, rect)
	pygame.display.update()
	pygame.time.wait(500)
	while True:
		for event in pygame.event.get():
			if event.type == QUIT:
				Stop()
			elif event.type == KEYDOWN:
				if event.key == K_ESCAPE:
					Stop()

效果展示

Python自制图片拼图小游戏

Python自制图片拼图小游戏

Python自制图片拼图小游戏

Python自制图片拼图小游戏

最后

今天的分享到这里就结束了

给大家推荐一些Python视频教程,希望对大家有所帮助:

Python零基础教学合集

对文章有问题的,或者有其他关于python的问题,可以在评论区留言或者私信我哦
觉得我分享的文章不错的话,可以关注一下我,或者给文章点赞(/≧▽≦)/

Python自制图片拼图小游戏