前言
嗨喽~大家好呀,这里是魔王呐 !
拼图游戏是广受欢迎的一种智力游戏,它的变化多端,难度不一
它分为单面拼图、双面拼图、立体拼图、球形拼图、虚拟拼图
今天我就给带来虚拟的单面拼图小游戏
准备素材
字体文件
图片素材
这个大家可以自行准备或者在文末推广找作者领取哦~
图片可以准备大家喜欢的人物、风景、美女等等哦~不限哒
代码展示
🎯 这里仅展示部分代码,完整的免费源码领取处:点击
导入模块
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()
判断游戏是否结束
# 源码解答加Q裙:926207505
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
获得打乱的拼图
# 源码解答加Q裙:926207505
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)
# 源码解答加Q裙:926207505
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视频给大家,希望对大家有所帮助:
尾语
🏵
🏵 小时候的要求:吃好睡好学习好;
🏵 长大了的期盼:吃饱睡饱卡里饱;
🏵 小时候遇见困难,总希望超人出现;
🏵 长大了遇见困难,却只靠自己摆平。
🏵 人生坎坷,需坚强面对!
————————心灵鸡汤
转载:https://blog.csdn.net/python56123/article/details/127578965
查看评论