python做小游戏之一小迷宫游戏

Posted jackwsd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python做小游戏之一小迷宫游戏相关的知识,希望对你有一定的参考价值。

趣味python一迷宫小游戏

既然是编写小游戏,那么自然少不了pygame模块,编译环境使用的是pycharm。

迷宫小游戏设计思想是,我们自己绘制迷宫地图文档,然后程序根据我们设计的地图把迷宫绘制到pygame游戏界面当中来。因为本人手残党,所以所有图片素材都来源于网络,如有侵权,我立刻销毁。

首先设计地图,这里一共设计了四个关卡的地图,其中前2关是我自己设计的,第3关地图版权归属了我们家的大哥,三年级逍遥哥。

上地图:

这个地图是存放在txt文档中的,所有的“w”都会被程序绘制成墙,“p”是角色出现的位置,“t”是树,“r”是岩石, “g”是目的地,“k”是钥匙的位置,“d”是门的位置。那么第一张图的绘制结果是这样的:

控制小人走到星星的位置就算游戏成功了,进入下一关。

第二关

第三关

由于设计地图能力太有限,也就做了三关的地图,高手可以自己做新地图。

整个游戏的思路就是,首先从地图文档中获取到每一关的地图,然后把地图数据存放到字典变量maze_map_dic中。然后根据关卡取出每一关的地图数据,再根据对应的地图数据再pygame中绘制出对应的图形。

控制方式为上下左右控制角色移动,同时角色移动之前,判断该移动是否合法,如果合法,则交换迷宫对应位置的内容,然后再次绘制地图,造成角色移动的假象。

需要注意的地方是,当地图游玩游戏时,字典变量里的地图数据会根据游玩的不同而发生变化,所以如果将来要选关或者别的情况需要重置地图是,那么我们就需要在使用时深度拷贝地图信息到新的变量中为好。

程序代码:

import random
import copy
import pygame

fps = 30
fps_clock = pygame.time.Clock()
screen_width = 1024
screen_height = 768

display = pygame.display.set_mode((screen_width, screen_height), 0, 32)
pygame.display.set_caption(‘迷宫小游戏’)

tile_width = 30
tile_height = 30

x_margin = 0
y_margin = 0

line_color = ‘white’

level = 0

maze_maps_dic =

directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]
move_direction = (0, 0)

maze = []
player_location = ()
destination_location = ()

keys = []
doors = []

主函数所有内容先晒出来

if name == ‘main’:

pygame.init()
maze_maps_dic = read_map()

go_to_next_level()
# x_margin = int((screen_width - tile_width * len(maze[0])) / 2)
# y_margin = int((screen_height - tile_height * len(maze)) / 2)
draw_game_board(maze, player_image, destination_image, player_location, destination_location)

while True:
    if player_location == destination_location:
        pygame.time.wait(300)
        level_cartoon()
        go_to_next_level()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        # 如果按键被按下且抬起,则代表用户按下了某一个按键
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                move_direction = directions[0]
            elif event.key == pygame.K_RIGHT:
                move_direction = directions[1]
            elif event.key == pygame.K_UP:
                move_direction = directions[2]
            elif event.key == pygame.K_DOWN:
                move_direction = directions[3]
    if is_right_direction(move_direction, player_location):

        # 当移动是被允许的,那么交换当前player坐标内容和player移动到下一步的坐标内容,下一次绘制时,角色位置就发生变化了
        maze[player_location[0]][player_location[1]] = '0'
        player_location = (player_location[0] + move_direction[0], player_location[1] + move_direction[1])
        maze[player_location[0]][player_location[1]] = 'p'
        # 移动完成,重置方向
        move_direction = (0, 0)
    check_player_and_key()
    draw_game_board(maze, player_image, destination_image, player_location, destination_location)
    pygame.display.update()
    fps_clock.tick(fps)

比较重要的一步是读取地图函数,首先需要根据地图的内容读取,定义了一个字典变量来存放地图maze_maps_dic =

读取外部迷宫地图

def read_map():
# maze_maps = []
maze_level = ‘’
level_map = []
with open(‘maze_maps.txt’, ‘r’) as f:
for line in f:
line = line.strip(‘\\r\\n’)
if ‘#’ in line:
maze_level = line.strip('# ‘)
elif line != ‘’:
line = line.split(’ ')
level_map.append(line)
elif line == ‘’ and len(level_map) > 0:
maze_maps_dic[maze_level] = level_map
level_map = []
maze_level = -1

通过读取函数,我们把迷宫地图存放到了字典变量中,格式是:
‘1’:
[[‘W’, ‘W’, ‘W’, ‘W’, ‘W’, ‘W’],
[‘W’, ‘p’, ‘0’, ‘0’, ‘0’, ‘W’],
[‘W’, ‘0’, ‘T’, ‘0’, ‘0’, ‘W’],
[‘W’, ‘0’, ‘0’, ‘0’, ‘0’, ‘W’],
[‘W’, ‘T’, ‘T’, ‘0’, ‘g’, ‘W’],
[‘W’, ‘W’, ‘W’, ‘W’, ‘W’, ‘W’]],
‘2’:
[[‘p’, ‘0’, ‘0’, ‘w’, ‘0’, ‘w’, ‘0’, ‘0’], [‘w’, ‘w’, ‘0’, ‘r’, ‘0’, ‘0’, ‘t’, ‘0’], [‘k’, ‘r’, ‘0’, ‘t’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘r’, ‘0’, ‘0’, ‘t’, ‘0’], [‘w’, ‘t’, ‘0’, ‘r’, ‘0’, ‘t’, ‘0’, ‘0’], [‘w’, ‘r’, ‘d’, ‘0’, ‘0’, ‘r’, ‘r’, ‘r’], [‘0’, ‘r’, ‘t’, ‘r’, ‘0’, ‘g’, ‘t’, ‘0’], [‘w’, ‘r’, ‘r’, ‘r’, ‘t’, ‘t’, ‘t’, ‘t’]],
‘3’:
[[‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’], [‘w’, ‘p’, ‘0’, ‘0’, ‘t’, ‘w’, ‘0’, ‘0’, ‘0’, ‘w’, ‘0’, ‘0’, ‘0’, ‘w’], [‘w’, ‘0’, ‘r’, ‘0’, ‘0’, ‘0’, ‘0’, ‘w’, ‘0’, ‘w’, ‘0’, ‘w’, ‘0’, ‘w’], [‘w’, ‘0’, ‘r’, ‘0’, ‘t’, ‘w’, ‘w’, ‘w’, ‘0’, ‘w’, ‘0’, ‘w’, ‘0’, ‘w’], [‘w’, ‘0’, ‘w’, ‘0’, ‘0’, ‘0’, ‘0’, ‘0’, ‘0’, ‘w’, ‘t’, ‘w’, ‘0’, ‘w’], [‘w’, ‘0’, ‘t’, ‘r’, ‘r’, ‘r’, ‘r’, ‘r’, ‘t’, ‘t’, ‘0’, ‘g’, ‘0’, ‘w’], [‘w’, ‘0’, ‘r’, ‘0’, ‘0’, ‘0’, ‘0’, ‘0’, ‘0’, ‘0’, ‘0’, ‘w’, ‘0’, ‘w’], [‘w’, ‘0’, ‘0’, ‘0’, ‘t’, ‘t’, ‘t’, ‘0’, ‘w’, ‘0’, ‘w’, ‘w’, ‘0’, ‘w’], [‘w’, ‘0’, ‘t’, ‘0’, ‘w’, ‘0’, ‘r’, ‘0’, ‘w’, ‘0’, ‘w’, ‘0’, ‘0’, ‘w’], [‘w’, ‘0’, ‘t’, ‘0’, ‘r’, ‘0’, ‘r’, ‘0’, ‘w’, ‘w’, ‘w’, ‘w’, ‘0’, ‘w’], [‘w’, ‘0’, ‘w’, ‘0’, ‘0’, ‘0’, ‘w’, ‘0’, ‘0’, ‘0’, ‘0’, ‘0’, ‘0’, ‘w’], [‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’, ‘w’]]

当得到了地图内容后,就可以开始在pygmae中画出地图了。

绘制方格
def go_to_next_level():
global level, maze, x_margin, y_margin, player_location, destination_location, keys, doors
level = level % len(maze_maps_dic) + 1
# 深层拷才不会改变原来的内容,如果增加选关内容,这个深度拷贝就很有用了
maze = copy.deepcopy(maze_maps_dic[str(level)])
x_margin = int((screen_width - tile_width * len(maze[0])) / 2)
y_margin = int((screen_height - tile_height * len(maze)) / 2)
for i in range(len(maze)):
for j in range(len(maze[i])):
if maze[i][j] == ‘p’:
player_location = (i, j)
elif maze[i][j] == ‘g’:
destination_location = (i, j)
keys.clear()
doors.clear()

绘制游戏面板所有素材
def draw_game_board(p_maze_map, p_player, p_destination, p_player_location, p_destination_location):
display.fill(‘sky blue’)
display_level(level)
draw_maze_lines(p_maze_map)
draw_all_maze_wall(p_maze_map)
draw_role(p_destination, p_destination_location[0], p_destination_location[1])
draw_role(p_player, p_player_location[0], p_player_location[1])

#显示关卡数
def display_level(level_number):
level_class = pygame.font.SysFont(‘Chalkboard’, 20, True, False)
level_class_sur = level_class.render(‘level: %d’ % level_number, True, ‘yellow’)
level_class_rect = level_class_sur.get_rect()
level_class_rect.topleft = (20, 20)
display.blit(level_class_sur, level_class_rect)

#加载图片
def image_load(name):
image_path = ‘pngs/’ + name
loaded_image = pygame.image.load(image_path)
loaded_image = pygame.transform.scale(loaded_image, (25, 25))
return loaded_image

player_image = image_load(‘boy.png’)
wall_images = [‘Tree.png’, ‘Rock.png’, ‘Wall.png’, ‘Door.png’, ‘Key.png’]
destination_image = image_load(‘Star.png’)
tile_image = ‘rock’: image_load(‘Rock.png’),
‘tree’: image_load(‘Tree.png’),
‘wall’: image_load(‘Wall.png’),
‘door’: image_load(‘Door.png’),
‘key’: image_load(‘Key.png’)

绘制出所有的墙,树,钥匙,门等等
def draw_all_maze_wall(p_maze):
for i in range(len(p_maze)):
for j in range(len(p_maze[0])):
match maze[i][j].lower():
case ‘t’:
tile = tile_image[‘tree’]
draw_role(tile, i, j)
case ‘r’:
tile = tile_image[‘rock’]
draw_role(tile, i, j)
case ‘d’:
tile = tile_image[‘door’]
draw_role(tile, i, j)
doors.append((i, j))
case ‘k’:
tile = tile_image[‘key’]
draw_role(tile, i, j)
keys.append((i, j))
case ‘w’:
tile = tile_image[‘wall’]
draw_role(tile, i, j)

剩余函数
目前比较粗暴的方式解密,有多少钥匙就有多少门,每捡到一把钥匙,则按顺序打开门,与钥匙所在地无关
def check_player_and_key():
global maze
for i in keys:
if i == player_location:
if doors:
maze[doors[0][0]][doors[0][1]] = ‘0’
del doors[0]
keys.remove(i)

过关动画
def level_cartoon():
for i in range(4):
display.fill(‘red’)
pygame.display.update()
pygame.time.wait(300)
display.fill(‘sky blue’)
pygame.display.update()
pygame.time.wait(300)
pygame.display.update()

判断是否合法

def is_right_direction(p_direction, p_player_location):

x = p_player_location[0] + p_direction[0]
y = p_player_location[1] + p_direction[1]

# 判断下一步的区域不能是界外
if x < 0 or x >= len(maze) or y < 0 or y >= len(maze[1]):
    return False
# 如果下一步的区域是空格或者是目的地或者是钥匙都可以动
if maze[x][y] == '0' or maze[x][y] == 'g' or maze[x][y] == 'k':
    return True
else:
    return False

然后一个简单的迷宫游戏就完成了,虽然简单,但是它还很简陋,虽然简陋,但是它写的很乱啊。

欢迎大家指正,反正我也不会改的😄

我半夜爬起来,用python给失眠的隔壁小姐姐:写了一个迷宫小游戏~~~

大家好,我是Lex 喜欢欺负超人那个Lex

划重点:

1、python开发小游戏,pygame环境搭建;

2、给失眠的小姐姐开发一个迷宫小游戏。

代码干货满满,建议收藏+实操!!!有问题及需要,请留言哦~~

事情是这样的

半夜听到隔壁来来回回 ,忙忙碌碌的声音

我这么心思细腻 体贴入微的python小哥哥

敏锐的感觉到

小姐姐肯定是失眠了

好担心哦,我都睡不着了呢

辗转反侧

最后爬起来撸出了我的python代码

 

一、环境要求

windows系统,python3.6+

安装游戏依赖模块

pip install pyqt5

pip install pygame

二、游戏介绍

1、游戏目标

随机生成一张迷宫地图,将玩家设置在迷宫内部,通过光标 上 下 左 右,来移动玩家,按照迷宫地图的道路来走出迷宫。

 

2、先上游戏效果图

 

三、完整开发流程

1、项目主结构

首先,先整理一下项目的主结构,其实看一下主结构,基本就清晰了

modules:存放自己写的python类
——mazes.py
——misc.py
——sprites.py

resources:存放引用到的图片、音频等等
——audios:音频资源
——images:图片资源

config.py:为主配置文件

maze.py:主程序文件

requirements.txt:需要引入的python依赖包

 

2、详细配置

config.py

配置文件中,需要引入os模块,并且配置打开游戏的屏幕大小,并将资源中引用到的图片、音频插入到合适的位置。

因为我们的迷宫游戏,需要划开模块。

'''配置文件'''
import os


'''屏幕大小'''
SCREENSIZE = (800, 625)
'''游戏素材'''
BGMPATH = os.path.join(os.getcwd(), 'resources/audios/bgm.mp3')
HEROPICPATH = os.path.join(os.getcwd(), 'resources/images/hero.png')
'''FPS'''
FPS = 20
'''块大小'''
BLOCKSIZE = 15
MAZESIZE = (35, 50) # num_rows * num_cols
BORDERSIZE = (25, 50) # 25 * 2 + 50 * 15 = 800, 50 * 2 + 35 * 15 = 625

 

3、随机生成迷宫地图

mazes.py

迷宫虽然是个小游戏,但是我们每次打开,进入 地图需要随机生成一个新地图。

定义randommaze 随机生成地图,并将地图投在主游戏屏幕上

'''
Function:
    随机生成迷宫
Author:
    lexsaints
'''
import pygame
import random
from .misc import *


'''一个游戏地图块'''
class Block():
    def __init__(self, coordinate, block_size, border_size, **kwargs):
        # (col, row)
        self.coordinate = coordinate
        self.block_size = block_size
        self.border_size = border_size
        self.is_visited = False
        # 上下左右有没有墙
        self.has_walls = [True, True, True, True]
        self.color = (0, 0, 0)
    '''画到屏幕上'''
    def draw(self, screen):
        directions = ['top', 'bottom', 'left', 'right']
        for idx, direction in enumerate(directions):
            if self.has_walls[idx]:
                if direction == 'top':
                    x1 = self.coordinate[0] * self.block_size + self.border_size[0]
                    y1 = self.coordinate[1] * self.block_size + self.border_size[1]
                    x2 = (self.coordinate[0] + 1) * self.block_size + self.border_size[0]
                    y2 = self.coordinate[1] * self.block_size + self.border_size[1]
                    pygame.draw.line(screen, self.color, (x1, y1), (x2, y2))
                elif direction == 'bottom':
                    x1 = self.coordinate[0] * self.block_size + self.border_size[0]
                    y1 = (self.coordinate[1] + 1) * self.block_size + self.border_size[1]
                    x2 = (self.coordinate[0] + 1) * self.block_size + self.border_size[0]
                    y2 = (self.coordinate[1] + 1) * self.block_size + self.border_size[1]
                    pygame.draw.line(screen, self.color, (x1, y1), (x2, y2))
                elif direction == 'left':
                    x1 = self.coordinate[0] * self.block_size + self.border_size[0]
                    y1 = self.coordinate[1] * self.block_size + self.border_size[1]
                    x2 = self.coordinate[0] * self.block_size + self.border_size[0]
                    y2 = (self.coordinate[1] + 1) * self.block_size + self.border_size[1]
                    pygame.draw.line(screen, self.color, (x1, y1), (x2, y2))
                elif direction == 'right':
                    x1 = (self.coordinate[0] + 1) * self.block_size + self.border_size[0]
                    y1 = self.coordinate[1] * self.block_size + self.border_size[1]
                    x2 = (self.coordinate[0] + 1) * self.block_size + self.border_size[0]
                    y2 = (self.coordinate[1] + 1) * self.block_size + self.border_size[1]
                    pygame.draw.line(screen, self.color, (x1, y1), (x2, y2))
        return True


'''随机生成迷宫类'''
class RandomMaze():
    def __init__(self, maze_size, block_size, border_size, **kwargs):
        self.block_size = block_size
        self.border_size = border_size
        self.maze_size = maze_size
        self.blocks_list = RandomMaze.createMaze(maze_size, block_size, border_size)
        self.font = pygame.font.SysFont('Consolas', 15)
    '''画到屏幕上'''
    def draw(self, screen):
        for row in range(self.maze_size[0]):
            for col in range(self.maze_size[1]):
                self.blocks_list[row][col].draw(screen)
        # 起点和终点标志
        showText(screen, self.font, 'S', (255, 0, 0), (self.border_size[0]-10, self.border_size[1]))
        showText(screen, self.font, 'D', (255, 0, 0), (self.border_size[0]+(self.maze_size[1]-1)*self.block_size, self.border_size[1]+self.maze_size[0]*self.block_size+5))
    '''创建迷宫'''
    @staticmethod
    def createMaze(maze_size, block_size, border_size):
        def nextBlock(block_now, blocks_list):
            directions = ['top', 'bottom', 'left', 'right']
            blocks_around = dict(zip(directions, [None]*4))
            block_next = None
            count = 0
            # 查看上边block
            if block_now.coordinate[1]-1 >= 0:
                block_now_top = blocks_list[block_now.coordinate[1]-1][block_now.coordinate[0]]
                if not block_now_top.is_visited:
                    blocks_around['top'] = block_now_top
                    count += 1
            # 查看下边block
            if block_now.coordinate[1]+1 < maze_size[0]:
                block_now_bottom = blocks_list[block_now.coordinate[1]+1][block_now.coordinate[0]]
                if not block_now_bottom.is_visited:
                    blocks_around['bottom'] = block_now_bottom
                    count += 1
            # 查看左边block
            if block_now.coordinate[0]-1 >= 0:
                block_now_left = blocks_list[block_now.coordinate[1]][block_now.coordinate[0]-1]
                if not block_now_left.is_visited:
                    blocks_around['left'] = block_now_left
                    count += 1
            # 查看右边block
            if block_now.coordinate[0]+1 < maze_size[1]:
                block_now_right = blocks_list[block_now.coordinate[1]][block_now.coordinate[0]+1]
                if not block_now_right.is_visited:
                    blocks_around['right'] = block_now_right
                    count += 1
            if count > 0:
                while True:
                    direction = random.choice(directions)
                    if blocks_around.get(direction):
                        block_next = blocks_around.get(direction)
                        if direction == 'top':
                            block_next.has_walls[1] = False
                            block_now.has_walls[0] = False
                        elif direction == 'bottom':
                            block_next.has_walls[0] = False
                            block_now.has_walls[1] = False
                        elif direction == 'left':
                            block_next.has_walls[3] = False
                            block_now.has_walls[2] = False
                        elif direction == 'right':
                            block_next.has_walls[2] = False
                            block_now.has_walls[3] = False
                        break
            return block_next
        blocks_list = [[Block([col, row], block_size, border_size) for col in range(maze_size[1])] for row in range(maze_size[0])]
        block_now = blocks_list[0][0]
        records = []
        while True:
            if block_now:
                if not block_now.is_visited:
                    block_now.is_visited = True
                    records.append(block_now)
                block_now = nextBlock(block_now, blocks_list)
            else:
                block_now = records.pop()
                if len(records) == 0:
                    break
        return blocks_list

4、光标控制玩家

misc.py

通过读取键盘的上下左右光标来移动我们的小可爱

'''
Function:
    定义其他必要模块
Author:
    lexsaints
'''
import sys
import pygame


'''在屏幕指定位置显示文字'''
def showText(screen, font, text, color, position):
    text_render = font.render(text, True, color)
    rect = text_render.get_rect()
    rect.left, rect.top = position
    screen.blit(text_render, rect)
    return rect.right


'''按钮'''
def Button(screen, position, text, font, buttoncolor=(120, 120, 120), linecolor=(20, 20, 20), textcolor=(255, 255, 255), bwidth=200, bheight=50):
    left, top = position
    pygame.draw.line(screen, linecolor, (left, top), (left+bwidth, top), 5)
    pygame.draw.line(screen, linecolor, (left, top-2), (left, top+bheight), 5)
    pygame.draw.line(screen, linecolor, (left, top+bheight), (left+bwidth, top+bheight), 5)
    pygame.draw.line(screen, linecolor, (left+bwidth, top+bheight), (left+bwidth, top), 5)
    pygame.draw.rect(screen, buttoncolor, (left, top, bwidth, bheight))
    text_render = font.render(text, 1, textcolor)
    rect = text_render.get_rect()
    rect.centerx, rect.centery = left + bwidth / 2, top + bheight / 2
    return screen.blit(text_render, rect)


'''游戏开始/关卡切换/游戏结束界面'''
def Interface(screen, config, mode='game_start'):
    pygame.display.set_mode(config.SCREENSIZE)
    font = pygame.font.SysFont('Consolas', 30)
    if mode == 'game_start':
        clock = pygame.time.Clock()
        while True:
            screen.fill((192, 192, 192))
            button_1 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//3), 'START', font)
            button_2 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//2), 'QUIT', font)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit(-1)
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    if button_1.collidepoint(pygame.mouse.get_pos()):
                        return True
                    elif button_2.collidepoint(pygame.mouse.get_pos()):
                        pygame.quit()
                        sys.exit(-1)
            pygame.display.update()
            clock.tick(config.FPS)
    elif mode == 'game_switch':
        clock = pygame.time.Clock()
        while True:
            screen.fill((192, 192, 192))
            button_1 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//3), 'NEXT', font)
            button_2 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//2), 'QUIT', font)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit(-1)
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    if button_1.collidepoint(pygame.mouse.get_pos()):
                        return True
                    elif button_2.collidepoint(pygame.mouse.get_pos()):
                        pygame.quit()
                        sys.exit(-1)
            pygame.display.update()
            clock.tick(config.FPS)
    elif mode == 'game_end':
        clock = pygame.time.Clock()
        while True:
            screen.fill((192, 192, 192))
            button_1 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//3), 'RESTART', font)
            button_2 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//2), 'QUIT', font)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit(-1)
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    if button_1.collidepoint(pygame.mouse.get_pos()):
                        return True
                    elif button_2.collidepoint(pygame.mouse.get_pos()):
                        pygame.quit()
                        sys.exit(-1)
            pygame.display.update()
            clock.tick(config.FPS)
    else:
        raise ValueError('Interface.mode unsupport %s...' % mode)

5、定义主玩家 绘制全图

绘制完整游戏,并定义主角,就叫hero吧

sprites.py

'''
Function:
    定义游戏精灵类
Author:
    lexsaints
'''
import pygame


'''定义hero'''
class Hero(pygame.sprite.Sprite):
    def __init__(self, imagepath, coordinate, block_size, border_size, **kwargs):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(imagepath)
        self.image = pygame.transform.scale(self.image, (block_size, block_size))
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = coordinate[0] * block_size + border_size[0], coordinate[1] * block_size + border_size[1]
        self.coordinate = coordinate
        self.block_size = block_size
        self.border_size = border_size
    '''移动'''
    def move(self, direction, maze):
        blocks_list = maze.blocks_list
        if direction == 'up':
            if blocks_list[self.coordinate[1]][self.coordinate[0]].has_walls[0]:
                return False
            else:
                self.coordinate[1] = self.coordinate[1] - 1
                return True
        elif direction == 'down':
            if blocks_list[self.coordinate[1]][self.coordinate[0]].has_walls[1]:
                return False
            else:
                self.coordinate[1] = self.coordinate[1] + 1
                return True
        elif direction == 'left':
            if blocks_list[self.coordinate[1]][self.coordinate[0]].has_walls[2]:
                return False
            else:
                self.coordinate[0] = self.coordinate[0] - 1
                return True
        elif direction == 'right':
            if blocks_list[self.coordinate[1]][self.coordinate[0]].has_walls[3]:
                return False
            else:
                self.coordinate[0] = self.coordinate[0] + 1
                return True
        else:
            raise ValueError('Unsupport direction %s in Hero.move...' % direction)
    '''绑定到屏幕'''
    def draw(self, screen):
        self.rect.left, self.rect.top = self.coordinate[0] * self.block_size + self.border_size[0], self.coordinate[1] * self.block_size + self.border_size[1]
        screen.blit(self.image, self.rect)

6、引入音频、图片

启动游戏主程序

maze.py

在主程序中,通过读取配置文件,引入项目资源:包括图片、音频等,并通过定义类,加载游戏地图

'''
Function:
    迷宫小游戏
Author:
    lexsaints
'''
import config
import sys
import pygame
from modules import *


'''主函数'''
def main(config):
    # 初始化
    pygame.init()
    pygame.mixer.init()
    pygame.font.init()
    pygame.mixer.music.load(config.BGMPATH)
    pygame.mixer.music.play(-1, 0.0)
    screen = pygame.display.set_mode(config.SCREENSIZE)
    pygame.display.set_caption('一起来学pygame吧——迷宫')
    font = pygame.font.SysFont('Consolas', 15)
    # 开始界面
    Interface(screen, config, 'game_start')
    # 记录关卡数
    num_levels = 0
    # 记录最少用了多少步通关
    best_scores = 'None'
    # 关卡循环切换
    while True:
        num_levels += 1
        clock = pygame.time.Clock()
        screen = pygame.display.set_mode(config.SCREENSIZE)
        # --随机生成关卡地图
        maze_now = RandomMaze(config.MAZESIZE, config.BLOCKSIZE, config.BORDERSIZE)
        # --生成hero
        hero_now = Hero(config.HEROPICPATH, [0, 0], config.BLOCKSIZE, config.BORDERSIZE)
        # --统计步数
        num_steps = 0
        # --关卡内主循环
        while True:
            dt = clock.tick(config.FPS)
            screen.fill((255, 255, 255))
            is_move = False
            # ----↑↓←→控制hero
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit(-1)
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_UP:
                        is_move = hero_now.move('up', maze_now)
                    elif event.key == pygame.K_DOWN:
                        is_move = hero_now.move('down', maze_now)
                    elif event.key == pygame.K_LEFT:
                        is_move = hero_now.move('left', maze_now)
                    elif event.key == pygame.K_RIGHT:
                        is_move = hero_now.move('right', maze_now)
            num_steps += int(is_move)
            hero_now.draw(screen)
            maze_now.draw(screen)
            # ----显示一些信息
            showText(screen, font, 'LEVELDONE: %d' % num_levels, (255, 0, 0), (10, 10))
            showText(screen, font, 'BESTSCORE: %s' % best_scores, (255, 0, 0), (210, 10))
            showText(screen, font, 'USEDSTEPS: %s' % num_steps, (255, 0, 0), (410, 10))
            showText(screen, font, 'S: your starting point    D: your destination', (255, 0, 0), (10, 600))
            # ----判断游戏是否胜利
            if (hero_now.coordinate[0] == config.MAZESIZE[1] - 1) and (hero_now.coordinate[1] == config.MAZESIZE[0] - 1):
                break
            pygame.display.update()
        # --更新最优成绩
        if best_scores == 'None':
            best_scores = num_steps
        else:
            if best_scores > num_steps:
                best_scores = num_steps
        # --关卡切换
        Interface(screen, config, mode='game_switch')


'''run'''
if __name__ == '__main__':
    main(config)

四、游戏启动方法

1、开发工具启动

如果你配置了开发工具的环境VScode、sublimeText、notepad+、pycharm什么的,可以直接在工具中,运行游戏。

如果没配置,可以使用命令启动。

2、命令行启动 gif

 

推荐阅读

python实战

【python实战】前女友发来加密的 “520快乐.pdf“,我用python破解开之后,却发现。。。

【python实战】昨晚,我用python帮隔壁小姐姐P证件照 自拍,然后发现...

【python实战】女友半夜加班发自拍 python男友用30行代码发现惊天秘密

【python实战】python你TM太皮了——区区30行代码就能记录键盘的一举一动

python实战】女神相册密码忘记了,我只用Python写了20行代码~~~

渗透测试

【渗透案例】上班摸鱼误入陌生网址——结果被XSS劫持了

【渗透测试】密码暴力破解工具——九头蛇(hydra)使用详解及实战

【渗透学习】Web安全渗透详细教程+学习线路+详细笔记【全网最全+建议收藏】

【渗透案例】如何用ssh工具连接前台小姐姐的“小米手机”——雷总看了直呼内行!!!

【渗透测试】密码暴力破解工具——九头蛇(hydra)使用详解及实战

pygame系列文章

一起来学pygame吧 游戏开发30例(二)——塔防游戏

一起来学pygame吧 游戏开发30例(三)——射击外星人小游戏

一起来学pygame吧 游戏开发30例(四)——俄罗斯方块小游戏

一起来学pygame吧 游戏开发30例(五)——消消乐 小游戏

一起来学pygame吧 游戏开发30例(六)——高山滑雪 小游戏

 

CSDN官方学习推荐 ↓ ↓ ↓

CSDN出的Python全栈知识图谱,太强了,推荐给大家!

以上是关于python做小游戏之一小迷宫游戏的主要内容,如果未能解决你的问题,请参考以下文章

我半夜爬起来,用python给失眠的隔壁小姐姐:写了一个迷宫小游戏~~~

毕业设计 python小游戏设计 - 走迷宫游戏设计与实现

Python游戏开发,pygame模块,Python实现过迷宫小游戏

Python游戏开发,pygame模块,Python实现过迷宫小游戏

Pygame小游戏Python版有迷宫嘛?原来藏在个地方呀~

Python迷宫游戏