中秋节是中国四大传统节日之一,主要活动包括赏月、吃月饼、玩花灯、赏桂花、饮桂花酒等。
给大家分享用Python制作的一个中秋的拼图小游戏
模块安装
pip install pygame
实例代码
import pygame, sys, random
from pygame.locals import *
# 一些常量
# WINDOW_WIDTH = 30
# WINDOW_HEIGHT = 10
BACKG_ROUND_COLOR = (255, 255, 255)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
leng = 40
cubenum = 3
totalnum = cubenum * cubenum
maxtime = 100
# 退出
def close():
pygame.quit()
sys.exit()
# 随机生成游戏盘面
def newGameBoard():
board = []
for i in range(totalnum):
board.append(i)
blackCell = totalnum - 1
board[blackCell] = -1
for i in range(maxtime):
direction = random.randint(0, 3)
if (direction == 0):
blackCell = moveLeft(board, blackCell)
elif (direction == 1):
blackCell = moveRight(board, blackCell)
elif (direction == 2):
blackCell = moveUp(board, blackCell)
elif (direction == 3):
blackCell = moveDown(board, blackCell)
return board, blackCell
# 若空白图像块不在最左边,则将空白块左边的块移动到空白块位置
def moveRight(board, blackCell):
if blackCell % cubenum == 0:
return blackCell
board[blackCell - 1], board[blackCell] = board[blackCell], board[blackCell - 1]
return blackCell - 1
# 若空白图像块不在最右边,则将空白块右边的块移动到空白块位置
def moveLeft(board, blackCell):
if blackCell % cubenum == cubenum - 1:
return blackCell
board[blackCell + 1], board[blackCell] = board[blackCell], board[blackCell + 1]
return blackCell + 1
# 若空白图像块不在最上边,则将空白块上边的块移动到空白块位置
def moveDown(board, blackCell):
if blackCell < cubenum:
return blackCell
board[blackCell - cubenum], board[blackCell] = board[blackCell], board[blackCell - cubenum]
return blackCell - cubenum
# 若空白图像块不在最下边,则将空白块下边的块移动到空白块位置
def moveUp(board, blackCell):
if blackCell >= totalnum - cubenum:
return blackCell
board[blackCell + cubenum], board[blackCell] = board[blackCell], board[blackCell + cubenum]
return blackCell + cubenum
# 是否完成
def isFinished(board, blackCell):
for i in range(totalnum - 1):
if board[i] != i:
return False
return True
# 初始化
pygame.init()
mainClock = pygame.time.Clock()
# 加载图片
# 放一张本地图片
gameobject = pygame.image.load(r"D:\Users\Downloads\0815(1).png")
gameRect = gameobject.get_rect()
print(gameRect)
# 设置窗口,窗口的宽度和高度取决于图片的宽高
windowSurface = pygame.display.set_mode((gameRect.width, gameRect.height))
pygame.display.set_caption('python拼图中秋快乐-度假的小鱼')
cellWidth = int(gameRect.width / cubenum)
cellHeight = int(gameRect.height / cubenum)
finish = False
gameBoard, blackCell = newGameBoard()
# 游戏主循环
while True:
for event in pygame.event.get():
if event.type == QUIT:
close()
if finish:
continue
if event.type == KEYDOWN:
if event.key == K_LEFT or event.key == ord('a'):
blackCell = moveLeft(gameBoard, blackCell)
if event.key == K_RIGHT or event.key == ord('d'):
blackCell = moveRight(gameBoard, blackCell)
if event.key == K_UP or event.key == ord('w'):
blackCell = moveUp(gameBoard, blackCell)
if event.key == K_DOWN or event.key == ord('s'):
blackCell = moveDown(gameBoard, blackCell)
if event.type == MOUSEBUTTONDOWN and event.button == 1:
x, y = pygame.mouse.get_pos()
col = int(x / cellWidth)
row = int(y / cellHeight)
index = col + row * cubenum
if (
index == blackCell - 1 or index == blackCell + 1 or index == blackCell - cubenum or index == blackCell + cubenum):
gameBoard[blackCell], gameBoard[index] = gameBoard[index], gameBoard[blackCell]
blackCell = index
if (isFinished(gameBoard, blackCell)):
gameBoard[blackCell] = totalnum - 1
finish = True
windowSurface.fill(BACKG_ROUND_COLOR)
for i in range(totalnum):
rowDst = int(i / cubenum)
colDst = int(i % cubenum)
rectDst = pygame.Rect(colDst * cellWidth, rowDst * cellHeight, cellWidth, cellHeight)
if gameBoard[i] == -1:
continue
rowArea = int(gameBoard[i] / cubenum)
colArea = int(gameBoard[i] % cubenum)
rectArea = pygame.Rect(colArea * cellWidth, rowArea * cellHeight, cellWidth, cellHeight)
windowSurface.blit(gameobject, rectDst, rectArea)
for i in range(cubenum + 1):
pygame.draw.line(windowSurface, BLACK, (i * cellWidth, 0), (i * cellWidth, gameRect.height))
for i in range(cubenum + 1):
pygame.draw.line(windowSurface, BLACK, (0, i * cellHeight), (gameRect.width, i * cellHeight))
pygame.display.update()
mainClock.tick(leng)
注释:
游戏框取决于图片大小。
文末福利
最后,给大家分享一份全套的 Python 学习资料,给那些想学习 Python 的朋友们一点帮助!
Python大礼包:《2024年最新Python全套学习资料包】点这里免费领取!
包括:Python激活码+安装包、Python
web开发,Python爬虫,Python数据分析,人工智能、自动化办公等学习教程。带你从零基础系统性的学好Python!
① Python所有方向的学习路线图,清楚各个方向要学什么东西
② 100多节Python课程视频,涵盖必备基础、爬虫和数据分析
③ 100多个Python实战案例,学习不再是只会理论
④ 华为出品独家Python漫画教程,手机也能学习