教你使用Python的pygame模块实现拼图游戏

作者:飞扬青春88 时间:2022-06-28 03:20:20 

pygame介绍

Python Pygame 是一款专门为开发和设计 2D 电子游戏而生的软件包,它支 Windows、Linux、Mac OS 等操作系统,具有良好的跨平台性。Pygame 由 Pete Shinners 于 2000 年开发而成,是一款免费、开源的的软件包,因此您可以放心地使用它来开发游戏,不用担心有任何费用产生。

Pygame 在 SDL(Simple DirectMedia Layer,使用 C语言编写的多媒体开发库) 的基础上开发而成,它提供了诸多操作模块,比如图像模块(image)、声音模块(mixer)、输入/输出(鼠标、键盘、显示屏)模块等。相比于开发 3D 游戏而言,Pygame 更擅长开发 2D 游戏,比如于飞机大战、贪吃蛇、扫雷等游戏。

安装pygame

pip install pygame

pygame常用模块

  • pygame.cdrom 访问光驱

  • pygame.cursors 加载光标

  • pygame.display 访问显示设备

  • pygame.draw 绘制形状、线和点

  • pygame.event 管理事件

  • pygame.font 使用字体

  • pygame.image 加载和存储图片

  • pygame.joystick 使用游戏手柄或者类似的东西

  • pygame.key 读取键盘按键

  • pygame.mixer 声音

  • pygame.mouse 鼠标

  • pygame.movie 播放视频

  • pygame.music 播放音频

  • pygame.overlay 访问高级视频叠加

  • pygame.rect 管理矩形区域

  • pygame.scrap 本地剪贴板访问

  • pygame.sndarray 操作声音数据

  • pygame.sprite 操作移动图像

  • pygame.surface 管理图像和屏幕

  • pygame.surfarray 管理点阵图像数据

  • pygame.time 管理时间和帧信息

  • pygame.transform 缩放和移动图像

pygame入门案例

import pygame
import sys

pygame.init()  # 初始化pygame
size = width, height = 320, 240  # 设置窗口大小
screen = pygame.display.set_mode(size)  # 显示窗口

while True:  # 死循环确保窗口一直显示
   for event in pygame.event.get():  # 遍历所有事件
       if event.type == pygame.QUIT:  # 如果单击关闭窗口,则退出
           sys.exit()

pygame.quit()  # 退出pygame

pygame实现拼图游戏

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

# 一些常量
WINDOWWIDTH = 500
WINDOWHEIGHT = 500
BACKGROUNDCOLOR = (255, 255, 255)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
FPS = 40

VHNUMS = 3
CELLNUMS = VHNUMS * VHNUMS
MAXRANDTIME = 100

# 退出
def terminate():
   pygame.quit()
   sys.exit()

# 随机生成游戏盘面
def newGameBoard():
   board = []
   for i in range(CELLNUMS):
       board.append(i)
   blackCell = CELLNUMS - 1
   board[blackCell] = -1

for i in range(MAXRANDTIME):
       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 % VHNUMS == 0:
       return blackCell
   board[blackCell - 1], board[blackCell] = board[blackCell], board[blackCell - 1]
   return blackCell - 1

# 若空白图像块不在最右边,则将空白块右边的块移动到空白块位置
def moveLeft(board, blackCell):
   if blackCell % VHNUMS == VHNUMS - 1:
       return blackCell
   board[blackCell + 1], board[blackCell] = board[blackCell], board[blackCell + 1]
   return blackCell + 1

# 若空白图像块不在最上边,则将空白块上边的块移动到空白块位置
def moveDown(board, blackCell):
   if blackCell < VHNUMS:
       return blackCell
   board[blackCell - VHNUMS], board[blackCell] = board[blackCell], board[blackCell - VHNUMS]
   return blackCell - VHNUMS

# 若空白图像块不在最下边,则将空白块下边的块移动到空白块位置
def moveUp(board, blackCell):
   if blackCell >= CELLNUMS - VHNUMS:
       return blackCell
   board[blackCell + VHNUMS], board[blackCell] = board[blackCell], board[blackCell + VHNUMS]
   return blackCell + VHNUMS

# 是否完成
def isFinished(board, blackCell):
   for i in range(CELLNUMS - 1):
       if board[i] != i:
           return False
   return True

# 初始化
pygame.init()
mainClock = pygame.time.Clock()

# 加载图片
gameImage = pygame.image.load('1.jpg')
gameRect = gameImage.get_rect()

# 设置窗口,窗口的宽度和高度取决于图片的宽高
windowSurface = pygame.display.set_mode((gameRect.width, gameRect.height))
pygame.display.set_caption('拼图')

cellWidth = int(gameRect.width / VHNUMS)
cellHeight = int(gameRect.height / VHNUMS)

finish = False

gameBoard, blackCell = newGameBoard()

# 游戏主循环
while True:
   for event in pygame.event.get():
       if event.type == QUIT:
           terminate()
       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 * VHNUMS
           if (
                   index == blackCell - 1 or index == blackCell + 1 or index == blackCell - VHNUMS or index == blackCell + VHNUMS):
               gameBoard[blackCell], gameBoard[index] = gameBoard[index], gameBoard[blackCell]
               blackCell = index

if (isFinished(gameBoard, blackCell)):
       gameBoard[blackCell] = CELLNUMS - 1
       finish = True

windowSurface.fill(BACKGROUNDCOLOR)

for i in range(CELLNUMS):
       rowDst = int(i / VHNUMS)
       colDst = int(i % VHNUMS)
       rectDst = pygame.Rect(colDst * cellWidth, rowDst * cellHeight, cellWidth, cellHeight)

if gameBoard[i] == -1:
           continue

rowArea = int(gameBoard[i] / VHNUMS)
       colArea = int(gameBoard[i] % VHNUMS)
       rectArea = pygame.Rect(colArea * cellWidth, rowArea * cellHeight, cellWidth, cellHeight)
       windowSurface.blit(gameImage, rectDst, rectArea)

for i in range(VHNUMS + 1):
       pygame.draw.line(windowSurface, BLACK, (i * cellWidth, 0), (i * cellWidth, gameRect.height))
   for i in range(VHNUMS + 1):
       pygame.draw.line(windowSurface, BLACK, (0, i * cellHeight), (gameRect.width, i * cellHeight))

pygame.display.update()

mainClock.tick(FPS)

来源:https://blog.csdn.net/qq_23488347/article/details/123115713

标签:pygame,拼图,游戏
0
投稿

猜你喜欢

  • python assert断言的实例用法

    2023-06-24 22:15:42
  • 解决pycharm安装后代码区不能编辑的问题

    2022-07-26 22:26:24
  • D3.js 实现带伸缩时间轴拓扑图的示例代码

    2024-05-09 10:20:13
  • Java中@Pattern注解常用的校验正则表达式学习笔记

    2022-08-07 11:12:10
  • sql server自动生成拼音首字母的函数

    2024-01-25 15:27:19
  • Python绘制简单散点图的方法

    2023-02-22 02:01:07
  • 利用python中pymysql操作MySQL数据库的新手指南

    2024-01-24 22:09:49
  • 大数据量高并发的数据库优化详解

    2024-01-18 14:21:50
  • JavaScript资源预加载组件和滑屏组件的使用推荐

    2024-04-22 22:35:11
  • ant design vue datepicker日期选择器中文化操作

    2024-05-09 10:51:35
  • 用ASP+CSS实现随机背景

    2007-09-26 12:33:00
  • 玩体验,先忘掉自己

    2010-01-30 13:36:00
  • 企业网站该怎么做?

    2009-06-29 16:11:00
  • Python元组 tuple的概念与基本操作详解【定义、创建、访问、计数、推导式等】

    2022-12-08 10:14:45
  • python中reduce()函数的使用方法示例

    2021-08-09 12:32:53
  • Python实现将Excel转换为json的方法示例

    2023-10-13 00:59:38
  • Python3.5字符串常用操作实例详解

    2023-08-31 00:25:53
  • 使用python的turtle函数绘制一个滑稽表情

    2021-06-17 06:19:50
  • Python中类型关系和继承关系实例详解

    2023-10-08 06:15:06
  • IE 下 href 的 BUG

    2008-11-10 12:32:00
  • asp之家 网络编程 m.aspxhome.com