基于Python实现自制拼图小游戏
咱们Python 集中营有一个专题就是分享一些有意思的东西,今天大概看了一下pygame的这个非标准库就想着使用它来做个小游戏-拼图。
通过加入自己定义的图片,对这个图片完成一定数量的拆分后再将拆分后的小图片进行随机打乱,这也算是实现一个拼图小游戏的基本思路吧。
为了将其做成一个桌面应用,这里使用的是pygame这一个非标准库。python环境中还没有pygame的话可以选择使用pip的方式安装一下。
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/
下面将开发中使用到的python库都列举一下,除了pygame处理游戏之外,还有sys、random分别用来做系统操作和随机数的处理。
# Importing the pygame, sys, and random modules.
import pygame, sys, random
# Importing all the constants from the pygame.locals module.
from pygame.locals import *
创建一个python类GameMain,将所有的游戏处理相关的业务全部写到这个类中进行处理,包括一些初始化操作等等,最后我们通过main函数调用启动整个拼图游戏。
class GameMain():
def __init__(self, window_width, window_height, background_color,
fps, colums, max_round_time, game_image):
self.init_static_data(window_width=window_width, window_height=window_height,
background_color=background_color, fps=fps, colums=colums, max_round_time=max_round_time)
self.init_game_data(game_image='./wd.jpeg')
self.init_game_main()
通过GameMain类的初始化函数init来做类的初始化并且分别调用init_static_data、init_game_data、init_game_main三个主要的实现函数。
init_static_data函数主要用来将一些全局的静态参数进行初始化赋值,为了避免没有值时候的报错默认分别给这些参数都赋值,包括背景颜色、拼图游戏分割成小图片的列数等等。
def init_static_data(self, window_width=500, window_height=500,
background_color=(255, 255, 255), fps=40, colums=3, max_round_time=100):
"""
The function initializes the game data and the game main
:param window_width: The width of the game window
:param window_height: The height of the game window
:param background_color: The background color of the game window
:param fps: The number of frames per second that the game will run at
:param colums: The number of columns in the game
:param max_round_time: The maximum time for each round
:param game_image: The image that will be used for the game
"""
self.window_width = window_width
self.window_height = window_height
self.background_color = background_color
self.black_color = (0, 0, 0)
self.fps = fps
self.colums = colums
self.cell_nums = self.colums * self.colums
self.max_round_time = max_round_time
init_game_data函数,主要将游戏执行过程中的一些参数进行计算或者计算之后的数据值进行保存,因为在后面的游戏主循环中肯定是需要用到的,包括游戏载入的一整张图片的路径以及游戏窗口的标题等等。
def init_game_data(self, game_image='./wd.jpeg'):
"""
> This function initializes the game data by reading the game image and extracting the game data from it
:param game_image: The image of the game you want to play, defaults to ./wd.jpeg (optional)
"""
pygame.init()
self.main_clock = pygame.time.Clock()
self.game_image = pygame.image.load(game_image)
self.game_rect = self.game_image.get_rect()
self.window_surface = pygame.display.set_mode((self.game_rect.width, self.game_rect.height))
pygame.display.set_caption('拼图游戏')
self.cell_width = int(self.game_rect.width / self.colums)
self.cell_height = int(self.game_rect.height / self.colums)
self.finished = False
self.game_board, self.black_cell = self.generate_game_borad()
init_game_main函数中加入了死循环的方式让游戏一直处于执行中的状态,除非是已经完成了游戏或是直接退出游戏了才会停止。主要实现的是游戏步骤以及键盘的监听或鼠标的点击事件维持整个游戏状态的运行。
def init_game_main(self):
"""
> This function initializes the game data by reading the game image and extracting the game data from it
:param game_image: The image of the game you want to play, defaults to ./wd.jpeg (optional), defaults to ./wd.jpeg
(optional)
"""
while True:
for event in pygame.event.get():
if event.type == QUIT:
self.game_exit()
if self.finished:
continue
if event.type == KEYDOWN:
if event.key == K_LEFT or event.key == ord('a'):
self.black_cell = self.move_left(self.game_board, self.black_cell)
if event.key == K_RIGHT or event.key == ord('d'):
self.black_cell = self.move_right(self.game_board, self.black_cell)
if event.key == K_UP or event.key == ord('w'):
self.black_cell = self.move_up(self.game_board, self.black_cell)
if event.key == K_DOWN or event.key == ord('s'):
self.black_cell = self.move_down(self.game_board, self.black_cell)
if event.type == MOUSEBUTTONDOWN and event.button == 1:
x, y = pygame.mouse.get_pos()
col = int(x / self.cell_width)
row = int(y / self.cell_height)
index = col + row * self.colums
if (
index == self.black_cell - 1 or index == self.black_cell + 1 or index == self.black_cell - self.colums or index == self.black_cell + self.colums):
self.game_board[self.black_cell], self.game_board[index] = self.game_board[index], \
self.game_board[self.black_cell]
self.black_cell = index
if (self.is_finished(self.game_board)):
self.game_board[self.black_cell] = self.cell_nums - 1
self.finished = True
self.window_surface.fill(self.background_color)
for i in range(self.cell_nums):
row_dst = int(i / self.colums)
col_dst = int(i % self.colums)
rect_dst = pygame.Rect(col_dst * self.cell_width, row_dst * self.cell_height, self.cell_width,
self.cell_height)
if self.game_board[i] == -1:
continue
row_area = int(self.game_board[i] / self.colums)
col_area = int(self.game_board[i] % self.colums)
rect_area = pygame.Rect(col_area * self.cell_width, row_area * self.cell_height, self.cell_width,
self.cell_height)
self.window_surface.blit(self.game_image, rect_dst, rect_area)
for i in range(self.colums + 1):
pygame.draw.line(self.window_surface, self.black_color, (i * self.cell_height, 0),
(i * self.cell_width, self.game_rect.height))
for i in range(self.colums + 1):
pygame.draw.line(self.window_surface, self.black_color, (0, i * self.cell_height),
(self.game_rect.width, i * self.cell_height))
pygame.display.update()
self.main_clock.tick(self.fps)
game_exit函数,执行游戏退出操作。
def game_exit(self):
"""
It exits the game.
"""
pygame.quit()
sys.exit()
generate_game_borad函数,生成游戏运行的主布局。
def generate_game_borad(self):
"""
It generates the game board.
"""
board = []
for i in range(self.cell_nums):
board.append(i)
black_cell = self.cell_nums - 1
board[black_cell] = -1
for i in range(self.max_round_time):
direction = random.randint(0, 3)
if (direction == 0):
black_cell = self.move_left(board, black_cell)
elif (direction == 1):
black_cell = self.move_right(board, black_cell)
elif (direction == 2):
black_cell = self.move_up(board, black_cell)
elif (direction == 3):
black_cell = self.move_down(board, black_cell)
return board, black_cell
move_right函数,执行向右移动的操作。
def move_right(self, board, black_cell):
"""
> The function `move_right` takes in a board and a black cell and returns the board after the black cell has moved
right
:param board: the board that the game is being played on
:param black_cell: the cell that is currently black
"""
if black_cell % self.colums == 0:
return black_cell
board[black_cell - 1], board[black_cell] = board[black_cell], board[black_cell - 1]
return black_cell - 1
move_left函数,执行向左移动的操作。
def move_left(self, board, black_cell):
"""
It moves the black cell to the left.
:param board: the board that the game is being played on
:param black_cell: the cell that is currently black
"""
if black_cell % self.colums == self.colums - 1:
return black_cell
board[black_cell + 1], board[black_cell] = board[black_cell], board[black_cell + 1]
return black_cell + 1
move_down函数,执行向下移动的操作。
def move_left(self, board, black_cell):
"""
It moves the black cell to the left.
:param board: the board that the game is being played on
:param black_cell: the cell that is currently black
"""
if black_cell % self.colums == self.colums - 1:
return black_cell
board[black_cell + 1], board[black_cell] = board[black_cell], board[black_cell + 1]
return black_cell + 1
def move_down(self, board, black_cell):
"""
It moves the black cell down.
:param board: the board that the game is being played on
:param black_cell: the cell that the player is currently on
"""
if black_cell < self.colums:
return black_cell
board[black_cell - self.colums], board[black_cell] = board[black_cell], board[black_cell - self.colums]
return black_cell - self.colums
move_up函数,执行向下移动的操作。
def move_up(self, board, black_cell):
"""
It moves the black cell up one space.
:param board: the board that the game is being played on
:param black_cell: the cell that the player is currently on
"""
if black_cell >= self.cell_nums - self.colums:
return black_cell
board[black_cell + self.colums], board[black_cell] = board[black_cell], board[black_cell + self.colums]
return black_cell + self.colums
is_finished函数,校验拼图是否已经完成的操作。
def is_finished(self, board):
"""
If the board is full, the game is over
:param board: a 2D array representing the current state of the board. The board is composed of 3 characters: 'X',
'O', and ' '. 'X' and 'O' represent the two players. ' ' represents an empty space
"""
for i in range(self.cell_nums - 1):
if board[i] != i:
return False
return True
最后,只需要通过main的主函数将这个游戏应用调用就可以直接打开拼图游戏了,并且可以设置相关的图片参数想对哪个图片拼图就设置哪个,一般建议设置1024像素以内的图片效果会比较好。
# This is a special variable in Python that is set when the program is run. If the program is being run directly (as
# opposed to being imported), then `__name__` will be set to `'__main__'`.
if __name__ == '__main__':
"""
It moves the black cell up one space
:param board: the board that the game is being played on
:param black_cell: the cell that the player is currently on
:return: The new position of the black cell.
"""
GameMain(window_width=500, window_height=500,
background_color=(255, 255, 255), fps=40,
colums=3, max_round_time=100, game_image='./wd.jpeg')
以上就是基于Python实现自制拼图小游戏的详细内容,更多关于Python拼图游戏的资料请关注编程网其它相关文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341